| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/environment.h" | 8 #include "base/environment.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/prefs/pref_set_observer.h" | 22 #include "chrome/browser/prefs/pref_set_observer.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/rlz/rlz.h" | 24 #include "chrome/browser/rlz/rlz.h" |
| 25 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 25 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
| 26 #include "chrome/browser/search_engines/search_terms_data.h" | 26 #include "chrome/browser/search_engines/search_terms_data.h" |
| 27 #include "chrome/browser/search_engines/template_url.h" | 27 #include "chrome/browser/search_engines/template_url.h" |
| 28 #include "chrome/browser/search_engines/template_url_service_observer.h" | 28 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 29 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 29 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 30 #include "chrome/browser/search_engines/util.h" | 30 #include "chrome/browser/search_engines/util.h" |
| 31 #include "chrome/common/chrome_notification_types.h" |
| 31 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/env_vars.h" | 33 #include "chrome/common/env_vars.h" |
| 33 #include "chrome/common/extensions/extension.h" | 34 #include "chrome/common/extensions/extension.h" |
| 34 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 35 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
| 36 #include "content/common/notification_service.h" | 37 #include "content/common/notification_service.h" |
| 37 #include "net/base/net_util.h" | 38 #include "net/base/net_util.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 39 | 40 |
| 40 using base::Time; | 41 using base::Time; |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService | 573 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService |
| 573 // to track changes to the model, this should become a DCHECK. | 574 // to track changes to the model, this should become a DCHECK. |
| 574 if (template_url) { | 575 if (template_url) { |
| 575 *is_extension_keyword = template_url->IsExtensionKeyword(); | 576 *is_extension_keyword = template_url->IsExtensionKeyword(); |
| 576 return template_url->AdjustedShortNameForLocaleDirection(); | 577 return template_url->AdjustedShortNameForLocaleDirection(); |
| 577 } | 578 } |
| 578 *is_extension_keyword = false; | 579 *is_extension_keyword = false; |
| 579 return string16(); | 580 return string16(); |
| 580 } | 581 } |
| 581 | 582 |
| 582 void TemplateURLService::Observe(NotificationType type, | 583 void TemplateURLService::Observe(int type, |
| 583 const NotificationSource& source, | 584 const NotificationSource& source, |
| 584 const NotificationDetails& details) { | 585 const NotificationDetails& details) { |
| 585 if (type == NotificationType::HISTORY_URL_VISITED) { | 586 if (type == chrome::NOTIFICATION_HISTORY_URL_VISITED) { |
| 586 Details<history::URLVisitedDetails> visit_details(details); | 587 Details<history::URLVisitedDetails> visit_details(details); |
| 587 if (!loaded()) | 588 if (!loaded()) |
| 588 visits_to_add_.push_back(*visit_details.ptr()); | 589 visits_to_add_.push_back(*visit_details.ptr()); |
| 589 else | 590 else |
| 590 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); | 591 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); |
| 591 } else if (type == NotificationType::GOOGLE_URL_UPDATED) { | 592 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { |
| 592 if (loaded_) | 593 if (loaded_) |
| 593 GoogleBaseURLChanged(); | 594 GoogleBaseURLChanged(); |
| 594 } else if (type == NotificationType::PREF_CHANGED) { | 595 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 595 const std::string* pref_name = Details<std::string>(details).ptr(); | 596 const std::string* pref_name = Details<std::string>(details).ptr(); |
| 596 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) { | 597 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) { |
| 597 // A preference related to default search engine has changed. | 598 // A preference related to default search engine has changed. |
| 598 // Update the model if needed. | 599 // Update the model if needed. |
| 599 UpdateDefaultSearch(); | 600 UpdateDefaultSearch(); |
| 600 } | 601 } |
| 601 } else { | 602 } else { |
| 602 NOTREACHED(); | 603 NOTREACHED(); |
| 603 } | 604 } |
| 604 } | 605 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 } | 649 } |
| 649 | 650 |
| 650 void TemplateURLService::Init(const Initializer* initializers, | 651 void TemplateURLService::Init(const Initializer* initializers, |
| 651 int num_initializers) { | 652 int num_initializers) { |
| 652 // Register for notifications. | 653 // Register for notifications. |
| 653 if (profile_) { | 654 if (profile_) { |
| 654 // TODO(sky): bug 1166191. The keywords should be moved into the history | 655 // TODO(sky): bug 1166191. The keywords should be moved into the history |
| 655 // db, which will mean we no longer need this notification and the history | 656 // db, which will mean we no longer need this notification and the history |
| 656 // backend can handle automatically adding the search terms as the user | 657 // backend can handle automatically adding the search terms as the user |
| 657 // navigates. | 658 // navigates. |
| 658 registrar_.Add(this, NotificationType::HISTORY_URL_VISITED, | 659 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, |
| 659 Source<Profile>(profile_->GetOriginalProfile())); | 660 Source<Profile>(profile_->GetOriginalProfile())); |
| 660 PrefService* prefs = GetPrefs(); | 661 PrefService* prefs = GetPrefs(); |
| 661 default_search_prefs_.reset( | 662 default_search_prefs_.reset( |
| 662 PrefSetObserver::CreateDefaultSearchPrefSetObserver(prefs, this)); | 663 PrefSetObserver::CreateDefaultSearchPrefSetObserver(prefs, this)); |
| 663 } | 664 } |
| 664 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED, | 665 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 665 NotificationService::AllSources()); | 666 NotificationService::AllSources()); |
| 666 | 667 |
| 667 if (num_initializers > 0) { | 668 if (num_initializers > 0) { |
| 668 // This path is only hit by test code and is used to simulate a loaded | 669 // This path is only hit by test code and is used to simulate a loaded |
| 669 // TemplateURLService. | 670 // TemplateURLService. |
| 670 ChangeToLoadedState(); | 671 ChangeToLoadedState(); |
| 671 | 672 |
| 672 // Add specific initializers, if any. | 673 // Add specific initializers, if any. |
| 673 for (int i(0); i < num_initializers; ++i) { | 674 for (int i(0); i < num_initializers; ++i) { |
| 674 DCHECK(initializers[i].keyword); | 675 DCHECK(initializers[i].keyword); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 void TemplateURLService::ChangeToLoadedState() { | 769 void TemplateURLService::ChangeToLoadedState() { |
| 769 DCHECK(!loaded_); | 770 DCHECK(!loaded_); |
| 770 | 771 |
| 771 UIThreadSearchTermsData search_terms_data; | 772 UIThreadSearchTermsData search_terms_data; |
| 772 provider_map_.Init(template_urls_, search_terms_data); | 773 provider_map_.Init(template_urls_, search_terms_data); |
| 773 loaded_ = true; | 774 loaded_ = true; |
| 774 } | 775 } |
| 775 | 776 |
| 776 void TemplateURLService::NotifyLoaded() { | 777 void TemplateURLService::NotifyLoaded() { |
| 777 NotificationService::current()->Notify( | 778 NotificationService::current()->Notify( |
| 778 NotificationType::TEMPLATE_URL_SERVICE_LOADED, | 779 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
| 779 Source<TemplateURLService>(this), | 780 Source<TemplateURLService>(this), |
| 780 NotificationService::NoDetails()); | 781 NotificationService::NoDetails()); |
| 781 | 782 |
| 782 for (size_t i = 0; i < pending_extension_ids_.size(); ++i) { | 783 for (size_t i = 0; i < pending_extension_ids_.size(); ++i) { |
| 783 const Extension* extension = profile_->GetExtensionService()-> | 784 const Extension* extension = profile_->GetExtensionService()-> |
| 784 GetExtensionById(pending_extension_ids_[i], true); | 785 GetExtensionById(pending_extension_ids_[i], true); |
| 785 if (extension) | 786 if (extension) |
| 786 RegisterExtensionKeyword(extension); | 787 RegisterExtensionKeyword(extension); |
| 787 } | 788 } |
| 788 pending_extension_ids_.clear(); | 789 pending_extension_ids_.clear(); |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1259 // Remove it from the vector containing all TemplateURLs. | 1260 // Remove it from the vector containing all TemplateURLs. |
| 1260 template_urls_.erase(i); | 1261 template_urls_.erase(i); |
| 1261 | 1262 |
| 1262 if (service_.get()) | 1263 if (service_.get()) |
| 1263 service_->RemoveKeyword(*template_url); | 1264 service_->RemoveKeyword(*template_url); |
| 1264 | 1265 |
| 1265 if (profile_) { | 1266 if (profile_) { |
| 1266 Source<Profile> source(profile_); | 1267 Source<Profile> source(profile_); |
| 1267 TemplateURLID id = template_url->id(); | 1268 TemplateURLID id = template_url->id(); |
| 1268 NotificationService::current()->Notify( | 1269 NotificationService::current()->Notify( |
| 1269 NotificationType::TEMPLATE_URL_REMOVED, | 1270 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
| 1270 source, | 1271 source, |
| 1271 Details<TemplateURLID>(&id)); | 1272 Details<TemplateURLID>(&id)); |
| 1272 } | 1273 } |
| 1273 | 1274 |
| 1274 // We own the TemplateURL and need to delete it. | 1275 // We own the TemplateURL and need to delete it. |
| 1275 delete template_url; | 1276 delete template_url; |
| 1276 } | 1277 } |
| 1277 | 1278 |
| 1278 void TemplateURLService::NotifyObservers() { | 1279 void TemplateURLService::NotifyObservers() { |
| 1279 if (!loaded_) | 1280 if (!loaded_) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1322 | 1323 |
| 1323 i = template_urls->erase(i); | 1324 i = template_urls->erase(i); |
| 1324 if (service_.get()) | 1325 if (service_.get()) |
| 1325 service_->RemoveKeyword(*template_url); | 1326 service_->RemoveKeyword(*template_url); |
| 1326 delete template_url; | 1327 delete template_url; |
| 1327 } else { | 1328 } else { |
| 1328 ++i; | 1329 ++i; |
| 1329 } | 1330 } |
| 1330 } | 1331 } |
| 1331 } | 1332 } |
| OLD | NEW |