| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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_TEMPLATE_URL_MODEL_H__ | |
| 6 #define CHROME_BROWSER_TEMPLATE_URL_MODEL_H__ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/observer_list.h" | |
| 13 #include "chrome/browser/history/history_notifications.h" | |
| 14 #include "chrome/browser/history/history_types.h" | |
| 15 #include "chrome/browser/webdata/web_data_service.h" | |
| 16 #include "chrome/common/notification_service.h" | |
| 17 | |
| 18 class GURL; | |
| 19 class PrefService; | |
| 20 class Profile; | |
| 21 class TemplateURL; | |
| 22 class TemplateURLModelTest; | |
| 23 | |
| 24 // TemplateURLModel is the backend for keywords. It's used by | |
| 25 // KeywordAutocomplete. | |
| 26 // | |
| 27 // TemplateURLModel stores a vector of TemplateURLs. The TemplateURLs are | |
| 28 // persisted to the database maintained by WebDataService. *ALL* mutations | |
| 29 // to the TemplateURLs must funnel through TemplateURLModel. This allows | |
| 30 // TemplateURLModel to notify listeners of changes as well as keep the | |
| 31 // database in sync. | |
| 32 // | |
| 33 // There is a TemplateURLModel per Profile. | |
| 34 // | |
| 35 // TemplateURLModel does not load the vector of TemplateURLs in it's | |
| 36 // constructor (except for testing). Use the Load method to trigger a load. | |
| 37 // When TemplateURLModel has completed loading, observers are notified via | |
| 38 // OnTemplateURLModelChanged as well as the TEMPLATE_URL_MODEL_LOADED | |
| 39 // notification message. | |
| 40 // | |
| 41 // TemplateURLModel takes ownership of any TemplateURL passed to it. If there | |
| 42 // is a WebDataService, deletion is handled by WebDataService, otherwise | |
| 43 // TemplateURLModel handles deletion. | |
| 44 | |
| 45 // TemplateURLModelObserver is notified whenever the set of TemplateURLs | |
| 46 // are modified. | |
| 47 class TemplateURLModelObserver { | |
| 48 public: | |
| 49 // Notification that the template url model has changed in some way. | |
| 50 virtual void OnTemplateURLModelChanged() = 0; | |
| 51 }; | |
| 52 | |
| 53 class TemplateURLModel : public WebDataServiceConsumer, | |
| 54 public NotificationObserver { | |
| 55 public: | |
| 56 typedef std::map<std::string, std::string> QueryTerms; | |
| 57 | |
| 58 // Struct used for initializing the data store with fake data. | |
| 59 // Each initializer is mapped to a TemplateURL. | |
| 60 struct Initializer { | |
| 61 const wchar_t* const keyword; | |
| 62 const wchar_t* const url; | |
| 63 const wchar_t* const content; | |
| 64 }; | |
| 65 | |
| 66 explicit TemplateURLModel(Profile* profile); | |
| 67 // The following is for testing. | |
| 68 TemplateURLModel(const Initializer* initializers, const int count); | |
| 69 | |
| 70 ~TemplateURLModel(); | |
| 71 | |
| 72 // Generates a suitable keyword for the specified url. Returns an empty | |
| 73 // string if a keyword couldn't be generated. If |autodetected| is true, we | |
| 74 // don't generate keywords for a variety of situations where we would probably | |
| 75 // not want to auto-add keywords, such as keywords for searches on pages that | |
| 76 // themselves come from form submissions. | |
| 77 static std::wstring GenerateKeyword(const GURL& url, bool autodetected); | |
| 78 | |
| 79 // Removes any unnecessary characters from a user input keyword. | |
| 80 // This removes the leading scheme, "www." and any trailing slash. | |
| 81 static std::wstring CleanUserInputKeyword(const std::wstring& keyword); | |
| 82 | |
| 83 // Returns the search url for t_url. Returns an empty GURL if t_url has no | |
| 84 // url(). | |
| 85 static GURL GenerateSearchURL(const TemplateURL* t_url); | |
| 86 | |
| 87 // Returns true if there is no TemplateURL that conflicts with the | |
| 88 // keyword/url pair, or there is one but it can be replaced. If there is an | |
| 89 // existing keyword that can be replaced and template_url_to_replace is | |
| 90 // non-NULL, template_url_to_replace is set to the keyword to replace. | |
| 91 // | |
| 92 // url gives the url of the search query. The url is used to avoid generating | |
| 93 // a TemplateURL for an existing TemplateURL that shares the same host. | |
| 94 bool CanReplaceKeyword(const std::wstring& keyword, | |
| 95 const std::wstring& url, | |
| 96 const TemplateURL** template_url_to_replace); | |
| 97 | |
| 98 // Returns (in |matches|) all keywords beginning with |prefix|, sorted | |
| 99 // shortest-first. If support_replacement_only is true, only keywords that | |
| 100 // support replacement are returned. | |
| 101 void FindMatchingKeywords(const std::wstring& prefix, | |
| 102 bool support_replacement_only, | |
| 103 std::vector<std::wstring>* matches) const; | |
| 104 | |
| 105 // Looks up |keyword| and returns the element it maps to. Returns NULL if | |
| 106 // the keyword was not found. | |
| 107 // The caller should not try to delete the returned pointer; the data store | |
| 108 // retains ownership of it. | |
| 109 const TemplateURL* GetTemplateURLForKeyword( | |
| 110 const std::wstring& keyword) const; | |
| 111 | |
| 112 // Returns the first TemplateURL found with a URL using the specified |host|, | |
| 113 // or NULL if there are no such TemplateURLs | |
| 114 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; | |
| 115 | |
| 116 // Adds a new TemplateURL to this model. TemplateURLModel will own the | |
| 117 // reference, and delete it when the TemplateURL is removed. | |
| 118 void Add(TemplateURL* template_url); | |
| 119 | |
| 120 // Removes the keyword from the model. This deletes the supplied TemplateURL. | |
| 121 // This fails if the supplied template_url is the default search provider. | |
| 122 void Remove(const TemplateURL* template_url); | |
| 123 | |
| 124 // Removes all auto-generated keywords that were created in the specified | |
| 125 // range. | |
| 126 void RemoveAutoGeneratedBetween(base::Time created_after, base::Time created_b
efore); | |
| 127 | |
| 128 // Replaces existing_turl with new_turl. new_turl is given the same ID as | |
| 129 // existing_turl. If existing_turl was the default, new_turl is made the | |
| 130 // default. After this call existing_turl is deleted. As with Add, | |
| 131 // TemplateURLModel takes ownership of existing_turl. | |
| 132 void Replace(const TemplateURL* existing_turl, | |
| 133 TemplateURL* new_turl); | |
| 134 | |
| 135 // Removes all auto-generated keywords that were created on or after the | |
| 136 // date passed in. | |
| 137 void RemoveAutoGeneratedSince(base::Time created_after); | |
| 138 | |
| 139 // Returns the set of URLs describing the keywords. The elements are owned | |
| 140 // by TemplateURLModel and should not be deleted. | |
| 141 std::vector<const TemplateURL*> GetTemplateURLs() const; | |
| 142 | |
| 143 // Increment the usage count of a keyword. | |
| 144 // Called when a URL is loaded that was generated from a keyword. | |
| 145 void IncrementUsageCount(const TemplateURL* url); | |
| 146 | |
| 147 // Resets the title, keyword and search url of the specified TemplateURL. | |
| 148 // The TemplateURL is marked as not replaceable. | |
| 149 void ResetTemplateURL(const TemplateURL* url, | |
| 150 const std::wstring& title, | |
| 151 const std::wstring& keyword, | |
| 152 const std::wstring& search_url); | |
| 153 | |
| 154 // The default search provider. This may be null. | |
| 155 void SetDefaultSearchProvider(const TemplateURL* url); | |
| 156 | |
| 157 // Returns the default search provider. If the TemplateURLModel hasn't been | |
| 158 // loaded, the default search provider is pulled from preferences. | |
| 159 // | |
| 160 // NOTE: At least in unittest mode, this may return NULL. | |
| 161 const TemplateURL* GetDefaultSearchProvider(); | |
| 162 | |
| 163 // Observers used to listen for changes to the model. | |
| 164 // TemplateURLModel does NOT delete the observers when deleted. | |
| 165 void AddObserver(TemplateURLModelObserver* observer); | |
| 166 void RemoveObserver(TemplateURLModelObserver* observer); | |
| 167 | |
| 168 // Loads the keywords. This has no effect if the keywords have already been | |
| 169 // loaded. | |
| 170 // Observers are notified when loading completes via the method | |
| 171 // OnTemplateURLsReset. | |
| 172 void Load(); | |
| 173 | |
| 174 // Whether or not the keywords have been loaded. | |
| 175 bool loaded() { return loaded_; } | |
| 176 | |
| 177 // Notification that the keywords have been loaded. | |
| 178 // This is invoked from WebDataService, and should not be directly | |
| 179 // invoked. | |
| 180 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, | |
| 181 const WDTypedResult* result); | |
| 182 | |
| 183 // Removes (and deletes) TemplateURLs from |urls| that have duplicate | |
| 184 // prepopulate ids. Duplicate prepopulate ids are not allowed, but due to a | |
| 185 // bug it was possible get dups. This step is only called when the version | |
| 186 // number changes. | |
| 187 void RemoveDuplicatePrepopulateIDs(std::vector<const TemplateURL*>* urls); | |
| 188 | |
| 189 // NotificationObserver method. TemplateURLModel listens for three | |
| 190 // notification types: | |
| 191 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit | |
| 192 // corresponds to a keyword. | |
| 193 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing | |
| 194 // a google base url replacement term. | |
| 195 virtual void Observe(NotificationType type, | |
| 196 const NotificationSource& source, | |
| 197 const NotificationDetails& details); | |
| 198 | |
| 199 Profile* profile() const { return profile_; } | |
| 200 | |
| 201 protected: | |
| 202 // Cover method for the method of the same name on the HistoryService. | |
| 203 // url is the one that was visited with the given search terms. | |
| 204 // | |
| 205 // This exists and is virtual for testing. | |
| 206 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, | |
| 207 const GURL& url, | |
| 208 const std::wstring& term); | |
| 209 | |
| 210 private: | |
| 211 FRIEND_TEST(TemplateURLModelTest, BuildQueryTerms); | |
| 212 FRIEND_TEST(TemplateURLModelTest, UpdateKeywordSearchTermsForURL); | |
| 213 FRIEND_TEST(TemplateURLModelTest, DontUpdateKeywordSearchForNonReplaceable); | |
| 214 FRIEND_TEST(TemplateURLModelTest, ChangeGoogleBaseValue); | |
| 215 friend class TemplateURLModelTest; | |
| 216 | |
| 217 typedef std::map<std::wstring, const TemplateURL*> KeywordToTemplateMap; | |
| 218 typedef std::vector<const TemplateURL*> TemplateURLVector; | |
| 219 | |
| 220 // Helper functor for FindMatchingKeywords(), for finding the range of | |
| 221 // keywords which begin with a prefix. | |
| 222 class LessWithPrefix; | |
| 223 | |
| 224 void Init(const Initializer* initializers, int num_initializers); | |
| 225 | |
| 226 void RemoveFromMaps(const TemplateURL* template_url); | |
| 227 | |
| 228 // Removes the supplied template_url from the maps. This searches through all | |
| 229 // entries in the maps and does not generate the host or keyword. | |
| 230 // This is used when the cached content of the TemplateURL changes. | |
| 231 void RemoveFromMapsByPointer(const TemplateURL* template_url); | |
| 232 | |
| 233 void AddToMaps(const TemplateURL* template_url); | |
| 234 | |
| 235 // Sets the keywords. This is used once the keywords have been loaded. | |
| 236 // This does NOT notify the delegate or the database. | |
| 237 void SetTemplateURLs(const std::vector<const TemplateURL*>& urls); | |
| 238 | |
| 239 void DeleteGeneratedKeywordsMatchingHost(const std::wstring& host); | |
| 240 | |
| 241 // If there is a notification service, sends TEMPLATE_URL_MODEL_LOADED | |
| 242 // notification. | |
| 243 void NotifyLoaded(); | |
| 244 | |
| 245 // Loads engines from prepopulate data and merges them in with the existing | |
| 246 // engines. This is invoked when the version of the prepopulate data changes. | |
| 247 void MergeEnginesFromPrepopulateData(); | |
| 248 | |
| 249 // Saves enough of url to preferences so that it can be loaded from | |
| 250 // preferences on start up. | |
| 251 void SaveDefaultSearchProviderToPrefs(const TemplateURL* url); | |
| 252 | |
| 253 // Creates a TemplateURL that was previously saved to prefs via | |
| 254 // SaveDefaultSearchProviderToPrefs. Returns true if successful, false | |
| 255 // otherwise. This is used if GetDefaultSearchProvider is invoked before the | |
| 256 // TemplateURL has loaded. If the user has opted for no default search, this | |
| 257 // returns true but default_provider is set to NULL. | |
| 258 bool LoadDefaultSearchProviderFromPrefs(TemplateURL** default_provider); | |
| 259 | |
| 260 // Registers the preferences used to save a TemplateURL to prefs. | |
| 261 void RegisterPrefs(PrefService* prefs); | |
| 262 | |
| 263 // Returns true if there is no TemplateURL that has a search url with the | |
| 264 // specified host, or the only TemplateURLs matching the specified host can | |
| 265 // be replaced. | |
| 266 bool CanReplaceKeywordForHost(const std::string& host, | |
| 267 const TemplateURL** to_replace); | |
| 268 | |
| 269 // Returns true if the TemplateURL is replaceable. This doesn't look at the | |
| 270 // uniqueness of the keyword or host and is intended to be called after those | |
| 271 // checks have been done. This returns true if the TemplateURL doesn't appear | |
| 272 // in the default list and is marked as safe_for_autoreplace. | |
| 273 bool CanReplace(const TemplateURL* t_url); | |
| 274 | |
| 275 // Returns the preferences we use. | |
| 276 PrefService* GetPrefs(); | |
| 277 | |
| 278 // Iterates through the TemplateURLs to see if one matches the visited url. | |
| 279 // For each TemplateURL whose url matches the visited url | |
| 280 // SetKeywordSearchTermsForURL is invoked. | |
| 281 void UpdateKeywordSearchTermsForURL(const history::URLRow& row); | |
| 282 | |
| 283 // Adds each of the query terms in the specified url whose key and value are | |
| 284 // non-empty to query_terms. If a query key appears multiple times, the value | |
| 285 // is set to an empty string. Returns true if there is at least one key that | |
| 286 // does not occur multiple times. | |
| 287 static bool BuildQueryTerms( | |
| 288 const GURL& url, | |
| 289 std::map<std::string,std::string>* query_terms); | |
| 290 | |
| 291 // Invoked when the Google base URL has changed. Updates the mapping for all | |
| 292 // TemplateURLs that have a replacement term of {google:baseURL} or | |
| 293 // {google:baseSuggestURL}. | |
| 294 void GoogleBaseURLChanged(); | |
| 295 | |
| 296 // Mapping from keyword to the TemplateURL. | |
| 297 KeywordToTemplateMap keyword_to_template_map_; | |
| 298 | |
| 299 TemplateURLVector template_urls_; | |
| 300 | |
| 301 ObserverList<TemplateURLModelObserver> model_observers_; | |
| 302 | |
| 303 // Maps from host to set of TemplateURLs whose search url host is host. | |
| 304 typedef std::set<const TemplateURL*> TemplateURLSet; | |
| 305 typedef std::map<std::string, TemplateURLSet> HostToURLsMap; | |
| 306 HostToURLsMap host_to_urls_map_; | |
| 307 | |
| 308 // Used to obtain the WebDataService. | |
| 309 // When Load is invoked, if we haven't yet loaded, the WebDataService is | |
| 310 // obtained from the Profile. This allows us to lazily access the database. | |
| 311 Profile* profile_; | |
| 312 | |
| 313 // Whether the keywords have been loaded. | |
| 314 bool loaded_; | |
| 315 | |
| 316 // If non-zero, we're waiting on a load. | |
| 317 WebDataService::Handle load_handle_; | |
| 318 | |
| 319 // Service used to store entries. | |
| 320 scoped_refptr<WebDataService> service_; | |
| 321 | |
| 322 // List of hosts to feed to DeleteGeneratedKeywordsMatchingHost. When | |
| 323 // we receive NOTIFY_HOST_DELETED_FROM_HISTORY if we haven't loaded yet, | |
| 324 // we force a load and add the host to hosts_to_delete_. When done loading | |
| 325 // we invoke DeleteGeneratedKeywordsMatchingHost with all the elements of | |
| 326 // the vector. | |
| 327 std::vector<std::wstring> hosts_to_delete_; | |
| 328 | |
| 329 // All visits that occurred before we finished loading. Once loaded | |
| 330 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. | |
| 331 std::vector<history::URLRow> visits_to_add_; | |
| 332 | |
| 333 const TemplateURL* default_search_provider_; | |
| 334 | |
| 335 // The default search provider from preferences. This is only valid if | |
| 336 // GetDefaultSearchProvider is invoked and we haven't been loaded. Once loaded | |
| 337 // this is not used. | |
| 338 scoped_ptr<TemplateURL> prefs_default_search_provider_; | |
| 339 | |
| 340 // ID assigned to next TemplateURL added to this model. This is an ever | |
| 341 // increasing integer that is initialized from the database. | |
| 342 TemplateURL::IDType next_id_; | |
| 343 | |
| 344 DISALLOW_EVIL_CONSTRUCTORS(TemplateURLModel); | |
| 345 }; | |
| 346 | |
| 347 #endif // CHROME_BROWSER_TEMPLATE_URL_MODEL_H__ | |
| 348 | |
| OLD | NEW |