| 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 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 return; | 765 return; |
| 766 | 766 |
| 767 if (!service_.get()) { | 767 if (!service_.get()) { |
| 768 service_ = WebDataService::FromBrowserContext(profile_); | 768 service_ = WebDataService::FromBrowserContext(profile_); |
| 769 } | 769 } |
| 770 | 770 |
| 771 if (service_.get()) { | 771 if (service_.get()) { |
| 772 load_handle_ = service_->GetKeywords(this); | 772 load_handle_ = service_->GetKeywords(this); |
| 773 } else { | 773 } else { |
| 774 ChangeToLoadedState(); | 774 ChangeToLoadedState(); |
| 775 NotifyLoaded(); | 775 on_loaded_callbacks_.Notify(); |
| 776 } | 776 } |
| 777 } | 777 } |
| 778 | 778 |
| 779 scoped_ptr<TemplateURLService::Subscription> |
| 780 TemplateURLService::RegisterOnLoadedCallback( |
| 781 const base::Closure& callback) { |
| 782 return loaded_ ? |
| 783 scoped_ptr<TemplateURLService::Subscription>() : |
| 784 on_loaded_callbacks_.Add(callback); |
| 785 } |
| 786 |
| 779 void TemplateURLService::OnWebDataServiceRequestDone( | 787 void TemplateURLService::OnWebDataServiceRequestDone( |
| 780 WebDataService::Handle h, | 788 WebDataService::Handle h, |
| 781 const WDTypedResult* result) { | 789 const WDTypedResult* result) { |
| 782 // Reset the load_handle so that we don't try and cancel the load in | 790 // Reset the load_handle so that we don't try and cancel the load in |
| 783 // the destructor. | 791 // the destructor. |
| 784 load_handle_ = 0; | 792 load_handle_ = 0; |
| 785 | 793 |
| 786 if (!result) { | 794 if (!result) { |
| 787 // Results are null if the database went away or (most likely) wasn't | 795 // Results are null if the database went away or (most likely) wasn't |
| 788 // loaded. | 796 // loaded. |
| 789 load_failed_ = true; | 797 load_failed_ = true; |
| 790 ChangeToLoadedState(); | 798 ChangeToLoadedState(); |
| 791 NotifyLoaded(); | 799 on_loaded_callbacks_.Notify(); |
| 792 return; | 800 return; |
| 793 } | 801 } |
| 794 | 802 |
| 795 // initial_default_search_provider_ is only needed before we've finished | 803 // initial_default_search_provider_ is only needed before we've finished |
| 796 // loading. Now that we've loaded we can nuke it. | 804 // loading. Now that we've loaded we can nuke it. |
| 797 initial_default_search_provider_.reset(); | 805 initial_default_search_provider_.reset(); |
| 798 | 806 |
| 799 TemplateURLVector template_urls; | 807 TemplateURLVector template_urls; |
| 800 TemplateURL* default_search_provider = NULL; | 808 TemplateURL* default_search_provider = NULL; |
| 801 int new_resource_keyword_version = 0; | 809 int new_resource_keyword_version = 0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 813 for (size_t i = 0; i < visits_to_add_.size(); ++i) | 821 for (size_t i = 0; i < visits_to_add_.size(); ++i) |
| 814 UpdateKeywordSearchTermsForURL(visits_to_add_[i]); | 822 UpdateKeywordSearchTermsForURL(visits_to_add_[i]); |
| 815 visits_to_add_.clear(); | 823 visits_to_add_.clear(); |
| 816 | 824 |
| 817 if (new_resource_keyword_version) | 825 if (new_resource_keyword_version) |
| 818 service_->SetBuiltinKeywordVersion(new_resource_keyword_version); | 826 service_->SetBuiltinKeywordVersion(new_resource_keyword_version); |
| 819 | 827 |
| 820 EnsureDefaultSearchProviderExists(); | 828 EnsureDefaultSearchProviderExists(); |
| 821 | 829 |
| 822 NotifyObservers(); | 830 NotifyObservers(); |
| 823 NotifyLoaded(); | 831 on_loaded_callbacks_.Notify(); |
| 824 } | 832 } |
| 825 | 833 |
| 826 string16 TemplateURLService::GetKeywordShortName(const string16& keyword, | 834 string16 TemplateURLService::GetKeywordShortName(const string16& keyword, |
| 827 bool* is_extension_keyword) { | 835 bool* is_extension_keyword) { |
| 828 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); | 836 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); |
| 829 | 837 |
| 830 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService | 838 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService |
| 831 // to track changes to the model, this should become a DCHECK. | 839 // to track changes to the model, this should become a DCHECK. |
| 832 if (template_url) { | 840 if (template_url) { |
| 833 *is_extension_keyword = template_url->IsExtensionKeyword(); | 841 *is_extension_keyword = template_url->IsExtensionKeyword(); |
| (...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1563 } | 1571 } |
| 1564 | 1572 |
| 1565 void TemplateURLService::ChangeToLoadedState() { | 1573 void TemplateURLService::ChangeToLoadedState() { |
| 1566 DCHECK(!loaded_); | 1574 DCHECK(!loaded_); |
| 1567 | 1575 |
| 1568 UIThreadSearchTermsData search_terms_data(profile_); | 1576 UIThreadSearchTermsData search_terms_data(profile_); |
| 1569 provider_map_->Init(template_urls_, search_terms_data); | 1577 provider_map_->Init(template_urls_, search_terms_data); |
| 1570 loaded_ = true; | 1578 loaded_ = true; |
| 1571 } | 1579 } |
| 1572 | 1580 |
| 1573 void TemplateURLService::NotifyLoaded() { | |
| 1574 content::NotificationService::current()->Notify( | |
| 1575 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
| 1576 content::Source<TemplateURLService>(this), | |
| 1577 content::NotificationService::NoDetails()); | |
| 1578 } | |
| 1579 | |
| 1580 void TemplateURLService::SaveDefaultSearchProviderToPrefs( | 1581 void TemplateURLService::SaveDefaultSearchProviderToPrefs( |
| 1581 const TemplateURL* t_url) { | 1582 const TemplateURL* t_url) { |
| 1582 PrefService* prefs = GetPrefs(); | 1583 PrefService* prefs = GetPrefs(); |
| 1583 if (!prefs) | 1584 if (!prefs) |
| 1584 return; | 1585 return; |
| 1585 | 1586 |
| 1586 bool enabled = false; | 1587 bool enabled = false; |
| 1587 std::string search_url; | 1588 std::string search_url; |
| 1588 std::string suggest_url; | 1589 std::string suggest_url; |
| 1589 std::string instant_url; | 1590 std::string instant_url; |
| (...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2632 const ExtensionKeyword& extension_keyword) const { | 2633 const ExtensionKeyword& extension_keyword) const { |
| 2633 TemplateURLData data; | 2634 TemplateURLData data; |
| 2634 data.short_name = UTF8ToUTF16(extension_keyword.extension_name); | 2635 data.short_name = UTF8ToUTF16(extension_keyword.extension_name); |
| 2635 data.SetKeyword(UTF8ToUTF16(extension_keyword.extension_keyword)); | 2636 data.SetKeyword(UTF8ToUTF16(extension_keyword.extension_keyword)); |
| 2636 // This URL is not actually used for navigation. It holds the extension's | 2637 // This URL is not actually used for navigation. It holds the extension's |
| 2637 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 2638 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
| 2638 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + | 2639 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + |
| 2639 extension_keyword.extension_id + "/?q={searchTerms}"); | 2640 extension_keyword.extension_id + "/?q={searchTerms}"); |
| 2640 return new TemplateURL(profile_, data); | 2641 return new TemplateURL(profile_, data); |
| 2641 } | 2642 } |
| OLD | NEW |