| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_service.h" | 5 #include "components/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 const base::string16& prefix, | 389 const base::string16& prefix, |
| 390 bool supports_replacement_only, | 390 bool supports_replacement_only, |
| 391 TURLsAndMeaningfulLengths* matches) { | 391 TURLsAndMeaningfulLengths* matches) { |
| 392 AddMatchingKeywordsHelper( | 392 AddMatchingKeywordsHelper( |
| 393 keyword_domain_to_turl_and_length_, prefix, supports_replacement_only, | 393 keyword_domain_to_turl_and_length_, prefix, supports_replacement_only, |
| 394 matches); | 394 matches); |
| 395 } | 395 } |
| 396 | 396 |
| 397 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( | 397 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( |
| 398 const base::string16& keyword) { | 398 const base::string16& keyword) { |
| 399 return const_cast<TemplateURL*>( |
| 400 static_cast<const TemplateURLService*>(this)-> |
| 401 GetTemplateURLForKeyword(keyword)); |
| 402 } |
| 403 |
| 404 const TemplateURL* TemplateURLService::GetTemplateURLForKeyword( |
| 405 const base::string16& keyword) const { |
| 399 KeywordToTURLAndMeaningfulLength::const_iterator elem( | 406 KeywordToTURLAndMeaningfulLength::const_iterator elem( |
| 400 keyword_to_turl_and_length_.find(keyword)); | 407 keyword_to_turl_and_length_.find(keyword)); |
| 401 if (elem != keyword_to_turl_and_length_.end()) | 408 if (elem != keyword_to_turl_and_length_.end()) |
| 402 return elem->second.first; | 409 return elem->second.first; |
| 403 return (!loaded_ && initial_default_search_provider_ && | 410 return (!loaded_ && initial_default_search_provider_ && |
| 404 (initial_default_search_provider_->keyword() == keyword)) | 411 (initial_default_search_provider_->keyword() == keyword)) |
| 405 ? initial_default_search_provider_.get() | 412 ? initial_default_search_provider_.get() |
| 406 : nullptr; | 413 : nullptr; |
| 407 } | 414 } |
| 408 | 415 |
| 409 TemplateURL* TemplateURLService::GetTemplateURLForGUID( | 416 TemplateURL* TemplateURLService::GetTemplateURLForGUID( |
| 410 const std::string& sync_guid) { | 417 const std::string& sync_guid) { |
| 418 return const_cast<TemplateURL*>( |
| 419 static_cast<const TemplateURLService*>(this)-> |
| 420 GetTemplateURLForGUID(sync_guid)); |
| 421 } |
| 422 |
| 423 const TemplateURL* TemplateURLService::GetTemplateURLForGUID( |
| 424 const std::string& sync_guid) const { |
| 411 GUIDToTURL::const_iterator elem(guid_to_turl_.find(sync_guid)); | 425 GUIDToTURL::const_iterator elem(guid_to_turl_.find(sync_guid)); |
| 412 if (elem != guid_to_turl_.end()) | 426 if (elem != guid_to_turl_.end()) |
| 413 return elem->second; | 427 return elem->second; |
| 414 return (!loaded_ && initial_default_search_provider_ && | 428 return (!loaded_ && initial_default_search_provider_ && |
| 415 (initial_default_search_provider_->sync_guid() == sync_guid)) | 429 (initial_default_search_provider_->sync_guid() == sync_guid)) |
| 416 ? initial_default_search_provider_.get() | 430 ? initial_default_search_provider_.get() |
| 417 : nullptr; | 431 : nullptr; |
| 418 } | 432 } |
| 419 | 433 |
| 420 TemplateURL* TemplateURLService::GetTemplateURLForHost( | 434 TemplateURL* TemplateURLService::GetTemplateURLForHost( |
| 421 const std::string& host) { | 435 const std::string& host) { |
| 436 return const_cast<TemplateURL*>( |
| 437 static_cast<const TemplateURLService*>(this)-> |
| 438 GetTemplateURLForHost(host)); |
| 439 } |
| 440 |
| 441 const TemplateURL* TemplateURLService::GetTemplateURLForHost( |
| 442 const std::string& host) const { |
| 422 if (loaded_) | 443 if (loaded_) |
| 423 return provider_map_->GetTemplateURLForHost(host); | 444 return provider_map_->GetTemplateURLForHost(host); |
| 424 TemplateURL* initial_dsp = initial_default_search_provider_.get(); | 445 TemplateURL* initial_dsp = initial_default_search_provider_.get(); |
| 425 return (initial_dsp && | 446 return (initial_dsp && |
| 426 (initial_dsp->GenerateSearchURL(search_terms_data()).host_piece() == | 447 (initial_dsp->GenerateSearchURL(search_terms_data()).host_piece() == |
| 427 host)) | 448 host)) |
| 428 ? initial_dsp | 449 ? initial_dsp |
| 429 : nullptr; | 450 : nullptr; |
| 430 } | 451 } |
| 431 | 452 |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 net::registry_controlled_domains::GetDomainAndRegistry( | 865 net::registry_controlled_domains::GetDomainAndRegistry( |
| 845 default_search_provider_->url_ref().GetHost(search_terms_data()), | 866 default_search_provider_->url_ref().GetHost(search_terms_data()), |
| 846 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); | 867 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); |
| 847 } | 868 } |
| 848 } | 869 } |
| 849 } | 870 } |
| 850 | 871 |
| 851 base::string16 TemplateURLService::GetKeywordShortName( | 872 base::string16 TemplateURLService::GetKeywordShortName( |
| 852 const base::string16& keyword, | 873 const base::string16& keyword, |
| 853 bool* is_omnibox_api_extension_keyword) const { | 874 bool* is_omnibox_api_extension_keyword) const { |
| 854 // TODO(jeffschiller): Make GetTemplateURLForKeyword const and remove the | 875 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); |
| 855 // const_cast. | |
| 856 const TemplateURL* template_url = | |
| 857 const_cast<TemplateURLService*>(this)->GetTemplateURLForKeyword(keyword); | |
| 858 | 876 |
| 859 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService | 877 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService |
| 860 // to track changes to the model, this should become a DCHECK. | 878 // to track changes to the model, this should become a DCHECK. |
| 861 if (template_url) { | 879 if (template_url) { |
| 862 *is_omnibox_api_extension_keyword = | 880 *is_omnibox_api_extension_keyword = |
| 863 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; | 881 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; |
| 864 return template_url->AdjustedShortNameForLocaleDirection(); | 882 return template_url->AdjustedShortNameForLocaleDirection(); |
| 865 } | 883 } |
| 866 *is_omnibox_api_extension_keyword = false; | 884 *is_omnibox_api_extension_keyword = false; |
| 867 return base::string16(); | 885 return base::string16(); |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1257 se_specifics->keyword(), | 1275 se_specifics->keyword(), |
| 1258 specifics); | 1276 specifics); |
| 1259 } | 1277 } |
| 1260 | 1278 |
| 1261 // static | 1279 // static |
| 1262 std::unique_ptr<TemplateURL> | 1280 std::unique_ptr<TemplateURL> |
| 1263 TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( | 1281 TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 1264 TemplateURLServiceClient* client, | 1282 TemplateURLServiceClient* client, |
| 1265 PrefService* prefs, | 1283 PrefService* prefs, |
| 1266 const SearchTermsData& search_terms_data, | 1284 const SearchTermsData& search_terms_data, |
| 1267 TemplateURL* existing_turl, | 1285 const TemplateURL* existing_turl, |
| 1268 const syncer::SyncData& sync_data, | 1286 const syncer::SyncData& sync_data, |
| 1269 syncer::SyncChangeList* change_list) { | 1287 syncer::SyncChangeList* change_list) { |
| 1270 DCHECK(change_list); | 1288 DCHECK(change_list); |
| 1271 | 1289 |
| 1272 sync_pb::SearchEngineSpecifics specifics = | 1290 sync_pb::SearchEngineSpecifics specifics = |
| 1273 sync_data.GetSpecifics().search_engine(); | 1291 sync_data.GetSpecifics().search_engine(); |
| 1274 | 1292 |
| 1275 // Past bugs might have caused either of these fields to be empty. Just | 1293 // Past bugs might have caused either of these fields to be empty. Just |
| 1276 // delete this data off the server. | 1294 // delete this data off the server. |
| 1277 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1295 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
| (...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2230 keyword_candidate.append(base::ASCIIToUTF16("_")); | 2248 keyword_candidate.append(base::ASCIIToUTF16("_")); |
| 2231 } while (GetTemplateURLForKeyword(keyword_candidate)); | 2249 } while (GetTemplateURLForKeyword(keyword_candidate)); |
| 2232 | 2250 |
| 2233 return keyword_candidate; | 2251 return keyword_candidate; |
| 2234 } | 2252 } |
| 2235 | 2253 |
| 2236 bool TemplateURLService::IsLocalTemplateURLBetter( | 2254 bool TemplateURLService::IsLocalTemplateURLBetter( |
| 2237 const TemplateURL* local_turl, | 2255 const TemplateURL* local_turl, |
| 2238 const TemplateURL* sync_turl, | 2256 const TemplateURL* sync_turl, |
| 2239 bool prefer_local_default) const { | 2257 bool prefer_local_default) const { |
| 2240 // TODO(jeffschiller): Make GetTemplateURLForKeyword const and remove the | 2258 DCHECK(GetTemplateURLForGUID(local_turl->sync_guid())); |
| 2241 // const_cast. | |
| 2242 DCHECK(const_cast<TemplateURLService*>(this)->GetTemplateURLForGUID( | |
| 2243 local_turl->sync_guid())); | |
| 2244 return local_turl->last_modified() > sync_turl->last_modified() || | 2259 return local_turl->last_modified() > sync_turl->last_modified() || |
| 2245 local_turl->created_by_policy() || | 2260 local_turl->created_by_policy() || |
| 2246 (prefer_local_default && local_turl == GetDefaultSearchProvider()); | 2261 (prefer_local_default && local_turl == GetDefaultSearchProvider()); |
| 2247 } | 2262 } |
| 2248 | 2263 |
| 2249 void TemplateURLService::ResolveSyncKeywordConflict( | 2264 void TemplateURLService::ResolveSyncKeywordConflict( |
| 2250 TemplateURL* unapplied_sync_turl, | 2265 TemplateURL* unapplied_sync_turl, |
| 2251 TemplateURL* applied_sync_turl, | 2266 TemplateURL* applied_sync_turl, |
| 2252 syncer::SyncChangeList* change_list) { | 2267 syncer::SyncChangeList* change_list) { |
| 2253 DCHECK(loaded_); | 2268 DCHECK(loaded_); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2411 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2426 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2412 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); | 2427 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); |
| 2413 | 2428 |
| 2414 std::string new_guid = | 2429 std::string new_guid = |
| 2415 prefs_->GetString(prefs::kSyncedDefaultSearchProviderGUID); | 2430 prefs_->GetString(prefs::kSyncedDefaultSearchProviderGUID); |
| 2416 if (new_guid.empty()) { | 2431 if (new_guid.empty()) { |
| 2417 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); | 2432 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
| 2418 return; | 2433 return; |
| 2419 } | 2434 } |
| 2420 | 2435 |
| 2421 TemplateURL* turl = GetTemplateURLForGUID(new_guid); | 2436 const TemplateURL* turl = GetTemplateURLForGUID(new_guid); |
| 2422 if (turl) | 2437 if (turl) |
| 2423 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); | 2438 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); |
| 2424 } | 2439 } |
| 2425 | 2440 |
| 2426 template <typename Container> | 2441 template <typename Container> |
| 2427 void TemplateURLService::AddMatchingKeywordsHelper( | 2442 void TemplateURLService::AddMatchingKeywordsHelper( |
| 2428 const Container& keyword_to_turl_and_length, | 2443 const Container& keyword_to_turl_and_length, |
| 2429 const base::string16& prefix, | 2444 const base::string16& prefix, |
| 2430 bool supports_replacement_only, | 2445 bool supports_replacement_only, |
| 2431 TURLsAndMeaningfulLengths* matches) { | 2446 TURLsAndMeaningfulLengths* matches) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2490 if (!IsCreatedByExtension(template_url.get())) { | 2505 if (!IsCreatedByExtension(template_url.get())) { |
| 2491 if (keyword_to_template_url.find(template_url->keyword()) != | 2506 if (keyword_to_template_url.find(template_url->keyword()) != |
| 2492 keyword_to_template_url.end()) { | 2507 keyword_to_template_url.end()) { |
| 2493 return true; | 2508 return true; |
| 2494 } | 2509 } |
| 2495 keyword_to_template_url[template_url->keyword()] = template_url.get(); | 2510 keyword_to_template_url[template_url->keyword()] = template_url.get(); |
| 2496 } | 2511 } |
| 2497 } | 2512 } |
| 2498 return false; | 2513 return false; |
| 2499 } | 2514 } |
| OLD | NEW |