Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / base / MimeType.h
blob56276f3369a5b8901c1d4df7ea219cb4fac45617
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 "mozilla/UniquePtr.h"
12 #include "nsTHashMap.h"
13 #include "nsTArray.h"
15 template <typename char_type>
16 struct HashKeyType;
17 template <>
18 struct HashKeyType<char16_t> {
19 using HashType = nsStringHashKey;
21 template <>
22 struct HashKeyType<char> {
23 using HashType = nsCStringHashKey;
26 template <typename char_type>
27 class TMimeType final {
28 private:
29 class ParameterValue : public nsTString<char_type> {
30 public:
31 bool mRequiresQuoting;
33 ParameterValue() : mRequiresQuoting(false) {}
36 bool mIsBase64{false};
37 nsTString<char_type> mType;
38 nsTString<char_type> mSubtype;
39 nsTHashMap<typename HashKeyType<char_type>::HashType, ParameterValue>
40 mParameters;
41 nsTArray<nsTString<char_type>> mParameterNames;
43 public:
44 TMimeType(const nsTSubstring<char_type>& aType,
45 const nsTSubstring<char_type>& aSubtype)
46 : mType(aType), mSubtype(aSubtype) {}
48 static mozilla::UniquePtr<TMimeType<char_type>> Parse(
49 const nsTSubstring<char_type>& aStr);
51 void Serialize(nsTSubstring<char_type>& aStr) const;
53 // Returns the `<mType>/<mSubtype>`
54 void GetFullType(nsTSubstring<char_type>& aStr) const;
56 bool IsBase64() const { return mIsBase64; }
58 // @param aName - the name of the parameter
59 // @return true if the parameter name is found, false otherwise.
60 bool HasParameter(const nsTSubstring<char_type>& aName) const;
62 // @param aName - the name of the parameter
63 // @param aOutput - will hold the value of the parameter (quoted if necessary)
64 // @param aAppend - if true, the method will append to the string;
65 // otherwise the string is truncated before appending.
66 // @return true if the parameter name is found, false otherwise.
67 bool GetParameterValue(const nsTSubstring<char_type>& aName,
68 nsTSubstring<char_type>& aOutput,
69 bool aAppend = false) const;
71 // @param aName - the name of the parameter
72 // @param aValue - the value of the parameter
73 void SetParameterValue(const nsTSubstring<char_type>& aName,
74 const nsTSubstring<char_type>& aValue);
77 using MimeType = TMimeType<char16_t>;
78 using CMimeType = TMimeType<char>;
80 #endif // mozilla_dom_MimeType_h