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.
5 #ifndef COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
6 #define COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
12 #include "base/containers/flat_set.h"
13 #include "base/strings/string_piece.h"
14 #include "components/search_engines/template_url.h"
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 {
21 using TemplateURLSet = base::flat_set<TemplateURL*>;
23 SearchHostToURLsMap();
25 SearchHostToURLsMap(const SearchHostToURLsMap&) = delete;
26 SearchHostToURLsMap& operator=(const SearchHostToURLsMap&) = delete;
28 ~SearchHostToURLsMap();
30 // Initializes the map.
31 void Init(const TemplateURL::OwnedTemplateURLVector& template_urls,
32 const SearchTermsData& search_terms_data);
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);
39 // Removes the TemplateURL from the lookup.
40 void Remove(const TemplateURL* template_url);
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);
46 // Return the TemplateURLSet for the given the |host| or NULL if there are
48 TemplateURLSet* GetURLsForHost(base::StringPiece host);
51 friend class SearchHostToURLsMapTest;
53 typedef std::map<std::string, TemplateURLSet, std::less<>> HostToURLsMap;
55 // Adds many URLs to the map.
56 void Add(const TemplateURL::OwnedTemplateURLVector& template_urls,
57 const SearchTermsData& search_terms_data);
59 // Maps from host to set of TemplateURLs whose search url host is host.
60 HostToURLsMap host_to_urls_map_;
62 // The security origin for the default search provider.
63 std::string default_search_origin_;
65 // Has Init been called?
69 #endif // COMPONENTS_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_