OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
23 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
24 #include "sync/api/sync_change.h" | 24 #include "sync/api/sync_change.h" |
25 #include "sync/api/syncable_service.h" | 25 #include "sync/api/syncable_service.h" |
26 | 26 |
27 class GURL; | 27 class GURL; |
28 class PrefService; | 28 class PrefService; |
29 class Profile; | 29 class Profile; |
30 class SearchHostToURLsMap; | 30 class SearchHostToURLsMap; |
31 class SearchTermsData; | 31 class SearchTermsData; |
| 32 class TemplateURLServiceObserver; |
| 33 |
| 34 namespace csync { |
32 class SyncData; | 35 class SyncData; |
33 class SyncErrorFactory; | 36 class SyncErrorFactory; |
34 class TemplateURLServiceObserver; | 37 } |
35 | 38 |
36 namespace extensions { | 39 namespace extensions { |
37 class Extension; | 40 class Extension; |
38 } | 41 } |
39 | 42 |
40 namespace history { | 43 namespace history { |
41 struct URLVisitedDetails; | 44 struct URLVisitedDetails; |
42 } | 45 } |
43 | 46 |
44 // TemplateURLService is the backend for keywords. It's used by | 47 // TemplateURLService is the backend for keywords. It's used by |
(...skipping 13 matching lines...) Expand all Loading... |
58 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED | 61 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED |
59 // notification message. | 62 // notification message. |
60 // | 63 // |
61 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 64 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
62 // is a WebDataService, deletion is handled by WebDataService, otherwise | 65 // is a WebDataService, deletion is handled by WebDataService, otherwise |
63 // TemplateURLService handles deletion. | 66 // TemplateURLService handles deletion. |
64 | 67 |
65 class TemplateURLService : public WebDataServiceConsumer, | 68 class TemplateURLService : public WebDataServiceConsumer, |
66 public ProfileKeyedService, | 69 public ProfileKeyedService, |
67 public content::NotificationObserver, | 70 public content::NotificationObserver, |
68 public SyncableService { | 71 public csync::SyncableService { |
69 public: | 72 public: |
70 typedef std::map<std::string, std::string> QueryTerms; | 73 typedef std::map<std::string, std::string> QueryTerms; |
71 typedef std::vector<TemplateURL*> TemplateURLVector; | 74 typedef std::vector<TemplateURL*> TemplateURLVector; |
72 // Type for a static function pointer that acts as a time source. | 75 // Type for a static function pointer that acts as a time source. |
73 typedef base::Time(TimeProvider)(); | 76 typedef base::Time(TimeProvider)(); |
74 typedef std::map<std::string, SyncData> SyncDataMap; | 77 typedef std::map<std::string, csync::SyncData> SyncDataMap; |
75 | 78 |
76 // Struct used for initializing the data store with fake data. | 79 // Struct used for initializing the data store with fake data. |
77 // Each initializer is mapped to a TemplateURL. | 80 // Each initializer is mapped to a TemplateURL. |
78 struct Initializer { | 81 struct Initializer { |
79 const char* const keyword; | 82 const char* const keyword; |
80 const char* const url; | 83 const char* const url; |
81 const char* const content; | 84 const char* const content; |
82 }; | 85 }; |
83 | 86 |
84 explicit TemplateURLService(Profile* profile); | 87 explicit TemplateURLService(Profile* profile); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 // Returns the locale-direction-adjusted short name for the given keyword. | 258 // Returns the locale-direction-adjusted short name for the given keyword. |
256 // Also sets the out param to indicate whether the keyword belongs to an | 259 // Also sets the out param to indicate whether the keyword belongs to an |
257 // extension. | 260 // extension. |
258 string16 GetKeywordShortName(const string16& keyword, | 261 string16 GetKeywordShortName(const string16& keyword, |
259 bool* is_extension_keyword); | 262 bool* is_extension_keyword); |
260 | 263 |
261 virtual void Observe(int type, | 264 virtual void Observe(int type, |
262 const content::NotificationSource& source, | 265 const content::NotificationSource& source, |
263 const content::NotificationDetails& details) OVERRIDE; | 266 const content::NotificationDetails& details) OVERRIDE; |
264 | 267 |
265 // SyncableService implementation. | 268 // csync::SyncableService implementation. |
266 | 269 |
267 // Returns all syncable TemplateURLs from this model as SyncData. This should | 270 // Returns all syncable TemplateURLs from this model as SyncData. This should |
268 // include every search engine and no Extension keywords. | 271 // include every search engine and no Extension keywords. |
269 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | 272 virtual csync::SyncDataList GetAllSyncData( |
| 273 syncable::ModelType type) const OVERRIDE; |
270 // Process new search engine changes from Sync, merging them into our local | 274 // Process new search engine changes from Sync, merging them into our local |
271 // data. This may send notifications if local search engines are added, | 275 // data. This may send notifications if local search engines are added, |
272 // updated or removed. | 276 // updated or removed. |
273 virtual SyncError ProcessSyncChanges( | 277 virtual csync::SyncError ProcessSyncChanges( |
274 const tracked_objects::Location& from_here, | 278 const tracked_objects::Location& from_here, |
275 const SyncChangeList& change_list) OVERRIDE; | 279 const csync::SyncChangeList& change_list) OVERRIDE; |
276 // Merge initial search engine data from Sync and push any local changes up | 280 // Merge initial search engine data from Sync and push any local changes up |
277 // to Sync. This may send notifications if local search engines are added, | 281 // to Sync. This may send notifications if local search engines are added, |
278 // updated or removed. | 282 // updated or removed. |
279 virtual SyncError MergeDataAndStartSyncing( | 283 virtual csync::SyncError MergeDataAndStartSyncing( |
280 syncable::ModelType type, | 284 syncable::ModelType type, |
281 const SyncDataList& initial_sync_data, | 285 const csync::SyncDataList& initial_sync_data, |
282 scoped_ptr<SyncChangeProcessor> sync_processor, | 286 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
283 scoped_ptr<SyncErrorFactory> sync_error_factory) OVERRIDE; | 287 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) OVERRIDE; |
284 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | 288 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
285 | 289 |
286 // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL | 290 // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL |
287 // that has been modified, and |type| is the Sync ChangeType that took place. | 291 // that has been modified, and |type| is the Sync ChangeType that took place. |
288 // This may send a new SyncChange to the cloud. If our model has not yet been | 292 // This may send a new SyncChange to the cloud. If our model has not yet been |
289 // associated with Sync, or if this is triggered by a Sync change, then this | 293 // associated with Sync, or if this is triggered by a Sync change, then this |
290 // does nothing. | 294 // does nothing. |
291 void ProcessTemplateURLChange(const TemplateURL* turl, | 295 void ProcessTemplateURLChange(const TemplateURL* turl, |
292 SyncChange::SyncChangeType type); | 296 csync::SyncChange::SyncChangeType type); |
293 | 297 |
294 Profile* profile() const { return profile_; } | 298 Profile* profile() const { return profile_; } |
295 | 299 |
296 // Returns a SyncData with a sync representation of the search engine data | 300 // Returns a SyncData with a sync representation of the search engine data |
297 // from |turl|. | 301 // from |turl|. |
298 static SyncData CreateSyncDataFromTemplateURL(const TemplateURL& turl); | 302 static csync::SyncData CreateSyncDataFromTemplateURL(const TemplateURL& turl); |
299 | 303 |
300 // Creates a new heap-allocated TemplateURL* which is populated by overlaying | 304 // Creates a new heap-allocated TemplateURL* which is populated by overlaying |
301 // |sync_data| atop |existing_turl|. |existing_turl| may be NULL; if not it | 305 // |sync_data| atop |existing_turl|. |existing_turl| may be NULL; if not it |
302 // remains unmodified. The caller owns the returned TemplateURL*. | 306 // remains unmodified. The caller owns the returned TemplateURL*. |
303 // | 307 // |
304 // If the created TemplateURL is migrated in some way from out-of-date sync | 308 // If the created TemplateURL is migrated in some way from out-of-date sync |
305 // data, an appropriate SyncChange is added to |change_list|. If the sync | 309 // data, an appropriate SyncChange is added to |change_list|. If the sync |
306 // data is bad for some reason, an ACTION_DELETE change is added and the | 310 // data is bad for some reason, an ACTION_DELETE change is added and the |
307 // function returns NULL. | 311 // function returns NULL. |
308 static TemplateURL* CreateTemplateURLFromTemplateURLAndSyncData( | 312 static TemplateURL* CreateTemplateURLFromTemplateURLAndSyncData( |
309 Profile* profile, | 313 Profile* profile, |
310 TemplateURL* existing_turl, | 314 TemplateURL* existing_turl, |
311 const SyncData& sync_data, | 315 const csync::SyncData& sync_data, |
312 SyncChangeList* change_list); | 316 csync::SyncChangeList* change_list); |
313 | 317 |
314 // Returns a map mapping Sync GUIDs to pointers to SyncData. | 318 // Returns a map mapping Sync GUIDs to pointers to csync::SyncData. |
315 static SyncDataMap CreateGUIDToSyncDataMap(const SyncDataList& sync_data); | 319 static SyncDataMap CreateGUIDToSyncDataMap( |
| 320 const csync::SyncDataList& sync_data); |
316 | 321 |
317 #if defined(UNIT_TEST) | 322 #if defined(UNIT_TEST) |
318 // Set a different time provider function, such as | 323 // Set a different time provider function, such as |
319 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. | 324 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. |
320 void set_time_provider(TimeProvider* time_provider) { | 325 void set_time_provider(TimeProvider* time_provider) { |
321 time_provider_ = time_provider; | 326 time_provider_ = time_provider; |
322 } | 327 } |
323 #endif | 328 #endif |
324 | 329 |
325 protected: | 330 protected: |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 | 532 |
528 // Given a TemplateURL from Sync (cloud) and a local, non-extension | 533 // Given a TemplateURL from Sync (cloud) and a local, non-extension |
529 // TemplateURL with the same keyword, selects "better" and "worse" entries: | 534 // TemplateURL with the same keyword, selects "better" and "worse" entries: |
530 // * If one of the TemplateURLs is replaceable and the other is not, the | 535 // * If one of the TemplateURLs is replaceable and the other is not, the |
531 // non-replaceable entry is better. | 536 // non-replaceable entry is better. |
532 // * Otherwise, if |local_turl| was created by policy, is the default | 537 // * Otherwise, if |local_turl| was created by policy, is the default |
533 // provider, or was modified more recently, it is better. | 538 // provider, or was modified more recently, it is better. |
534 // * Otherwise |sync_turl| is better. | 539 // * Otherwise |sync_turl| is better. |
535 // Then resolves the conflict: | 540 // Then resolves the conflict: |
536 // * If the "worse" entry is |sync_turl|, and it is replaceable, add a | 541 // * If the "worse" entry is |sync_turl|, and it is replaceable, add a |
537 // SyncChange to delete it, and return false. | 542 // csync::SyncChange to delete it, and return false. |
538 // * If the "worse" entry is |local_turl|, and it is replaceable, remove it | 543 // * If the "worse" entry is |local_turl|, and it is replaceable, remove it |
539 // from the service and return true. | 544 // from the service and return true. |
540 // * Otherwise, uniquify the keyword of the "worse" entry. If it is | 545 // * Otherwise, uniquify the keyword of the "worse" entry. If it is |
541 // |local_turl|, update it within the service. Add a SyncChange to update | 546 // |local_turl|, update it within the service. Add a SyncChange to update |
542 // things (always for |sync_turl|, sometimes for |local_turl|; see | 547 // things (always for |sync_turl|, sometimes for |local_turl|; see |
543 // comments in implementation), and return true. | 548 // comments in implementation), and return true. |
544 // When the function returns true, callers can then go ahead and add or update | 549 // When the function returns true, callers can then go ahead and add or update |
545 // |sync_turl| within the service. If it returns false, callers must not add | 550 // |sync_turl| within the service. If it returns false, callers must not add |
546 // the |sync_turl|, and must Remove() the |sync_turl| if it was being updated. | 551 // the |sync_turl|, and must Remove() the |sync_turl| if it was being updated. |
547 // (Be careful; calling Remove() could add an ACTION_DELETE sync change, which | 552 // (Be careful; calling Remove() could add an ACTION_DELETE sync change, which |
548 // this function has already done. Make sure to avoid duplicates.) | 553 // this function has already done. Make sure to avoid duplicates.) |
549 // | 554 // |
550 // Note that we never call this for conflicts with extension keywords because | 555 // Note that we never call this for conflicts with extension keywords because |
551 // other code (e.g. AddToMaps()) is responsible for correctly prioritizing | 556 // other code (e.g. AddToMaps()) is responsible for correctly prioritizing |
552 // extension- vs. non-extension-based TemplateURLs with the same keyword. | 557 // extension- vs. non-extension-based TemplateURLs with the same keyword. |
553 bool ResolveSyncKeywordConflict(TemplateURL* sync_turl, | 558 bool ResolveSyncKeywordConflict(TemplateURL* sync_turl, |
554 TemplateURL* local_turl, | 559 TemplateURL* local_turl, |
555 SyncChangeList* change_list); | 560 csync::SyncChangeList* change_list); |
556 | 561 |
557 // Returns a TemplateURL from the service that has the same keyword and search | 562 // Returns a TemplateURL from the service that has the same keyword and search |
558 // URL as |sync_turl|, if it exists. | 563 // URL as |sync_turl|, if it exists. |
559 TemplateURL* FindDuplicateOfSyncTemplateURL(const TemplateURL& sync_turl); | 564 TemplateURL* FindDuplicateOfSyncTemplateURL(const TemplateURL& sync_turl); |
560 | 565 |
561 // Given a TemplateURL from the cloud and a local matching duplicate found by | 566 // Given a TemplateURL from the cloud and a local matching duplicate found by |
562 // FindDuplicateOfSyncTemplateURL, merges the two. If |sync_turl| is newer, | 567 // FindDuplicateOfSyncTemplateURL, merges the two. If |sync_turl| is newer, |
563 // this replaces |local_turl| with |sync_turl| using the service's Remove and | 568 // this replaces |local_turl| with |sync_turl| using the service's Remove and |
564 // Add. If |local_turl| is newer, this replaces |sync_turl| with |local_turl| | 569 // Add. If |local_turl| is newer, this replaces |sync_turl| with |local_turl| |
565 // through through adding appropriate SyncChanges to |change_list|. This | 570 // through through adding appropriate SyncChanges to |change_list|. This |
566 // method takes ownership of |sync_turl|, and adds it to the model if it is | 571 // method takes ownership of |sync_turl|, and adds it to the model if it is |
567 // newer, so the caller must release it if need be. | 572 // newer, so the caller must release it if need be. |
568 void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_turl, | 573 void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_turl, |
569 TemplateURL* local_turl, | 574 TemplateURL* local_turl, |
570 SyncChangeList* change_list); | 575 csync::SyncChangeList* change_list); |
571 | 576 |
572 // Checks a newly added TemplateURL from Sync by its sync_guid and sets it as | 577 // Checks a newly added TemplateURL from Sync by its sync_guid and sets it as |
573 // the default search provider if we were waiting for it. | 578 // the default search provider if we were waiting for it. |
574 void SetDefaultSearchProviderIfNewlySynced(const std::string& guid); | 579 void SetDefaultSearchProviderIfNewlySynced(const std::string& guid); |
575 | 580 |
576 // Retrieve the pending default search provider according to Sync. Returns | 581 // Retrieve the pending default search provider according to Sync. Returns |
577 // NULL if there was no pending search provider from Sync. | 582 // NULL if there was no pending search provider from Sync. |
578 TemplateURL* GetPendingSyncedDefaultSearchProvider(); | 583 TemplateURL* GetPendingSyncedDefaultSearchProvider(); |
579 | 584 |
580 // Goes through a vector of TemplateURLs and ensure that both the in-memory | 585 // Goes through a vector of TemplateURLs and ensure that both the in-memory |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 // Do we have an active association between the TemplateURLs and sync models? | 650 // Do we have an active association between the TemplateURLs and sync models? |
646 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not | 651 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not |
647 // set, we ignore any local search engine changes (when we start syncing we | 652 // set, we ignore any local search engine changes (when we start syncing we |
648 // will look up the most recent values anyways). | 653 // will look up the most recent values anyways). |
649 bool models_associated_; | 654 bool models_associated_; |
650 | 655 |
651 // Whether we're currently processing changes from the syncer. While this is | 656 // Whether we're currently processing changes from the syncer. While this is |
652 // true, we ignore any local search engine changes, since we triggered them. | 657 // true, we ignore any local search engine changes, since we triggered them. |
653 bool processing_syncer_changes_; | 658 bool processing_syncer_changes_; |
654 | 659 |
655 // Sync's SyncChange handler. We push all our changes through this. | 660 // Sync's csync::SyncChange handler. We push all our changes through this. |
656 scoped_ptr<SyncChangeProcessor> sync_processor_; | 661 scoped_ptr<csync::SyncChangeProcessor> sync_processor_; |
657 | 662 |
658 // Sync's error handler. We use it to create a sync error. | 663 // Sync's error handler. We use it to create a sync error. |
659 scoped_ptr<SyncErrorFactory> sync_error_factory_; | 664 scoped_ptr<csync::SyncErrorFactory> sync_error_factory_; |
660 | 665 |
661 // Whether or not we are waiting on the default search provider to come in | 666 // Whether or not we are waiting on the default search provider to come in |
662 // from Sync. This is to facilitate the fact that changes to the value of | 667 // from Sync. This is to facilitate the fact that changes to the value of |
663 // prefs::kSyncedDefaultSearchProviderGUID do not always come before the | 668 // prefs::kSyncedDefaultSearchProviderGUID do not always come before the |
664 // TemplateURL entry it refers to, and to handle the case when we want to use | 669 // TemplateURL entry it refers to, and to handle the case when we want to use |
665 // the Synced default when the default search provider becomes unmanaged. | 670 // the Synced default when the default search provider becomes unmanaged. |
666 bool pending_synced_default_search_; | 671 bool pending_synced_default_search_; |
667 | 672 |
668 // A set of sync GUIDs denoting TemplateURLs that have been removed from this | 673 // A set of sync GUIDs denoting TemplateURLs that have been removed from this |
669 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. | 674 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. |
670 // This set is used to determine what entries from the server we want to | 675 // This set is used to determine what entries from the server we want to |
671 // ignore locally and return a delete command for. | 676 // ignore locally and return a delete command for. |
672 std::set<std::string> pre_sync_deletes_; | 677 std::set<std::string> pre_sync_deletes_; |
673 | 678 |
674 // This is used to log the origin of changes to the default search provider. | 679 // This is used to log the origin of changes to the default search provider. |
675 // We set this value to increasingly specific values when we know what is the | 680 // We set this value to increasingly specific values when we know what is the |
676 // cause/origin of a default search change. | 681 // cause/origin of a default search change. |
677 DefaultSearchChangeOrigin dsp_change_origin_; | 682 DefaultSearchChangeOrigin dsp_change_origin_; |
678 | 683 |
679 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 684 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
680 }; | 685 }; |
681 | 686 |
682 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 687 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |