OLD | NEW |
---|---|
1 // Copyright (c) 2011 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_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_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> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "chrome/browser/profiles/profile_keyed_service.h" | 17 #include "chrome/browser/profiles/profile_keyed_service.h" |
18 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 18 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
19 #include "chrome/browser/search_engines/template_url_id.h" | 19 #include "chrome/browser/search_engines/template_url_id.h" |
20 #include "chrome/browser/sync/api/sync_change.h" | |
Nicolas Zea
2011/08/15 17:01:02
Do you have to include sync_change?
SteveT
2011/08/15 21:13:58
So the three dependencies I had were:
- SyncChang
Nicolas Zea
2011/08/15 23:40:00
Fair enough.
| |
21 #include "chrome/browser/sync/api/syncable_service.h" | |
22 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" | |
Nicolas Zea
2011/08/15 17:01:02
Same, I don't see the specifics being used anywher
SteveT
2011/08/15 21:13:58
Oops, yeah. I moved all the Specifics dependencies
| |
20 #include "chrome/browser/webdata/web_data_service.h" | 23 #include "chrome/browser/webdata/web_data_service.h" |
21 #include "content/common/notification_observer.h" | 24 #include "content/common/notification_observer.h" |
22 #include "content/common/notification_registrar.h" | 25 #include "content/common/notification_registrar.h" |
23 | 26 |
24 class GURL; | 27 class GURL; |
25 class Extension; | 28 class Extension; |
26 class PrefService; | 29 class PrefService; |
27 class Profile; | 30 class Profile; |
28 class PrefSetObserver; | 31 class PrefSetObserver; |
29 class SearchHostToURLsMap; | 32 class SearchHostToURLsMap; |
30 class SearchTermsData; | 33 class SearchTermsData; |
34 class SyncData; | |
31 class TemplateURLServiceObserver; | 35 class TemplateURLServiceObserver; |
32 class TemplateURLRef; | 36 class TemplateURLRef; |
33 | 37 |
34 namespace history { | 38 namespace history { |
35 struct URLVisitedDetails; | 39 struct URLVisitedDetails; |
36 } | 40 } |
37 | 41 |
38 // TemplateURLService is the backend for keywords. It's used by | 42 // TemplateURLService is the backend for keywords. It's used by |
39 // KeywordAutocomplete. | 43 // KeywordAutocomplete. |
40 // | 44 // |
(...skipping 10 matching lines...) Expand all Loading... | |
51 // When TemplateURLService has completed loading, observers are notified via | 55 // When TemplateURLService has completed loading, observers are notified via |
52 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED | 56 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED |
53 // notification message. | 57 // notification message. |
54 // | 58 // |
55 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 59 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
56 // is a WebDataService, deletion is handled by WebDataService, otherwise | 60 // is a WebDataService, deletion is handled by WebDataService, otherwise |
57 // TemplateURLService handles deletion. | 61 // TemplateURLService handles deletion. |
58 | 62 |
59 class TemplateURLService : public WebDataServiceConsumer, | 63 class TemplateURLService : public WebDataServiceConsumer, |
60 public ProfileKeyedService, | 64 public ProfileKeyedService, |
61 public NotificationObserver { | 65 public NotificationObserver, |
66 public SyncableService { | |
62 public: | 67 public: |
63 typedef std::map<std::string, std::string> QueryTerms; | 68 typedef std::map<std::string, std::string> QueryTerms; |
64 typedef std::vector<const TemplateURL*> TemplateURLVector; | 69 typedef std::vector<const TemplateURL*> TemplateURLVector; |
65 // Type for a static function pointer that acts as a time source. | 70 // Type for a static function pointer that acts as a time source. |
66 typedef base::Time(TimeProvider)(); | 71 typedef base::Time(TimeProvider)(); |
72 typedef std::map<std::string, SyncData> SyncDataMap; | |
67 | 73 |
68 // Struct used for initializing the data store with fake data. | 74 // Struct used for initializing the data store with fake data. |
69 // Each initializer is mapped to a TemplateURL. | 75 // Each initializer is mapped to a TemplateURL. |
70 struct Initializer { | 76 struct Initializer { |
71 const char* const keyword; | 77 const char* const keyword; |
72 const char* const url; | 78 const char* const url; |
73 const char* const content; | 79 const char* const content; |
74 }; | 80 }; |
75 | 81 |
76 explicit TemplateURLService(Profile* profile); | 82 explicit TemplateURLService(Profile* profile); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 void FindMatchingKeywords(const string16& prefix, | 123 void FindMatchingKeywords(const string16& prefix, |
118 bool support_replacement_only, | 124 bool support_replacement_only, |
119 std::vector<string16>* matches) const; | 125 std::vector<string16>* matches) const; |
120 | 126 |
121 // Looks up |keyword| and returns the element it maps to. Returns NULL if | 127 // Looks up |keyword| and returns the element it maps to. Returns NULL if |
122 // the keyword was not found. | 128 // the keyword was not found. |
123 // The caller should not try to delete the returned pointer; the data store | 129 // The caller should not try to delete the returned pointer; the data store |
124 // retains ownership of it. | 130 // retains ownership of it. |
125 const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; | 131 const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; |
126 | 132 |
133 // Looks up |sync_guid| and returns the element it maps to. Returns NULL if | |
134 // the guid was not found. | |
135 // The caller should not try to delete the returned pointer; the data store | |
136 // retains ownership of it. | |
137 const TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid) const; | |
138 | |
127 // Returns the first TemplateURL found with a URL using the specified |host|, | 139 // Returns the first TemplateURL found with a URL using the specified |host|, |
128 // or NULL if there are no such TemplateURLs | 140 // or NULL if there are no such TemplateURLs |
129 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; | 141 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; |
130 | 142 |
131 // Adds a new TemplateURL to this model. TemplateURLService will own the | 143 // Adds a new TemplateURL to this model. TemplateURLService will own the |
132 // reference, and delete it when the TemplateURL is removed. | 144 // reference, and delete it when the TemplateURL is removed. |
133 void Add(TemplateURL* template_url); | 145 void Add(TemplateURL* template_url); |
134 | 146 |
135 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 147 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
136 // This fails if the supplied template_url is the default search provider. | 148 // This fails if the supplied template_url is the default search provider. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 // notification types: | 238 // notification types: |
227 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit | 239 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit |
228 // corresponds to a keyword. | 240 // corresponds to a keyword. |
229 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing | 241 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing |
230 // a google base url replacement term. | 242 // a google base url replacement term. |
231 // . PREF_CHANGED: checks whether the default search engine has changed. | 243 // . PREF_CHANGED: checks whether the default search engine has changed. |
232 virtual void Observe(int type, | 244 virtual void Observe(int type, |
233 const NotificationSource& source, | 245 const NotificationSource& source, |
234 const NotificationDetails& details); | 246 const NotificationDetails& details); |
235 | 247 |
248 // SyncableService implementation. | |
249 | |
250 // Returns all syncable TemplateURLs from this model as SyncData. This should | |
251 // include every search engine and no Extension keywords. | |
252 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | |
253 // Process new search engine changes from Sync, merging them into our local | |
254 // data. This may send notifications if local search engines are added, | |
255 // updated or removed. | |
256 virtual SyncError ProcessSyncChanges( | |
257 const tracked_objects::Location& from_here, | |
258 const SyncChangeList& change_list) OVERRIDE; | |
259 // Merge initial search engine data from Sync and push any local changes up | |
260 // to Sync. This may send notifications if local search engines are added, | |
261 // updated or removed. | |
262 virtual SyncError MergeDataAndStartSyncing( | |
263 syncable::ModelType type, | |
264 const SyncDataList& initial_sync_data, | |
265 SyncChangeProcessor* sync_processor) OVERRIDE; | |
266 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | |
267 | |
268 // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL | |
269 // that has been modified, and |type| is the Sync ChangeType that took place. | |
270 // This may send a new SyncChange to the cloud. If our model has not yet been | |
271 // associated with Sync, or if this is triggered by a Sync change, then this | |
272 // does nothing. | |
273 void ProcessTemplateURLChange(const TemplateURL* turl, | |
274 SyncChange::SyncChangeType type); | |
275 | |
236 Profile* profile() const { return profile_; } | 276 Profile* profile() const { return profile_; } |
237 | 277 |
238 void SetSearchEngineDialogSlot(int slot) { | 278 void SetSearchEngineDialogSlot(int slot) { |
239 search_engine_dialog_chosen_slot_ = slot; | 279 search_engine_dialog_chosen_slot_ = slot; |
240 } | 280 } |
241 | 281 |
242 int GetSearchEngineDialogSlot() const { | 282 int GetSearchEngineDialogSlot() const { |
243 return search_engine_dialog_chosen_slot_; | 283 return search_engine_dialog_chosen_slot_; |
244 } | 284 } |
245 | 285 |
246 // Registers the preferences used to save a TemplateURL to prefs. | 286 // Registers the preferences used to save a TemplateURL to prefs. |
247 static void RegisterUserPrefs(PrefService* prefs); | 287 static void RegisterUserPrefs(PrefService* prefs); |
248 | 288 |
289 // Returns a SyncData with a sync representation of the search engine data | |
290 // from |turl|. | |
291 static SyncData CreateSyncDataFromTemplateURL(const TemplateURL& turl); | |
292 | |
293 // Returns a heap-allocated TemplateURL, populated by |sync_data|'s fields. | |
294 // This does the opposite of CreateSyncDataFromTemplateURL. The caller owns | |
295 // the returned TemplateURL*. | |
296 static TemplateURL* CreateTemplateURLFromSyncData(const SyncData& sync_data); | |
297 | |
298 // Returns a map mapping Sync GUIDs to pointers to SyncData. | |
299 static SyncDataMap CreateGUIDToSyncDataMap(const SyncDataList& sync_data); | |
300 | |
249 #if defined(UNIT_TEST) | 301 #if defined(UNIT_TEST) |
250 // Set a different time provider function, such as | 302 // Set a different time provider function, such as |
251 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. | 303 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. |
252 void set_time_provider(TimeProvider* time_provider) { | 304 void set_time_provider(TimeProvider* time_provider) { |
253 time_provider_ = time_provider; | 305 time_provider_ = time_provider; |
254 } | 306 } |
255 #endif | 307 #endif |
256 | 308 |
257 protected: | 309 protected: |
258 // Cover method for the method of the same name on the HistoryService. | 310 // Cover method for the method of the same name on the HistoryService. |
259 // url is the one that was visited with the given search terms. | 311 // url is the one that was visited with the given search terms. |
260 // | 312 // |
261 // This exists and is virtual for testing. | 313 // This exists and is virtual for testing. |
262 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, | 314 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, |
263 const GURL& url, | 315 const GURL& url, |
264 const string16& term); | 316 const string16& term); |
265 | 317 |
266 private: | 318 private: |
267 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, BuildQueryTerms); | 319 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, BuildQueryTerms); |
268 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch); | 320 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch); |
269 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, | 321 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, |
270 UpdateKeywordSearchTermsForURL); | 322 UpdateKeywordSearchTermsForURL); |
271 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, | 323 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, |
272 DontUpdateKeywordSearchForNonReplaceable); | 324 DontUpdateKeywordSearchForNonReplaceable); |
273 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); | 325 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); |
274 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); | 326 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); |
327 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
328 CreateSyncDataFromTemplateURL); | |
329 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
330 CreateTemplateURLFromSyncData); | |
331 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); | |
332 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
333 ResolveSyncKeywordConflict); | |
334 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
335 FindDuplicateOfSyncTemplateURL); | |
336 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
337 MergeSyncAndLocalURLDuplicates); | |
338 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
339 CreateGUIDToSyncDataMap); | |
340 | |
275 friend class TemplateURLServiceTestUtil; | 341 friend class TemplateURLServiceTestUtil; |
276 | 342 |
277 typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; | 343 typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; |
344 typedef std::map<std::string, const TemplateURL*> GUIDToTemplateMap; | |
278 | 345 |
279 // Helper functor for FindMatchingKeywords(), for finding the range of | 346 // Helper functor for FindMatchingKeywords(), for finding the range of |
280 // keywords which begin with a prefix. | 347 // keywords which begin with a prefix. |
281 class LessWithPrefix; | 348 class LessWithPrefix; |
282 | 349 |
283 void Init(const Initializer* initializers, int num_initializers); | 350 void Init(const Initializer* initializers, int num_initializers); |
284 | 351 |
285 void RemoveFromMaps(const TemplateURL* template_url); | 352 void RemoveFromMaps(const TemplateURL* template_url); |
286 | 353 |
287 // Removes the supplied template_url from the keyword maps. This searches | 354 // Removes the supplied template_url from the keyword maps. This searches |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 | 455 |
389 // Removes from the vector any template URL that was created because of | 456 // Removes from the vector any template URL that was created because of |
390 // policy. These TemplateURLs are freed and removed from the database. | 457 // policy. These TemplateURLs are freed and removed from the database. |
391 // Sets default_search_provider to NULL if it was one of them, unless it is | 458 // Sets default_search_provider to NULL if it was one of them, unless it is |
392 // the same as the current default from preferences and it is managed. | 459 // the same as the current default from preferences and it is managed. |
393 void RemoveProvidersCreatedByPolicy( | 460 void RemoveProvidersCreatedByPolicy( |
394 std::vector<TemplateURL*>* template_urls, | 461 std::vector<TemplateURL*>* template_urls, |
395 const TemplateURL** default_search_provider, | 462 const TemplateURL** default_search_provider, |
396 const TemplateURL* default_from_prefs); | 463 const TemplateURL* default_from_prefs); |
397 | 464 |
465 // Resets the sync GUID of the specified TemplateURL and persists the change | |
466 // to the database. This does not notify observers. | |
467 void ResetTemplateURLGUID(const TemplateURL* url, const std::string& guid); | |
468 | |
469 // Attempts to generate a unique keyword for |turl| based on its original | |
470 // keyword. If its keyword is already unique, that is returned. Otherwise, it | |
471 // tries to return the autogenerated keyword if that is unique to the Service, | |
472 // and finally it repeatedly appends special characters to the keyword until | |
473 // it is unique to the Service. | |
474 string16 UniquifyKeyword(const TemplateURL& turl) const; | |
475 | |
476 // Given a TemplateURL from Sync, resolves any keyword conflicts by checking | |
477 // the local keywords and uniquifying either the cloud keyword or a | |
478 // conflicting local keyword (whichever is older). If the cloud TURL is | |
Nicolas Zea
2011/08/15 17:01:02
Just a thought, are you sure the oldest one is the
SteveT
2011/08/15 21:13:58
Yeah, so that is a good point, and I haven been th
| |
479 // changed, then an appropriate SyncChange is appended to |change_list|. If | |
480 // a local TURL is changed, the service is updated with the new keyword. If | |
481 // there was no conflict to begin with, this does nothing. In the case of tied | |
482 // last_modified dates, |sync_turl| wins. Returns true iff there was a | |
483 // conflict. | |
484 bool ResolveSyncKeywordConflict(TemplateURL* sync_turl, | |
485 SyncChangeList& change_list); | |
486 | |
487 // Returns a TemplateURL from the service that has the same keyword and search | |
488 // URL as |sync_turl|, if it exists. | |
489 const TemplateURL* FindDuplicateOfSyncTemplateURL( | |
490 const TemplateURL& sync_turl); | |
491 | |
492 // Given a TemplateURL from the cloud and a local matching duplicate found by | |
493 // FindDuplcateOfSyncTemplateURL, merges the two. If |sync_url| is newer, this | |
494 // replaces |local_url| with |sync_url| using the service's Remove and Add. | |
495 // If |local_url| is newer, this copies the GUID from |sync_url| over to | |
496 // |local_url| and adds an update to change_list to notify the server of the | |
497 // change. | |
498 void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_url, | |
499 TemplateURL* local_url, | |
500 SyncChangeList& change_list); | |
501 | |
398 NotificationRegistrar registrar_; | 502 NotificationRegistrar registrar_; |
399 | 503 |
400 // Mapping from keyword to the TemplateURL. | 504 // Mapping from keyword to the TemplateURL. |
401 KeywordToTemplateMap keyword_to_template_map_; | 505 KeywordToTemplateMap keyword_to_template_map_; |
402 | 506 |
507 // Mapping from Sync GUIDs to the TemplateURL. | |
508 GUIDToTemplateMap guid_to_template_map_; | |
509 | |
403 TemplateURLVector template_urls_; | 510 TemplateURLVector template_urls_; |
404 | 511 |
405 ObserverList<TemplateURLServiceObserver> model_observers_; | 512 ObserverList<TemplateURLServiceObserver> model_observers_; |
406 | 513 |
407 // Maps from host to set of TemplateURLs whose search url host is host. | 514 // Maps from host to set of TemplateURLs whose search url host is host. |
408 SearchHostToURLsMap provider_map_; | 515 SearchHostToURLsMap provider_map_; |
409 | 516 |
410 // Used to obtain the WebDataService. | 517 // Used to obtain the WebDataService. |
411 // When Load is invoked, if we haven't yet loaded, the WebDataService is | 518 // When Load is invoked, if we haven't yet loaded, the WebDataService is |
412 // obtained from the Profile. This allows us to lazily access the database. | 519 // obtained from the Profile. This allows us to lazily access the database. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 // ID assigned to next TemplateURL added to this model. This is an ever | 557 // ID assigned to next TemplateURL added to this model. This is an ever |
451 // increasing integer that is initialized from the database. | 558 // increasing integer that is initialized from the database. |
452 TemplateURLID next_id_; | 559 TemplateURLID next_id_; |
453 | 560 |
454 // List of extension IDs waiting for Load to have keywords registered. | 561 // List of extension IDs waiting for Load to have keywords registered. |
455 std::vector<std::string> pending_extension_ids_; | 562 std::vector<std::string> pending_extension_ids_; |
456 | 563 |
457 // Function returning current time in base::Time units. | 564 // Function returning current time in base::Time units. |
458 TimeProvider* time_provider_; | 565 TimeProvider* time_provider_; |
459 | 566 |
567 // Do we have an active association between the TemplateURLs and sync models? | |
568 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not | |
569 // set, we ignore any local search engine changes (when we start syncing we | |
570 // will look up the most recent values anyways). | |
571 bool models_associated_; | |
572 | |
573 // Whether we're currently processing changes from the syncer. While this is | |
574 // true, we ignore any local search engine changes, since we triggered them. | |
575 bool processing_syncer_changes_; | |
576 | |
577 // Sync's SyncChange handler. We push all our changes through this. | |
578 SyncChangeProcessor* sync_processor_; | |
579 | |
460 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 580 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
461 }; | 581 }; |
462 | 582 |
463 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 583 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |