| OLD | NEW | 
|    1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ |    5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ | 
|    6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ |    6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ | 
|    7 #pragma once |    7 #pragma once | 
|    8  |    8  | 
|    9 #include <map> |    9 #include <map> | 
|   10 #include <set> |   10 #include <set> | 
|   11 #include <string> |   11 #include <string> | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
|   38 // KeywordAutocomplete. |   38 // KeywordAutocomplete. | 
|   39 // |   39 // | 
|   40 // TemplateURLModel stores a vector of TemplateURLs. The TemplateURLs are |   40 // TemplateURLModel stores a vector of TemplateURLs. The TemplateURLs are | 
|   41 // persisted to the database maintained by WebDataService. *ALL* mutations |   41 // persisted to the database maintained by WebDataService. *ALL* mutations | 
|   42 // to the TemplateURLs must funnel through TemplateURLModel. This allows |   42 // to the TemplateURLs must funnel through TemplateURLModel. This allows | 
|   43 // TemplateURLModel to notify listeners of changes as well as keep the |   43 // TemplateURLModel to notify listeners of changes as well as keep the | 
|   44 // database in sync. |   44 // database in sync. | 
|   45 // |   45 // | 
|   46 // There is a TemplateURLModel per Profile. |   46 // There is a TemplateURLModel per Profile. | 
|   47 // |   47 // | 
|   48 // TemplateURLModel does not load the vector of TemplateURLs in its |   48 // TemplateURLModel does not load the vector of TemplateURLs in it's | 
|   49 // constructor (except for testing). Use the Load method to trigger a load. |   49 // constructor (except for testing). Use the Load method to trigger a load. | 
|   50 // When TemplateURLModel has completed loading, observers are notified via |   50 // When TemplateURLModel has completed loading, observers are notified via | 
|   51 // OnTemplateURLModelChanged as well as the TEMPLATE_URL_MODEL_LOADED |   51 // OnTemplateURLModelChanged as well as the TEMPLATE_URL_MODEL_LOADED | 
|   52 // notification message. |   52 // notification message. | 
|   53 // |   53 // | 
|   54 // TemplateURLModel takes ownership of any TemplateURL passed to it. If there |   54 // TemplateURLModel takes ownership of any TemplateURL passed to it. If there | 
|   55 // is a WebDataService, deletion is handled by WebDataService, otherwise |   55 // is a WebDataService, deletion is handled by WebDataService, otherwise | 
|   56 // TemplateURLModel handles deletion. |   56 // TemplateURLModel handles deletion. | 
|   57  |   57  | 
|   58 class TemplateURLModel : public WebDataServiceConsumer, |   58 class TemplateURLModel : public WebDataServiceConsumer, | 
|   59                          public NotificationObserver { |   59                          public NotificationObserver { | 
|   60  public: |   60  public: | 
|   61   typedef std::map<std::string, std::string> QueryTerms; |   61   typedef std::map<std::string, std::string> QueryTerms; | 
|   62  |   62  | 
|   63   // Struct used for initializing the data store with fake data. |   63   // Struct used for initializing the data store with fake data. | 
|   64   // Each initializer is mapped to a TemplateURL. |   64   // Each initializer is mapped to a TemplateURL. | 
|   65   struct Initializer { |   65   struct Initializer { | 
|   66     const char* const keyword; |   66     const wchar_t* const keyword; | 
|   67     const char* const url; |   67     const char* const url; | 
|   68     const char* const content; |   68     const wchar_t* const content; | 
|   69   }; |   69   }; | 
|   70  |   70  | 
|   71   explicit TemplateURLModel(Profile* profile); |   71   explicit TemplateURLModel(Profile* profile); | 
|   72   // The following is for testing. |   72   // The following is for testing. | 
|   73   TemplateURLModel(const Initializer* initializers, const int count); |   73   TemplateURLModel(const Initializer* initializers, const int count); | 
|   74   virtual ~TemplateURLModel(); |   74   virtual ~TemplateURLModel(); | 
|   75  |   75  | 
|   76   // Generates a suitable keyword for the specified url.  Returns an empty |   76   // Generates a suitable keyword for the specified url.  Returns an empty | 
|   77   // string if a keyword couldn't be generated.  If |autodetected| is true, we |   77   // string if a keyword couldn't be generated.  If |autodetected| is true, we | 
|   78   // don't generate keywords for a variety of situations where we would probably |   78   // don't generate keywords for a variety of situations where we would probably | 
|   79   // not want to auto-add keywords, such as keywords for searches on pages that |   79   // not want to auto-add keywords, such as keywords for searches on pages that | 
|   80   // themselves come from form submissions. |   80   // themselves come from form submissions. | 
|   81   static string16 GenerateKeyword(const GURL& url, bool autodetected); |   81   static std::wstring GenerateKeyword(const GURL& url, bool autodetected); | 
|   82  |   82  | 
|   83   // Removes any unnecessary characters from a user input keyword. |   83   // Removes any unnecessary characters from a user input keyword. | 
|   84   // This removes the leading scheme, "www." and any trailing slash. |   84   // This removes the leading scheme, "www." and any trailing slash. | 
|   85   static string16 CleanUserInputKeyword(const string16& keyword); |   85   static std::wstring CleanUserInputKeyword(const std::wstring& keyword); | 
|   86  |   86  | 
|   87   // Returns the search url for t_url.  Returns an empty GURL if t_url has no |   87   // Returns the search url for t_url.  Returns an empty GURL if t_url has no | 
|   88   // url(). |   88   // url(). | 
|   89   static GURL GenerateSearchURL(const TemplateURL* t_url); |   89   static GURL GenerateSearchURL(const TemplateURL* t_url); | 
|   90  |   90  | 
|   91   // Just like GenerateSearchURL except that it takes SearchTermsData to supply |   91   // Just like GenerateSearchURL except that it takes SearchTermsData to supply | 
|   92   // the data for some search terms. Most of the time GenerateSearchURL should |   92   // the data for some search terms. Most of the time GenerateSearchURL should | 
|   93   // be called. |   93   // be called. | 
|   94   static GURL GenerateSearchURLUsingTermsData( |   94   static GURL GenerateSearchURLUsingTermsData( | 
|   95       const TemplateURL* t_url, |   95       const TemplateURL* t_url, | 
|   96       const SearchTermsData& search_terms_data); |   96       const SearchTermsData& search_terms_data); | 
|   97  |   97  | 
|   98   // Returns true if there is no TemplateURL that conflicts with the |   98   // Returns true if there is no TemplateURL that conflicts with the | 
|   99   // keyword/url pair, or there is one but it can be replaced. If there is an |   99   // keyword/url pair, or there is one but it can be replaced. If there is an | 
|  100   // existing keyword that can be replaced and template_url_to_replace is |  100   // existing keyword that can be replaced and template_url_to_replace is | 
|  101   // non-NULL, template_url_to_replace is set to the keyword to replace. |  101   // non-NULL, template_url_to_replace is set to the keyword to replace. | 
|  102   // |  102   // | 
|  103   // url gives the url of the search query. The url is used to avoid generating |  103   // url gives the url of the search query. The url is used to avoid generating | 
|  104   // a TemplateURL for an existing TemplateURL that shares the same host. |  104   // a TemplateURL for an existing TemplateURL that shares the same host. | 
|  105   bool CanReplaceKeyword(const string16& keyword, |  105   bool CanReplaceKeyword(const std::wstring& keyword, | 
|  106                          const GURL& url, |  106                          const GURL& url, | 
|  107                          const TemplateURL** template_url_to_replace); |  107                          const TemplateURL** template_url_to_replace); | 
|  108  |  108  | 
|  109   // Returns (in |matches|) all keywords beginning with |prefix|, sorted |  109   // Returns (in |matches|) all keywords beginning with |prefix|, sorted | 
|  110   // shortest-first. If support_replacement_only is true, only keywords that |  110   // shortest-first. If support_replacement_only is true, only keywords that | 
|  111   // support replacement are returned. |  111   // support replacement are returned. | 
|  112   void FindMatchingKeywords(const string16& prefix, |  112   void FindMatchingKeywords(const std::wstring& prefix, | 
|  113                             bool support_replacement_only, |  113                             bool support_replacement_only, | 
|  114                             std::vector<string16>* matches) const; |  114                             std::vector<std::wstring>* matches) const; | 
|  115  |  115  | 
|  116   // Looks up |keyword| and returns the element it maps to.  Returns NULL if |  116   // Looks up |keyword| and returns the element it maps to.  Returns NULL if | 
|  117   // the keyword was not found. |  117   // the keyword was not found. | 
|  118   // The caller should not try to delete the returned pointer; the data store |  118   // The caller should not try to delete the returned pointer; the data store | 
|  119   // retains ownership of it. |  119   // retains ownership of it. | 
|  120   const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; |  120   const TemplateURL* GetTemplateURLForKeyword( | 
 |  121     const std::wstring& keyword) const; | 
|  121  |  122  | 
|  122   // Returns the first TemplateURL found with a URL using the specified |host|, |  123   // Returns the first TemplateURL found with a URL using the specified |host|, | 
|  123   // or NULL if there are no such TemplateURLs |  124   // or NULL if there are no such TemplateURLs | 
|  124   const TemplateURL* GetTemplateURLForHost(const std::string& host) const; |  125   const TemplateURL* GetTemplateURLForHost(const std::string& host) const; | 
|  125  |  126  | 
|  126   // Adds a new TemplateURL to this model. TemplateURLModel will own the |  127   // Adds a new TemplateURL to this model. TemplateURLModel will own the | 
|  127   // reference, and delete it when the TemplateURL is removed. |  128   // reference, and delete it when the TemplateURL is removed. | 
|  128   void Add(TemplateURL* template_url); |  129   void Add(TemplateURL* template_url); | 
|  129  |  130  | 
|  130   // Removes the keyword from the model. This deletes the supplied TemplateURL. |  131   // Removes the keyword from the model. This deletes the supplied TemplateURL. | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
|  159   // by TemplateURLModel and should not be deleted. |  160   // by TemplateURLModel and should not be deleted. | 
|  160   std::vector<const TemplateURL*> GetTemplateURLs() const; |  161   std::vector<const TemplateURL*> GetTemplateURLs() const; | 
|  161  |  162  | 
|  162   // Increment the usage count of a keyword. |  163   // Increment the usage count of a keyword. | 
|  163   // Called when a URL is loaded that was generated from a keyword. |  164   // Called when a URL is loaded that was generated from a keyword. | 
|  164   void IncrementUsageCount(const TemplateURL* url); |  165   void IncrementUsageCount(const TemplateURL* url); | 
|  165  |  166  | 
|  166   // Resets the title, keyword and search url of the specified TemplateURL. |  167   // Resets the title, keyword and search url of the specified TemplateURL. | 
|  167   // The TemplateURL is marked as not replaceable. |  168   // The TemplateURL is marked as not replaceable. | 
|  168   void ResetTemplateURL(const TemplateURL* url, |  169   void ResetTemplateURL(const TemplateURL* url, | 
|  169                         const string16& title, |  170                         const std::wstring& title, | 
|  170                         const string16& keyword, |  171                         const std::wstring& keyword, | 
|  171                         const std::string& search_url); |  172                         const std::string& search_url); | 
|  172  |  173  | 
|  173   // Return true if the given |url| can be made the default. |  174   // Return true if the given |url| can be made the default. | 
|  174   bool CanMakeDefault(const TemplateURL* url); |  175   bool CanMakeDefault(const TemplateURL* url); | 
|  175  |  176  | 
|  176   // Set the default search provider.  |url| may be null. |  177   // Set the default search provider.  |url| may be null. | 
|  177   // This will assert if the default search is managed; the UI should not be |  178   // This will assert if the default search is managed; the UI should not be | 
|  178   // invoking this method in that situation. |  179   // invoking this method in that situation. | 
|  179   void SetDefaultSearchProvider(const TemplateURL* url); |  180   void SetDefaultSearchProvider(const TemplateURL* url); | 
|  180  |  181  | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|  203  |  204  | 
|  204   // Notification that the keywords have been loaded. |  205   // Notification that the keywords have been loaded. | 
|  205   // This is invoked from WebDataService, and should not be directly |  206   // This is invoked from WebDataService, and should not be directly | 
|  206   // invoked. |  207   // invoked. | 
|  207   virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, |  208   virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, | 
|  208                                            const WDTypedResult* result); |  209                                            const WDTypedResult* result); | 
|  209  |  210  | 
|  210   // Returns the locale-direction-adjusted short name for the given keyword. |  211   // Returns the locale-direction-adjusted short name for the given keyword. | 
|  211   // Also sets the out param to indicate whether the keyword belongs to an |  212   // Also sets the out param to indicate whether the keyword belongs to an | 
|  212   // extension. |  213   // extension. | 
|  213   string16 GetKeywordShortName(const string16& keyword, |  214   std::wstring GetKeywordShortName(const std::wstring& keyword, | 
|  214                                bool* is_extension_keyword); |  215                                    bool* is_extension_keyword); | 
|  215  |  216  | 
|  216   // NotificationObserver method. TemplateURLModel listens for three |  217   // NotificationObserver method. TemplateURLModel listens for three | 
|  217   // notification types: |  218   // notification types: | 
|  218   // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit |  219   // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit | 
|  219   //   corresponds to a keyword. |  220   //   corresponds to a keyword. | 
|  220   // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing |  221   // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing | 
|  221   //   a google base url replacement term. |  222   //   a google base url replacement term. | 
|  222   // . PREF_CHANGED: checks whether the default search engine has changed. |  223   // . PREF_CHANGED: checks whether the default search engine has changed. | 
|  223   virtual void Observe(NotificationType type, |  224   virtual void Observe(NotificationType type, | 
|  224                        const NotificationSource& source, |  225                        const NotificationSource& source, | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
|  237   // Registers the preferences used to save a TemplateURL to prefs. |  238   // Registers the preferences used to save a TemplateURL to prefs. | 
|  238   static void RegisterUserPrefs(PrefService* prefs); |  239   static void RegisterUserPrefs(PrefService* prefs); | 
|  239  |  240  | 
|  240  protected: |  241  protected: | 
|  241   // Cover method for the method of the same name on the HistoryService. |  242   // Cover method for the method of the same name on the HistoryService. | 
|  242   // url is the one that was visited with the given search terms. |  243   // url is the one that was visited with the given search terms. | 
|  243   // |  244   // | 
|  244   // This exists and is virtual for testing. |  245   // This exists and is virtual for testing. | 
|  245   virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, |  246   virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, | 
|  246                                            const GURL& url, |  247                                            const GURL& url, | 
|  247                                            const string16& term); |  248                                            const std::wstring& term); | 
|  248  |  249  | 
|  249  private: |  250  private: | 
|  250   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, BuildQueryTerms); |  251   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, BuildQueryTerms); | 
|  251   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, TestManagedDefaultSearch); |  252   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, TestManagedDefaultSearch); | 
|  252   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, |  253   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, | 
|  253                            UpdateKeywordSearchTermsForURL); |  254                            UpdateKeywordSearchTermsForURL); | 
|  254   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, |  255   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, | 
|  255                            DontUpdateKeywordSearchForNonReplaceable); |  256                            DontUpdateKeywordSearchForNonReplaceable); | 
|  256   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, ChangeGoogleBaseValue); |  257   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, ChangeGoogleBaseValue); | 
|  257   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, MergeDeletesUnusedProviders); |  258   FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, MergeDeletesUnusedProviders); | 
|  258   friend class TemplateURLModelTestUtil; |  259   friend class TemplateURLModelTestUtil; | 
|  259  |  260  | 
|  260   typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; |  261   typedef std::map<std::wstring, const TemplateURL*> KeywordToTemplateMap; | 
|  261   typedef std::vector<const TemplateURL*> TemplateURLVector; |  262   typedef std::vector<const TemplateURL*> TemplateURLVector; | 
|  262  |  263  | 
|  263   // Helper functor for FindMatchingKeywords(), for finding the range of |  264   // Helper functor for FindMatchingKeywords(), for finding the range of | 
|  264   // keywords which begin with a prefix. |  265   // keywords which begin with a prefix. | 
|  265   class LessWithPrefix; |  266   class LessWithPrefix; | 
|  266  |  267  | 
|  267   void Init(const Initializer* initializers, int num_initializers); |  268   void Init(const Initializer* initializers, int num_initializers); | 
|  268  |  269  | 
|  269   void RemoveFromMaps(const TemplateURL* template_url); |  270   void RemoveFromMaps(const TemplateURL* template_url); | 
|  270  |  271  | 
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  426   // increasing integer that is initialized from the database. |  427   // increasing integer that is initialized from the database. | 
|  427   TemplateURLID next_id_; |  428   TemplateURLID next_id_; | 
|  428  |  429  | 
|  429   // List of extension IDs waiting for Load to have keywords registered. |  430   // List of extension IDs waiting for Load to have keywords registered. | 
|  430   std::vector<std::string> pending_extension_ids_; |  431   std::vector<std::string> pending_extension_ids_; | 
|  431  |  432  | 
|  432   DISALLOW_COPY_AND_ASSIGN(TemplateURLModel); |  433   DISALLOW_COPY_AND_ASSIGN(TemplateURLModel); | 
|  433 }; |  434 }; | 
|  434  |  435  | 
|  435 #endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ |  436 #endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ | 
| OLD | NEW |