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

Side by Side Diff: components/search_engines/template_url_service.cc

Issue 2487633003: Change behaivor to decide whether a search engine should be shown in the default list (Closed)
Patch Set: Created 4 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
OLDNEW
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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 // We don't have a TemplateURL with keyword. We still may not allow this 388 // We don't have a TemplateURL with keyword. We still may not allow this
389 // keyword if there's evidence we may have created this keyword before and 389 // keyword if there's evidence we may have created this keyword before and
390 // the user renamed it (because, for instance, the keyword is a common word 390 // the user renamed it (because, for instance, the keyword is a common word
391 // that may interfere with search queries). An easy heuristic for this is 391 // that may interfere with search queries). An easy heuristic for this is
392 // whether the user has a TemplateURL that has been manually modified (e.g., 392 // whether the user has a TemplateURL that has been manually modified (e.g.,
393 // renamed) connected to the same host. 393 // renamed) connected to the same host.
394 return !url.is_valid() || url.host().empty() || 394 return !url.is_valid() || url.host().empty() ||
395 CanAddAutogeneratedKeywordForHost(url.host()); 395 CanAddAutogeneratedKeywordForHost(url.host());
396 } 396 }
397 397
398 bool TemplateURLService::ShowInDefaultList(const TemplateURL* t_url) {
399 return t_url == default_search_provider_ || t_url->prepopulate_id() > 0 ||
400 t_url->created_by_policy();
401 }
402
398 void TemplateURLService::AddMatchingKeywords( 403 void TemplateURLService::AddMatchingKeywords(
399 const base::string16& prefix, 404 const base::string16& prefix,
400 bool supports_replacement_only, 405 bool supports_replacement_only,
401 TURLsAndMeaningfulLengths* matches) { 406 TURLsAndMeaningfulLengths* matches) {
402 AddMatchingKeywordsHelper( 407 AddMatchingKeywordsHelper(
403 keyword_to_turl_and_length_, prefix, supports_replacement_only, matches); 408 keyword_to_turl_and_length_, prefix, supports_replacement_only, matches);
404 } 409 }
405 410
406 void TemplateURLService::AddMatchingDomainKeywords( 411 void TemplateURLService::AddMatchingDomainKeywords(
407 const base::string16& prefix, 412 const base::string16& prefix,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 } 475 }
471 476
472 TemplateURL* TemplateURLService::AddExtensionControlledTURL( 477 TemplateURL* TemplateURLService::AddExtensionControlledTURL(
473 std::unique_ptr<TemplateURL> template_url, 478 std::unique_ptr<TemplateURL> template_url,
474 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { 479 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) {
475 DCHECK(loaded_); 480 DCHECK(loaded_);
476 DCHECK(template_url); 481 DCHECK(template_url);
477 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 482 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
478 DCHECK(info); 483 DCHECK(info);
479 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); 484 DCHECK_NE(TemplateURL::NORMAL, template_url->type());
480 DCHECK_EQ(info->wants_to_be_default_engine,
481 template_url->show_in_default_list());
482 DCHECK( 485 DCHECK(
483 !FindTemplateURLForExtension(info->extension_id, template_url->type())); 486 !FindTemplateURLForExtension(info->extension_id, template_url->type()));
484 template_url->extension_info_.swap(info); 487 template_url->extension_info_.swap(info);
485 488
486 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 489 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
487 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); 490 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true);
488 if (template_url_ptr) { 491 if (template_url_ptr) {
489 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { 492 if (template_url_ptr->extension_info_->wants_to_be_default_engine) {
490 UpdateExtensionDefaultSearchEngine(); 493 UpdateExtensionDefaultSearchEngine();
491 } 494 }
492 NotifyObservers(); 495 NotifyObservers();
493 } 496 }
494 497
498 DCHECK_EQ(template_url_ptr->extension_info_->wants_to_be_default_engine,
499 ShowInDefaultList(template_url_ptr));
Peter Kasting 2016/11/10 06:41:07 Hmm... is this safe? In the future, if ShowInDefa
ltian 2016/11/11 03:52:13 I guess for most cases it is fine because within U
500
495 return template_url_ptr; 501 return template_url_ptr;
496 } 502 }
497 503
498 void TemplateURLService::Remove(TemplateURL* template_url) { 504 void TemplateURLService::Remove(TemplateURL* template_url) {
499 RemoveNoNotify(template_url); 505 RemoveNoNotify(template_url);
500 NotifyObservers(); 506 NotifyObservers();
501 } 507 }
502 508
503 void TemplateURLService::RemoveExtensionControlledTURL( 509 void TemplateURLService::RemoveExtensionControlledTURL(
504 const std::string& extension_id, 510 const std::string& extension_id,
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 specifics.mutable_search_engine(); 1243 specifics.mutable_search_engine();
1238 se_specifics->set_short_name(base::UTF16ToUTF8(turl.short_name())); 1244 se_specifics->set_short_name(base::UTF16ToUTF8(turl.short_name()));
1239 se_specifics->set_keyword(base::UTF16ToUTF8(turl.keyword())); 1245 se_specifics->set_keyword(base::UTF16ToUTF8(turl.keyword()));
1240 se_specifics->set_favicon_url(turl.favicon_url().spec()); 1246 se_specifics->set_favicon_url(turl.favicon_url().spec());
1241 se_specifics->set_url(turl.url()); 1247 se_specifics->set_url(turl.url());
1242 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); 1248 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace());
1243 se_specifics->set_originating_url(turl.originating_url().spec()); 1249 se_specifics->set_originating_url(turl.originating_url().spec());
1244 se_specifics->set_date_created(turl.date_created().ToInternalValue()); 1250 se_specifics->set_date_created(turl.date_created().ToInternalValue());
1245 se_specifics->set_input_encodings( 1251 se_specifics->set_input_encodings(
1246 base::JoinString(turl.input_encodings(), ";")); 1252 base::JoinString(turl.input_encodings(), ";"));
1247 se_specifics->set_show_in_default_list(turl.show_in_default_list());
1248 se_specifics->set_suggestions_url(turl.suggestions_url()); 1253 se_specifics->set_suggestions_url(turl.suggestions_url());
1249 se_specifics->set_prepopulate_id(turl.prepopulate_id()); 1254 se_specifics->set_prepopulate_id(turl.prepopulate_id());
1250 se_specifics->set_instant_url(turl.instant_url()); 1255 se_specifics->set_instant_url(turl.instant_url());
1251 if (!turl.image_url().empty()) 1256 if (!turl.image_url().empty())
1252 se_specifics->set_image_url(turl.image_url()); 1257 se_specifics->set_image_url(turl.image_url());
1253 se_specifics->set_new_tab_url(turl.new_tab_url()); 1258 se_specifics->set_new_tab_url(turl.new_tab_url());
1254 if (!turl.search_url_post_params().empty()) 1259 if (!turl.search_url_post_params().empty())
1255 se_specifics->set_search_url_post_params(turl.search_url_post_params()); 1260 se_specifics->set_search_url_post_params(turl.search_url_post_params());
1256 if (!turl.suggestions_url_post_params().empty()) { 1261 if (!turl.suggestions_url_post_params().empty()) {
1257 se_specifics->set_suggestions_url_post_params( 1262 se_specifics->set_suggestions_url_post_params(
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 data.SetURL(specifics.url()); 1322 data.SetURL(specifics.url());
1318 data.suggestions_url = specifics.suggestions_url(); 1323 data.suggestions_url = specifics.suggestions_url();
1319 data.instant_url = specifics.instant_url(); 1324 data.instant_url = specifics.instant_url();
1320 data.image_url = specifics.image_url(); 1325 data.image_url = specifics.image_url();
1321 data.new_tab_url = specifics.new_tab_url(); 1326 data.new_tab_url = specifics.new_tab_url();
1322 data.search_url_post_params = specifics.search_url_post_params(); 1327 data.search_url_post_params = specifics.search_url_post_params();
1323 data.suggestions_url_post_params = specifics.suggestions_url_post_params(); 1328 data.suggestions_url_post_params = specifics.suggestions_url_post_params();
1324 data.instant_url_post_params = specifics.instant_url_post_params(); 1329 data.instant_url_post_params = specifics.instant_url_post_params();
1325 data.image_url_post_params = specifics.image_url_post_params(); 1330 data.image_url_post_params = specifics.image_url_post_params();
1326 data.favicon_url = GURL(specifics.favicon_url()); 1331 data.favicon_url = GURL(specifics.favicon_url());
1327 data.show_in_default_list = specifics.show_in_default_list();
1328 data.safe_for_autoreplace = specifics.safe_for_autoreplace(); 1332 data.safe_for_autoreplace = specifics.safe_for_autoreplace();
1329 data.input_encodings = base::SplitString( 1333 data.input_encodings = base::SplitString(
1330 specifics.input_encodings(), ";", 1334 specifics.input_encodings(), ";",
1331 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 1335 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
1332 // If the server data has duplicate encodings, we'll want to push an update 1336 // If the server data has duplicate encodings, we'll want to push an update
1333 // below to correct it. Note that we also fix this in 1337 // below to correct it. Note that we also fix this in
1334 // GetSearchProvidersUsingKeywordResult(), since otherwise we'd never correct 1338 // GetSearchProvidersUsingKeywordResult(), since otherwise we'd never correct
1335 // local problems for clients which have disabled search engine sync. 1339 // local problems for clients which have disabled search engine sync.
1336 bool deduped = DeDupeEncodings(&data.input_encodings); 1340 bool deduped = DeDupeEncodings(&data.input_encodings);
1337 data.date_created = base::Time::FromInternalValue(specifics.date_created()); 1341 data.date_created = base::Time::FromInternalValue(specifics.date_created());
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 if (!urls) 1638 if (!urls)
1635 return true; 1639 return true;
1636 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { 1640 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
1637 if (!(*i)->safe_for_autoreplace()) 1641 if (!(*i)->safe_for_autoreplace())
1638 return false; 1642 return false;
1639 } 1643 }
1640 return true; 1644 return true;
1641 } 1645 }
1642 1646
1643 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { 1647 bool TemplateURLService::CanReplace(const TemplateURL* t_url) {
1644 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && 1648 return !ShowInDefaultList(t_url) && t_url->safe_for_autoreplace();
1645 t_url->safe_for_autoreplace());
1646 } 1649 }
1647 1650
1648 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( 1651 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
1649 const base::string16& keyword) { 1652 const base::string16& keyword) {
1650 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); 1653 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword);
1651 if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL)) 1654 if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL))
1652 return keyword_turl; 1655 return keyword_turl;
1653 // The extension keyword in the model may be hiding a replaceable 1656 // The extension keyword in the model may be hiding a replaceable
1654 // non-extension keyword. Look for it. 1657 // non-extension keyword. Look for it.
1655 for (const auto& turl : template_urls_) { 1658 for (const auto& turl : template_urls_) {
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
1960 if (AddNoNotify(std::move(new_dse_ptr), true)) 1963 if (AddNoNotify(std::move(new_dse_ptr), true))
1961 default_search_provider_ = new_dse; 1964 default_search_provider_ = new_dse;
1962 } 1965 }
1963 } else if (source == DefaultSearchManager::FROM_USER) { 1966 } else if (source == DefaultSearchManager::FROM_USER) {
1964 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); 1967 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid);
1965 if (!default_search_provider_ && data->prepopulate_id) { 1968 if (!default_search_provider_ && data->prepopulate_id) {
1966 default_search_provider_ = 1969 default_search_provider_ =
1967 FindPrepopulatedTemplateURL(data->prepopulate_id); 1970 FindPrepopulatedTemplateURL(data->prepopulate_id);
1968 } 1971 }
1969 TemplateURLData new_data(*data); 1972 TemplateURLData new_data(*data);
1970 new_data.show_in_default_list = true;
1971 if (default_search_provider_) { 1973 if (default_search_provider_) {
1972 UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); 1974 UpdateNoNotify(default_search_provider_, TemplateURL(new_data));
1973 } else { 1975 } else {
1974 new_data.id = kInvalidTemplateURLID; 1976 new_data.id = kInvalidTemplateURLID;
1975 std::unique_ptr<TemplateURL> new_dse_ptr = 1977 std::unique_ptr<TemplateURL> new_dse_ptr =
1976 base::MakeUnique<TemplateURL>(new_data); 1978 base::MakeUnique<TemplateURL>(new_data);
1977 TemplateURL* new_dse = new_dse_ptr.get(); 1979 TemplateURL* new_dse = new_dse_ptr.get();
1978 if (AddNoNotify(std::move(new_dse_ptr), true)) 1980 if (AddNoNotify(std::move(new_dse_ptr), true))
1979 default_search_provider_ = new_dse; 1981 default_search_provider_ = new_dse;
1980 } 1982 }
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
2473 2475
2474 if (most_recently_intalled_default) { 2476 if (most_recently_intalled_default) {
2475 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2477 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2476 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2478 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2477 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2479 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2478 most_recently_intalled_default->data()); 2480 most_recently_intalled_default->data());
2479 } else { 2481 } else {
2480 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2482 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2481 } 2483 }
2482 } 2484 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698