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

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

Issue 273153004: Revert of Handle TemplateURLService load failure better, and make some test correctness fixes that will be ne… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 <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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #include "sync/api/sync_error_factory.h" 50 #include "sync/api/sync_error_factory.h"
51 #include "sync/protocol/search_engine_specifics.pb.h" 51 #include "sync/protocol/search_engine_specifics.pb.h"
52 #include "sync/protocol/sync.pb.h" 52 #include "sync/protocol/sync.pb.h"
53 #include "ui/base/l10n/l10n_util.h" 53 #include "ui/base/l10n/l10n_util.h"
54 54
55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; 55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
56 typedef TemplateURLService::SyncDataMap SyncDataMap; 56 typedef TemplateURLService::SyncDataMap SyncDataMap;
57 57
58 namespace { 58 namespace {
59 59
60 bool TemplateURLMatchesData(const TemplateURL* url1,
61 const TemplateURLData* url2) {
62 if (!url1 || !url2)
63 return !url1 && !url2;
64
65 return (url1->short_name() == url2->short_name) &&
66 url1->HasSameKeywordAs(*url2) &&
67 (url1->url() == url2->url()) &&
68 (url1->suggestions_url() == url2->suggestions_url) &&
69 (url1->instant_url() == url2->instant_url) &&
70 (url1->image_url() == url2->image_url) &&
71 (url1->new_tab_url() == url2->new_tab_url) &&
72 (url1->search_url_post_params() == url2->search_url_post_params) &&
73 (url1->suggestions_url_post_params() ==
74 url2->suggestions_url_post_params) &&
75 (url1->instant_url_post_params() == url2->instant_url_post_params) &&
76 (url1->image_url_post_params() == url2->image_url_post_params) &&
77 (url1->favicon_url() == url2->favicon_url) &&
78 (url1->safe_for_autoreplace() == url2->safe_for_autoreplace) &&
79 (url1->show_in_default_list() == url2->show_in_default_list) &&
80 (url1->input_encodings() == url2->input_encodings) &&
81 (url1->alternate_urls() == url2->alternate_urls) &&
82 (url1->search_terms_replacement_key() ==
83 url2->search_terms_replacement_key);
84 }
85
60 const char kFirstPotentialEngineHistogramName[] = 86 const char kFirstPotentialEngineHistogramName[] =
61 "Search.FirstPotentialEngineCalled"; 87 "Search.FirstPotentialEngineCalled";
62 88
63 // Values for an enumerated histogram used to track whenever 89 // Values for an enumerated histogram used to track whenever
64 // FirstPotentialDefaultEngine is called, and from where. 90 // FirstPotentialDefaultEngine is called, and from where.
65 enum FirstPotentialEngineCaller { 91 enum FirstPotentialEngineCaller {
66 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP, 92 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP,
67 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_PROCESSING_SYNC_CHANGES, 93 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_PROCESSING_SYNC_CHANGES,
68 FIRST_POTENTIAL_CALLSITE_ON_LOAD, 94 FIRST_POTENTIAL_CALLSITE_ON_LOAD,
69 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_SYNCING, 95 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_SYNCING,
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 } 413 }
388 if (!prepopulate_id.empty() && !*is_managed) { 414 if (!prepopulate_id.empty() && !*is_managed) {
389 int value; 415 int value;
390 base::StringToInt(prepopulate_id, &value); 416 base::StringToInt(prepopulate_id, &value);
391 (*default_provider_data)->prepopulate_id = value; 417 (*default_provider_data)->prepopulate_id = value;
392 } 418 }
393 return true; 419 return true;
394 } 420 }
395 421
396 // static 422 // static
397 base::string16 TemplateURLService::GenerateKeyword(const GURL& url) {
398 DCHECK(url.is_valid());
399 // Strip "www." off the front of the keyword; otherwise the keyword won't work
400 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
401 // Special case: if the host was exactly "www." (not sure this can happen but
402 // perhaps with some weird intranet and custom DNS server?), ensure we at
403 // least don't return the empty string.
404 base::string16 keyword(net::StripWWWFromHost(url));
405 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword;
406 }
407
408 // static
409 base::string16 TemplateURLService::CleanUserInputKeyword(
410 const base::string16& keyword) {
411 // Remove the scheme.
412 base::string16 result(base::i18n::ToLower(keyword));
413 base::TrimWhitespace(result, base::TRIM_ALL, &result);
414 url::Component scheme_component;
415 if (url::ExtractScheme(base::UTF16ToUTF8(keyword).c_str(),
416 static_cast<int>(keyword.length()),
417 &scheme_component)) {
418 // If the scheme isn't "http" or "https", bail. The user isn't trying to
419 // type a web address, but rather an FTP, file:, or other scheme URL, or a
420 // search query with some sort of initial operator (e.g. "site:").
421 if (result.compare(0, scheme_component.end(),
422 base::ASCIIToUTF16(url::kHttpScheme)) &&
423 result.compare(0, scheme_component.end(),
424 base::ASCIIToUTF16(url::kHttpsScheme)))
425 return base::string16();
426
427 // Include trailing ':'.
428 result.erase(0, scheme_component.end() + 1);
429 // Many schemes usually have "//" after them, so strip it too.
430 const base::string16 after_scheme(base::ASCIIToUTF16("//"));
431 if (result.compare(0, after_scheme.length(), after_scheme) == 0)
432 result.erase(0, after_scheme.length());
433 }
434
435 // Remove leading "www.".
436 result = net::StripWWW(result);
437
438 // Remove trailing "/".
439 return (result.length() > 0 && result[result.length() - 1] == '/') ?
440 result.substr(0, result.length() - 1) : result;
441 }
442
443 // static
444 GURL TemplateURLService::GenerateSearchURL(TemplateURL* t_url) {
445 DCHECK(t_url);
446 UIThreadSearchTermsData search_terms_data(t_url->profile());
447 return GenerateSearchURLUsingTermsData(t_url, search_terms_data);
448 }
449
450 // static
451 GURL TemplateURLService::GenerateSearchURLUsingTermsData(
452 const TemplateURL* t_url,
453 const SearchTermsData& search_terms_data) {
454 DCHECK(t_url);
455
456 const TemplateURLRef& search_ref = t_url->url_ref();
457 if (!search_ref.IsValidUsingTermsData(search_terms_data))
458 return GURL();
459
460 if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data))
461 return GURL(t_url->url());
462
463 // Use something obscure for the search terms argument so that in the rare
464 // case the term replaces the URL it's unlikely another keyword would have the
465 // same url.
466 // TODO(jnd): Add additional parameters to get post data when the search URL
467 // has post parameters.
468 return GURL(search_ref.ReplaceSearchTermsUsingTermsData(
469 TemplateURLRef::SearchTermsArgs(
470 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
471 search_terms_data, NULL));
472 }
473
474 void TemplateURLService::SaveDefaultSearchProviderToPrefs( 423 void TemplateURLService::SaveDefaultSearchProviderToPrefs(
475 const TemplateURL* t_url, 424 const TemplateURL* t_url, PrefService* prefs) {
476 PrefService* prefs) const { 425 if (!prefs)
477 if (!prefs || load_failed_)
478 return; 426 return;
479 427
480 bool enabled = false; 428 bool enabled = false;
481 std::string search_url; 429 std::string search_url;
482 std::string suggest_url; 430 std::string suggest_url;
483 std::string instant_url; 431 std::string instant_url;
484 std::string image_url; 432 std::string image_url;
485 std::string new_tab_url; 433 std::string new_tab_url;
486 std::string search_url_post_params; 434 std::string search_url_post_params;
487 std::string suggest_url_post_params; 435 std::string suggest_url_post_params;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); 485 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
538 prefs->SetString(prefs::kDefaultSearchProviderName, short_name); 486 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
539 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); 487 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
540 prefs->SetString(prefs::kDefaultSearchProviderID, id_string); 488 prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
541 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); 489 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
542 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); 490 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
543 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, 491 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
544 search_terms_replacement_key); 492 search_terms_replacement_key);
545 } 493 }
546 494
495 // static
496 base::string16 TemplateURLService::GenerateKeyword(const GURL& url) {
497 DCHECK(url.is_valid());
498 // Strip "www." off the front of the keyword; otherwise the keyword won't work
499 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
500 // Special case: if the host was exactly "www." (not sure this can happen but
501 // perhaps with some weird intranet and custom DNS server?), ensure we at
502 // least don't return the empty string.
503 base::string16 keyword(net::StripWWWFromHost(url));
504 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword;
505 }
506
507 // static
508 base::string16 TemplateURLService::CleanUserInputKeyword(
509 const base::string16& keyword) {
510 // Remove the scheme.
511 base::string16 result(base::i18n::ToLower(keyword));
512 base::TrimWhitespace(result, base::TRIM_ALL, &result);
513 url::Component scheme_component;
514 if (url::ExtractScheme(base::UTF16ToUTF8(keyword).c_str(),
515 static_cast<int>(keyword.length()),
516 &scheme_component)) {
517 // If the scheme isn't "http" or "https", bail. The user isn't trying to
518 // type a web address, but rather an FTP, file:, or other scheme URL, or a
519 // search query with some sort of initial operator (e.g. "site:").
520 if (result.compare(0, scheme_component.end(),
521 base::ASCIIToUTF16(url::kHttpScheme)) &&
522 result.compare(0, scheme_component.end(),
523 base::ASCIIToUTF16(url::kHttpsScheme)))
524 return base::string16();
525
526 // Include trailing ':'.
527 result.erase(0, scheme_component.end() + 1);
528 // Many schemes usually have "//" after them, so strip it too.
529 const base::string16 after_scheme(base::ASCIIToUTF16("//"));
530 if (result.compare(0, after_scheme.length(), after_scheme) == 0)
531 result.erase(0, after_scheme.length());
532 }
533
534 // Remove leading "www.".
535 result = net::StripWWW(result);
536
537 // Remove trailing "/".
538 return (result.length() > 0 && result[result.length() - 1] == '/') ?
539 result.substr(0, result.length() - 1) : result;
540 }
541
542 // static
543 GURL TemplateURLService::GenerateSearchURL(TemplateURL* t_url) {
544 DCHECK(t_url);
545 UIThreadSearchTermsData search_terms_data(t_url->profile());
546 return GenerateSearchURLUsingTermsData(t_url, search_terms_data);
547 }
548
549 // static
550 GURL TemplateURLService::GenerateSearchURLUsingTermsData(
551 const TemplateURL* t_url,
552 const SearchTermsData& search_terms_data) {
553 DCHECK(t_url);
554
555 const TemplateURLRef& search_ref = t_url->url_ref();
556 if (!search_ref.IsValidUsingTermsData(search_terms_data))
557 return GURL();
558
559 if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data))
560 return GURL(t_url->url());
561
562 // Use something obscure for the search terms argument so that in the rare
563 // case the term replaces the URL it's unlikely another keyword would have the
564 // same url.
565 // TODO(jnd): Add additional parameters to get post data when the search URL
566 // has post parameters.
567 return GURL(search_ref.ReplaceSearchTermsUsingTermsData(
568 TemplateURLRef::SearchTermsArgs(
569 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
570 search_terms_data, NULL));
571 }
572
547 bool TemplateURLService::CanReplaceKeyword( 573 bool TemplateURLService::CanReplaceKeyword(
548 const base::string16& keyword, 574 const base::string16& keyword,
549 const GURL& url, 575 const GURL& url,
550 TemplateURL** template_url_to_replace) { 576 TemplateURL** template_url_to_replace) {
551 DCHECK(!keyword.empty()); // This should only be called for non-empty 577 DCHECK(!keyword.empty()); // This should only be called for non-empty
552 // keywords. If we need to support empty kewords 578 // keywords. If we need to support empty kewords
553 // the code needs to change slightly. 579 // the code needs to change slightly.
554 TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); 580 TemplateURL* existing_url = GetTemplateURLForKeyword(keyword);
555 if (template_url_to_replace) 581 if (template_url_to_replace)
556 *template_url_to_replace = existing_url; 582 *template_url_to_replace = existing_url;
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { 805 void TemplateURLService::IncrementUsageCount(TemplateURL* url) {
780 DCHECK(url); 806 DCHECK(url);
781 // Extension-controlled search engines are not persisted. 807 // Extension-controlled search engines are not persisted.
782 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) 808 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)
783 return; 809 return;
784 if (std::find(template_urls_.begin(), template_urls_.end(), url) == 810 if (std::find(template_urls_.begin(), template_urls_.end(), url) ==
785 template_urls_.end()) 811 template_urls_.end())
786 return; 812 return;
787 ++url->data_.usage_count; 813 ++url->data_.usage_count;
788 814
789 if (service_) 815 if (service_.get())
790 service_->UpdateKeyword(url->data()); 816 service_.get()->UpdateKeyword(url->data());
791 } 817 }
792 818
793 void TemplateURLService::ResetTemplateURL(TemplateURL* url, 819 void TemplateURLService::ResetTemplateURL(TemplateURL* url,
794 const base::string16& title, 820 const base::string16& title,
795 const base::string16& keyword, 821 const base::string16& keyword,
796 const std::string& search_url) { 822 const std::string& search_url) {
797 if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) 823 if (!loaded_)
824 return;
825 DCHECK(!keyword.empty());
826 DCHECK(!search_url.empty());
827 TemplateURLData data(url->data());
828 data.short_name = title;
829 data.SetKeyword(keyword);
830 if (search_url != data.url()) {
831 data.SetURL(search_url);
832 // The urls have changed, reset the favicon url.
833 data.favicon_url = GURL();
834 }
835 data.safe_for_autoreplace = false;
836 data.last_modified = time_provider_();
837 TemplateURL new_url(url->profile(), data);
838 UIThreadSearchTermsData search_terms_data(url->profile());
839 if (UpdateNoNotify(url, new_url, search_terms_data))
798 NotifyObservers(); 840 NotifyObservers();
799 } 841 }
800 842
801 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { 843 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) {
802 return !is_default_search_managed() && 844 return !is_default_search_managed() &&
803 !IsExtensionControlledDefaultSearch() && 845 !IsExtensionControlledDefaultSearch() &&
804 (url != GetDefaultSearchProvider()) && 846 (url != GetDefaultSearchProvider()) &&
805 url->url_ref().SupportsReplacement() && 847 url->url_ref().SupportsReplacement() &&
806 (url->GetType() == TemplateURL::NORMAL); 848 (url->GetType() == TemplateURL::NORMAL);
807 } 849 }
808 850
809 void TemplateURLService::SetUserSelectedDefaultSearchProvider( 851 void TemplateURLService::SetUserSelectedDefaultSearchProvider(
810 TemplateURL* url) { 852 TemplateURL* url) {
811 SetDefaultSearchProvider(url); 853 SetDefaultSearchProvider(url);
812 if (default_search_manager_) { 854 if (default_search_manager_) {
813 if (url) 855 if (url)
814 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); 856 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data());
815 else 857 else
816 default_search_manager_->ClearUserSelectedDefaultSearchEngine(); 858 default_search_manager_->ClearUserSelectedDefaultSearchEngine();
817 } 859 }
818 } 860 }
819 861
820 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { 862 TemplateURL* TemplateURLService::GetDefaultSearchProvider() {
821 return (loaded_ && !load_failed_) ? 863 if (loaded_ && !load_failed_)
822 default_search_provider_ : initial_default_search_provider_.get(); 864 return default_search_provider_;
865 // We're not loaded, rely on the default search provider stored in prefs.
866 return initial_default_search_provider_.get();
823 } 867 }
824 868
825 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( 869 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider(
826 const GURL& url) { 870 const GURL& url) {
827 TemplateURL* default_provider = GetDefaultSearchProvider(); 871 TemplateURL* default_provider = GetDefaultSearchProvider();
828 return default_provider && default_provider->IsSearchURL(url); 872 return default_provider && default_provider->IsSearchURL(url);
829 } 873 }
830 874
831 bool TemplateURLService::IsExtensionControlledDefaultSearch() { 875 bool TemplateURLService::IsExtensionControlledDefaultSearch() {
832 const TemplateURL* default_provider = GetDefaultSearchProvider(); 876 const TemplateURL* default_provider = GetDefaultSearchProvider();
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 } 967 }
924 968
925 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { 969 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) {
926 model_observers_.RemoveObserver(observer); 970 model_observers_.RemoveObserver(observer);
927 } 971 }
928 972
929 void TemplateURLService::Load() { 973 void TemplateURLService::Load() {
930 if (loaded_ || load_handle_) 974 if (loaded_ || load_handle_)
931 return; 975 return;
932 976
933 if (!service_) 977 if (!service_.get()) {
934 service_ = WebDataService::FromBrowserContext(profile_); 978 service_ = WebDataService::FromBrowserContext(profile_);
979 }
935 980
936 if (service_) { 981 if (service_.get()) {
937 load_handle_ = service_->GetKeywords(this); 982 load_handle_ = service_->GetKeywords(this);
938 } else { 983 } else {
939 ChangeToLoadedState(); 984 ChangeToLoadedState();
940 on_loaded_callbacks_.Notify(); 985 on_loaded_callbacks_.Notify();
941 } 986 }
942 } 987 }
943 988
944 scoped_ptr<TemplateURLService::Subscription> 989 scoped_ptr<TemplateURLService::Subscription>
945 TemplateURLService::RegisterOnLoadedCallback( 990 TemplateURLService::RegisterOnLoadedCallback(
946 const base::Closure& callback) { 991 const base::Closure& callback) {
947 return loaded_ ? 992 return loaded_ ?
948 scoped_ptr<TemplateURLService::Subscription>() : 993 scoped_ptr<TemplateURLService::Subscription>() :
949 on_loaded_callbacks_.Add(callback); 994 on_loaded_callbacks_.Add(callback);
950 } 995 }
951 996
952 void TemplateURLService::OnWebDataServiceRequestDone( 997 void TemplateURLService::OnWebDataServiceRequestDone(
953 WebDataService::Handle h, 998 WebDataService::Handle h,
954 const WDTypedResult* result) { 999 const WDTypedResult* result) {
955 // Reset the load_handle so that we don't try and cancel the load in 1000 // Reset the load_handle so that we don't try and cancel the load in
956 // the destructor. 1001 // the destructor.
957 load_handle_ = 0; 1002 load_handle_ = 0;
958 1003
959 if (!result) { 1004 if (!result) {
960 // Results are null if the database went away or (most likely) wasn't 1005 // Results are null if the database went away or (most likely) wasn't
961 // loaded. 1006 // loaded.
962 load_failed_ = true; 1007 load_failed_ = true;
963 service_ = NULL;
964 ChangeToLoadedState(); 1008 ChangeToLoadedState();
965 on_loaded_callbacks_.Notify(); 1009 on_loaded_callbacks_.Notify();
966 return; 1010 return;
967 } 1011 }
968 1012
969 // initial_default_search_provider_ is only needed before we've finished 1013 // initial_default_search_provider_ is only needed before we've finished
970 // loading. Now that we've loaded we can nuke it. 1014 // loading. Now that we've loaded we can nuke it.
971 initial_default_search_provider_.reset(); 1015 initial_default_search_provider_.reset();
972 1016
973 TemplateURLVector template_urls; 1017 TemplateURLVector template_urls;
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
1231 syncer::ModelType type, 1275 syncer::ModelType type,
1232 const syncer::SyncDataList& initial_sync_data, 1276 const syncer::SyncDataList& initial_sync_data,
1233 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 1277 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1234 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { 1278 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
1235 DCHECK(loaded_); 1279 DCHECK(loaded_);
1236 DCHECK_EQ(type, syncer::SEARCH_ENGINES); 1280 DCHECK_EQ(type, syncer::SEARCH_ENGINES);
1237 DCHECK(!sync_processor_.get()); 1281 DCHECK(!sync_processor_.get());
1238 DCHECK(sync_processor.get()); 1282 DCHECK(sync_processor.get());
1239 DCHECK(sync_error_factory.get()); 1283 DCHECK(sync_error_factory.get());
1240 syncer::SyncMergeResult merge_result(type); 1284 syncer::SyncMergeResult merge_result(type);
1241
1242 // Disable sync if we failed to load.
1243 if (load_failed_) {
1244 merge_result.set_error(syncer::SyncError(
1245 FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
1246 "Local database load failed.", syncer::SEARCH_ENGINES));
1247 return merge_result;
1248 }
1249
1250 sync_processor_ = sync_processor.Pass(); 1285 sync_processor_ = sync_processor.Pass();
1251 sync_error_factory_ = sync_error_factory.Pass(); 1286 sync_error_factory_ = sync_error_factory.Pass();
1252 1287
1253 // We just started syncing, so set our wait-for-default flag if we are 1288 // We just started syncing, so set our wait-for-default flag if we are
1254 // expecting a default from Sync. 1289 // expecting a default from Sync.
1255 if (GetPrefs()) { 1290 if (GetPrefs()) {
1256 std::string default_guid = GetPrefs()->GetString( 1291 std::string default_guid = GetPrefs()->GetString(
1257 prefs::kSyncedDefaultSearchProviderGUID); 1292 prefs::kSyncedDefaultSearchProviderGUID);
1258 const TemplateURL* current_default = GetDefaultSearchProvider(); 1293 const TemplateURL* current_default = GetDefaultSearchProvider();
1259 1294
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
1629 if (i == 0) 1664 if (i == 0)
1630 SetDefaultSearchProviderNoNotify(template_url); 1665 SetDefaultSearchProviderNoNotify(template_url);
1631 } 1666 }
1632 } 1667 }
1633 1668
1634 // Initialize default search. 1669 // Initialize default search.
1635 UpdateDefaultSearch(); 1670 UpdateDefaultSearch();
1636 1671
1637 // Request a server check for the correct Google URL if Google is the 1672 // Request a server check for the correct Google URL if Google is the
1638 // default search engine and not in headless mode. 1673 // default search engine and not in headless mode.
1639 if (profile_ && initial_default_search_provider_ && 1674 if (profile_ && initial_default_search_provider_.get() &&
1640 initial_default_search_provider_->HasGoogleBaseURLs()) { 1675 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) {
1641 scoped_ptr<base::Environment> env(base::Environment::Create()); 1676 scoped_ptr<base::Environment> env(base::Environment::Create());
1642 if (!env->HasVar(env_vars::kHeadless)) 1677 if (!env->HasVar(env_vars::kHeadless))
1643 GoogleURLTracker::RequestServerCheck(profile_, false); 1678 GoogleURLTracker::RequestServerCheck(profile_, false);
1644 } 1679 }
1645 } 1680 }
1646 1681
1647 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { 1682 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) {
1648 const base::string16& keyword = template_url->keyword(); 1683 const base::string16& keyword = template_url->keyword();
1649 DCHECK_NE(0U, keyword_to_template_map_.count(keyword)); 1684 DCHECK_NE(0U, keyword_to_template_map_.count(keyword));
1650 if (keyword_to_template_map_[keyword] == template_url) { 1685 if (keyword_to_template_map_[keyword] == template_url) {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1845 RemoveNoNotify(existing_keyword_turl); 1880 RemoveNoNotify(existing_keyword_turl);
1846 } else { 1881 } else {
1847 existing_turl->data_.SetKeyword(old_keyword); 1882 existing_turl->data_.SetKeyword(old_keyword);
1848 keyword_to_template_map_[old_keyword] = existing_turl; 1883 keyword_to_template_map_[old_keyword] = existing_turl;
1849 } 1884 }
1850 } 1885 }
1851 } 1886 }
1852 if (!existing_turl->sync_guid().empty()) 1887 if (!existing_turl->sync_guid().empty())
1853 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; 1888 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
1854 1889
1855 if (service_) 1890 if (service_.get())
1856 service_->UpdateKeyword(existing_turl->data()); 1891 service_->UpdateKeyword(existing_turl->data());
1857 1892
1858 // Inform sync of the update. 1893 // Inform sync of the update.
1859 ProcessTemplateURLChange(FROM_HERE, 1894 ProcessTemplateURLChange(FROM_HERE,
1860 existing_turl, 1895 existing_turl,
1861 syncer::SyncChange::ACTION_UPDATE); 1896 syncer::SyncChange::ACTION_UPDATE);
1862 1897
1863 if (default_search_provider_ == existing_turl) { 1898 if (default_search_provider_ == existing_turl) {
1864 bool success = SetDefaultSearchProviderNoNotify(existing_turl); 1899 bool success = SetDefaultSearchProviderNoNotify(existing_turl);
1865 DCHECK(success); 1900 DCHECK(success);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 content::PAGE_TRANSITION_KEYWORD_GENERATED, 1983 content::PAGE_TRANSITION_KEYWORD_GENERATED,
1949 history::SOURCE_BROWSED, false); 1984 history::SOURCE_BROWSED, false);
1950 } 1985 }
1951 1986
1952 void TemplateURLService::GoogleBaseURLChanged(const GURL& old_base_url) { 1987 void TemplateURLService::GoogleBaseURLChanged(const GURL& old_base_url) {
1953 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); 1988 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get());
1954 bool something_changed = false; 1989 bool something_changed = false;
1955 for (TemplateURLVector::iterator i(template_urls_.begin()); 1990 for (TemplateURLVector::iterator i(template_urls_.begin());
1956 i != template_urls_.end(); ++i) { 1991 i != template_urls_.end(); ++i) {
1957 TemplateURL* t_url = *i; 1992 TemplateURL* t_url = *i;
1958 if (t_url->HasGoogleBaseURLs()) { 1993 if (t_url->url_ref().HasGoogleBaseURLs() ||
1994 t_url->suggestions_url_ref().HasGoogleBaseURLs()) {
1959 TemplateURL updated_turl(t_url->profile(), t_url->data()); 1995 TemplateURL updated_turl(t_url->profile(), t_url->data());
1960 updated_turl.ResetKeywordIfNecessary(false); 1996 updated_turl.ResetKeywordIfNecessary(false);
1961 KeywordToTemplateMap::const_iterator existing_entry = 1997 KeywordToTemplateMap::const_iterator existing_entry =
1962 keyword_to_template_map_.find(updated_turl.keyword()); 1998 keyword_to_template_map_.find(updated_turl.keyword());
1963 if ((existing_entry != keyword_to_template_map_.end()) && 1999 if ((existing_entry != keyword_to_template_map_.end()) &&
1964 (existing_entry->second != t_url)) { 2000 (existing_entry->second != t_url)) {
1965 // The new autogenerated keyword conflicts with another TemplateURL. 2001 // The new autogenerated keyword conflicts with another TemplateURL.
1966 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its 2002 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its
1967 // current keyword. (This will not prevent |t_url| from auto-updating 2003 // current keyword. (This will not prevent |t_url| from auto-updating
1968 // the keyword in the future if the conflicting TemplateURL disappears.) 2004 // the keyword in the future if the conflicting TemplateURL disappears.)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2017 if (!is_default_search_managed_ && !new_is_default_managed) { 2053 if (!is_default_search_managed_ && !new_is_default_managed) {
2018 // We're not interested in cases where the default was and remains 2054 // We're not interested in cases where the default was and remains
2019 // unmanaged. In that case, preferences have no impact on the default. 2055 // unmanaged. In that case, preferences have no impact on the default.
2020 return; 2056 return;
2021 } 2057 }
2022 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); 2058 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get());
2023 if (is_default_search_managed_ && new_is_default_managed) { 2059 if (is_default_search_managed_ && new_is_default_managed) {
2024 // The default was managed and remains managed. Update the default only 2060 // The default was managed and remains managed. Update the default only
2025 // if it has changed; we don't want to respond to changes triggered by 2061 // if it has changed; we don't want to respond to changes triggered by
2026 // SaveDefaultSearchProviderToPrefs. 2062 // SaveDefaultSearchProviderToPrefs.
2027 if (TemplateURL::MatchesData(default_search_provider_, 2063 if (TemplateURLMatchesData(default_search_provider_,
2028 new_default_from_prefs.get())) 2064 new_default_from_prefs.get()))
2029 return; 2065 return;
2030 if (!new_default_from_prefs) { 2066 if (!new_default_from_prefs) {
2031 // |default_search_provider_| can't be NULL or MatchesData() would have 2067 // default_search_provider_ can't be NULL otherwise
2032 // returned true. Remove this now invalid value. 2068 // TemplateURLMatchesData would have returned true. Remove this now
2069 // invalid value.
2033 TemplateURL* old_default = default_search_provider_; 2070 TemplateURL* old_default = default_search_provider_;
2034 bool success = SetDefaultSearchProviderNoNotify(NULL); 2071 bool success = SetDefaultSearchProviderNoNotify(NULL);
2035 DCHECK(success); 2072 DCHECK(success);
2036 RemoveNoNotify(old_default); 2073 RemoveNoNotify(old_default);
2037 } else if (default_search_provider_) { 2074 } else if (default_search_provider_) {
2038 new_default_from_prefs->created_by_policy = true; 2075 new_default_from_prefs->created_by_policy = true;
2039 TemplateURL new_values(profile_, *new_default_from_prefs); 2076 TemplateURL new_values(profile_, *new_default_from_prefs);
2040 UIThreadSearchTermsData search_terms_data( 2077 UIThreadSearchTermsData search_terms_data(
2041 default_search_provider_->profile()); 2078 default_search_provider_->profile());
2042 UpdateNoNotify(default_search_provider_, new_values, search_terms_data); 2079 UpdateNoNotify(default_search_provider_, new_values, search_terms_data);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
2132 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin", 2169 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin",
2133 dsp_change_origin_, DSP_CHANGE_MAX); 2170 dsp_change_origin_, DSP_CHANGE_MAX);
2134 default_search_provider_ = url; 2171 default_search_provider_ = url;
2135 } 2172 }
2136 } 2173 }
2137 2174
2138 if (url) { 2175 if (url) {
2139 // Don't mark the url as edited, otherwise we won't be able to rev the 2176 // Don't mark the url as edited, otherwise we won't be able to rev the
2140 // template urls we ship with. 2177 // template urls we ship with.
2141 url->data_.show_in_default_list = true; 2178 url->data_.show_in_default_list = true;
2142 if (service_ && (url->GetType() == TemplateURL::NORMAL)) 2179 if (service_.get() && (url->GetType() == TemplateURL::NORMAL))
2143 service_->UpdateKeyword(url->data()); 2180 service_->UpdateKeyword(url->data());
2144 2181
2145 if (url->HasGoogleBaseURLs()) { 2182 if (url->url_ref().HasGoogleBaseURLs()) {
2146 GoogleURLTracker::RequestServerCheck(profile_, false); 2183 GoogleURLTracker::RequestServerCheck(profile_, false);
2147 #if defined(ENABLE_RLZ) 2184 #if defined(ENABLE_RLZ)
2148 RLZTracker::RecordProductEvent(rlz_lib::CHROME, 2185 RLZTracker::RecordProductEvent(rlz_lib::CHROME,
2149 RLZTracker::CHROME_OMNIBOX, 2186 RLZTracker::CHROME_OMNIBOX,
2150 rlz_lib::SET_TO_GOOGLE); 2187 rlz_lib::SET_TO_GOOGLE);
2151 #endif 2188 #endif
2152 } 2189 }
2153 } 2190 }
2154 2191
2155 // Extension-controlled search engines shouldn't be persisted anywhere. 2192 // Extension-controlled search engines shouldn't be persisted anywhere.
(...skipping 11 matching lines...) Expand all
2167 // needs to set a new default as well. If we update the default here, we're 2204 // needs to set a new default as well. If we update the default here, we're
2168 // likely to race with the update from the other client, resulting in 2205 // likely to race with the update from the other client, resulting in
2169 // a possibly random default search provider. 2206 // a possibly random default search provider.
2170 if (sync_processor_.get() && url && !url->sync_guid().empty() && 2207 if (sync_processor_.get() && url && !url->sync_guid().empty() &&
2171 GetPrefs() && !processing_syncer_changes_) { 2208 GetPrefs() && !processing_syncer_changes_) {
2172 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, 2209 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
2173 url->sync_guid()); 2210 url->sync_guid());
2174 } 2211 }
2175 } 2212 }
2176 2213
2177 if (service_) 2214 if (service_.get())
2178 service_->SetDefaultSearchProviderID(url ? url->id() : 0); 2215 service_->SetDefaultSearchProviderID(url ? url->id() : 0);
2179 2216
2180 // Inform sync the change to the show_in_default_list flag. 2217 // Inform sync the change to the show_in_default_list flag.
2181 if (url) 2218 if (url)
2182 ProcessTemplateURLChange(FROM_HERE, 2219 ProcessTemplateURLChange(FROM_HERE,
2183 url, 2220 url,
2184 syncer::SyncChange::ACTION_UPDATE); 2221 syncer::SyncChange::ACTION_UPDATE);
2185 return true; 2222 return true;
2186 } 2223 }
2187 2224
(...skipping 20 matching lines...) Expand all
2208 bool are_same_type = existing_keyword_turl->GetType() == 2245 bool are_same_type = existing_keyword_turl->GetType() ==
2209 template_url->GetType(); 2246 template_url->GetType();
2210 if (CanReplace(existing_keyword_turl) && are_same_type) { 2247 if (CanReplace(existing_keyword_turl) && are_same_type) {
2211 RemoveNoNotify(existing_keyword_turl); 2248 RemoveNoNotify(existing_keyword_turl);
2212 } else if (CanReplace(template_url) && are_same_type) { 2249 } else if (CanReplace(template_url) && are_same_type) {
2213 delete template_url; 2250 delete template_url;
2214 return false; 2251 return false;
2215 } else { 2252 } else {
2216 base::string16 new_keyword = 2253 base::string16 new_keyword =
2217 UniquifyKeyword(*existing_keyword_turl, false); 2254 UniquifyKeyword(*existing_keyword_turl, false);
2218 ResetTemplateURLNoNotify(existing_keyword_turl, 2255 ResetTemplateURL(existing_keyword_turl,
2219 existing_keyword_turl->short_name(), new_keyword, 2256 existing_keyword_turl->short_name(), new_keyword,
2220 existing_keyword_turl->url()); 2257 existing_keyword_turl->url());
2221 } 2258 }
2222 } 2259 }
2223 template_urls_.push_back(template_url); 2260 template_urls_.push_back(template_url);
2224 AddToMaps(template_url); 2261 AddToMaps(template_url);
2225 2262
2226 if (newly_adding && 2263 if (newly_adding &&
2227 (template_url->GetType() != 2264 (template_url->GetType() !=
2228 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { 2265 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) {
2229 if (service_) 2266 if (service_.get())
2230 service_->AddKeyword(template_url->data()); 2267 service_->AddKeyword(template_url->data());
2231 2268
2232 // Inform sync of the addition. Note that this will assign a GUID to 2269 // Inform sync of the addition. Note that this will assign a GUID to
2233 // template_url and add it to the guid_to_template_map_. 2270 // template_url and add it to the guid_to_template_map_.
2234 ProcessTemplateURLChange(FROM_HERE, 2271 ProcessTemplateURLChange(FROM_HERE,
2235 template_url, 2272 template_url,
2236 syncer::SyncChange::ACTION_ADD); 2273 syncer::SyncChange::ACTION_ADD);
2237 } 2274 }
2238 2275
2239 return true; 2276 return true;
2240 } 2277 }
2241 2278
2242 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2279 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2243 DCHECK(template_url != default_search_provider_); 2280 DCHECK(template_url != default_search_provider_);
2244 2281
2245 TemplateURLVector::iterator i = 2282 TemplateURLVector::iterator i =
2246 std::find(template_urls_.begin(), template_urls_.end(), template_url); 2283 std::find(template_urls_.begin(), template_urls_.end(), template_url);
2247 if (i == template_urls_.end()) 2284 if (i == template_urls_.end())
2248 return; 2285 return;
2249 2286
2250 RemoveFromMaps(template_url); 2287 RemoveFromMaps(template_url);
2251 2288
2252 // Remove it from the vector containing all TemplateURLs. 2289 // Remove it from the vector containing all TemplateURLs.
2253 template_urls_.erase(i); 2290 template_urls_.erase(i);
2254 2291
2255 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { 2292 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) {
2256 if (service_) 2293 if (service_.get())
2257 service_->RemoveKeyword(template_url->id()); 2294 service_->RemoveKeyword(template_url->id());
2258 2295
2259 // Inform sync of the deletion. 2296 // Inform sync of the deletion.
2260 ProcessTemplateURLChange(FROM_HERE, 2297 ProcessTemplateURLChange(FROM_HERE,
2261 template_url, 2298 template_url,
2262 syncer::SyncChange::ACTION_DELETE); 2299 syncer::SyncChange::ACTION_DELETE);
2263 2300
2264 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2301 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2265 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2302 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2266 } 2303 }
2267 2304
2268 if (profile_) { 2305 if (profile_) {
2269 content::Source<Profile> source(profile_); 2306 content::Source<Profile> source(profile_);
2270 TemplateURLID id = template_url->id(); 2307 TemplateURLID id = template_url->id();
2271 content::NotificationService::current()->Notify( 2308 content::NotificationService::current()->Notify(
2272 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, 2309 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
2273 source, 2310 source,
2274 content::Details<TemplateURLID>(&id)); 2311 content::Details<TemplateURLID>(&id));
2275 } 2312 }
2276 2313
2277 // We own the TemplateURL and need to delete it. 2314 // We own the TemplateURL and need to delete it.
2278 delete template_url; 2315 delete template_url;
2279 } 2316 }
2280 2317
2281 bool TemplateURLService::ResetTemplateURLNoNotify(
2282 TemplateURL* url,
2283 const base::string16& title,
2284 const base::string16& keyword,
2285 const std::string& search_url) {
2286 if (!loaded_)
2287 return false;
2288 DCHECK(!keyword.empty());
2289 DCHECK(!search_url.empty());
2290 TemplateURLData data(url->data());
2291 data.short_name = title;
2292 data.SetKeyword(keyword);
2293 if (search_url != data.url()) {
2294 data.SetURL(search_url);
2295 // The urls have changed, reset the favicon url.
2296 data.favicon_url = GURL();
2297 }
2298 data.safe_for_autoreplace = false;
2299 data.last_modified = time_provider_();
2300 TemplateURL new_url(url->profile(), data);
2301 UIThreadSearchTermsData search_terms_data(url->profile());
2302 return UpdateNoNotify(url, new_url, search_terms_data);
2303 }
2304
2305 void TemplateURLService::NotifyObservers() { 2318 void TemplateURLService::NotifyObservers() {
2306 if (!loaded_) 2319 if (!loaded_)
2307 return; 2320 return;
2308 2321
2309 FOR_EACH_OBSERVER(TemplateURLServiceObserver, model_observers_, 2322 FOR_EACH_OBSERVER(TemplateURLServiceObserver, model_observers_,
2310 OnTemplateURLServiceChanged()); 2323 OnTemplateURLServiceChanged());
2311 } 2324 }
2312 2325
2313 // |template_urls| are the TemplateURLs loaded from the database. 2326 // |template_urls| are the TemplateURLs loaded from the database.
2314 // |default_search_provider| points to one of them, if it was set in the db. 2327 // |default_search_provider| points to one of them, if it was set in the db.
2315 // |default_from_prefs| is the default search provider from the preferences. 2328 // |default_from_prefs| is the default search provider from the preferences.
2316 // Check |is_default_search_managed_| to determine if it was set by policy. 2329 // Check |is_default_search_managed_| to determine if it was set by policy.
2317 // 2330 //
2318 // This function removes from the vector and the database all the TemplateURLs 2331 // This function removes from the vector and the database all the TemplateURLs
2319 // that were set by policy, unless it is the current default search provider 2332 // that were set by policy, unless it is the current default search provider
2320 // and matches what is set by a managed preference. 2333 // and matches what is set by a managed preference.
2321 void TemplateURLService::RemoveProvidersCreatedByPolicy( 2334 void TemplateURLService::RemoveProvidersCreatedByPolicy(
2322 TemplateURLVector* template_urls, 2335 TemplateURLVector* template_urls,
2323 TemplateURL** default_search_provider, 2336 TemplateURL** default_search_provider,
2324 TemplateURLData* default_from_prefs) { 2337 TemplateURLData* default_from_prefs) {
2325 DCHECK(template_urls); 2338 DCHECK(template_urls);
2326 DCHECK(default_search_provider); 2339 DCHECK(default_search_provider);
2327 for (TemplateURLVector::iterator i = template_urls->begin(); 2340 for (TemplateURLVector::iterator i = template_urls->begin();
2328 i != template_urls->end(); ) { 2341 i != template_urls->end(); ) {
2329 TemplateURL* template_url = *i; 2342 TemplateURL* template_url = *i;
2330 if (template_url->created_by_policy()) { 2343 if (template_url->created_by_policy()) {
2331 if (template_url == *default_search_provider && 2344 if (template_url == *default_search_provider &&
2332 is_default_search_managed_ && 2345 is_default_search_managed_ &&
2333 TemplateURL::MatchesData(template_url, default_from_prefs)) { 2346 TemplateURLMatchesData(template_url, default_from_prefs)) {
2334 // If the database specified a default search provider that was set 2347 // If the database specified a default search provider that was set
2335 // by policy, and the default search provider from the preferences 2348 // by policy, and the default search provider from the preferences
2336 // is also set by policy and they are the same, keep the entry in the 2349 // is also set by policy and they are the same, keep the entry in the
2337 // database and the |default_search_provider|. 2350 // database and the |default_search_provider|.
2338 ++i; 2351 ++i;
2339 continue; 2352 continue;
2340 } 2353 }
2341 2354
2342 // The database loaded a managed |default_search_provider|, but it has 2355 // The database loaded a managed |default_search_provider|, but it has
2343 // been updated in the prefs. Remove it from the database, and update the 2356 // been updated in the prefs. Remove it from the database, and update the
2344 // |default_search_provider| pointer here. 2357 // |default_search_provider| pointer here.
2345 if (*default_search_provider && 2358 if (*default_search_provider &&
2346 (*default_search_provider)->id() == template_url->id()) 2359 (*default_search_provider)->id() == template_url->id())
2347 *default_search_provider = NULL; 2360 *default_search_provider = NULL;
2348 2361
2349 i = template_urls->erase(i); 2362 i = template_urls->erase(i);
2350 if (service_) 2363 if (service_.get())
2351 service_->RemoveKeyword(template_url->id()); 2364 service_->RemoveKeyword(template_url->id());
2352 delete template_url; 2365 delete template_url;
2353 } else { 2366 } else {
2354 ++i; 2367 ++i;
2355 } 2368 }
2356 } 2369 }
2357 } 2370 }
2358 2371
2359 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, 2372 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url,
2360 const std::string& guid) { 2373 const std::string& guid) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
2564 TemplateURLVector* template_urls) { 2577 TemplateURLVector* template_urls) {
2565 DCHECK(template_urls); 2578 DCHECK(template_urls);
2566 for (TemplateURLVector::iterator i = template_urls->begin(); 2579 for (TemplateURLVector::iterator i = template_urls->begin();
2567 i != template_urls->end(); ++i) { 2580 i != template_urls->end(); ++i) {
2568 TemplateURL* template_url = *i; 2581 TemplateURL* template_url = *i;
2569 DCHECK(template_url); 2582 DCHECK(template_url);
2570 if (template_url->sync_guid().empty() && 2583 if (template_url->sync_guid().empty() &&
2571 (template_url->GetType() != 2584 (template_url->GetType() !=
2572 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { 2585 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) {
2573 template_url->data_.sync_guid = base::GenerateGUID(); 2586 template_url->data_.sync_guid = base::GenerateGUID();
2574 if (service_) 2587 if (service_.get())
2575 service_->UpdateKeyword(template_url->data()); 2588 service_->UpdateKeyword(template_url->data());
2576 } 2589 }
2577 } 2590 }
2578 } 2591 }
2579 2592
2580 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( 2593 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine(
2581 TemplateURLVector* template_urls, 2594 TemplateURLVector* template_urls,
2582 TemplateURL* default_search_provider) { 2595 TemplateURL* default_search_provider) {
2583 DCHECK(template_urls); 2596 DCHECK(template_urls);
2584 is_default_search_managed_ = false; 2597 is_default_search_managed_ = false;
2585 bool database_specified_a_default = (default_search_provider != NULL); 2598 bool database_specified_a_default = (default_search_provider != NULL);
2586 2599
2587 // Check if default search provider is now managed. 2600 // Check if default search provider is now managed.
2588 scoped_ptr<TemplateURLData> default_from_prefs; 2601 scoped_ptr<TemplateURLData> default_from_prefs;
2589 LoadDefaultSearchProviderFromPrefs( 2602 LoadDefaultSearchProviderFromPrefs(
2590 GetPrefs(), &default_from_prefs, &is_default_search_managed_); 2603 GetPrefs(), &default_from_prefs, &is_default_search_managed_);
2591 2604
2592 // Remove entries that were created because of policy as they may have 2605 // Remove entries that were created because of policy as they may have
2593 // changed since the database was saved. 2606 // changed since the database was saved.
2594 RemoveProvidersCreatedByPolicy(template_urls, 2607 RemoveProvidersCreatedByPolicy(template_urls,
2595 &default_search_provider, 2608 &default_search_provider,
2596 default_from_prefs.get()); 2609 default_from_prefs.get());
2597 2610
2598 PatchMissingSyncGUIDs(template_urls); 2611 PatchMissingSyncGUIDs(template_urls);
2599 2612
2600 if (is_default_search_managed_) { 2613 if (is_default_search_managed_) {
2601 SetTemplateURLs(template_urls); 2614 SetTemplateURLs(template_urls);
2602 2615
2603 if (TemplateURL::MatchesData(default_search_provider, 2616 if (TemplateURLMatchesData(default_search_provider,
2604 default_from_prefs.get())) { 2617 default_from_prefs.get())) {
2605 // The value from the preferences was previously stored in the database. 2618 // The value from the preferences was previously stored in the database.
2606 // Reuse it. 2619 // Reuse it.
2607 } else { 2620 } else {
2608 // The value from the preferences takes over. 2621 // The value from the preferences takes over.
2609 default_search_provider = NULL; 2622 default_search_provider = NULL;
2610 if (default_from_prefs) { 2623 if (default_from_prefs) {
2611 default_from_prefs->created_by_policy = true; 2624 default_from_prefs->created_by_policy = true;
2612 default_from_prefs->id = kInvalidTemplateURLID; 2625 default_from_prefs->id = kInvalidTemplateURLID;
2613 default_search_provider = 2626 default_search_provider =
2614 new TemplateURL(profile_, *default_from_prefs); 2627 new TemplateURL(profile_, *default_from_prefs);
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 new_dse = *i; 2756 new_dse = *i;
2744 break; 2757 break;
2745 } 2758 }
2746 } 2759 }
2747 } 2760 }
2748 } 2761 }
2749 if (!new_dse) 2762 if (!new_dse)
2750 new_dse = FindNewDefaultSearchProvider(); 2763 new_dse = FindNewDefaultSearchProvider();
2751 SetDefaultSearchProviderNoNotify(new_dse); 2764 SetDefaultSearchProviderNoNotify(new_dse);
2752 } 2765 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_service.h ('k') | chrome/browser/search_engines/template_url_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698