Line data Source code
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/. */
6 :
7 : #ifndef mozilla_dom_URLSearchParams_h
8 : #define mozilla_dom_URLSearchParams_h
9 :
10 : #include "js/StructuredClone.h"
11 : #include "mozilla/dom/BindingDeclarations.h"
12 : #include "mozilla/ErrorResult.h"
13 : #include "nsCycleCollectionParticipant.h"
14 : #include "nsWrapperCache.h"
15 : #include "nsISupports.h"
16 : #include "nsIXMLHttpRequest.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 : class URLSearchParams;
22 : class USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString;
23 :
24 3 : class URLSearchParamsObserver : public nsISupports
25 : {
26 : public:
27 0 : virtual ~URLSearchParamsObserver() {}
28 :
29 : virtual void URLSearchParamsUpdated(URLSearchParams* aFromThis) = 0;
30 : };
31 :
32 : // This class is used in BasePrincipal and it's _extremely_ important that the
33 : // attributes are kept in the correct order. If this changes, please, update
34 : // BasePrincipal code.
35 :
36 : class URLParams final
37 : {
38 : public:
39 549 : URLParams() {}
40 :
41 549 : ~URLParams()
42 549 : {
43 549 : DeleteAll();
44 549 : }
45 :
46 0 : class ForEachIterator
47 : {
48 : public:
49 : virtual bool
50 : URLParamsIterator(const nsString& aName, const nsString& aValue) = 0;
51 : };
52 :
53 : void
54 : ParseInput(const nsACString& aInput);
55 :
56 : bool
57 0 : ForEach(ForEachIterator& aIterator) const
58 : {
59 0 : for (uint32_t i = 0; i < mParams.Length(); ++i) {
60 0 : if (!aIterator.URLParamsIterator(mParams[i].mKey, mParams[i].mValue)) {
61 0 : return false;
62 : }
63 : }
64 :
65 0 : return true;
66 : }
67 :
68 : void Serialize(nsAString& aValue) const;
69 :
70 : void Get(const nsAString& aName, nsString& aRetval);
71 :
72 : void GetAll(const nsAString& aName, nsTArray<nsString>& aRetval);
73 :
74 : void Set(const nsAString& aName, const nsAString& aValue);
75 :
76 : void Append(const nsAString& aName, const nsAString& aValue);
77 :
78 : bool Has(const nsAString& aName);
79 :
80 : // Returns true if aName was found and deleted, false otherwise.
81 : bool Delete(const nsAString& aName);
82 :
83 549 : void DeleteAll()
84 : {
85 549 : mParams.Clear();
86 549 : }
87 :
88 0 : uint32_t Length() const
89 : {
90 0 : return mParams.Length();
91 : }
92 :
93 0 : const nsAString& GetKeyAtIndex(uint32_t aIndex) const
94 : {
95 0 : MOZ_ASSERT(aIndex < mParams.Length());
96 0 : return mParams[aIndex].mKey;
97 : }
98 :
99 0 : const nsAString& GetValueAtIndex(uint32_t aIndex) const
100 : {
101 0 : MOZ_ASSERT(aIndex < mParams.Length());
102 0 : return mParams[aIndex].mValue;
103 : }
104 :
105 : nsresult Sort();
106 :
107 : bool
108 : ReadStructuredClone(JSStructuredCloneReader* aReader);
109 :
110 : bool
111 : WriteStructuredClone(JSStructuredCloneWriter* aWriter) const;
112 :
113 : private:
114 : void DecodeString(const nsACString& aInput, nsAString& aOutput);
115 : void ConvertString(const nsACString& aInput, nsAString& aOutput);
116 :
117 22 : struct Param
118 : {
119 : nsString mKey;
120 : nsString mValue;
121 : };
122 :
123 : nsTArray<Param> mParams;
124 : };
125 :
126 : class URLSearchParams final : public nsIXHRSendable,
127 : public nsWrapperCache
128 : {
129 : ~URLSearchParams();
130 :
131 : public:
132 : NS_DECL_NSIXHRSENDABLE
133 :
134 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
135 3 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URLSearchParams)
136 :
137 : explicit URLSearchParams(nsISupports* aParent,
138 : URLSearchParamsObserver* aObserver=nullptr);
139 :
140 : // WebIDL methods
141 0 : nsISupports* GetParentObject() const
142 : {
143 0 : return mParent;
144 : }
145 :
146 : virtual JSObject*
147 : WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
148 :
149 : static already_AddRefed<URLSearchParams>
150 : Constructor(const GlobalObject& aGlobal,
151 : const USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString& aInit,
152 : ErrorResult& aRv);
153 :
154 : void ParseInput(const nsACString& aInput);
155 :
156 : void Serialize(nsAString& aValue) const;
157 :
158 : void Get(const nsAString& aName, nsString& aRetval);
159 :
160 : void GetAll(const nsAString& aName, nsTArray<nsString>& aRetval);
161 :
162 : void Set(const nsAString& aName, const nsAString& aValue);
163 :
164 : void Append(const nsAString& aName, const nsAString& aValue);
165 :
166 : bool Has(const nsAString& aName);
167 :
168 : void Delete(const nsAString& aName);
169 :
170 : uint32_t GetIterableLength() const;
171 : const nsAString& GetKeyAtIndex(uint32_t aIndex) const;
172 : const nsAString& GetValueAtIndex(uint32_t aIndex) const;
173 :
174 : void Sort(ErrorResult& aRv);
175 :
176 0 : void Stringify(nsString& aRetval) const
177 : {
178 0 : Serialize(aRetval);
179 0 : }
180 :
181 : typedef URLParams::ForEachIterator ForEachIterator;
182 :
183 : bool
184 : ForEach(ForEachIterator& aIterator) const
185 : {
186 : return mParams->ForEach(aIterator);
187 :
188 : return true;
189 : }
190 :
191 : bool
192 : ReadStructuredClone(JSStructuredCloneReader* aReader);
193 :
194 : bool
195 : WriteStructuredClone(JSStructuredCloneWriter* aWriter) const;
196 :
197 : private:
198 : void AppendInternal(const nsAString& aName, const nsAString& aValue);
199 :
200 : void DeleteAll();
201 :
202 : void NotifyObserver();
203 :
204 : UniquePtr<URLParams> mParams;
205 : nsCOMPtr<nsISupports> mParent;
206 : RefPtr<URLSearchParamsObserver> mObserver;
207 : };
208 :
209 : } // namespace dom
210 : } // namespace mozilla
211 :
212 : #endif /* mozilla_dom_URLSearchParams_h */
|