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 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, | 345 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, |
346 DontUpdateKeywordSearchForNonReplaceable); | 346 DontUpdateKeywordSearchForNonReplaceable); |
347 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); | 347 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); |
348 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); | 348 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); |
349 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 349 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
350 CreateSyncDataFromTemplateURL); | 350 CreateSyncDataFromTemplateURL); |
351 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 351 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
352 CreateTemplateURLFromSyncData); | 352 CreateTemplateURLFromSyncData); |
353 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); | 353 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); |
354 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 354 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
355 ResolveSyncKeywordConflict); | 355 SyncKeywordConflictNeitherAutoreplace); |
356 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); | |
357 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 356 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
358 IsLocalTemplateURLBetter); | 357 SyncKeywordConflictBothAutoreplace); |
359 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); | 358 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 359 SyncKeywordConflictOneAutoreplace); |
| 360 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 361 FindDuplicateOfSyncTemplateURL); |
| 362 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 363 MergeSyncAndLocalURLDuplicates); |
| 364 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 365 PreSyncDeletes); |
360 | 366 |
361 friend class TemplateURLServiceTestUtil; | 367 friend class TemplateURLServiceTestUtil; |
362 | 368 |
363 typedef std::map<string16, TemplateURL*> KeywordToTemplateMap; | 369 typedef std::map<string16, TemplateURL*> KeywordToTemplateMap; |
364 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; | 370 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; |
365 typedef std::list<std::string> PendingExtensionIDs; | 371 typedef std::list<std::string> PendingExtensionIDs; |
366 | 372 |
367 // Declaration of values to be used in an enumerated histogram to tally | 373 // Declaration of values to be used in an enumerated histogram to tally |
368 // changes to the default search provider from various entry points. In | 374 // changes to the default search provider from various entry points. In |
369 // particular, we use this to see what proportion of changes are from Sync | 375 // particular, we use this to see what proportion of changes are from Sync |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); | 525 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); |
520 | 526 |
521 // Attempts to generate a unique keyword for |turl| based on its original | 527 // Attempts to generate a unique keyword for |turl| based on its original |
522 // keyword. If its keyword is already unique, that is returned. Otherwise, it | 528 // keyword. If its keyword is already unique, that is returned. Otherwise, it |
523 // tries to return the autogenerated keyword if that is unique to the Service, | 529 // tries to return the autogenerated keyword if that is unique to the Service, |
524 // and finally it repeatedly appends special characters to the keyword until | 530 // and finally it repeatedly appends special characters to the keyword until |
525 // it is unique to the Service. If |force| is true, then this will only | 531 // it is unique to the Service. If |force| is true, then this will only |
526 // execute the special character appending functionality. | 532 // execute the special character appending functionality. |
527 string16 UniquifyKeyword(const TemplateURL& turl, bool force); | 533 string16 UniquifyKeyword(const TemplateURL& turl, bool force); |
528 | 534 |
529 // Returns true iff |local_turl| is considered "better" than |sync_turl| for | 535 // Given a TemplateURL from Sync (cloud) and a local, non-extension |
530 // the purposes of resolving conflicts. |local_turl| must be a TemplateURL | 536 // TemplateURL with the same keyword, selects "better" and "worse" entries: |
531 // known to the local model (though it may already be synced), and |sync_turl| | 537 // * If one of the TemplateURLs is replaceable and the other is not, the |
532 // is a new TemplateURL known to Sync but not yet known to the local model. | 538 // non-replaceable entry is better. |
533 // The criteria for if |local_turl| is better than |sync_turl| is whether any | 539 // * Otherwise, if |local_turl| was created by policy, is the default |
534 // of the following are true: | 540 // provider, or was modified more recently, it is better. |
535 // * |local_turl|'s last_modified timestamp is newer than sync_turl. | 541 // * Otherwise |sync_turl| is better. |
536 // * |local_turl| is created by policy. | 542 // Then resolves the conflict: |
537 // * |local_turl| is the local default search provider. | 543 // * If the "worse" entry is |sync_turl|, and it is replaceable, add a |
538 bool IsLocalTemplateURLBetter(const TemplateURL* local_turl, | 544 // syncer::SyncChange to delete it, and return false. |
539 const TemplateURL* sync_turl); | 545 // * If the "worse" entry is |local_turl|, and it is replaceable, remove it |
540 | 546 // from the service and return true. |
541 // Given two synced TemplateURLs with a conflicting keyword, one of which | 547 // * Otherwise, uniquify the keyword of the "worse" entry. If it is |
542 // needs to be added to or updated in the local model (|unapplied_sync_turl|) | 548 // |local_turl|, update it within the service. Add a SyncChange to update |
543 // and one which is already known to the local model (|applied_sync_turl|), | 549 // things (always for |sync_turl|, sometimes for |local_turl|; see |
544 // prepares the local model so that |unapplied_sync_turl| can be added to it, | 550 // comments in implementation), and return true. |
545 // or applied as an update to an existing TemplateURL. | 551 // When the function returns true, callers can then go ahead and add or update |
546 // Since both entries are known to Sync and one of their keywords will change, | 552 // |sync_turl| within the service. If it returns false, callers must not add |
547 // an ACTION_UPDATE will be appended to |change_list| to reflect this change. | 553 // the |sync_turl|, and must Remove() the |sync_turl| if it was being updated. |
548 // Note that |applied_sync_turl| must not be an extension keyword. | 554 // (Be careful; calling Remove() could add an ACTION_DELETE sync change, which |
549 void ResolveSyncKeywordConflict(TemplateURL* unapplied_sync_turl, | 555 // this function has already done. Make sure to avoid duplicates.) |
550 TemplateURL* applied_sync_turl, | 556 // |
| 557 // Note that we never call this for conflicts with extension keywords because |
| 558 // other code (e.g. AddToMaps()) is responsible for correctly prioritizing |
| 559 // extension- vs. non-extension-based TemplateURLs with the same keyword. |
| 560 bool ResolveSyncKeywordConflict(TemplateURL* sync_turl, |
| 561 TemplateURL* local_turl, |
551 syncer::SyncChangeList* change_list); | 562 syncer::SyncChangeList* change_list); |
552 | 563 |
553 // Adds |sync_turl| into the local model, possibly removing or updating a | 564 // Returns a TemplateURL from the service that has the same keyword and search |
554 // local TemplateURL to make room for it. This expects |sync_turl| to be a new | 565 // URL as |sync_turl|, if it exists. |
555 // entry from Sync, not currently known to the local model. |sync_data| should | 566 TemplateURL* FindDuplicateOfSyncTemplateURL(const TemplateURL& sync_turl); |
556 // be a SyncDataMap where the contents are entries initially known to Sync | 567 |
557 // during MergeDataAndStartSyncing. | 568 // Given a TemplateURL from the cloud and a local matching duplicate found by |
558 // Any necessary updates to Sync will be appended to |change_list|. This can | 569 // FindDuplicateOfSyncTemplateURL, merges the two. If |sync_turl| is newer, |
559 // include updates on local TemplateURLs, if they are found in |sync_data|. | 570 // this replaces |local_turl| with |sync_turl| using the service's Remove and |
560 // |initial_data| should be a SyncDataMap of the entries known to the local | 571 // Add. If |local_turl| is newer, this replaces |sync_turl| with |local_turl| |
561 // model during MergeDataAndStartSyncing. If |sync_turl| replaces a local | 572 // through through adding appropriate SyncChanges to |change_list|. This |
562 // entry, that entry is removed from |initial_data| to prevent it from being | 573 // method takes ownership of |sync_turl|, and adds it to the model if it is |
563 // sent up to Sync. | 574 // newer, so the caller must release it if need be. |
564 // This should only be called from MergeDataAndStartSyncing. | 575 void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_turl, |
565 void MergeInSyncTemplateURL(TemplateURL* sync_turl, | 576 TemplateURL* local_turl, |
566 const SyncDataMap& sync_data, | 577 syncer::SyncChangeList* change_list); |
567 syncer::SyncChangeList* change_list, | |
568 SyncDataMap* local_data); | |
569 | 578 |
570 // Checks a newly added TemplateURL from Sync by its sync_guid and sets it as | 579 // Checks a newly added TemplateURL from Sync by its sync_guid and sets it as |
571 // the default search provider if we were waiting for it. | 580 // the default search provider if we were waiting for it. |
572 void SetDefaultSearchProviderIfNewlySynced(const std::string& guid); | 581 void SetDefaultSearchProviderIfNewlySynced(const std::string& guid); |
573 | 582 |
574 // Retrieve the pending default search provider according to Sync. Returns | 583 // Retrieve the pending default search provider according to Sync. Returns |
575 // NULL if there was no pending search provider from Sync. | 584 // NULL if there was no pending search provider from Sync. |
576 TemplateURL* GetPendingSyncedDefaultSearchProvider(); | 585 TemplateURL* GetPendingSyncedDefaultSearchProvider(); |
577 | 586 |
578 // Goes through a vector of TemplateURLs and ensure that both the in-memory | 587 // Goes through a vector of TemplateURLs and ensure that both the in-memory |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 680 |
672 // This is used to log the origin of changes to the default search provider. | 681 // This is used to log the origin of changes to the default search provider. |
673 // We set this value to increasingly specific values when we know what is the | 682 // We set this value to increasingly specific values when we know what is the |
674 // cause/origin of a default search change. | 683 // cause/origin of a default search change. |
675 DefaultSearchChangeOrigin dsp_change_origin_; | 684 DefaultSearchChangeOrigin dsp_change_origin_; |
676 | 685 |
677 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 686 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
678 }; | 687 }; |
679 | 688 |
680 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 689 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |