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

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

Issue 12084076: Ensure post-sync TemplateURL of prepopulated engines use built-in version. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change according to pkasting comments. Created 7 years, 10 months 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) 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 #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/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/environment.h" 10 #include "base/environment.h"
11 #include "base/guid.h" 11 #include "base/guid.h"
12 #include "base/i18n/case_conversion.h" 12 #include "base/i18n/case_conversion.h"
13 #include "base/memory/scoped_vector.h"
13 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
14 #include "base/stl_util.h" 15 #include "base/stl_util.h"
15 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
16 #include "base/string_split.h" 17 #include "base/string_split.h"
17 #include "base/string_util.h" 18 #include "base/string_util.h"
18 #include "base/time.h" 19 #include "base/time.h"
19 #include "base/utf_string_conversions.h" 20 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/google/google_url_tracker.h" 21 #include "chrome/browser/google/google_url_tracker.h"
21 #include "chrome/browser/history/history_notifications.h" 22 #include "chrome/browser/history/history_notifications.h"
22 #include "chrome/browser/history/history_service.h" 23 #include "chrome/browser/history/history_service.h"
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 622
622 // Always persist the setting in the database, that way if the backup 623 // Always persist the setting in the database, that way if the backup
623 // signature has changed out from under us it gets reset correctly. 624 // signature has changed out from under us it gets reset correctly.
624 if (SetDefaultSearchProviderNoNotify(url)) 625 if (SetDefaultSearchProviderNoNotify(url))
625 NotifyObservers(); 626 NotifyObservers();
626 } 627 }
627 628
628 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { 629 TemplateURL* TemplateURLService::GetDefaultSearchProvider() {
629 if (loaded_ && !load_failed_) 630 if (loaded_ && !load_failed_)
630 return default_search_provider_; 631 return default_search_provider_;
631
632 // We're not loaded, rely on the default search provider stored in prefs. 632 // We're not loaded, rely on the default search provider stored in prefs.
633 return initial_default_search_provider_.get(); 633 return initial_default_search_provider_.get();
634 } 634 }
635 635
636 TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { 636 TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() {
637 // See if the prepopulated default still exists. 637 // See if the prepopulated default still exists.
638 scoped_ptr<TemplateURL> prepopulated_default( 638 scoped_ptr<TemplateURL> prepopulated_default(
639 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_)); 639 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_));
640 for (TemplateURLVector::iterator i = template_urls_.begin(); 640 for (TemplateURLVector::iterator i = template_urls_.begin();
641 i != template_urls_.end(); ++i) { 641 i != template_urls_.end(); ++i) {
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 data.date_created = base::Time::FromInternalValue(specifics.date_created()); 1309 data.date_created = base::Time::FromInternalValue(specifics.date_created());
1310 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); 1310 data.last_modified = base::Time::FromInternalValue(specifics.last_modified());
1311 data.prepopulate_id = specifics.prepopulate_id(); 1311 data.prepopulate_id = specifics.prepopulate_id();
1312 data.sync_guid = specifics.sync_guid(); 1312 data.sync_guid = specifics.sync_guid();
1313 data.alternate_urls.clear(); 1313 data.alternate_urls.clear();
1314 for (int i = 0; i < specifics.alternate_urls_size(); ++i) 1314 for (int i = 0; i < specifics.alternate_urls_size(); ++i)
1315 data.alternate_urls.push_back(specifics.alternate_urls(i)); 1315 data.alternate_urls.push_back(specifics.alternate_urls(i));
1316 data.search_terms_replacement_key = specifics.search_terms_replacement_key(); 1316 data.search_terms_replacement_key = specifics.search_terms_replacement_key();
1317 1317
1318 TemplateURL* turl = new TemplateURL(profile, data); 1318 TemplateURL* turl = new TemplateURL(profile, data);
1319 // If this TemplateURL matches a built-in prepopulated template URL, it's
1320 // possible that sync is trying to modify fields that should not be touched.
1321 // Revert these fields to the built-in values.
1322 UpdateTemplateURLIfPrepopulated(turl, profile);
1319 DCHECK(!turl->IsExtensionKeyword()); 1323 DCHECK(!turl->IsExtensionKeyword());
1320 if (reset_keyword || deduped) { 1324 if (reset_keyword || deduped) {
1321 if (reset_keyword) 1325 if (reset_keyword)
1322 turl->ResetKeywordIfNecessary(true); 1326 turl->ResetKeywordIfNecessary(true);
1323 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1327 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1324 change_list->push_back( 1328 change_list->push_back(syncer::SyncChange(FROM_HERE,
1325 syncer::SyncChange(FROM_HERE, 1329 syncer::SyncChange::ACTION_UPDATE,
1326 syncer::SyncChange::ACTION_UPDATE, 1330 sync_data));
1327 sync_data));
1328 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1331 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1329 if (!existing_turl) { 1332 if (!existing_turl) {
1330 // We're adding a new TemplateURL that uses the Google base URL, so set 1333 // We're adding a new TemplateURL that uses the Google base URL, so set
1331 // its keyword appropriately for the local environment. 1334 // its keyword appropriately for the local environment.
1332 turl->ResetKeywordIfNecessary(false); 1335 turl->ResetKeywordIfNecessary(false);
1333 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1336 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1334 // Ignore keyword changes triggered by the Google base URL changing on 1337 // Ignore keyword changes triggered by the Google base URL changing on
1335 // another client. If the base URL changes in this client as well, we'll 1338 // another client. If the base URL changes in this client as well, we'll
1336 // pick that up separately at the appropriate time. Otherwise, changing 1339 // pick that up separately at the appropriate time. Otherwise, changing
1337 // the keyword here could result in having the wrong keyword for the local 1340 // the keyword here could result in having the wrong keyword for the local
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 existing_turl, 1801 existing_turl,
1799 syncer::SyncChange::ACTION_UPDATE); 1802 syncer::SyncChange::ACTION_UPDATE);
1800 1803
1801 if (default_search_provider_ == existing_turl) { 1804 if (default_search_provider_ == existing_turl) {
1802 bool success = SetDefaultSearchProviderNoNotify(existing_turl); 1805 bool success = SetDefaultSearchProviderNoNotify(existing_turl);
1803 DCHECK(success); 1806 DCHECK(success);
1804 } 1807 }
1805 return true; 1808 return true;
1806 } 1809 }
1807 1810
1811 // static
1812 void TemplateURLService::UpdateTemplateURLIfPrepopulated(
1813 TemplateURL* template_url,
1814 Profile* profile) {
1815 int prepopulate_id = template_url->prepopulate_id();
1816 if (template_url->prepopulate_id() == 0)
1817 return;
1818
1819 ScopedVector<TemplateURL> prepopulated_urls;
1820 size_t default_search_index;
1821 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile,
1822 &prepopulated_urls.get(), &default_search_index);
1823
1824 for (size_t i = 0; i < prepopulated_urls.size(); ++i) {
1825 if (prepopulated_urls[i]->prepopulate_id() == prepopulate_id) {
1826 MergeIntoPrepopulatedEngineData(&prepopulated_urls[i]->data_,
1827 template_url);
1828 template_url->CopyFrom(*prepopulated_urls[i]);
1829 }
1830 }
1831 }
1832
1808 PrefService* TemplateURLService::GetPrefs() { 1833 PrefService* TemplateURLService::GetPrefs() {
1809 return profile_ ? profile_->GetPrefs() : NULL; 1834 return profile_ ? profile_->GetPrefs() : NULL;
1810 } 1835 }
1811 1836
1812 void TemplateURLService::UpdateKeywordSearchTermsForURL( 1837 void TemplateURLService::UpdateKeywordSearchTermsForURL(
1813 const history::URLVisitedDetails& details) { 1838 const history::URLVisitedDetails& details) {
1814 const history::URLRow& row = details.row; 1839 const history::URLRow& row = details.row;
1815 if (!row.url().is_valid() || 1840 if (!row.url().is_valid() ||
1816 !row.url().parsed_for_possibly_invalid_spec().query.is_nonempty()) { 1841 !row.url().parsed_for_possibly_invalid_spec().query.is_nonempty()) {
1817 return; 1842 return;
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after
2499 // TODO(mpcomplete): If we allow editing extension keywords, then those 2524 // TODO(mpcomplete): If we allow editing extension keywords, then those
2500 // should be persisted to disk and synced. 2525 // should be persisted to disk and synced.
2501 if (template_url->sync_guid().empty() && 2526 if (template_url->sync_guid().empty() &&
2502 !template_url->IsExtensionKeyword()) { 2527 !template_url->IsExtensionKeyword()) {
2503 template_url->data_.sync_guid = base::GenerateGUID(); 2528 template_url->data_.sync_guid = base::GenerateGUID();
2504 if (service_.get()) 2529 if (service_.get())
2505 service_->UpdateKeyword(template_url->data()); 2530 service_->UpdateKeyword(template_url->data());
2506 } 2531 }
2507 } 2532 }
2508 } 2533 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698