Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / search_host_to_urls_map.h
1 // Copyright 2014 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 COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
6 #define COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
7
8 #include <functional>
9 #include <map>
10 #include <string>
11
12 #include "base/containers/flat_set.h"
13 #include "base/strings/string_piece.h"
14 #include "components/search_engines/template_url.h"
15
16 // Holds the host to template url mappings for the search providers. WARNING:
17 // This class does not own any TemplateURLs passed to it and it is up to the
18 // caller to ensure the right lifetime of them.
19 class SearchHostToURLsMap {
20  public:
21   using TemplateURLSet = base::flat_set<TemplateURL*>;
22
23   SearchHostToURLsMap();
24
25   SearchHostToURLsMap(const SearchHostToURLsMap&) = delete;
26   SearchHostToURLsMap& operator=(const SearchHostToURLsMap&) = delete;
27
28   ~SearchHostToURLsMap();
29
30   // Initializes the map.
31   void Init(const TemplateURL::OwnedTemplateURLVector& template_urls,
32             const SearchTermsData& search_terms_data);
33
34   // Adds a new TemplateURL to the map. Since |template_url| is owned
35   // externally, Remove or RemoveAll should be called if it becomes invalid.
36   void Add(TemplateURL* template_url,
37            const SearchTermsData& search_terms_data);
38
39   // Removes the TemplateURL from the lookup.
40   void Remove(const TemplateURL* template_url);
41
42   // Returns the best TemplateURL found with a URL using the specified |host|,
43   // or nullptr if there are no such TemplateURLs
44   TemplateURL* GetTemplateURLForHost(base::StringPiece host);
45
46   // Return the TemplateURLSet for the given the |host| or NULL if there are
47   // none.
48   TemplateURLSet* GetURLsForHost(base::StringPiece host);
49
50  private:
51   friend class SearchHostToURLsMapTest;
52
53   typedef std::map<std::string, TemplateURLSet, std::less<>> HostToURLsMap;
54
55   // Adds many URLs to the map.
56   void Add(const TemplateURL::OwnedTemplateURLVector& template_urls,
57            const SearchTermsData& search_terms_data);
58
59   // Maps from host to set of TemplateURLs whose search url host is host.
60   HostToURLsMap host_to_urls_map_;
61
62   // The security origin for the default search provider.
63   std::string default_search_origin_;
64
65   // Has Init been called?
66   bool initialized_;
67 };
68
69 #endif  // COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_