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