1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_MimeType_h
8 #define mozilla_dom_MimeType_h
10 #include "mozilla/TextUtils.h"
11 #include "nsTHashMap.h"
14 template <typename char_type
>
17 struct HashKeyType
<char16_t
> {
18 using HashType
= nsStringHashKey
;
21 struct HashKeyType
<char> {
22 using HashType
= nsCStringHashKey
;
25 template <typename char_type
>
26 class TMimeType final
{
28 ~TMimeType() = default;
30 class ParameterValue
: public nsTString
<char_type
> {
32 bool mRequiresQuoting
;
34 ParameterValue() : mRequiresQuoting(false) {}
37 static nsTArray
<nsTDependentSubstring
<char_type
>> SplitMimetype(
38 const nsTSubstring
<char_type
>& aMimeType
);
40 bool mIsBase64
{false};
41 nsTString
<char_type
> mType
;
42 nsTString
<char_type
> mSubtype
;
43 nsTHashMap
<typename HashKeyType
<char_type
>::HashType
, ParameterValue
>
45 nsTArray
<nsTString
<char_type
>> mParameterNames
;
48 TMimeType(const nsTSubstring
<char_type
>& aType
,
49 const nsTSubstring
<char_type
>& aSubtype
)
50 : mType(aType
), mSubtype(aSubtype
) {}
52 static RefPtr
<TMimeType
<char_type
>> Parse(
53 const nsTSubstring
<char_type
>& aMimeType
);
55 // @param aMimeType - the mimetype string
56 // @param aOutEssence - will hold the value of the content-type
57 // @param aOutCharset - will hold the value of the charset
58 // @return true if the mimetype was parsed, false otherwise.
59 static bool Parse(const nsTSubstring
<char_type
>& aMimeType
,
60 nsTSubstring
<char_type
>& aOutEssence
,
61 nsTSubstring
<char_type
>& aOutCharset
);
63 void Serialize(nsTSubstring
<char_type
>& aStr
) const;
65 // Returns the `<mType>/<mSubtype>`
66 void GetEssence(nsTSubstring
<char_type
>& aOutput
) const;
68 bool IsBase64() const { return mIsBase64
; }
70 // @param aName - the name of the parameter
71 // @return true if the parameter name is found, false otherwise.
72 bool HasParameter(const nsTSubstring
<char_type
>& aName
) const;
74 // @param aName - the name of the parameter
75 // @param aOutput - will hold the value of the parameter (quoted if necessary)
76 // @param aAppend - if true, the method will append to the string;
77 // otherwise the string is truncated before appending.
78 // @param aWithQuotes - if true, output can contain quoted string
79 // @return true if the parameter name is found, false otherwise.
80 bool GetParameterValue(const nsTSubstring
<char_type
>& aName
,
81 nsTSubstring
<char_type
>& aOutput
, bool aAppend
= false,
82 bool aWithQuotes
= true) const;
84 // @param aName - the name of the parameter
85 // @param aValue - the value of the parameter
86 void SetParameterValue(const nsTSubstring
<char_type
>& aName
,
87 const nsTSubstring
<char_type
>& aValue
);
89 NS_INLINE_DECL_REFCOUNTING(TMimeType
)
92 using MimeType
= TMimeType
<char16_t
>;
93 using CMimeType
= TMimeType
<char>;
95 #endif // mozilla_dom_MimeType_h