Home | History | Annotate | Download | only in search_engines
      1 // Copyright (c) 2010 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 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/basictypes.h"
     15 
     16 class SearchTermsData;
     17 class TemplateURL;
     18 
     19 // Holds the host to template url mappings for the search providers. WARNING:
     20 // This class does not own any TemplateURLs passed to it and it is up to the
     21 // caller to ensure the right lifetime of them.
     22 class SearchHostToURLsMap {
     23  public:
     24   typedef std::set<const TemplateURL*> TemplateURLSet;
     25 
     26   SearchHostToURLsMap();
     27   ~SearchHostToURLsMap();
     28 
     29   // Initializes the map.
     30   void Init(const std::vector<const TemplateURL*>& template_urls,
     31             const SearchTermsData& search_terms_data);
     32 
     33   // Adds a new TemplateURL to the map. Since |template_url| is owned
     34   // externally, Remove or RemoveAll should be called if it becomes invalid.
     35   void Add(const TemplateURL* template_url,
     36            const SearchTermsData& search_terms_data);
     37 
     38   // Removes the TemplateURL from the lookup.
     39   void Remove(const TemplateURL* template_url);
     40 
     41   // Updates information in an existing TemplateURL. Note: Using Remove and
     42   // then Add separately would lead to race conditions in reading because the
     43   // lock would be released inbetween the calls.
     44   void Update(const TemplateURL* existing_turl,
     45               const TemplateURL& new_values,
     46               const SearchTermsData& search_terms_data);
     47 
     48   // Updates all search providers which have a google base url.
     49   void UpdateGoogleBaseURLs(const SearchTermsData& search_terms_data);
     50 
     51   // Returns the first TemplateURL found with a URL using the specified |host|,
     52   // or NULL if there are no such TemplateURLs
     53   const TemplateURL* GetTemplateURLForHost(const std::string& host) const;
     54 
     55   // Return the TemplateURLSet for the given the |host| or NULL if there are
     56   // none.
     57   const TemplateURLSet* GetURLsForHost(const std::string& host) const;
     58 
     59  private:
     60   friend class SearchHostToURLsMapTest;
     61 
     62   typedef std::map<std::string, TemplateURLSet> HostToURLsMap;
     63 
     64   // Removes the given template_url using only the pointer instead of the value.
     65   // This is useful when the value may have changed before being updated in the
     66   // map. (Specifically when the GoogleBaseURLValue changes.)
     67   void RemoveByPointer(const TemplateURL* template_url);
     68 
     69   // Maps from host to set of TemplateURLs whose search url host is host.
     70   HostToURLsMap host_to_urls_map_;
     71 
     72   // The security origin for the default search provider.
     73   std::string default_search_origin_;
     74 
     75   // Has Init been called?
     76   bool initialized_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMap);
     79 };
     80 
     81 #endif  // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_HOST_TO_URLS_MAP_H_
     82