| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 it's | 48 // TemplateURLModel does not load the vector of TemplateURLs in its |
| 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 wchar_t* const keyword; | 66 const char* const keyword; |
| 67 const char* const url; | 67 const char* const url; |
| 68 const wchar_t* const content; | 68 const char* 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 std::wstring GenerateKeyword(const GURL& url, bool autodetected); | 81 static string16 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 std::wstring CleanUserInputKeyword(const std::wstring& keyword); | 85 static string16 CleanUserInputKeyword(const string16& 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 std::wstring& keyword, | 105 bool CanReplaceKeyword(const string16& 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 std::wstring& prefix, | 112 void FindMatchingKeywords(const string16& prefix, |
| 113 bool support_replacement_only, | 113 bool support_replacement_only, |
| 114 std::vector<std::wstring>* matches) const; | 114 std::vector<string16>* 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( | 120 const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; |
| 121 const std::wstring& keyword) const; | |
| 122 | 121 |
| 123 // Returns the first TemplateURL found with a URL using the specified |host|, | 122 // Returns the first TemplateURL found with a URL using the specified |host|, |
| 124 // or NULL if there are no such TemplateURLs | 123 // or NULL if there are no such TemplateURLs |
| 125 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; | 124 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; |
| 126 | 125 |
| 127 // Adds a new TemplateURL to this model. TemplateURLModel will own the | 126 // Adds a new TemplateURL to this model. TemplateURLModel will own the |
| 128 // reference, and delete it when the TemplateURL is removed. | 127 // reference, and delete it when the TemplateURL is removed. |
| 129 void Add(TemplateURL* template_url); | 128 void Add(TemplateURL* template_url); |
| 130 | 129 |
| 131 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 130 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 160 // by TemplateURLModel and should not be deleted. | 159 // by TemplateURLModel and should not be deleted. |
| 161 std::vector<const TemplateURL*> GetTemplateURLs() const; | 160 std::vector<const TemplateURL*> GetTemplateURLs() const; |
| 162 | 161 |
| 163 // Increment the usage count of a keyword. | 162 // Increment the usage count of a keyword. |
| 164 // Called when a URL is loaded that was generated from a keyword. | 163 // Called when a URL is loaded that was generated from a keyword. |
| 165 void IncrementUsageCount(const TemplateURL* url); | 164 void IncrementUsageCount(const TemplateURL* url); |
| 166 | 165 |
| 167 // Resets the title, keyword and search url of the specified TemplateURL. | 166 // Resets the title, keyword and search url of the specified TemplateURL. |
| 168 // The TemplateURL is marked as not replaceable. | 167 // The TemplateURL is marked as not replaceable. |
| 169 void ResetTemplateURL(const TemplateURL* url, | 168 void ResetTemplateURL(const TemplateURL* url, |
| 170 const std::wstring& title, | 169 const string16& title, |
| 171 const std::wstring& keyword, | 170 const string16& keyword, |
| 172 const std::string& search_url); | 171 const std::string& search_url); |
| 173 | 172 |
| 174 // Return true if the given |url| can be made the default. | 173 // Return true if the given |url| can be made the default. |
| 175 bool CanMakeDefault(const TemplateURL* url); | 174 bool CanMakeDefault(const TemplateURL* url); |
| 176 | 175 |
| 177 // Set the default search provider. |url| may be null. | 176 // Set the default search provider. |url| may be null. |
| 178 // This will assert if the default search is managed; the UI should not be | 177 // This will assert if the default search is managed; the UI should not be |
| 179 // invoking this method in that situation. | 178 // invoking this method in that situation. |
| 180 void SetDefaultSearchProvider(const TemplateURL* url); | 179 void SetDefaultSearchProvider(const TemplateURL* url); |
| 181 | 180 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 204 | 203 |
| 205 // Notification that the keywords have been loaded. | 204 // Notification that the keywords have been loaded. |
| 206 // This is invoked from WebDataService, and should not be directly | 205 // This is invoked from WebDataService, and should not be directly |
| 207 // invoked. | 206 // invoked. |
| 208 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, | 207 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, |
| 209 const WDTypedResult* result); | 208 const WDTypedResult* result); |
| 210 | 209 |
| 211 // Returns the locale-direction-adjusted short name for the given keyword. | 210 // Returns the locale-direction-adjusted short name for the given keyword. |
| 212 // Also sets the out param to indicate whether the keyword belongs to an | 211 // Also sets the out param to indicate whether the keyword belongs to an |
| 213 // extension. | 212 // extension. |
| 214 std::wstring GetKeywordShortName(const std::wstring& keyword, | 213 string16 GetKeywordShortName(const string16& keyword, |
| 215 bool* is_extension_keyword); | 214 bool* is_extension_keyword); |
| 216 | 215 |
| 217 // NotificationObserver method. TemplateURLModel listens for three | 216 // NotificationObserver method. TemplateURLModel listens for three |
| 218 // notification types: | 217 // notification types: |
| 219 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit | 218 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit |
| 220 // corresponds to a keyword. | 219 // corresponds to a keyword. |
| 221 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing | 220 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing |
| 222 // a google base url replacement term. | 221 // a google base url replacement term. |
| 223 // . PREF_CHANGED: checks whether the default search engine has changed. | 222 // . PREF_CHANGED: checks whether the default search engine has changed. |
| 224 virtual void Observe(NotificationType type, | 223 virtual void Observe(NotificationType type, |
| 225 const NotificationSource& source, | 224 const NotificationSource& source, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 238 // Registers the preferences used to save a TemplateURL to prefs. | 237 // Registers the preferences used to save a TemplateURL to prefs. |
| 239 static void RegisterUserPrefs(PrefService* prefs); | 238 static void RegisterUserPrefs(PrefService* prefs); |
| 240 | 239 |
| 241 protected: | 240 protected: |
| 242 // Cover method for the method of the same name on the HistoryService. | 241 // Cover method for the method of the same name on the HistoryService. |
| 243 // url is the one that was visited with the given search terms. | 242 // url is the one that was visited with the given search terms. |
| 244 // | 243 // |
| 245 // This exists and is virtual for testing. | 244 // This exists and is virtual for testing. |
| 246 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, | 245 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, |
| 247 const GURL& url, | 246 const GURL& url, |
| 248 const std::wstring& term); | 247 const string16& term); |
| 249 | 248 |
| 250 private: | 249 private: |
| 251 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, BuildQueryTerms); | 250 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, BuildQueryTerms); |
| 252 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, TestManagedDefaultSearch); | 251 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, TestManagedDefaultSearch); |
| 253 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, | 252 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, |
| 254 UpdateKeywordSearchTermsForURL); | 253 UpdateKeywordSearchTermsForURL); |
| 255 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, | 254 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, |
| 256 DontUpdateKeywordSearchForNonReplaceable); | 255 DontUpdateKeywordSearchForNonReplaceable); |
| 257 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, ChangeGoogleBaseValue); | 256 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, ChangeGoogleBaseValue); |
| 258 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, MergeDeletesUnusedProviders); | 257 FRIEND_TEST_ALL_PREFIXES(TemplateURLModelTest, MergeDeletesUnusedProviders); |
| 259 friend class TemplateURLModelTestUtil; | 258 friend class TemplateURLModelTestUtil; |
| 260 | 259 |
| 261 typedef std::map<std::wstring, const TemplateURL*> KeywordToTemplateMap; | 260 typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; |
| 262 typedef std::vector<const TemplateURL*> TemplateURLVector; | 261 typedef std::vector<const TemplateURL*> TemplateURLVector; |
| 263 | 262 |
| 264 // Helper functor for FindMatchingKeywords(), for finding the range of | 263 // Helper functor for FindMatchingKeywords(), for finding the range of |
| 265 // keywords which begin with a prefix. | 264 // keywords which begin with a prefix. |
| 266 class LessWithPrefix; | 265 class LessWithPrefix; |
| 267 | 266 |
| 268 void Init(const Initializer* initializers, int num_initializers); | 267 void Init(const Initializer* initializers, int num_initializers); |
| 269 | 268 |
| 270 void RemoveFromMaps(const TemplateURL* template_url); | 269 void RemoveFromMaps(const TemplateURL* template_url); |
| 271 | 270 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 // increasing integer that is initialized from the database. | 426 // increasing integer that is initialized from the database. |
| 428 TemplateURLID next_id_; | 427 TemplateURLID next_id_; |
| 429 | 428 |
| 430 // List of extension IDs waiting for Load to have keywords registered. | 429 // List of extension IDs waiting for Load to have keywords registered. |
| 431 std::vector<std::string> pending_extension_ids_; | 430 std::vector<std::string> pending_extension_ids_; |
| 432 | 431 |
| 433 DISALLOW_COPY_AND_ASSIGN(TemplateURLModel); | 432 DISALLOW_COPY_AND_ASSIGN(TemplateURLModel); |
| 434 }; | 433 }; |
| 435 | 434 |
| 436 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ | 435 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_H_ |
| OLD | NEW |