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