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