| 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 "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/environment.h" | 10 #include "base/environment.h" |
| 11 #include "base/guid.h" | 11 #include "base/guid.h" |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/google/google_url_tracker.h" | 22 #include "chrome/browser/google/google_url_tracker.h" |
| 23 #include "chrome/browser/google/google_util.h" |
| 23 #include "chrome/browser/history/history.h" | 24 #include "chrome/browser/history/history.h" |
| 24 #include "chrome/browser/history/history_notifications.h" | 25 #include "chrome/browser/history/history_notifications.h" |
| 25 #include "chrome/browser/history/history_service_factory.h" | 26 #include "chrome/browser/history/history_service_factory.h" |
| 26 #include "chrome/browser/net/url_fixer_upper.h" | 27 #include "chrome/browser/net/url_fixer_upper.h" |
| 27 #include "chrome/browser/prefs/pref_service.h" | 28 #include "chrome/browser/prefs/pref_service.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/protector/base_setting_change.h" | 30 #include "chrome/browser/protector/base_setting_change.h" |
| 30 #include "chrome/browser/protector/protector_service.h" | 31 #include "chrome/browser/protector/protector_service.h" |
| 31 #include "chrome/browser/protector/protector_service_factory.h" | 32 #include "chrome/browser/protector/protector_service_factory.h" |
| 32 #include "chrome/browser/protector/protector_utils.h" | 33 #include "chrome/browser/protector/protector_utils.h" |
| 33 #include "chrome/browser/rlz/rlz.h" | 34 #include "chrome/browser/rlz/rlz.h" |
| 34 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 35 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
| 35 #include "chrome/browser/search_engines/search_terms_data.h" | 36 #include "chrome/browser/search_engines/search_terms_data.h" |
| 36 #include "chrome/browser/search_engines/template_url.h" | 37 #include "chrome/browser/search_engines/template_url.h" |
| 37 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 38 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 38 #include "chrome/browser/search_engines/template_url_service_observer.h" | 39 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 39 #include "chrome/browser/search_engines/util.h" | 40 #include "chrome/browser/search_engines/util.h" |
| 41 #include "chrome/browser/ui/search/search.h" |
| 40 #include "chrome/browser/webdata/web_data_service_factory.h" | 42 #include "chrome/browser/webdata/web_data_service_factory.h" |
| 41 #include "chrome/common/chrome_notification_types.h" | 43 #include "chrome/common/chrome_notification_types.h" |
| 42 #include "chrome/common/chrome_switches.h" | 44 #include "chrome/common/chrome_switches.h" |
| 43 #include "chrome/common/env_vars.h" | 45 #include "chrome/common/env_vars.h" |
| 44 #include "chrome/common/extensions/extension.h" | 46 #include "chrome/common/extensions/extension.h" |
| 45 #include "chrome/common/pref_names.h" | 47 #include "chrome/common/pref_names.h" |
| 46 #include "chrome/common/url_constants.h" | 48 #include "chrome/common/url_constants.h" |
| 47 #include "content/public/browser/notification_service.h" | 49 #include "content/public/browser/notification_service.h" |
| 48 #include "extensions/common/constants.h" | 50 #include "extensions/common/constants.h" |
| 49 #include "net/base/net_util.h" | 51 #include "net/base/net_util.h" |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 LessWithPrefix())); | 433 LessWithPrefix())); |
| 432 | 434 |
| 433 // Return vector of matching keywords. | 435 // Return vector of matching keywords. |
| 434 for (KeywordToTemplateMap::const_iterator i(match_range.first); | 436 for (KeywordToTemplateMap::const_iterator i(match_range.first); |
| 435 i != match_range.second; ++i) { | 437 i != match_range.second; ++i) { |
| 436 if (!support_replacement_only || i->second->url_ref().SupportsReplacement()) | 438 if (!support_replacement_only || i->second->url_ref().SupportsReplacement()) |
| 437 matches->push_back(i->first); | 439 matches->push_back(i->first); |
| 438 } | 440 } |
| 439 } | 441 } |
| 440 | 442 |
| 443 string16 TemplateURLService::TryToExtractSearchTermsFromURL(const GURL& url) { |
| 444 TemplateURL *template_url = GetDefaultSearchProvider(); |
| 445 if (!template_url) |
| 446 return string16(); |
| 447 |
| 448 // Ensure instant extended API is enabled and query URL is HTTPS. |
| 449 if (!profile_ || !chrome::search::IsInstantExtendedAPIEnabled(profile_) || |
| 450 !url.SchemeIs(chrome::kHttpsScheme) || |
| 451 !google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) |
| 452 return string16(); |
| 453 |
| 454 string16 result; |
| 455 template_url->ExtractSearchTermsFromURL(url, &result); |
| 456 return result; |
| 457 } |
| 458 |
| 441 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( | 459 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( |
| 442 const string16& keyword) { | 460 const string16& keyword) { |
| 443 KeywordToTemplateMap::const_iterator elem( | 461 KeywordToTemplateMap::const_iterator elem( |
| 444 keyword_to_template_map_.find(keyword)); | 462 keyword_to_template_map_.find(keyword)); |
| 445 if (elem != keyword_to_template_map_.end()) | 463 if (elem != keyword_to_template_map_.end()) |
| 446 return elem->second; | 464 return elem->second; |
| 447 return ((!loaded_ || load_failed_) && | 465 return ((!loaded_ || load_failed_) && |
| 448 initial_default_search_provider_.get() && | 466 initial_default_search_provider_.get() && |
| 449 (initial_default_search_provider_->keyword() == keyword)) ? | 467 (initial_default_search_provider_->keyword() == keyword)) ? |
| 450 initial_default_search_provider_.get() : NULL; | 468 initial_default_search_provider_.get() : NULL; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 | 656 |
| 639 // Always persist the setting in the database, that way if the backup | 657 // Always persist the setting in the database, that way if the backup |
| 640 // signature has changed out from under us it gets reset correctly. | 658 // signature has changed out from under us it gets reset correctly. |
| 641 if (SetDefaultSearchProviderNoNotify(url)) | 659 if (SetDefaultSearchProviderNoNotify(url)) |
| 642 NotifyObservers(); | 660 NotifyObservers(); |
| 643 } | 661 } |
| 644 | 662 |
| 645 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 663 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
| 646 if (loaded_ && !load_failed_) | 664 if (loaded_ && !load_failed_) |
| 647 return default_search_provider_; | 665 return default_search_provider_; |
| 648 | |
| 649 // We're not loaded, rely on the default search provider stored in prefs. | 666 // We're not loaded, rely on the default search provider stored in prefs. |
| 650 return initial_default_search_provider_.get(); | 667 return initial_default_search_provider_.get(); |
| 651 } | 668 } |
| 652 | 669 |
| 653 TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { | 670 TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { |
| 654 // See if the prepopulated default still exists. | 671 // See if the prepopulated default still exists. |
| 655 scoped_ptr<TemplateURL> prepopulated_default( | 672 scoped_ptr<TemplateURL> prepopulated_default( |
| 656 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_)); | 673 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_)); |
| 657 for (TemplateURLVector::iterator i = template_urls_.begin(); | 674 for (TemplateURLVector::iterator i = template_urls_.begin(); |
| 658 i != template_urls_.end(); ++i) { | 675 i != template_urls_.end(); ++i) { |
| (...skipping 1904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2563 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2580 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2564 // should be persisted to disk and synced. | 2581 // should be persisted to disk and synced. |
| 2565 if (template_url->sync_guid().empty() && | 2582 if (template_url->sync_guid().empty() && |
| 2566 !template_url->IsExtensionKeyword()) { | 2583 !template_url->IsExtensionKeyword()) { |
| 2567 template_url->data_.sync_guid = base::GenerateGUID(); | 2584 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2568 if (service_.get()) | 2585 if (service_.get()) |
| 2569 service_->UpdateKeyword(template_url->data()); | 2586 service_->UpdateKeyword(template_url->data()); |
| 2570 } | 2587 } |
| 2571 } | 2588 } |
| 2572 } | 2589 } |
| OLD | NEW |