Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / MimeType.h
blob5b104248e24adcf01439db96ce635144851558ee
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 "nsDataHashtable.h"
13 #include "nsTArray.h"
15 template <typename char_type>
16 struct HashKeyType;
17 template <>
18 struct HashKeyType<char16_t> {
19 typedef nsStringHashKey HashType;
21 template <>
22 struct HashKeyType<char> {
23 typedef nsCStringHashKey HashType;
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 nsTString<char_type> mType;
37 nsTString<char_type> mSubtype;
38 nsDataHashtable<typename HashKeyType<char_type>::HashType, ParameterValue>
39 mParameters;
40 nsTArray<nsTString<char_type>> mParameterNames;
42 public:
43 TMimeType(const nsTSubstring<char_type>& aType,
44 const nsTSubstring<char_type>& aSubtype)
45 : mType(aType), mSubtype(aSubtype) {}
47 static mozilla::UniquePtr<TMimeType<char_type>> Parse(
48 const nsTSubstring<char_type>& aStr);
50 void Serialize(nsTSubstring<char_type>& aStr) const;
52 // Returns the `<mType>/<mSubtype>`
53 void GetFullType(nsTSubstring<char_type>& aStr) const;
55 // @param aName - the name of the parameter
56 // @return true if the parameter name is found, false otherwise.
57 bool HasParameter(const nsTSubstring<char_type>& aName) const;
59 // @param aName - the name of the parameter
60 // @param aOutput - will hold the value of the parameter (quoted if necessary)
61 // @param aAppend - if true, the method will append to the string;
62 // otherwise the string is truncated before appending.
63 // @return true if the parameter name is found, false otherwise.
64 bool GetParameterValue(const nsTSubstring<char_type>& aName,
65 nsTSubstring<char_type>& aOutput,
66 bool aAppend = false) const;
68 // @param aName - the name of the parameter
69 // @param aValue - the value of the parameter
70 void SetParameterValue(const nsTSubstring<char_type>& aName,
71 const nsTSubstring<char_type>& aValue);
74 using MimeType = TMimeType<char16_t>;
75 using CMimeType = TMimeType<char>;
77 #endif // mozilla_dom_MimeType_h