Chromium Code Reviews| 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 } | 411 } |
| 412 if (!prepopulate_id.empty() && !*is_managed) { | 412 if (!prepopulate_id.empty() && !*is_managed) { |
| 413 int value; | 413 int value; |
| 414 base::StringToInt(prepopulate_id, &value); | 414 base::StringToInt(prepopulate_id, &value); |
| 415 (*default_provider_data)->prepopulate_id = value; | 415 (*default_provider_data)->prepopulate_id = value; |
| 416 } | 416 } |
| 417 return true; | 417 return true; |
| 418 } | 418 } |
| 419 | 419 |
| 420 // static | 420 // static |
| 421 void TemplateURLService::SaveDefaultSearchProviderToPrefs( | |
| 422 const TemplateURL* t_url, PrefService* prefs) { | |
| 423 if (!prefs) | |
| 424 return; | |
| 425 | |
| 426 bool enabled = false; | |
| 427 std::string search_url; | |
| 428 std::string suggest_url; | |
| 429 std::string instant_url; | |
| 430 std::string image_url; | |
| 431 std::string new_tab_url; | |
| 432 std::string search_url_post_params; | |
| 433 std::string suggest_url_post_params; | |
| 434 std::string instant_url_post_params; | |
| 435 std::string image_url_post_params; | |
| 436 std::string icon_url; | |
| 437 std::string encodings; | |
| 438 std::string short_name; | |
| 439 std::string keyword; | |
| 440 std::string id_string; | |
| 441 std::string prepopulate_id; | |
| 442 base::ListValue alternate_urls; | |
| 443 std::string search_terms_replacement_key; | |
| 444 if (t_url) { | |
| 445 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType()); | |
| 446 enabled = true; | |
| 447 search_url = t_url->url(); | |
| 448 suggest_url = t_url->suggestions_url(); | |
| 449 instant_url = t_url->instant_url(); | |
| 450 image_url = t_url->image_url(); | |
| 451 new_tab_url = t_url->new_tab_url(); | |
| 452 search_url_post_params = t_url->search_url_post_params(); | |
| 453 suggest_url_post_params = t_url->suggestions_url_post_params(); | |
| 454 instant_url_post_params = t_url->instant_url_post_params(); | |
| 455 image_url_post_params = t_url->image_url_post_params(); | |
| 456 GURL icon_gurl = t_url->favicon_url(); | |
| 457 if (!icon_gurl.is_empty()) | |
| 458 icon_url = icon_gurl.spec(); | |
| 459 encodings = JoinString(t_url->input_encodings(), ';'); | |
| 460 short_name = base::UTF16ToUTF8(t_url->short_name()); | |
| 461 keyword = base::UTF16ToUTF8(t_url->keyword()); | |
| 462 id_string = base::Int64ToString(t_url->id()); | |
| 463 prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); | |
| 464 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i) | |
| 465 alternate_urls.AppendString(t_url->alternate_urls()[i]); | |
| 466 search_terms_replacement_key = t_url->search_terms_replacement_key(); | |
| 467 } | |
| 468 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); | |
| 469 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); | |
| 470 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); | |
| 471 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); | |
| 472 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url); | |
| 473 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url); | |
| 474 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams, | |
| 475 search_url_post_params); | |
| 476 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams, | |
| 477 suggest_url_post_params); | |
| 478 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams, | |
| 479 instant_url_post_params); | |
| 480 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams, | |
| 481 image_url_post_params); | |
| 482 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); | |
| 483 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); | |
| 484 prefs->SetString(prefs::kDefaultSearchProviderName, short_name); | |
| 485 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); | |
| 486 prefs->SetString(prefs::kDefaultSearchProviderID, id_string); | |
| 487 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); | |
| 488 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); | |
| 489 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, | |
| 490 search_terms_replacement_key); | |
| 491 } | |
| 492 | |
| 493 // static | |
| 494 base::string16 TemplateURLService::GenerateKeyword(const GURL& url) { | 421 base::string16 TemplateURLService::GenerateKeyword(const GURL& url) { |
|
Peter Kasting
2014/05/05 18:46:24
This apparent diff is just fallout from moving Sav
| |
| 495 DCHECK(url.is_valid()); | 422 DCHECK(url.is_valid()); |
| 496 // Strip "www." off the front of the keyword; otherwise the keyword won't work | 423 // Strip "www." off the front of the keyword; otherwise the keyword won't work |
| 497 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . | 424 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . |
| 498 // Special case: if the host was exactly "www." (not sure this can happen but | 425 // Special case: if the host was exactly "www." (not sure this can happen but |
| 499 // perhaps with some weird intranet and custom DNS server?), ensure we at | 426 // perhaps with some weird intranet and custom DNS server?), ensure we at |
| 500 // least don't return the empty string. | 427 // least don't return the empty string. |
| 501 base::string16 keyword(net::StripWWWFromHost(url)); | 428 base::string16 keyword(net::StripWWWFromHost(url)); |
| 502 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword; | 429 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword; |
| 503 } | 430 } |
| 504 | 431 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 // case the term replaces the URL it's unlikely another keyword would have the | 488 // case the term replaces the URL it's unlikely another keyword would have the |
| 562 // same url. | 489 // same url. |
| 563 // TODO(jnd): Add additional parameters to get post data when the search URL | 490 // TODO(jnd): Add additional parameters to get post data when the search URL |
| 564 // has post parameters. | 491 // has post parameters. |
| 565 return GURL(search_ref.ReplaceSearchTermsUsingTermsData( | 492 return GURL(search_ref.ReplaceSearchTermsUsingTermsData( |
| 566 TemplateURLRef::SearchTermsArgs( | 493 TemplateURLRef::SearchTermsArgs( |
| 567 base::ASCIIToUTF16("blah.blah.blah.blah.blah")), | 494 base::ASCIIToUTF16("blah.blah.blah.blah.blah")), |
| 568 search_terms_data, NULL)); | 495 search_terms_data, NULL)); |
| 569 } | 496 } |
| 570 | 497 |
| 498 void TemplateURLService::SaveDefaultSearchProviderToPrefs( | |
| 499 const TemplateURL* t_url, | |
| 500 PrefService* prefs) const { | |
| 501 if (!prefs || load_failed_) | |
| 502 return; | |
| 503 | |
| 504 bool enabled = false; | |
| 505 std::string search_url; | |
| 506 std::string suggest_url; | |
| 507 std::string instant_url; | |
| 508 std::string image_url; | |
| 509 std::string new_tab_url; | |
| 510 std::string search_url_post_params; | |
| 511 std::string suggest_url_post_params; | |
| 512 std::string instant_url_post_params; | |
| 513 std::string image_url_post_params; | |
| 514 std::string icon_url; | |
| 515 std::string encodings; | |
| 516 std::string short_name; | |
| 517 std::string keyword; | |
| 518 std::string id_string; | |
| 519 std::string prepopulate_id; | |
| 520 base::ListValue alternate_urls; | |
| 521 std::string search_terms_replacement_key; | |
| 522 if (t_url) { | |
| 523 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType()); | |
| 524 enabled = true; | |
| 525 search_url = t_url->url(); | |
| 526 suggest_url = t_url->suggestions_url(); | |
| 527 instant_url = t_url->instant_url(); | |
| 528 image_url = t_url->image_url(); | |
| 529 new_tab_url = t_url->new_tab_url(); | |
| 530 search_url_post_params = t_url->search_url_post_params(); | |
| 531 suggest_url_post_params = t_url->suggestions_url_post_params(); | |
| 532 instant_url_post_params = t_url->instant_url_post_params(); | |
| 533 image_url_post_params = t_url->image_url_post_params(); | |
| 534 GURL icon_gurl = t_url->favicon_url(); | |
| 535 if (!icon_gurl.is_empty()) | |
| 536 icon_url = icon_gurl.spec(); | |
| 537 encodings = JoinString(t_url->input_encodings(), ';'); | |
| 538 short_name = base::UTF16ToUTF8(t_url->short_name()); | |
| 539 keyword = base::UTF16ToUTF8(t_url->keyword()); | |
| 540 id_string = base::Int64ToString(t_url->id()); | |
| 541 prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); | |
| 542 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i) | |
| 543 alternate_urls.AppendString(t_url->alternate_urls()[i]); | |
| 544 search_terms_replacement_key = t_url->search_terms_replacement_key(); | |
| 545 } | |
| 546 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); | |
| 547 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); | |
| 548 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); | |
| 549 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); | |
| 550 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url); | |
| 551 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url); | |
| 552 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams, | |
| 553 search_url_post_params); | |
| 554 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams, | |
| 555 suggest_url_post_params); | |
| 556 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams, | |
| 557 instant_url_post_params); | |
| 558 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams, | |
| 559 image_url_post_params); | |
| 560 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); | |
| 561 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); | |
| 562 prefs->SetString(prefs::kDefaultSearchProviderName, short_name); | |
| 563 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); | |
| 564 prefs->SetString(prefs::kDefaultSearchProviderID, id_string); | |
| 565 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); | |
| 566 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); | |
| 567 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, | |
| 568 search_terms_replacement_key); | |
| 569 } | |
| 570 | |
| 571 bool TemplateURLService::CanReplaceKeyword( | 571 bool TemplateURLService::CanReplaceKeyword( |
| 572 const base::string16& keyword, | 572 const base::string16& keyword, |
| 573 const GURL& url, | 573 const GURL& url, |
| 574 TemplateURL** template_url_to_replace) { | 574 TemplateURL** template_url_to_replace) { |
| 575 DCHECK(!keyword.empty()); // This should only be called for non-empty | 575 DCHECK(!keyword.empty()); // This should only be called for non-empty |
| 576 // keywords. If we need to support empty kewords | 576 // keywords. If we need to support empty kewords |
| 577 // the code needs to change slightly. | 577 // the code needs to change slightly. |
| 578 TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); | 578 TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); |
| 579 if (template_url_to_replace) | 579 if (template_url_to_replace) |
| 580 *template_url_to_replace = existing_url; | 580 *template_url_to_replace = existing_url; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 matches->push_back(i->second); | 623 matches->push_back(i->second); |
| 624 } | 624 } |
| 625 } | 625 } |
| 626 | 626 |
| 627 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( | 627 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( |
| 628 const base::string16& keyword) { | 628 const base::string16& keyword) { |
| 629 KeywordToTemplateMap::const_iterator elem( | 629 KeywordToTemplateMap::const_iterator elem( |
| 630 keyword_to_template_map_.find(keyword)); | 630 keyword_to_template_map_.find(keyword)); |
| 631 if (elem != keyword_to_template_map_.end()) | 631 if (elem != keyword_to_template_map_.end()) |
| 632 return elem->second; | 632 return elem->second; |
| 633 return ((!loaded_ || load_failed_) && | 633 return (!loaded_ && initial_default_search_provider_.get() && |
|
Peter Kasting
2014/05/05 18:46:24
Leaving the "load_failed_" checks in these next th
| |
| 634 initial_default_search_provider_.get() && | |
| 635 (initial_default_search_provider_->keyword() == keyword)) ? | 634 (initial_default_search_provider_->keyword() == keyword)) ? |
| 636 initial_default_search_provider_.get() : NULL; | 635 initial_default_search_provider_.get() : NULL; |
| 637 } | 636 } |
| 638 | 637 |
| 639 TemplateURL* TemplateURLService::GetTemplateURLForGUID( | 638 TemplateURL* TemplateURLService::GetTemplateURLForGUID( |
| 640 const std::string& sync_guid) { | 639 const std::string& sync_guid) { |
| 641 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); | 640 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); |
| 642 if (elem != guid_to_template_map_.end()) | 641 if (elem != guid_to_template_map_.end()) |
| 643 return elem->second; | 642 return elem->second; |
| 644 return ((!loaded_ || load_failed_) && | 643 return (!loaded_ && initial_default_search_provider_.get() && |
| 645 initial_default_search_provider_.get() && | |
| 646 (initial_default_search_provider_->sync_guid() == sync_guid)) ? | 644 (initial_default_search_provider_->sync_guid() == sync_guid)) ? |
| 647 initial_default_search_provider_.get() : NULL; | 645 initial_default_search_provider_.get() : NULL; |
| 648 } | 646 } |
| 649 | 647 |
| 650 TemplateURL* TemplateURLService::GetTemplateURLForHost( | 648 TemplateURL* TemplateURLService::GetTemplateURLForHost( |
| 651 const std::string& host) { | 649 const std::string& host) { |
| 652 if (loaded_) { | 650 if (loaded_) { |
| 653 TemplateURL* t_url = provider_map_->GetTemplateURLForHost(host); | 651 TemplateURL* t_url = provider_map_->GetTemplateURLForHost(host); |
| 654 if (t_url) | 652 if (t_url) |
| 655 return t_url; | 653 return t_url; |
| 656 } | 654 } |
| 657 return ((!loaded_ || load_failed_) && | 655 return (!loaded_ && initial_default_search_provider_.get() && |
| 658 initial_default_search_provider_.get() && | |
| 659 (GenerateSearchURL(initial_default_search_provider_.get()).host() == | 656 (GenerateSearchURL(initial_default_search_provider_.get()).host() == |
| 660 host)) ? initial_default_search_provider_.get() : NULL; | 657 host)) ? initial_default_search_provider_.get() : NULL; |
| 661 } | 658 } |
| 662 | 659 |
| 663 void TemplateURLService::Add(TemplateURL* template_url) { | 660 void TemplateURLService::Add(TemplateURL* template_url) { |
| 664 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); | 661 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); |
| 665 if (AddNoNotify(template_url, true)) | 662 if (AddNoNotify(template_url, true)) |
| 666 NotifyObservers(); | 663 NotifyObservers(); |
| 667 } | 664 } |
| 668 | 665 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 803 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { | 800 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { |
| 804 DCHECK(url); | 801 DCHECK(url); |
| 805 // Extension-controlled search engines are not persisted. | 802 // Extension-controlled search engines are not persisted. |
| 806 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) | 803 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) |
| 807 return; | 804 return; |
| 808 if (std::find(template_urls_.begin(), template_urls_.end(), url) == | 805 if (std::find(template_urls_.begin(), template_urls_.end(), url) == |
| 809 template_urls_.end()) | 806 template_urls_.end()) |
| 810 return; | 807 return; |
| 811 ++url->data_.usage_count; | 808 ++url->data_.usage_count; |
| 812 | 809 |
| 813 if (service_.get()) | 810 if (service_) |
| 814 service_.get()->UpdateKeyword(url->data()); | 811 service_->UpdateKeyword(url->data()); |
|
Peter Kasting
2014/05/05 18:46:24
I removed some unnecessary get()s here and below w
| |
| 815 } | 812 } |
| 816 | 813 |
| 817 void TemplateURLService::ResetTemplateURL(TemplateURL* url, | 814 void TemplateURLService::ResetTemplateURL(TemplateURL* url, |
| 818 const base::string16& title, | 815 const base::string16& title, |
| 819 const base::string16& keyword, | 816 const base::string16& keyword, |
| 820 const std::string& search_url) { | 817 const std::string& search_url) { |
| 821 if (!loaded_) | 818 if (!loaded_) |
| 822 return; | 819 return; |
| 823 DCHECK(!keyword.empty()); | 820 DCHECK(!keyword.empty()); |
| 824 DCHECK(!search_url.empty()); | 821 DCHECK(!search_url.empty()); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 851 SetDefaultSearchProvider(url); | 848 SetDefaultSearchProvider(url); |
| 852 if (default_search_manager_) { | 849 if (default_search_manager_) { |
| 853 if (url) | 850 if (url) |
| 854 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); | 851 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); |
| 855 else | 852 else |
| 856 default_search_manager_->ClearUserSelectedDefaultSearchEngine(); | 853 default_search_manager_->ClearUserSelectedDefaultSearchEngine(); |
| 857 } | 854 } |
| 858 } | 855 } |
| 859 | 856 |
| 860 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 857 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
| 861 if (loaded_ && !load_failed_) | 858 return loaded_ ? |
|
Peter Kasting
2014/05/05 18:46:24
Another case where examining |load_failed_| is act
| |
| 862 return default_search_provider_; | 859 default_search_provider_ : initial_default_search_provider_.get(); |
| 863 // We're not loaded, rely on the default search provider stored in prefs. | |
| 864 return initial_default_search_provider_.get(); | |
| 865 } | 860 } |
| 866 | 861 |
| 867 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( | 862 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( |
| 868 const GURL& url) { | 863 const GURL& url) { |
| 869 TemplateURL* default_provider = GetDefaultSearchProvider(); | 864 TemplateURL* default_provider = GetDefaultSearchProvider(); |
| 870 return default_provider && default_provider->IsSearchURL(url); | 865 return default_provider && default_provider->IsSearchURL(url); |
| 871 } | 866 } |
| 872 | 867 |
| 873 bool TemplateURLService::IsExtensionControlledDefaultSearch() { | 868 bool TemplateURLService::IsExtensionControlledDefaultSearch() { |
| 874 const TemplateURL* default_provider = GetDefaultSearchProvider(); | 869 const TemplateURL* default_provider = GetDefaultSearchProvider(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 965 } | 960 } |
| 966 | 961 |
| 967 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { | 962 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { |
| 968 model_observers_.RemoveObserver(observer); | 963 model_observers_.RemoveObserver(observer); |
| 969 } | 964 } |
| 970 | 965 |
| 971 void TemplateURLService::Load() { | 966 void TemplateURLService::Load() { |
| 972 if (loaded_ || load_handle_) | 967 if (loaded_ || load_handle_) |
| 973 return; | 968 return; |
| 974 | 969 |
| 975 if (!service_.get()) { | 970 if (!service_) |
| 976 service_ = WebDataService::FromBrowserContext(profile_); | 971 service_ = WebDataService::FromBrowserContext(profile_); |
| 977 } | |
| 978 | 972 |
| 979 if (service_.get()) { | 973 if (service_) |
| 980 load_handle_ = service_->GetKeywords(this); | 974 load_handle_ = service_->GetKeywords(this); |
| 981 } else { | 975 else |
| 982 ChangeToLoadedState(); | 976 OnFailedLoad(); |
| 983 on_loaded_callbacks_.Notify(); | |
| 984 } | |
| 985 } | 977 } |
| 986 | 978 |
| 987 scoped_ptr<TemplateURLService::Subscription> | 979 scoped_ptr<TemplateURLService::Subscription> |
| 988 TemplateURLService::RegisterOnLoadedCallback( | 980 TemplateURLService::RegisterOnLoadedCallback( |
| 989 const base::Closure& callback) { | 981 const base::Closure& callback) { |
| 990 return loaded_ ? | 982 return loaded_ ? |
| 991 scoped_ptr<TemplateURLService::Subscription>() : | 983 scoped_ptr<TemplateURLService::Subscription>() : |
| 992 on_loaded_callbacks_.Add(callback); | 984 on_loaded_callbacks_.Add(callback); |
| 993 } | 985 } |
| 994 | 986 |
| 995 void TemplateURLService::OnWebDataServiceRequestDone( | 987 void TemplateURLService::OnWebDataServiceRequestDone( |
| 996 WebDataService::Handle h, | 988 WebDataService::Handle h, |
| 997 const WDTypedResult* result) { | 989 const WDTypedResult* result) { |
| 998 // Reset the load_handle so that we don't try and cancel the load in | 990 // Reset the load_handle so that we don't try and cancel the load in |
| 999 // the destructor. | 991 // the destructor. |
| 1000 load_handle_ = 0; | 992 load_handle_ = 0; |
| 1001 | 993 |
| 1002 if (!result) { | 994 if (!result) { |
| 1003 // Results are null if the database went away or (most likely) wasn't | 995 // Results are null if the database went away or (most likely) wasn't |
| 1004 // loaded. | 996 // loaded. |
| 1005 load_failed_ = true; | 997 OnFailedLoad(); |
| 1006 ChangeToLoadedState(); | |
| 1007 on_loaded_callbacks_.Notify(); | |
| 1008 return; | 998 return; |
| 1009 } | 999 } |
| 1010 | 1000 |
| 1011 // initial_default_search_provider_ is only needed before we've finished | 1001 // initial_default_search_provider_ is only needed before we've finished |
| 1012 // loading. Now that we've loaded we can nuke it. | 1002 // loading. Now that we've loaded we can nuke it. |
| 1013 initial_default_search_provider_.reset(); | 1003 initial_default_search_provider_.reset(); |
| 1014 | 1004 |
| 1015 TemplateURLVector template_urls; | 1005 TemplateURLVector template_urls; |
| 1016 TemplateURL* default_search_provider = NULL; | 1006 TemplateURL* default_search_provider = NULL; |
| 1017 int new_resource_keyword_version = 0; | 1007 int new_resource_keyword_version = 0; |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1273 syncer::ModelType type, | 1263 syncer::ModelType type, |
| 1274 const syncer::SyncDataList& initial_sync_data, | 1264 const syncer::SyncDataList& initial_sync_data, |
| 1275 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 1265 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 1276 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 1266 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
| 1277 DCHECK(loaded_); | 1267 DCHECK(loaded_); |
| 1278 DCHECK_EQ(type, syncer::SEARCH_ENGINES); | 1268 DCHECK_EQ(type, syncer::SEARCH_ENGINES); |
| 1279 DCHECK(!sync_processor_.get()); | 1269 DCHECK(!sync_processor_.get()); |
| 1280 DCHECK(sync_processor.get()); | 1270 DCHECK(sync_processor.get()); |
| 1281 DCHECK(sync_error_factory.get()); | 1271 DCHECK(sync_error_factory.get()); |
| 1282 syncer::SyncMergeResult merge_result(type); | 1272 syncer::SyncMergeResult merge_result(type); |
| 1273 | |
| 1274 // Disable sync if we failed to load. | |
|
Peter Kasting
2014/05/05 18:46:24
Because the in-memory state won't match the on-dis
| |
| 1275 if (load_failed_) { | |
| 1276 merge_result.set_error(syncer::SyncError( | |
| 1277 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | |
| 1278 "Local database load failed.", syncer::SEARCH_ENGINES)); | |
| 1279 return merge_result; | |
| 1280 } | |
| 1281 | |
| 1283 sync_processor_ = sync_processor.Pass(); | 1282 sync_processor_ = sync_processor.Pass(); |
| 1284 sync_error_factory_ = sync_error_factory.Pass(); | 1283 sync_error_factory_ = sync_error_factory.Pass(); |
| 1285 | 1284 |
| 1286 // We just started syncing, so set our wait-for-default flag if we are | 1285 // We just started syncing, so set our wait-for-default flag if we are |
| 1287 // expecting a default from Sync. | 1286 // expecting a default from Sync. |
| 1288 if (GetPrefs()) { | 1287 if (GetPrefs()) { |
| 1289 std::string default_guid = GetPrefs()->GetString( | 1288 std::string default_guid = GetPrefs()->GetString( |
| 1290 prefs::kSyncedDefaultSearchProviderGUID); | 1289 prefs::kSyncedDefaultSearchProviderGUID); |
| 1291 const TemplateURL* current_default = GetDefaultSearchProvider(); | 1290 const TemplateURL* current_default = GetDefaultSearchProvider(); |
| 1292 | 1291 |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1778 } | 1777 } |
| 1779 | 1778 |
| 1780 void TemplateURLService::ChangeToLoadedState() { | 1779 void TemplateURLService::ChangeToLoadedState() { |
| 1781 DCHECK(!loaded_); | 1780 DCHECK(!loaded_); |
| 1782 | 1781 |
| 1783 UIThreadSearchTermsData search_terms_data(profile_); | 1782 UIThreadSearchTermsData search_terms_data(profile_); |
| 1784 provider_map_->Init(template_urls_, search_terms_data); | 1783 provider_map_->Init(template_urls_, search_terms_data); |
| 1785 loaded_ = true; | 1784 loaded_ = true; |
| 1786 } | 1785 } |
| 1787 | 1786 |
| 1787 void TemplateURLService::OnFailedLoad() { | |
| 1788 load_failed_ = true; | |
| 1789 service_ = NULL; | |
| 1790 ChangeToLoadedState(); | |
| 1791 if (!GetDefaultSearchProvider()) { | |
|
Peter Kasting
2014/05/05 18:46:24
Some tests set a new default search provider befor
| |
| 1792 TemplateURL* default_search_provider = | |
| 1793 initial_default_search_provider_.release(); | |
| 1794 if (default_search_provider) { | |
| 1795 AddNoNotify(default_search_provider, | |
| 1796 default_search_provider->id() == kInvalidTemplateURLID); | |
|
Peter Kasting
2014/05/05 18:46:24
We want AddNoNotify() to ensure the provided Templ
| |
| 1797 SetDefaultSearchProviderNoNotify(default_search_provider); | |
| 1798 } | |
| 1799 } | |
| 1800 on_loaded_callbacks_.Notify(); | |
| 1801 } | |
| 1802 | |
| 1788 void TemplateURLService::ClearDefaultProviderFromPrefs() { | 1803 void TemplateURLService::ClearDefaultProviderFromPrefs() { |
| 1789 // We overwrite user preferences. If the default search engine is managed, | 1804 // We overwrite user preferences. If the default search engine is managed, |
| 1790 // there is no effect. | 1805 // there is no effect. |
| 1791 SaveDefaultSearchProviderToPrefs(NULL, GetPrefs()); | 1806 SaveDefaultSearchProviderToPrefs(NULL, GetPrefs()); |
| 1792 // Default value for kDefaultSearchProviderEnabled is true. | 1807 // Default value for kDefaultSearchProviderEnabled is true. |
| 1793 PrefService* prefs = GetPrefs(); | 1808 PrefService* prefs = GetPrefs(); |
| 1794 if (prefs) | 1809 if (prefs) |
| 1795 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, true); | 1810 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, true); |
| 1796 } | 1811 } |
| 1797 | 1812 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1878 RemoveNoNotify(existing_keyword_turl); | 1893 RemoveNoNotify(existing_keyword_turl); |
| 1879 } else { | 1894 } else { |
| 1880 existing_turl->data_.SetKeyword(old_keyword); | 1895 existing_turl->data_.SetKeyword(old_keyword); |
| 1881 keyword_to_template_map_[old_keyword] = existing_turl; | 1896 keyword_to_template_map_[old_keyword] = existing_turl; |
| 1882 } | 1897 } |
| 1883 } | 1898 } |
| 1884 } | 1899 } |
| 1885 if (!existing_turl->sync_guid().empty()) | 1900 if (!existing_turl->sync_guid().empty()) |
| 1886 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1901 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
| 1887 | 1902 |
| 1888 if (service_.get()) | 1903 if (service_) |
| 1889 service_->UpdateKeyword(existing_turl->data()); | 1904 service_->UpdateKeyword(existing_turl->data()); |
| 1890 | 1905 |
| 1891 // Inform sync of the update. | 1906 // Inform sync of the update. |
| 1892 ProcessTemplateURLChange(FROM_HERE, | 1907 ProcessTemplateURLChange(FROM_HERE, |
| 1893 existing_turl, | 1908 existing_turl, |
| 1894 syncer::SyncChange::ACTION_UPDATE); | 1909 syncer::SyncChange::ACTION_UPDATE); |
| 1895 | 1910 |
| 1896 if (default_search_provider_ == existing_turl) { | 1911 if (default_search_provider_ == existing_turl) { |
| 1897 bool success = SetDefaultSearchProviderNoNotify(existing_turl); | 1912 bool success = SetDefaultSearchProviderNoNotify(existing_turl); |
| 1898 DCHECK(success); | 1913 DCHECK(success); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2167 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin", | 2182 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin", |
| 2168 dsp_change_origin_, DSP_CHANGE_MAX); | 2183 dsp_change_origin_, DSP_CHANGE_MAX); |
| 2169 default_search_provider_ = url; | 2184 default_search_provider_ = url; |
| 2170 } | 2185 } |
| 2171 } | 2186 } |
| 2172 | 2187 |
| 2173 if (url) { | 2188 if (url) { |
| 2174 // Don't mark the url as edited, otherwise we won't be able to rev the | 2189 // Don't mark the url as edited, otherwise we won't be able to rev the |
| 2175 // template urls we ship with. | 2190 // template urls we ship with. |
| 2176 url->data_.show_in_default_list = true; | 2191 url->data_.show_in_default_list = true; |
| 2177 if (service_.get() && (url->GetType() == TemplateURL::NORMAL)) | 2192 if (service_ && (url->GetType() == TemplateURL::NORMAL)) |
| 2178 service_->UpdateKeyword(url->data()); | 2193 service_->UpdateKeyword(url->data()); |
| 2179 | 2194 |
| 2180 if (url->url_ref().HasGoogleBaseURLs()) { | 2195 if (url->url_ref().HasGoogleBaseURLs()) { |
| 2181 GoogleURLTracker::RequestServerCheck(profile_, false); | 2196 GoogleURLTracker::RequestServerCheck(profile_, false); |
| 2182 #if defined(ENABLE_RLZ) | 2197 #if defined(ENABLE_RLZ) |
| 2183 RLZTracker::RecordProductEvent(rlz_lib::CHROME, | 2198 RLZTracker::RecordProductEvent(rlz_lib::CHROME, |
| 2184 RLZTracker::CHROME_OMNIBOX, | 2199 RLZTracker::CHROME_OMNIBOX, |
| 2185 rlz_lib::SET_TO_GOOGLE); | 2200 rlz_lib::SET_TO_GOOGLE); |
| 2186 #endif | 2201 #endif |
| 2187 } | 2202 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 2202 // needs to set a new default as well. If we update the default here, we're | 2217 // needs to set a new default as well. If we update the default here, we're |
| 2203 // likely to race with the update from the other client, resulting in | 2218 // likely to race with the update from the other client, resulting in |
| 2204 // a possibly random default search provider. | 2219 // a possibly random default search provider. |
| 2205 if (sync_processor_.get() && url && !url->sync_guid().empty() && | 2220 if (sync_processor_.get() && url && !url->sync_guid().empty() && |
| 2206 GetPrefs() && !processing_syncer_changes_) { | 2221 GetPrefs() && !processing_syncer_changes_) { |
| 2207 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 2222 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
| 2208 url->sync_guid()); | 2223 url->sync_guid()); |
| 2209 } | 2224 } |
| 2210 } | 2225 } |
| 2211 | 2226 |
| 2212 if (service_.get()) | 2227 if (service_) |
| 2213 service_->SetDefaultSearchProviderID(url ? url->id() : 0); | 2228 service_->SetDefaultSearchProviderID(url ? url->id() : 0); |
| 2214 | 2229 |
| 2215 // Inform sync the change to the show_in_default_list flag. | 2230 // Inform sync the change to the show_in_default_list flag. |
| 2216 if (url) | 2231 if (url) |
| 2217 ProcessTemplateURLChange(FROM_HERE, | 2232 ProcessTemplateURLChange(FROM_HERE, |
| 2218 url, | 2233 url, |
| 2219 syncer::SyncChange::ACTION_UPDATE); | 2234 syncer::SyncChange::ACTION_UPDATE); |
| 2220 return true; | 2235 return true; |
| 2221 } | 2236 } |
| 2222 | 2237 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2254 existing_keyword_turl->short_name(), new_keyword, | 2269 existing_keyword_turl->short_name(), new_keyword, |
| 2255 existing_keyword_turl->url()); | 2270 existing_keyword_turl->url()); |
| 2256 } | 2271 } |
| 2257 } | 2272 } |
| 2258 template_urls_.push_back(template_url); | 2273 template_urls_.push_back(template_url); |
| 2259 AddToMaps(template_url); | 2274 AddToMaps(template_url); |
| 2260 | 2275 |
| 2261 if (newly_adding && | 2276 if (newly_adding && |
| 2262 (template_url->GetType() != | 2277 (template_url->GetType() != |
| 2263 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { | 2278 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { |
| 2264 if (service_.get()) | 2279 if (service_) |
| 2265 service_->AddKeyword(template_url->data()); | 2280 service_->AddKeyword(template_url->data()); |
| 2266 | 2281 |
| 2267 // Inform sync of the addition. Note that this will assign a GUID to | 2282 // Inform sync of the addition. Note that this will assign a GUID to |
| 2268 // template_url and add it to the guid_to_template_map_. | 2283 // template_url and add it to the guid_to_template_map_. |
| 2269 ProcessTemplateURLChange(FROM_HERE, | 2284 ProcessTemplateURLChange(FROM_HERE, |
| 2270 template_url, | 2285 template_url, |
| 2271 syncer::SyncChange::ACTION_ADD); | 2286 syncer::SyncChange::ACTION_ADD); |
| 2272 } | 2287 } |
| 2273 | 2288 |
| 2274 return true; | 2289 return true; |
| 2275 } | 2290 } |
| 2276 | 2291 |
| 2277 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2292 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
| 2278 DCHECK(template_url != default_search_provider_); | 2293 DCHECK(template_url != default_search_provider_); |
| 2279 | 2294 |
| 2280 TemplateURLVector::iterator i = | 2295 TemplateURLVector::iterator i = |
| 2281 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2296 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
| 2282 if (i == template_urls_.end()) | 2297 if (i == template_urls_.end()) |
| 2283 return; | 2298 return; |
| 2284 | 2299 |
| 2285 RemoveFromMaps(template_url); | 2300 RemoveFromMaps(template_url); |
| 2286 | 2301 |
| 2287 // Remove it from the vector containing all TemplateURLs. | 2302 // Remove it from the vector containing all TemplateURLs. |
| 2288 template_urls_.erase(i); | 2303 template_urls_.erase(i); |
| 2289 | 2304 |
| 2290 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { | 2305 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { |
| 2291 if (service_.get()) | 2306 if (service_) |
| 2292 service_->RemoveKeyword(template_url->id()); | 2307 service_->RemoveKeyword(template_url->id()); |
| 2293 | 2308 |
| 2294 // Inform sync of the deletion. | 2309 // Inform sync of the deletion. |
| 2295 ProcessTemplateURLChange(FROM_HERE, | 2310 ProcessTemplateURLChange(FROM_HERE, |
| 2296 template_url, | 2311 template_url, |
| 2297 syncer::SyncChange::ACTION_DELETE); | 2312 syncer::SyncChange::ACTION_DELETE); |
| 2298 | 2313 |
| 2299 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, | 2314 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, |
| 2300 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); | 2315 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); |
| 2301 } | 2316 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2351 } | 2366 } |
| 2352 | 2367 |
| 2353 // The database loaded a managed |default_search_provider|, but it has | 2368 // The database loaded a managed |default_search_provider|, but it has |
| 2354 // been updated in the prefs. Remove it from the database, and update the | 2369 // been updated in the prefs. Remove it from the database, and update the |
| 2355 // |default_search_provider| pointer here. | 2370 // |default_search_provider| pointer here. |
| 2356 if (*default_search_provider && | 2371 if (*default_search_provider && |
| 2357 (*default_search_provider)->id() == template_url->id()) | 2372 (*default_search_provider)->id() == template_url->id()) |
| 2358 *default_search_provider = NULL; | 2373 *default_search_provider = NULL; |
| 2359 | 2374 |
| 2360 i = template_urls->erase(i); | 2375 i = template_urls->erase(i); |
| 2361 if (service_.get()) | 2376 if (service_) |
| 2362 service_->RemoveKeyword(template_url->id()); | 2377 service_->RemoveKeyword(template_url->id()); |
| 2363 delete template_url; | 2378 delete template_url; |
| 2364 } else { | 2379 } else { |
| 2365 ++i; | 2380 ++i; |
| 2366 } | 2381 } |
| 2367 } | 2382 } |
| 2368 } | 2383 } |
| 2369 | 2384 |
| 2370 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, | 2385 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, |
| 2371 const std::string& guid) { | 2386 const std::string& guid) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2575 TemplateURLVector* template_urls) { | 2590 TemplateURLVector* template_urls) { |
| 2576 DCHECK(template_urls); | 2591 DCHECK(template_urls); |
| 2577 for (TemplateURLVector::iterator i = template_urls->begin(); | 2592 for (TemplateURLVector::iterator i = template_urls->begin(); |
| 2578 i != template_urls->end(); ++i) { | 2593 i != template_urls->end(); ++i) { |
| 2579 TemplateURL* template_url = *i; | 2594 TemplateURL* template_url = *i; |
| 2580 DCHECK(template_url); | 2595 DCHECK(template_url); |
| 2581 if (template_url->sync_guid().empty() && | 2596 if (template_url->sync_guid().empty() && |
| 2582 (template_url->GetType() != | 2597 (template_url->GetType() != |
| 2583 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { | 2598 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { |
| 2584 template_url->data_.sync_guid = base::GenerateGUID(); | 2599 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2585 if (service_.get()) | 2600 if (service_) |
| 2586 service_->UpdateKeyword(template_url->data()); | 2601 service_->UpdateKeyword(template_url->data()); |
| 2587 } | 2602 } |
| 2588 } | 2603 } |
| 2589 } | 2604 } |
| 2590 | 2605 |
| 2591 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( | 2606 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( |
| 2592 TemplateURLVector* template_urls, | 2607 TemplateURLVector* template_urls, |
| 2593 TemplateURL* default_search_provider) { | 2608 TemplateURL* default_search_provider) { |
| 2594 DCHECK(template_urls); | 2609 DCHECK(template_urls); |
| 2595 is_default_search_managed_ = false; | 2610 is_default_search_managed_ = false; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2754 new_dse = *i; | 2769 new_dse = *i; |
| 2755 break; | 2770 break; |
| 2756 } | 2771 } |
| 2757 } | 2772 } |
| 2758 } | 2773 } |
| 2759 } | 2774 } |
| 2760 if (!new_dse) | 2775 if (!new_dse) |
| 2761 new_dse = FindNewDefaultSearchProvider(); | 2776 new_dse = FindNewDefaultSearchProvider(); |
| 2762 SetDefaultSearchProviderNoNotify(new_dse); | 2777 SetDefaultSearchProviderNoNotify(new_dse); |
| 2763 } | 2778 } |
| OLD | NEW |