• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
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 NET_BASE_URL_SEARCH_PARAMS_H_
6 #define NET_BASE_URL_SEARCH_PARAMS_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "base/containers/flat_set.h"
13 #include "net/base/net_export.h"
14 #include "url/gurl.h"
15 
16 namespace net {
17 
18 // Class that exposes the following functionality to parse a UTF-8, percent
19 // encoded url's `query`
20 //  - parse `query` parameters into a list of `(key,value)` pairs keeping the
21 //    same order as in `query`. While parsing the url's `query` the class does
22 //    percent decoding of both the `key` and `value`.
23 //  - stable sort of the `(key,value)` entries in the list based on `key`
24 //  - deletion of all `(key,value)` pairs for which `key`is part of a set of
25 //    specified `keys`
26 //  - deletion of all `(key, values)` pairs except pairs for which `key` is part
27 //    of a set of specified `keys`
28 class NET_EXPORT UrlSearchParams {
29  public:
30   explicit UrlSearchParams(const GURL& url);
31   UrlSearchParams(const UrlSearchParams&) = delete;
32   ~UrlSearchParams();
33   UrlSearchParams& operator=(UrlSearchParams&) = delete;
34   UrlSearchParams& operator=(const UrlSearchParams&) = delete;
35   // Runs a stable sort by key of all of the query search params.
36   // The stable sort will keep the order of query search params with the same
37   // key the same as in the original url.
38   void Sort();
39   // Deletes all query search params with specified keys.
40   void DeleteAllWithNames(const base::flat_set<std::string>& names);
41   // Deletes all query search params except the ones with specified keys.
42   void DeleteAllExceptWithNames(const base::flat_set<std::string>& names);
43   // Returns a list of key-value pairs representing all query search params.
44   const std::vector<std::pair<std::string, std::string>>& params() const;
45 
46  private:
47   // Keeps track of all key-value pairs representing all query search params.
48   // The order from the original url is important.
49   std::vector<std::pair<std::string, std::string>> params_;
50 };
51 
52 }  // namespace net
53 #endif  // NET_BASE_URL_SEARCH_PARAMS_H_
54