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

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

Issue 2811793007: Make several methods of TemplateUrlService const (Closed)
Patch Set: Remove unnecessary local variable and add a couple TODOs Created 3 years, 8 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
« no previous file with comments | « components/search_engines/template_url_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 // keyword if there's evidence we may have created this keyword before and 360 // keyword if there's evidence we may have created this keyword before and
361 // the user renamed it (because, for instance, the keyword is a common word 361 // the user renamed it (because, for instance, the keyword is a common word
362 // that may interfere with search queries). An easy heuristic for this is 362 // that may interfere with search queries). An easy heuristic for this is
363 // whether the user has a TemplateURL that has been manually modified (e.g., 363 // whether the user has a TemplateURL that has been manually modified (e.g.,
364 // renamed) connected to the same host. 364 // renamed) connected to the same host.
365 return !url.is_valid() || url.host().empty() || 365 return !url.is_valid() || url.host().empty() ||
366 CanAddAutogeneratedKeywordForHost(url.host()); 366 CanAddAutogeneratedKeywordForHost(url.host());
367 } 367 }
368 368
369 bool TemplateURLService::IsPrepopulatedOrCreatedByPolicy( 369 bool TemplateURLService::IsPrepopulatedOrCreatedByPolicy(
370 const TemplateURL* t_url) { 370 const TemplateURL* t_url) const {
371 return (t_url->prepopulate_id() > 0 || t_url->created_by_policy()) && 371 return (t_url->prepopulate_id() > 0 || t_url->created_by_policy()) &&
372 t_url->SupportsReplacement(search_terms_data()); 372 t_url->SupportsReplacement(search_terms_data());
373 } 373 }
374 374
375 bool TemplateURLService::ShowInDefaultList(const TemplateURL* t_url) { 375 bool TemplateURLService::ShowInDefaultList(const TemplateURL* t_url) const {
376 return t_url == default_search_provider_ || 376 return t_url == default_search_provider_ ||
377 IsPrepopulatedOrCreatedByPolicy(t_url); 377 IsPrepopulatedOrCreatedByPolicy(t_url);
378 } 378 }
379 379
380 void TemplateURLService::AddMatchingKeywords( 380 void TemplateURLService::AddMatchingKeywords(
381 const base::string16& prefix, 381 const base::string16& prefix,
382 bool supports_replacement_only, 382 bool supports_replacement_only,
383 TURLsAndMeaningfulLengths* matches) { 383 TURLsAndMeaningfulLengths* matches) {
384 AddMatchingKeywordsHelper( 384 AddMatchingKeywordsHelper(
385 keyword_to_turl_and_length_, prefix, supports_replacement_only, matches); 385 keyword_to_turl_and_length_, prefix, supports_replacement_only, matches);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 } 563 }
564 564
565 void TemplateURLService::ResetTemplateURL(TemplateURL* url, 565 void TemplateURLService::ResetTemplateURL(TemplateURL* url,
566 const base::string16& title, 566 const base::string16& title,
567 const base::string16& keyword, 567 const base::string16& keyword,
568 const std::string& search_url) { 568 const std::string& search_url) {
569 if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) 569 if (ResetTemplateURLNoNotify(url, title, keyword, search_url))
570 NotifyObservers(); 570 NotifyObservers();
571 } 571 }
572 572
573 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { 573 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) const {
574 return 574 return
575 ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 575 ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
576 (default_search_provider_source_ == 576 (default_search_provider_source_ ==
577 DefaultSearchManager::FROM_FALLBACK)) && 577 DefaultSearchManager::FROM_FALLBACK)) &&
578 (url != GetDefaultSearchProvider()) && 578 (url != GetDefaultSearchProvider()) &&
579 url->url_ref().SupportsReplacement(search_terms_data()) && 579 url->url_ref().SupportsReplacement(search_terms_data()) &&
580 (url->type() == TemplateURL::NORMAL); 580 (url->type() == TemplateURL::NORMAL);
581 } 581 }
582 582
583 void TemplateURLService::SetUserSelectedDefaultSearchProvider( 583 void TemplateURLService::SetUserSelectedDefaultSearchProvider(
(...skipping 30 matching lines...) Expand all
614 : initial_default_search_provider_.get(); 614 : initial_default_search_provider_.get();
615 } 615 }
616 616
617 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( 617 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider(
618 const GURL& url) const { 618 const GURL& url) const {
619 const TemplateURL* default_provider = GetDefaultSearchProvider(); 619 const TemplateURL* default_provider = GetDefaultSearchProvider();
620 return default_provider && 620 return default_provider &&
621 default_provider->IsSearchURL(url, search_terms_data()); 621 default_provider->IsSearchURL(url, search_terms_data());
622 } 622 }
623 623
624 bool TemplateURLService::IsExtensionControlledDefaultSearch() { 624 bool TemplateURLService::IsExtensionControlledDefaultSearch() const {
625 return default_search_provider_source_ == 625 return default_search_provider_source_ ==
626 DefaultSearchManager::FROM_EXTENSION; 626 DefaultSearchManager::FROM_EXTENSION;
627 } 627 }
628 628
629 void TemplateURLService::RepairPrepopulatedSearchEngines() { 629 void TemplateURLService::RepairPrepopulatedSearchEngines() {
630 // Can't clean DB if it hasn't been loaded. 630 // Can't clean DB if it hasn't been loaded.
631 DCHECK(loaded()); 631 DCHECK(loaded());
632 632
633 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 633 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
634 (default_search_provider_source_ == 634 (default_search_provider_source_ ==
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 "Search.DefaultSearchProvider", rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, 843 "Search.DefaultSearchProvider", rappor::ETLD_PLUS_ONE_RAPPOR_TYPE,
844 net::registry_controlled_domains::GetDomainAndRegistry( 844 net::registry_controlled_domains::GetDomainAndRegistry(
845 default_search_provider_->url_ref().GetHost(search_terms_data()), 845 default_search_provider_->url_ref().GetHost(search_terms_data()),
846 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); 846 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
847 } 847 }
848 } 848 }
849 } 849 }
850 850
851 base::string16 TemplateURLService::GetKeywordShortName( 851 base::string16 TemplateURLService::GetKeywordShortName(
852 const base::string16& keyword, 852 const base::string16& keyword,
853 bool* is_omnibox_api_extension_keyword) { 853 bool* is_omnibox_api_extension_keyword) const {
854 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); 854 // TODO(jeffschiller): Make GetTemplateURLForKeyword const and remove the
855 // const_cast.
856 const TemplateURL* template_url =
857 const_cast<TemplateURLService*>(this)->GetTemplateURLForKeyword(keyword);
855 858
856 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService 859 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService
857 // to track changes to the model, this should become a DCHECK. 860 // to track changes to the model, this should become a DCHECK.
858 if (template_url) { 861 if (template_url) {
859 *is_omnibox_api_extension_keyword = 862 *is_omnibox_api_extension_keyword =
860 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; 863 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION;
861 return template_url->AdjustedShortNameForLocaleDirection(); 864 return template_url->AdjustedShortNameForLocaleDirection();
862 } 865 }
863 *is_omnibox_api_extension_keyword = false; 866 *is_omnibox_api_extension_keyword = false;
864 return base::string16(); 867 return base::string16();
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after
1622 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is 1625 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
1623 // fixed. 1626 // fixed.
1624 tracked_objects::ScopedTracker tracking_profile3( 1627 tracked_objects::ScopedTracker tracking_profile3(
1625 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1628 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1626 "422460 TemplateURLService::ChangeToLoadedState 3")); 1629 "422460 TemplateURLService::ChangeToLoadedState 3"));
1627 1630
1628 on_loaded_callbacks_.Notify(); 1631 on_loaded_callbacks_.Notify();
1629 } 1632 }
1630 1633
1631 bool TemplateURLService::CanAddAutogeneratedKeywordForHost( 1634 bool TemplateURLService::CanAddAutogeneratedKeywordForHost(
1632 const std::string& host) { 1635 const std::string& host) const {
1633 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host); 1636 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host);
1634 if (!urls) 1637 if (!urls)
1635 return true; 1638 return true;
1636 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { 1639 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
1637 if (!(*i)->safe_for_autoreplace()) 1640 if (!(*i)->safe_for_autoreplace())
1638 return false; 1641 return false;
1639 } 1642 }
1640 return true; 1643 return true;
1641 } 1644 }
1642 1645
1643 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { 1646 bool TemplateURLService::CanReplace(const TemplateURL* t_url) const {
1644 return !ShowInDefaultList(t_url) && t_url->safe_for_autoreplace(); 1647 return !ShowInDefaultList(t_url) && t_url->safe_for_autoreplace();
1645 } 1648 }
1646 1649
1647 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( 1650 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
1648 const base::string16& keyword) { 1651 const base::string16& keyword) {
1649 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); 1652 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword);
1650 if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL)) 1653 if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL))
1651 return keyword_turl; 1654 return keyword_turl;
1652 // The extension keyword in the model may be hiding a replaceable 1655 // The extension keyword in the model may be hiding a replaceable
1653 // non-extension keyword. Look for it. 1656 // non-extension keyword. Look for it.
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
2223 // This is a best-effort approach where we try to preserve the original 2226 // This is a best-effort approach where we try to preserve the original
2224 // keyword and let the user do what they will after our attempt. 2227 // keyword and let the user do what they will after our attempt.
2225 base::string16 keyword_candidate(turl.keyword()); 2228 base::string16 keyword_candidate(turl.keyword());
2226 do { 2229 do {
2227 keyword_candidate.append(base::ASCIIToUTF16("_")); 2230 keyword_candidate.append(base::ASCIIToUTF16("_"));
2228 } while (GetTemplateURLForKeyword(keyword_candidate)); 2231 } while (GetTemplateURLForKeyword(keyword_candidate));
2229 2232
2230 return keyword_candidate; 2233 return keyword_candidate;
2231 } 2234 }
2232 2235
2233 bool TemplateURLService::IsLocalTemplateURLBetter(const TemplateURL* local_turl, 2236 bool TemplateURLService::IsLocalTemplateURLBetter(
2234 const TemplateURL* sync_turl, 2237 const TemplateURL* local_turl,
2235 bool prefer_local_default) { 2238 const TemplateURL* sync_turl,
2236 DCHECK(GetTemplateURLForGUID(local_turl->sync_guid())); 2239 bool prefer_local_default) const {
2240 // TODO(jeffschiller): Make GetTemplateURLForKeyword const and remove the
2241 // const_cast.
2242 DCHECK(const_cast<TemplateURLService*>(this)->GetTemplateURLForGUID(
2243 local_turl->sync_guid()));
2237 return local_turl->last_modified() > sync_turl->last_modified() || 2244 return local_turl->last_modified() > sync_turl->last_modified() ||
2238 local_turl->created_by_policy() || 2245 local_turl->created_by_policy() ||
2239 (prefer_local_default && local_turl == GetDefaultSearchProvider()); 2246 (prefer_local_default && local_turl == GetDefaultSearchProvider());
2240 } 2247 }
2241 2248
2242 void TemplateURLService::ResolveSyncKeywordConflict( 2249 void TemplateURLService::ResolveSyncKeywordConflict(
2243 TemplateURL* unapplied_sync_turl, 2250 TemplateURL* unapplied_sync_turl,
2244 TemplateURL* applied_sync_turl, 2251 TemplateURL* applied_sync_turl,
2245 syncer::SyncChangeList* change_list) { 2252 syncer::SyncChangeList* change_list) {
2246 DCHECK(loaded_); 2253 DCHECK(loaded_);
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
2483 if (!IsCreatedByExtension(template_url.get())) { 2490 if (!IsCreatedByExtension(template_url.get())) {
2484 if (keyword_to_template_url.find(template_url->keyword()) != 2491 if (keyword_to_template_url.find(template_url->keyword()) !=
2485 keyword_to_template_url.end()) { 2492 keyword_to_template_url.end()) {
2486 return true; 2493 return true;
2487 } 2494 }
2488 keyword_to_template_url[template_url->keyword()] = template_url.get(); 2495 keyword_to_template_url[template_url->keyword()] = template_url.get();
2489 } 2496 }
2490 } 2497 }
2491 return false; 2498 return false;
2492 } 2499 }
OLDNEW
« no previous file with comments | « components/search_engines/template_url_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698