1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_URL_URL_SEARCH_PARAMS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_URL_URL_SEARCH_PARAMS_H_
7 
8 #include <base/gtest_prod_util.h>
9 #include <utility>
10 #include "third_party/blink/renderer/bindings/core/v8/iterable.h"
11 #include "third_party/blink/renderer/bindings/core/v8/usv_string_sequence_sequence_or_usv_string_usv_string_record_or_usv_string.h"
12 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
13 #include "third_party/blink/renderer/platform/heap/handle.h"
14 #include "third_party/blink/renderer/platform/network/encoded_form_data.h"
15 #include "third_party/blink/renderer/platform/wtf/forward.h"
16 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
17 
18 namespace blink {
19 
20 class ExceptionState;
21 class DOMURL;
22 
23 typedef USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString
24     URLSearchParamsInit;
25 
26 class CORE_EXPORT URLSearchParams final : public ScriptWrappable,
27                                           public PairIterable<String, String> {
28   DEFINE_WRAPPERTYPEINFO();
29 
30  public:
31   static URLSearchParams* Create(const URLSearchParamsInit&, ExceptionState&);
32   static URLSearchParams* Create(const Vector<std::pair<String, String>>&,
33                                  ExceptionState&);
34   static URLSearchParams* Create(const Vector<Vector<String>>&,
35                                  ExceptionState&);
36 
37   static URLSearchParams* Create(const String& query_string,
38                                  DOMURL* url_object = nullptr) {
39     return MakeGarbageCollected<URLSearchParams>(query_string, url_object);
40   }
41 
42   explicit URLSearchParams(const String&, DOMURL* = nullptr);
43   ~URLSearchParams() override;
44 
45   // URLSearchParams interface methods
46   String toString() const;
47   void append(const String& name, const String& value);
48   void deleteAllWithName(const String&);
49   String get(const String&) const;
50   Vector<String> getAll(const String&) const;
51   bool has(const String&) const;
52   void set(const String& name, const String& value);
53   void sort();
54   void SetInputWithoutUpdate(const String&);
55 
56   // Internal helpers
57   scoped_refptr<EncodedFormData> ToEncodedFormData() const;
Params()58   const Vector<std::pair<String, String>>& Params() const { return params_; }
59 
60 #if DCHECK_IS_ON()
61   DOMURL* UrlObject() const;
62 #endif
63 
64   void Trace(Visitor*) override;
65 
66  private:
67   FRIEND_TEST_ALL_PREFIXES(URLSearchParamsTest, EncodedFormData);
68 
69   void RunUpdateSteps();
70   IterationSource* StartIteration(ScriptState*, ExceptionState&) override;
71   void EncodeAsFormData(Vector<char>&) const;
72 
73   void AppendWithoutUpdate(const String& name, const String& value);
74 
75   Vector<std::pair<String, String>> params_;
76 
77   WeakMember<DOMURL> url_object_;
78 };
79 
80 }  // namespace blink
81 
82 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_URL_URL_SEARCH_PARAMS_H_
83