Home | History | Annotate | Download | only in search_engines
      1 // Copyright (c) 2012 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 CHROME_BROWSER_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
      6 #define CHROME_BROWSER_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 "chrome/browser/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               const SearchTermsData& search_terms_data);
     37 
     38   // Returns the first TemplateURL found with a URL using the specified |host|,
     39   // or NULL if there are no such TemplateURLs
     40   TemplateURL* GetTemplateURLForHost(const std::string& host);
     41 
     42   // Return the TemplateURLSet for the given the |host| or NULL if there are
     43   // none.
     44   TemplateURLSet* GetURLsForHost(const std::string& host);
     45 
     46  private:
     47   friend class SearchHostToURLsMapTest;
     48 
     49   typedef std::map<std::string, TemplateURLSet> HostToURLsMap;
     50 
     51   // Adds many URLs to the map.
     52   void Add(const TemplateURLService::TemplateURLVector& template_urls,
     53            const SearchTermsData& search_terms_data);
     54 
     55   // Removes the given template_url using only the pointer instead of the value.
     56   // This is useful when the value may have changed before being updated in the
     57   // map. (Specifically when the GoogleBaseURLValue changes.)
     58   void RemoveByPointer(TemplateURL* template_url);
     59 
     60   // Maps from host to set of TemplateURLs whose search url host is host.
     61   HostToURLsMap host_to_urls_map_;
     62 
     63   // The security origin for the default search provider.
     64   std::string default_search_origin_;
     65 
     66   // Has Init been called?
     67   bool initialized_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMap);
     70 };
     71 
     72 #endif  // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
     73