Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/search_engines/template_url_service.cc

Issue 8334030: Merge search engines sync data type with Preferences. Sync the default search provider. Add some ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/search_engines/template_url_service.h" 5 #include "chrome/browser/search_engines/template_url_service.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/environment.h" 9 #include "base/environment.h"
10 #include "base/i18n/case_conversion.h" 10 #include "base/i18n/case_conversion.h"
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 450
451 // We're not loaded, rely on the default search provider stored in prefs. 451 // We're not loaded, rely on the default search provider stored in prefs.
452 return initial_default_search_provider_.get(); 452 return initial_default_search_provider_.get();
453 } 453 }
454 454
455 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { 455 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() {
456 // See if the prepoluated default still exists. 456 // See if the prepoluated default still exists.
457 scoped_ptr<TemplateURL> prepopulated_default( 457 scoped_ptr<TemplateURL> prepopulated_default(
458 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); 458 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs()));
459 for (TemplateURLVector::iterator i = template_urls_.begin(); 459 for (TemplateURLVector::iterator i = template_urls_.begin();
460 i != template_urls_.end(); ) { 460 i != template_urls_.end(); ++i) {
Nicolas Zea 2011/11/04 23:08:06 indent by one more
SteveT 2011/11/07 21:48:56 Done.
461 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) 461 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id())
462 return *i; 462 return *i;
463 } 463 }
464 // If not, use the first of the templates. 464 // If not, use the first of the templates.
465 return template_urls_.empty() ? NULL : template_urls_[0]; 465 return template_urls_.empty() ? NULL : template_urls_[0];
466 } 466 }
467 467
468 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { 468 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) {
469 model_observers_.AddObserver(observer); 469 model_observers_.AddObserver(observer);
470 } 470 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 &default_search_provider, 519 &default_search_provider,
520 &new_resource_keyword_version); 520 &new_resource_keyword_version);
521 521
522 bool database_specified_a_default = NULL != default_search_provider; 522 bool database_specified_a_default = NULL != default_search_provider;
523 523
524 // Check if default search provider is now managed. 524 // Check if default search provider is now managed.
525 scoped_ptr<TemplateURL> default_from_prefs; 525 scoped_ptr<TemplateURL> default_from_prefs;
526 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, 526 LoadDefaultSearchProviderFromPrefs(&default_from_prefs,
527 &is_default_search_managed_); 527 &is_default_search_managed_);
528 528
529 // TODO(sail): Re-enable the protector on Mac and Linux once various
530 // crashes and UI issues are fixed. See http://crbug.com/102765
531 #if defined(OS_WIN)
Nicolas Zea 2011/11/04 23:08:06 Is this supposed to be part of this patch, or one
SteveT 2011/11/07 21:48:56 Odd. I think this change was pulled in when I merg
532 // Check if the default search provider has been changed and notify 529 // Check if the default search provider has been changed and notify
533 // Protector instance about it. Don't check if the default search is 530 // Protector instance about it. Don't check if the default search is
534 // managed. 531 // managed.
535 const TemplateURL* backup_default_search_provider = NULL; 532 const TemplateURL* backup_default_search_provider = NULL;
536 if (!is_default_search_managed_ && 533 if (!is_default_search_managed_ &&
537 DidDefaultSearchProviderChange( 534 DidDefaultSearchProviderChange(
538 *result, 535 *result,
539 template_urls, 536 template_urls,
540 &backup_default_search_provider)) { 537 &backup_default_search_provider)) {
541 // Protector will delete itself when it's needed no longer. 538 // Protector will delete itself when it's needed no longer.
542 protector::Protector* protector = new protector::Protector(profile()); 539 protector::Protector* protector = new protector::Protector(profile());
543 protector->ShowChange(protector::CreateDefaultSearchProviderChange( 540 protector->ShowChange(protector::CreateDefaultSearchProviderChange(
544 default_search_provider, 541 default_search_provider,
545 backup_default_search_provider)); 542 backup_default_search_provider));
546 } 543 }
547 #endif
548 544
549 // Remove entries that were created because of policy as they may have 545 // Remove entries that were created because of policy as they may have
550 // changed since the database was saved. 546 // changed since the database was saved.
551 RemoveProvidersCreatedByPolicy(&template_urls, 547 RemoveProvidersCreatedByPolicy(&template_urls,
552 &default_search_provider, 548 &default_search_provider,
553 default_from_prefs.get()); 549 default_from_prefs.get());
554 550
555 if (is_default_search_managed_) { 551 if (is_default_search_managed_) {
556 SetTemplateURLs(template_urls); 552 SetTemplateURLs(template_urls);
557 553
(...skipping 10 matching lines...) Expand all
568 if (managed_default) { 564 if (managed_default) {
569 managed_default->set_created_by_policy(true); 565 managed_default->set_created_by_policy(true);
570 managed_default->set_id(0); 566 managed_default->set_id(0);
571 AddNoNotify(managed_default); 567 AddNoNotify(managed_default);
572 } 568 }
573 default_search_provider = managed_default; 569 default_search_provider = managed_default;
574 } 570 }
575 // Note that this saves the default search provider to prefs. 571 // Note that this saves the default search provider to prefs.
576 SetDefaultSearchProviderNoNotify(default_search_provider); 572 SetDefaultSearchProviderNoNotify(default_search_provider);
577 } else { 573 } else {
578 // If we had a managed default, replace it with the first provider of 574 // If we had a managed default, replace it with the synced default if
579 // the list. 575 // applicable, or the first provider of the list.
580 if (database_specified_a_default && 576 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvder();
581 NULL == default_search_provider && 577 if (synced_default) {
582 !template_urls.empty()) 578 default_search_provider = synced_default;
579 GetPrefs()->SetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange,
580 false);
581 } else if (database_specified_a_default &&
582 NULL == default_search_provider &&
583 !template_urls.empty()) {
583 default_search_provider = template_urls[0]; 584 default_search_provider = template_urls[0];
585 }
Nicolas Zea 2011/11/04 23:08:06 What about the else scenario in this case? Is it v
SteveT 2011/11/07 21:48:56 I believe it's still valid. Either the database di
Nicolas Zea 2011/11/08 19:57:27 Makes sense, although a comment about this might b
584 586
585 // If the default search provider existed previously, then just 587 // If the default search provider existed previously, then just
586 // set the member variable. Otherwise, we'll set it using the method 588 // set the member variable. Otherwise, we'll set it using the method
587 // to ensure that it is saved properly after its id is set. 589 // to ensure that it is saved properly after its id is set.
588 if (default_search_provider && default_search_provider->id() != 0) { 590 if (default_search_provider && default_search_provider->id() != 0) {
589 default_search_provider_ = default_search_provider; 591 default_search_provider_ = default_search_provider;
590 default_search_provider = NULL; 592 default_search_provider = NULL;
591 } 593 }
592 SetTemplateURLs(template_urls); 594 SetTemplateURLs(template_urls);
593 595
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 content::Details<history::URLVisitedDetails> visit_details(details); 641 content::Details<history::URLVisitedDetails> visit_details(details);
640 if (!loaded()) 642 if (!loaded())
641 visits_to_add_.push_back(*visit_details.ptr()); 643 visits_to_add_.push_back(*visit_details.ptr());
642 else 644 else
643 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); 645 UpdateKeywordSearchTermsForURL(*visit_details.ptr());
644 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { 646 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) {
645 if (loaded_) 647 if (loaded_)
646 GoogleBaseURLChanged(); 648 GoogleBaseURLChanged();
647 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) { 649 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) {
648 const std::string* pref_name = content::Details<std::string>(details).ptr(); 650 const std::string* pref_name = content::Details<std::string>(details).ptr();
649 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) { 651 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) {
Nicolas Zea 2011/11/04 23:08:06 I'm confused about this if statement. Why would pr
SteveT 2011/11/07 21:48:56 I wasn't sure about why pref_name would ever be NU
Nicolas Zea 2011/11/08 19:57:27 If it turns out the current way is necessary, a co
652 // Listen for changes to the default search from Sync. If it is
653 // specifically the synced default search provider GUID that changed, we
654 // have to set it (or wait for it).
655 PrefService* prefs = GetPrefs();
656 if (pref_name && *pref_name == prefs::kSyncedDefaultSearchProviderGUID &&
657 prefs) {
658 const TemplateURL* new_default_search = GetTemplateURLForGUID(
659 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
660 if (new_default_search && !is_default_search_managed_) {
Nicolas Zea 2011/11/04 23:08:06 where does is_default_search_managed_ get updated,
SteveT 2011/11/07 21:48:56 is_default_search_managed_ actually gets updated i
661 if (new_default_search != GetDefaultSearchProvider())
662 SetDefaultSearchProvider(new_default_search);
663 } else {
664 // If it's not there, or if default search is currently managed, set a
665 // flag to indicate that we waiting on the search engine entry to come
666 // in through Sync.
667 prefs->SetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange,
Nicolas Zea 2011/11/04 23:08:06 I'm not sure I like the approach of adding an extr
SteveT 2011/11/07 21:48:56 One issue is that we generally don't clear the Syn
668 true);
669 }
670 }
671
650 // A preference related to default search engine has changed. 672 // A preference related to default search engine has changed.
651 // Update the model if needed. 673 // Update the model if needed.
652 UpdateDefaultSearch(); 674 UpdateDefaultSearch();
653 } 675 }
654 } else { 676 } else {
655 NOTREACHED(); 677 NOTREACHED();
656 } 678 }
657 } 679 }
658 680
659 SyncDataList TemplateURLService::GetAllSyncData( 681 SyncDataList TemplateURLService::GetAllSyncData(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 if (!turl.get()) { 719 if (!turl.get()) {
698 NOTREACHED() << "Failed to read search engine."; 720 NOTREACHED() << "Failed to read search engine.";
699 continue; 721 continue;
700 } 722 }
701 723
702 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); 724 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid());
703 const TemplateURL* existing_keyword_turl = 725 const TemplateURL* existing_keyword_turl =
704 GetTemplateURLForKeyword(turl->keyword()); 726 GetTemplateURLForKeyword(turl->keyword());
705 727
706 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { 728 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) {
729 bool delete_default = existing_turl == GetDefaultSearchProvider();
Nicolas Zea 2011/11/04 23:08:06 prefer paren around expression: (existing... == ..
SteveT 2011/11/07 21:48:56 Done.
730 if (delete_default && is_default_search_managed_) {
731 // We are not allowed to delete managed default search providers.
732 continue;
Nicolas Zea 2011/11/04 23:08:06 should this clear the SyncedDefaultSearchProviderG
SteveT 2011/11/07 21:48:56 Hm, I don't think so. We have to keep SyncedDefaul
733 }
734 if (delete_default)
Nicolas Zea 2011/11/04 23:08:06 how about if (delete default) { if (!is_default_
SteveT 2011/11/07 21:48:56 So what we're trying to do here is to allow deleti
735 default_search_provider_ = NULL;
736
707 Remove(existing_turl); 737 Remove(existing_turl);
738
739 if (delete_default) {
740 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
741 }
708 } else if (iter->change_type() == SyncChange::ACTION_ADD && 742 } else if (iter->change_type() == SyncChange::ACTION_ADD &&
709 !existing_turl) { 743 !existing_turl) {
744 std::string guid = turl->sync_guid();
710 if (existing_keyword_turl) 745 if (existing_keyword_turl)
711 ResolveSyncKeywordConflict(turl.get(), &new_changes); 746 ResolveSyncKeywordConflict(turl.get(), &new_changes);
712 // Force the local ID to 0 so we can add it. 747 // Force the local ID to 0 so we can add it.
713 turl->set_id(0); 748 turl->set_id(0);
714 Add(turl.release()); 749 Add(turl.release());
750
751 // Possibly set the newly added |turl| as the default search provider.
752 CheckForSyncedDefaultSearchProvider(guid);
Nicolas Zea 2011/11/04 23:08:06 may as well just call this with turl->sync_guid()
SteveT 2011/11/07 21:48:56 We can't really do this since ownership of turl is
715 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && 753 } else if (iter->change_type() == SyncChange::ACTION_UPDATE &&
716 existing_turl) { 754 existing_turl) {
717 if (existing_keyword_turl) 755 // Possibly resolve a keyword conflict if they have the same keywords but
756 // are not the same entry.
757 if (existing_keyword_turl && existing_keyword_turl != existing_turl)
718 ResolveSyncKeywordConflict(turl.get(), &new_changes); 758 ResolveSyncKeywordConflict(turl.get(), &new_changes);
719 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(), 759 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(),
720 turl->url() ? turl->url()->url() : std::string()); 760 turl->url() ? turl->url()->url() : std::string());
721 } else { 761 } else {
722 // Something really unexpected happened. Either we received an 762 // Something really unexpected happened. Either we received an
723 // ACTION_INVALID, or Sync is in a crazy state: 763 // ACTION_INVALID, or Sync is in a crazy state:
724 // . Trying to DELETE or UPDATE a non-existent search engine. 764 // . Trying to DELETE or UPDATE a non-existent search engine.
725 // . Trying to ADD a search engine that already exists. 765 // . Trying to ADD a search engine that already exists.
726 NOTREACHED() << "Unexpected sync change state."; 766 NOTREACHED() << "Unexpected sync change state.";
727 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + 767 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " +
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 if (dupe_turl) { 841 if (dupe_turl) {
802 // Merge duplicates and remove the processed local TURL from the map. 842 // Merge duplicates and remove the processed local TURL from the map.
803 TemplateURL* modifiable_dupe_turl = 843 TemplateURL* modifiable_dupe_turl =
804 const_cast<TemplateURL*>(dupe_turl); 844 const_cast<TemplateURL*>(dupe_turl);
805 std::string old_guid = dupe_turl->sync_guid(); 845 std::string old_guid = dupe_turl->sync_guid();
806 MergeSyncAndLocalURLDuplicates(sync_turl.release(), 846 MergeSyncAndLocalURLDuplicates(sync_turl.release(),
807 modifiable_dupe_turl, 847 modifiable_dupe_turl,
808 &new_changes); 848 &new_changes);
809 local_data_map.erase(old_guid); 849 local_data_map.erase(old_guid);
810 } else { 850 } else {
851 std::string guid = sync_turl->sync_guid();
811 // Keyword conflict is possible in this case. Resolve it first before 852 // Keyword conflict is possible in this case. Resolve it first before
812 // adding the new TemplateURL. Note that we don't remove the local TURL 853 // adding the new TemplateURL. Note that we don't remove the local TURL
813 // from local_data_map in this case as it may still need to be pushed to 854 // from local_data_map in this case as it may still need to be pushed to
814 // the cloud. 855 // the cloud.
815 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); 856 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes);
816 // Force the local ID to 0 so we can add it. 857 // Force the local ID to 0 so we can add it.
817 sync_turl->set_id(0); 858 sync_turl->set_id(0);
818 Add(sync_turl.release()); 859 Add(sync_turl.release());
860
861 // Possibly set the newly added |turl| as the default search provider.
862 CheckForSyncedDefaultSearchProvider(guid);
Nicolas Zea 2011/11/04 23:08:06 same here
SteveT 2011/11/07 21:48:56 As above.
819 } 863 }
820 } 864 }
821 } // for 865 } // for
822 866
823 // The remaining SyncData in local_data_map should be everything that needs to 867 // The remaining SyncData in local_data_map should be everything that needs to
824 // be pushed as ADDs to sync. 868 // be pushed as ADDs to sync.
825 for (SyncDataMap::const_iterator iter = local_data_map.begin(); 869 for (SyncDataMap::const_iterator iter = local_data_map.begin();
826 iter != local_data_map.end(); ++iter) { 870 iter != local_data_map.end(); ++iter) {
827 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second)); 871 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second));
828 } 872 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
941 const GURL& url, 985 const GURL& url,
942 const string16& term) { 986 const string16& term) {
943 HistoryService* history = profile_ ? 987 HistoryService* history = profile_ ?
944 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 988 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
945 if (!history) 989 if (!history)
946 return; 990 return;
947 history->SetKeywordSearchTermsForURL(url, t_url->id(), term); 991 history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
948 } 992 }
949 993
950 void TemplateURLService::Init(const Initializer* initializers, 994 void TemplateURLService::Init(const Initializer* initializers,
951 int num_initializers) { 995 int num_initializers) {
952 // Register for notifications. 996 // Register for notifications.
953 if (profile_) { 997 if (profile_) {
954 // TODO(sky): bug 1166191. The keywords should be moved into the history 998 // TODO(sky): bug 1166191. The keywords should be moved into the history
955 // db, which will mean we no longer need this notification and the history 999 // db, which will mean we no longer need this notification and the history
956 // backend can handle automatically adding the search terms as the user 1000 // backend can handle automatically adding the search terms as the user
957 // navigates. 1001 // navigates.
958 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, 1002 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
959 content::Source<Profile>(profile_->GetOriginalProfile())); 1003 content::Source<Profile>(profile_->GetOriginalProfile()));
960 PrefService* prefs = GetPrefs(); 1004 PrefService* prefs = GetPrefs();
961 default_search_prefs_.reset( 1005 default_search_prefs_.reset(
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
1477 DCHECK(is_default_search_managed_ && !new_is_default_managed); 1521 DCHECK(is_default_search_managed_ && !new_is_default_managed);
1478 is_default_search_managed_ = new_is_default_managed; 1522 is_default_search_managed_ = new_is_default_managed;
1479 // If we had a default, delete the previous default if created by policy 1523 // If we had a default, delete the previous default if created by policy
1480 // and set a likely default. 1524 // and set a likely default.
1481 if (NULL != default_search_provider_ && 1525 if (NULL != default_search_provider_ &&
1482 default_search_provider_->created_by_policy()) { 1526 default_search_provider_->created_by_policy()) {
1483 const TemplateURL* old_default = default_search_provider_; 1527 const TemplateURL* old_default = default_search_provider_;
1484 default_search_provider_ = NULL; 1528 default_search_provider_ = NULL;
1485 RemoveNoNotify(old_default); 1529 RemoveNoNotify(old_default);
1486 } 1530 }
1487 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); 1531
1532 // The likely default should be from Sync if we were waiting on Sync.
1533 // Otherwise, it should be FindNewDefaultSearchProvider.
1534 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvder();
1535 if (synced_default)
1536 GetPrefs()->SetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange,
1537 false);
1538 SetDefaultSearchProviderNoNotify(synced_default ? synced_default :
1539 FindNewDefaultSearchProvider());
1488 } 1540 }
1489 NotifyObservers(); 1541 NotifyObservers();
1490 } 1542 }
1491 1543
1492 void TemplateURLService::SetDefaultSearchProviderNoNotify( 1544 void TemplateURLService::SetDefaultSearchProviderNoNotify(
1493 const TemplateURL* url) { 1545 const TemplateURL* url) {
1494 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != 1546 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) !=
1495 template_urls_.end()); 1547 template_urls_.end());
1496 default_search_provider_ = url; 1548 default_search_provider_ = url;
1497 1549
(...skipping 11 matching lines...) Expand all
1509 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) 1561 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1510 // Needs to be evaluated. See http://crbug.com/62328. 1562 // Needs to be evaluated. See http://crbug.com/62328.
1511 base::ThreadRestrictions::ScopedAllowIO allow_io; 1563 base::ThreadRestrictions::ScopedAllowIO allow_io;
1512 RLZTracker::RecordProductEvent(rlz_lib::CHROME, 1564 RLZTracker::RecordProductEvent(rlz_lib::CHROME,
1513 rlz_lib::CHROME_OMNIBOX, 1565 rlz_lib::CHROME_OMNIBOX,
1514 rlz_lib::SET_TO_GOOGLE); 1566 rlz_lib::SET_TO_GOOGLE);
1515 #endif 1567 #endif
1516 } 1568 }
1517 } 1569 }
1518 1570
1519 if (!is_default_search_managed_) 1571 if (!is_default_search_managed_) {
1520 SaveDefaultSearchProviderToPrefs(url); 1572 SaveDefaultSearchProviderToPrefs(url);
1521 1573
1574 // If we are syncing, we want to set the synced pref that will notify other
1575 // instances to change their default to this new search provider.
1576 if (sync_processor_ && !url->sync_guid().empty() && GetPrefs()) {
1577 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
1578 url->sync_guid());
1579 }
1580 }
1581
1522 if (service_.get()) 1582 if (service_.get())
1523 service_->SetDefaultSearchProvider(url); 1583 service_->SetDefaultSearchProvider(url);
1524 1584
1525 // Inform sync the change to the show_in_default_list flag. 1585 // Inform sync the change to the show_in_default_list flag.
1526 if (url) 1586 if (url)
1527 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); 1587 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE);
1528 } 1588 }
1529 1589
1530 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { 1590 void TemplateURLService::AddNoNotify(TemplateURL* template_url) {
1531 DCHECK(template_url); 1591 DCHECK(template_url);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1670 } 1730 }
1671 1731
1672 bool TemplateURLService::ResolveSyncKeywordConflict( 1732 bool TemplateURLService::ResolveSyncKeywordConflict(
1673 TemplateURL* sync_turl, 1733 TemplateURL* sync_turl,
1674 SyncChangeList* change_list) { 1734 SyncChangeList* change_list) {
1675 DCHECK(sync_turl); 1735 DCHECK(sync_turl);
1676 DCHECK(change_list); 1736 DCHECK(change_list);
1677 1737
1678 const TemplateURL* existing_turl = 1738 const TemplateURL* existing_turl =
1679 GetTemplateURLForKeyword(sync_turl->keyword()); 1739 GetTemplateURLForKeyword(sync_turl->keyword());
1680 if (!existing_turl) 1740 // If there is no conflict, or it's just conflicting with itself, return.
1741 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid())
1681 return false; 1742 return false;
1682 1743
1683 if (existing_turl->last_modified() > sync_turl->last_modified() || 1744 if (existing_turl->last_modified() > sync_turl->last_modified() ||
1684 existing_turl->created_by_policy()) { 1745 existing_turl->created_by_policy()) {
1685 string16 new_keyword = UniquifyKeyword(*sync_turl); 1746 string16 new_keyword = UniquifyKeyword(*sync_turl);
1686 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 1747 DCHECK(!GetTemplateURLForKeyword(new_keyword));
1687 sync_turl->set_keyword(new_keyword); 1748 sync_turl->set_keyword(new_keyword);
1688 // If we update the cloud TURL, we need to push an update back to sync 1749 // If we update the cloud TURL, we need to push an update back to sync
1689 // informing it that something has changed. 1750 // informing it that something has changed.
1690 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 1751 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1733 } else { 1794 } else {
1734 // Change the local TURL's GUID to the server's GUID and push an update to 1795 // Change the local TURL's GUID to the server's GUID and push an update to
1735 // Sync. This ensures that the rest of local_url's fields are sync'd up to 1796 // Sync. This ensures that the rest of local_url's fields are sync'd up to
1736 // the server, and the next time local_url is synced, it is recognized by 1797 // the server, and the next time local_url is synced, it is recognized by
1737 // having the same GUID. 1798 // having the same GUID.
1738 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); 1799 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid());
1739 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 1800 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
1740 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); 1801 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data));
1741 } 1802 }
1742 } 1803 }
1804
1805 void TemplateURLService::CheckForSyncedDefaultSearchProvider(
1806 const std::string& guid) {
1807 // If we're not syncing or if default search is managed by policy, ignore.
1808 if (!sync_processor_ || is_default_search_managed_)
1809 return;
1810
1811 PrefService* prefs = GetPrefs();
1812 if (prefs &&
1813 prefs->GetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange) &&
1814 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID) == guid) {
1815 // Make sure this actually exists. We should not be calling this unless we
1816 // really just added this TemplateURL.
1817 const TemplateURL* turl_from_sync = GetTemplateURLForGUID(guid);
1818 DCHECK(turl_from_sync);
1819 SetDefaultSearchProvider(turl_from_sync);
1820 prefs->SetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange, false);
1821 }
1822 }
1823
1824 const TemplateURL* TemplateURLService::GetPendingSyncedDefaultSearchProvder() {
1825 PrefService* prefs = GetPrefs();
1826 if (!prefs ||
1827 !prefs->GetBoolean(prefs::kPendingSyncedDefaultSearchProviderChange)) {
1828 return NULL;
1829 }
1830
1831 // Could be NULL if no such thing exists.
1832 return GetTemplateURLForGUID(
1833 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
1834 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698