| 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 #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 Loading... |
| 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 Loading... |
| 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( |
| 1325 syncer::SyncChange(FROM_HERE, | 1329 syncer::SyncChange(FROM_HERE, |
| 1326 syncer::SyncChange::ACTION_UPDATE, | 1330 syncer::SyncChange::ACTION_UPDATE, |
| 1327 sync_data)); | 1331 sync_data)); |
| 1328 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1332 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1749 | 1753 |
| 1750 string16 old_keyword(existing_turl->keyword()); | 1754 string16 old_keyword(existing_turl->keyword()); |
| 1751 keyword_to_template_map_.erase(old_keyword); | 1755 keyword_to_template_map_.erase(old_keyword); |
| 1752 if (!existing_turl->sync_guid().empty()) | 1756 if (!existing_turl->sync_guid().empty()) |
| 1753 guid_to_template_map_.erase(existing_turl->sync_guid()); | 1757 guid_to_template_map_.erase(existing_turl->sync_guid()); |
| 1754 | 1758 |
| 1755 provider_map_->Remove(existing_turl, old_search_terms_data); | 1759 provider_map_->Remove(existing_turl, old_search_terms_data); |
| 1756 TemplateURLID previous_id = existing_turl->id(); | 1760 TemplateURLID previous_id = existing_turl->id(); |
| 1757 existing_turl->CopyFrom(new_values); | 1761 existing_turl->CopyFrom(new_values); |
| 1758 existing_turl->data_.id = previous_id; | 1762 existing_turl->data_.id = previous_id; |
| 1763 |
| 1759 UIThreadSearchTermsData new_search_terms_data(profile_); | 1764 UIThreadSearchTermsData new_search_terms_data(profile_); |
| 1760 provider_map_->Add(existing_turl, new_search_terms_data); | 1765 provider_map_->Add(existing_turl, new_search_terms_data); |
| 1761 | 1766 |
| 1762 const string16& keyword = existing_turl->keyword(); | 1767 const string16& keyword = existing_turl->keyword(); |
| 1763 KeywordToTemplateMap::const_iterator i = | 1768 KeywordToTemplateMap::const_iterator i = |
| 1764 keyword_to_template_map_.find(keyword); | 1769 keyword_to_template_map_.find(keyword); |
| 1765 if (i == keyword_to_template_map_.end()) { | 1770 if (i == keyword_to_template_map_.end()) { |
| 1766 keyword_to_template_map_[keyword] = existing_turl; | 1771 keyword_to_template_map_[keyword] = existing_turl; |
| 1767 } else { | 1772 } else { |
| 1768 // We can theoretically reach here in two cases: | 1773 // We can theoretically reach here in two cases: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1798 existing_turl, | 1803 existing_turl, |
| 1799 syncer::SyncChange::ACTION_UPDATE); | 1804 syncer::SyncChange::ACTION_UPDATE); |
| 1800 | 1805 |
| 1801 if (default_search_provider_ == existing_turl) { | 1806 if (default_search_provider_ == existing_turl) { |
| 1802 bool success = SetDefaultSearchProviderNoNotify(existing_turl); | 1807 bool success = SetDefaultSearchProviderNoNotify(existing_turl); |
| 1803 DCHECK(success); | 1808 DCHECK(success); |
| 1804 } | 1809 } |
| 1805 return true; | 1810 return true; |
| 1806 } | 1811 } |
| 1807 | 1812 |
| 1813 // static |
| 1814 void TemplateURLService::UpdateTemplateURLIfPrepopulated( |
| 1815 TemplateURL* template_url, |
| 1816 Profile* profile) { |
| 1817 int prepopulate_id = template_url->prepopulate_id(); |
| 1818 if (template_url->prepopulate_id() == 0) |
| 1819 return; |
| 1820 |
| 1821 ScopedVector<TemplateURL> prepopulated_urls; |
| 1822 size_t default_search_index; |
| 1823 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, |
| 1824 &prepopulated_urls.get(), &default_search_index); |
| 1825 |
| 1826 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { |
| 1827 if (prepopulated_urls[i]->prepopulate_id() == prepopulate_id) { |
| 1828 MergeIntoPrepopulatedEngineData(&prepopulated_urls[i]->data_, |
| 1829 template_url); |
| 1830 template_url->CopyFrom(*prepopulated_urls[i]); |
| 1831 } |
| 1832 } |
| 1833 } |
| 1834 |
| 1808 PrefService* TemplateURLService::GetPrefs() { | 1835 PrefService* TemplateURLService::GetPrefs() { |
| 1809 return profile_ ? profile_->GetPrefs() : NULL; | 1836 return profile_ ? profile_->GetPrefs() : NULL; |
| 1810 } | 1837 } |
| 1811 | 1838 |
| 1812 void TemplateURLService::UpdateKeywordSearchTermsForURL( | 1839 void TemplateURLService::UpdateKeywordSearchTermsForURL( |
| 1813 const history::URLVisitedDetails& details) { | 1840 const history::URLVisitedDetails& details) { |
| 1814 const history::URLRow& row = details.row; | 1841 const history::URLRow& row = details.row; |
| 1815 if (!row.url().is_valid() || | 1842 if (!row.url().is_valid() || |
| 1816 !row.url().parsed_for_possibly_invalid_spec().query.is_nonempty()) { | 1843 !row.url().parsed_for_possibly_invalid_spec().query.is_nonempty()) { |
| 1817 return; | 1844 return; |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2128 if (url) | 2155 if (url) |
| 2129 ProcessTemplateURLChange(FROM_HERE, | 2156 ProcessTemplateURLChange(FROM_HERE, |
| 2130 url, | 2157 url, |
| 2131 syncer::SyncChange::ACTION_UPDATE); | 2158 syncer::SyncChange::ACTION_UPDATE); |
| 2132 return true; | 2159 return true; |
| 2133 } | 2160 } |
| 2134 | 2161 |
| 2135 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2162 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
| 2136 bool newly_adding) { | 2163 bool newly_adding) { |
| 2137 DCHECK(template_url); | 2164 DCHECK(template_url); |
| 2138 | |
| 2139 if (newly_adding) { | 2165 if (newly_adding) { |
| 2140 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2166 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 2141 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2167 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| 2142 template_url) == template_urls_.end()); | 2168 template_url) == template_urls_.end()); |
| 2143 template_url->data_.id = ++next_id_; | 2169 template_url->data_.id = ++next_id_; |
| 2144 } | 2170 } |
| 2145 | 2171 |
| 2146 template_url->ResetKeywordIfNecessary(false); | 2172 template_url->ResetKeywordIfNecessary(false); |
| 2147 if (!template_url->IsExtensionKeyword()) { | 2173 if (!template_url->IsExtensionKeyword()) { |
| 2148 // Check whether |template_url|'s keyword conflicts with any already in the | 2174 // Check whether |template_url|'s keyword conflicts with any already in the |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2499 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2525 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2500 // should be persisted to disk and synced. | 2526 // should be persisted to disk and synced. |
| 2501 if (template_url->sync_guid().empty() && | 2527 if (template_url->sync_guid().empty() && |
| 2502 !template_url->IsExtensionKeyword()) { | 2528 !template_url->IsExtensionKeyword()) { |
| 2503 template_url->data_.sync_guid = base::GenerateGUID(); | 2529 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2504 if (service_.get()) | 2530 if (service_.get()) |
| 2505 service_->UpdateKeyword(template_url->data()); | 2531 service_->UpdateKeyword(template_url->data()); |
| 2506 } | 2532 } |
| 2507 } | 2533 } |
| 2508 } | 2534 } |
| OLD | NEW |