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_WindowFeatures_h
8 #define mozilla_dom_WindowFeatures_h
11 #include "mozilla/Assertions.h" // MOZ_ASSERT
12 #include "mozilla/HashTable.h" // mozilla::HashMap
14 #include "nsStringFwd.h" // nsCString, nsACString, nsAutoCString, nsLiteralCString
15 #include "nsTStringHasher.h" // mozilla::DefaultHasher<nsCString>
20 // Represents `tokenizedFeatures` in
21 // https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-tokenize
22 // with accessor methods for values.
23 class WindowFeatures
{
25 WindowFeatures() = default;
27 WindowFeatures(const WindowFeatures
& aOther
) = delete;
28 WindowFeatures
& operator=(const WindowFeatures
& aOther
) = delete;
30 WindowFeatures(WindowFeatures
&& aOther
) = delete;
31 WindowFeatures
& operator=(WindowFeatures
&& aOther
) = delete;
33 // Tokenizes `aFeatures` and stores the result map in member field.
34 // This should be called at the begining, only once.
36 // Returns true if successfully tokenized, false otherwise.
37 bool Tokenize(const nsACString
& aFeatures
);
39 // Returns true if the `aName` feature is specified.
41 bool Exists(const char (&aName
)[N
]) const {
42 MOZ_ASSERT(IsLowerCase(aName
));
43 nsLiteralCString
name(aName
);
44 return tokenizedFeatures_
.has(name
);
47 // Returns string value of `aName` feature.
48 // The feature must exist.
50 const nsCString
& Get(const char (&aName
)[N
]) const {
51 MOZ_ASSERT(IsLowerCase(aName
));
52 nsLiteralCString
name(aName
);
53 auto p
= tokenizedFeatures_
.lookup(name
);
54 MOZ_ASSERT(p
.found());
59 // Returns integer value of `aName` feature.
60 // The feature must exist.
62 int32_t GetInt(const char (&aName
)[N
]) const {
63 const nsCString
& value
= Get(aName
);
64 return ParseIntegerWithFallback(value
);
67 // Returns bool value of `aName` feature.
68 // The feature must exist.
70 bool GetBool(const char (&aName
)[N
]) const {
71 const nsCString
& value
= Get(aName
);
72 return ParseBool(value
);
75 // Returns bool value of `aName` feature.
76 // If the feature doesn't exist, returns `aDefault`.
78 // If `aPresenceFlag` is provided and the feature exists, it's set to `true`.
79 // (note that the value isn't overwritten if the feature doesn't exist)
81 bool GetBoolWithDefault(const char (&aName
)[N
], bool aDefault
,
82 bool* aPresenceFlag
= nullptr) const {
83 MOZ_ASSERT(IsLowerCase(aName
));
84 nsLiteralCString
name(aName
);
85 auto p
= tokenizedFeatures_
.lookup(name
);
88 *aPresenceFlag
= true;
90 return ParseBool(p
->value());
95 // Remove the feature from the map.
97 void Remove(const char (&aName
)[N
]) {
98 MOZ_ASSERT(IsLowerCase(aName
));
99 nsLiteralCString
name(aName
);
100 tokenizedFeatures_
.remove(name
);
103 // Returns true if there was no feature specified, or all features are
104 // removed by `Remove`.
106 // Note that this can be true even if `aFeatures` parameter of `Tokenize`
107 // is not empty, in case it contains no feature with non-empty name.
108 bool IsEmpty() const { return tokenizedFeatures_
.empty(); }
110 // Stringify the map into `aOutput`.
111 // The result can be parsed again with `Tokenize`.
112 void Stringify(nsAutoCString
& aOutput
);
116 // Returns true if `text` does not contain any character that gets modified by
118 static bool IsLowerCase(const char* text
);
121 static int32_t ParseIntegerWithFallback(const nsCString
& aValue
);
122 static bool ParseBool(const nsCString
& aValue
);
124 // A map from feature name to feature value.
125 // If value is not provided, it's empty string.
126 mozilla::HashMap
<nsCString
, nsCString
> tokenizedFeatures_
;
130 } // namespace mozilla
132 #endif // #ifndef mozilla_dom_WindowFeatures_h