Bug 1329568 - Simple IsMediaMIMEType checker for strings - r=jya

Inside dom/media, we really only deal with audio and video MIME types.
IsMediaMIMEType will help check for that.

Note that 'application' is an acceptable MIME major type, as some A/V contents
do use it! E.g.: "application/ogg".

IsMediaMIMEType is constexpr to allow its use in static_assert's, so we will be
able to verify string literals at compile time.

MozReview-Commit-ID: InBicRRUeiP

--HG--
extra : rebase_source : f10355e7570b163473cee2548c04c6be11d9120f
This commit is contained in:
Gerald Squelart 2017-01-01 08:46:34 +11:00
Родитель fe26cb66da
Коммит 8558ce5e5e
1 изменённых файлов: 70 добавлений и 0 удалений

Просмотреть файл

@ -362,6 +362,76 @@ CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters(
const nsACString& aCodecMIMEType,
const MediaContentType& aContentType);
namespace detail {
// aString should start with aMajor + '/'.
constexpr bool
StartsWithMIMETypeMajor(const char* aString,
const char* aMajor, size_t aMajorRemaining)
{
return (aMajorRemaining == 0 && *aString == '/')
|| (*aString == *aMajor
&& StartsWithMIMETypeMajor(aString + 1,
aMajor + 1, aMajorRemaining - 1));
}
// aString should only contain [a-z0-9\-\.] and a final '\0'.
constexpr bool
EndsWithMIMESubtype(const char* aString, size_t aRemaining)
{
return aRemaining == 0
|| (((*aString >= 'a' && *aString <= 'z')
|| (*aString >= '0' && *aString <= '9')
|| *aString == '-'
|| *aString == '.')
&& EndsWithMIMESubtype(aString + 1, aRemaining - 1));
}
// Simple MIME-type literal string checker with a given (major) type.
// Only accepts "{aMajor}/[a-z0-9\-\.]+".
template <size_t MajorLengthPlus1>
constexpr bool
IsMIMETypeWithMajor(const char* aString, size_t aLength,
const char (&aMajor)[MajorLengthPlus1])
{
return aLength > MajorLengthPlus1 // Major + '/' + at least 1 char
&& StartsWithMIMETypeMajor(aString, aMajor, MajorLengthPlus1 - 1)
&& EndsWithMIMESubtype(aString + MajorLengthPlus1,
aLength - MajorLengthPlus1);
}
} // namespace detail
// Simple MIME-type string checker.
// Only accepts lowercase "{application,audio,video}/[a-z0-9\-\.]+".
// Add more if necessary.
constexpr bool
IsMediaMIMEType(const char* aString, size_t aLength)
{
return detail::IsMIMETypeWithMajor(aString, aLength, "application")
|| detail::IsMIMETypeWithMajor(aString, aLength, "audio")
|| detail::IsMIMETypeWithMajor(aString, aLength, "video");
}
// Simple MIME-type string literal checker.
// Only accepts lowercase "{application,audio,video}/[a-z0-9\-\.]+".
// Add more if necessary.
template <size_t LengthPlus1>
constexpr bool
IsMediaMIMEType(const char (&aString)[LengthPlus1])
{
return IsMediaMIMEType(aString, LengthPlus1 - 1);
}
// Simple MIME-type string checker.
// Only accepts lowercase "{application,audio,video}/[a-z0-9\-\.]+".
// Add more if necessary.
inline bool
IsMediaMIMEType(const nsACString& aString)
{
return IsMediaMIMEType(aString.Data(), aString.Length());
}
enum class StringListRangeEmptyItems
{
// Skip all empty items (empty string will process nothing)