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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
45 #include "chrome/common/url_constants.h" | 45 #include "chrome/common/url_constants.h" |
46 #include "content/public/browser/notification_service.h" | 46 #include "content/public/browser/notification_service.h" |
47 #include "extensions/common/constants.h" | 47 #include "extensions/common/constants.h" |
48 #include "net/base/net_util.h" | 48 #include "net/base/net_util.h" |
49 #include "sync/api/sync_change.h" | 49 #include "sync/api/sync_change.h" |
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 #include "url/gurl.h" |
54 | 55 |
55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; | 56 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; |
56 typedef TemplateURLService::SyncDataMap SyncDataMap; | 57 typedef TemplateURLService::SyncDataMap SyncDataMap; |
57 | 58 |
58 namespace { | 59 namespace { |
59 | 60 |
60 const char kFirstPotentialEngineHistogramName[] = | 61 const char kFirstPotentialEngineHistogramName[] = |
61 "Search.FirstPotentialEngineCalled"; | 62 "Search.FirstPotentialEngineCalled"; |
62 | 63 |
63 // Values for an enumerated histogram used to track whenever | 64 // Values for an enumerated histogram used to track whenever |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 load_handle_(0), | 259 load_handle_(0), |
259 default_search_provider_(NULL), | 260 default_search_provider_(NULL), |
260 is_default_search_managed_(false), | 261 is_default_search_managed_(false), |
261 next_id_(kInvalidTemplateURLID + 1), | 262 next_id_(kInvalidTemplateURLID + 1), |
262 time_provider_(&base::Time::Now), | 263 time_provider_(&base::Time::Now), |
263 models_associated_(false), | 264 models_associated_(false), |
264 processing_syncer_changes_(false), | 265 processing_syncer_changes_(false), |
265 pending_synced_default_search_(false), | 266 pending_synced_default_search_(false), |
266 dsp_change_origin_(DSP_CHANGE_OTHER), | 267 dsp_change_origin_(DSP_CHANGE_OTHER), |
267 default_search_manager_( | 268 default_search_manager_( |
268 new DefaultSearchManager(GetPrefs(), | 269 GetPrefs(), |
269 DefaultSearchManager::ObserverCallback())) { | 270 DefaultSearchManager::ObserverCallback()) { |
270 DCHECK(profile_); | 271 DCHECK(profile_); |
271 Init(NULL, 0); | 272 Init(NULL, 0); |
272 } | 273 } |
273 | 274 |
274 TemplateURLService::TemplateURLService(const Initializer* initializers, | 275 TemplateURLService::TemplateURLService(const Initializer* initializers, |
275 const int count) | 276 const int count) |
276 : provider_map_(new SearchHostToURLsMap), | 277 : provider_map_(new SearchHostToURLsMap), |
277 profile_(NULL), | 278 profile_(NULL), |
278 loaded_(false), | 279 loaded_(false), |
279 load_failed_(false), | 280 load_failed_(false), |
280 load_handle_(0), | 281 load_handle_(0), |
281 service_(NULL), | 282 service_(NULL), |
282 default_search_provider_(NULL), | 283 default_search_provider_(NULL), |
283 is_default_search_managed_(false), | 284 is_default_search_managed_(false), |
284 next_id_(kInvalidTemplateURLID + 1), | 285 next_id_(kInvalidTemplateURLID + 1), |
285 time_provider_(&base::Time::Now), | 286 time_provider_(&base::Time::Now), |
286 models_associated_(false), | 287 models_associated_(false), |
287 processing_syncer_changes_(false), | 288 processing_syncer_changes_(false), |
288 pending_synced_default_search_(false), | 289 pending_synced_default_search_(false), |
289 dsp_change_origin_(DSP_CHANGE_OTHER) { | 290 dsp_change_origin_(DSP_CHANGE_OTHER), |
| 291 default_search_manager_( |
| 292 GetPrefs(), |
| 293 DefaultSearchManager::ObserverCallback()) { |
290 Init(initializers, count); | 294 Init(initializers, count); |
291 } | 295 } |
292 | 296 |
293 TemplateURLService::~TemplateURLService() { | 297 TemplateURLService::~TemplateURLService() { |
294 // |service_| should be deleted during Shutdown(). | 298 // |service_| should be deleted during Shutdown(). |
295 DCHECK(!service_); | 299 DCHECK(!service_); |
296 STLDeleteElements(&template_urls_); | 300 STLDeleteElements(&template_urls_); |
297 } | 301 } |
298 | 302 |
299 // static | 303 // static |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 // be replaced. We do this to ensure that if the user assigns a different | 569 // be replaced. We do this to ensure that if the user assigns a different |
566 // keyword to a generated TemplateURL, we won't regenerate another keyword for | 570 // keyword to a generated TemplateURL, we won't regenerate another keyword for |
567 // the same host. | 571 // the same host. |
568 return !url.is_valid() || url.host().empty() || | 572 return !url.is_valid() || url.host().empty() || |
569 CanReplaceKeywordForHost(url.host(), template_url_to_replace); | 573 CanReplaceKeywordForHost(url.host(), template_url_to_replace); |
570 } | 574 } |
571 | 575 |
572 void TemplateURLService::FindMatchingKeywords( | 576 void TemplateURLService::FindMatchingKeywords( |
573 const base::string16& prefix, | 577 const base::string16& prefix, |
574 bool support_replacement_only, | 578 bool support_replacement_only, |
575 TemplateURLVector* matches) const { | 579 TemplateURLVector* matches) { |
576 // Sanity check args. | 580 // Sanity check args. |
577 if (prefix.empty()) | 581 if (prefix.empty()) |
578 return; | 582 return; |
579 DCHECK(matches != NULL); | 583 DCHECK(matches != NULL); |
580 DCHECK(matches->empty()); // The code for exact matches assumes this. | 584 DCHECK(matches->empty()); // The code for exact matches assumes this. |
581 | 585 |
582 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | 586 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair |
583 TemplateURL* const kNullTemplateURL = NULL; | 587 TemplateURL* const kNullTemplateURL = NULL; |
584 | 588 |
585 // Find matching keyword range. Searches the element map for keywords | 589 // Find matching keyword range. Searches the element map for keywords |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 return !is_default_search_managed() && | 822 return !is_default_search_managed() && |
819 !IsExtensionControlledDefaultSearch() && | 823 !IsExtensionControlledDefaultSearch() && |
820 (url != GetDefaultSearchProvider()) && | 824 (url != GetDefaultSearchProvider()) && |
821 url->url_ref().SupportsReplacement() && | 825 url->url_ref().SupportsReplacement() && |
822 (url->GetType() == TemplateURL::NORMAL); | 826 (url->GetType() == TemplateURL::NORMAL); |
823 } | 827 } |
824 | 828 |
825 void TemplateURLService::SetUserSelectedDefaultSearchProvider( | 829 void TemplateURLService::SetUserSelectedDefaultSearchProvider( |
826 TemplateURL* url) { | 830 TemplateURL* url) { |
827 SetDefaultSearchProvider(url); | 831 SetDefaultSearchProvider(url); |
828 if (default_search_manager_) { | 832 if (url) |
829 if (url) | 833 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data()); |
830 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); | 834 else |
831 else | 835 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
832 default_search_manager_->ClearUserSelectedDefaultSearchEngine(); | |
833 } | |
834 } | 836 } |
835 | 837 |
836 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 838 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
837 return (loaded_ && !load_failed_) ? | 839 return (loaded_ && !load_failed_) ? |
838 default_search_provider_ : initial_default_search_provider_.get(); | 840 default_search_provider_ : initial_default_search_provider_.get(); |
839 } | 841 } |
840 | 842 |
841 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( | 843 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( |
842 const GURL& url) { | 844 const GURL& url) { |
843 TemplateURL* default_provider = GetDefaultSearchProvider(); | 845 TemplateURL* default_provider = GetDefaultSearchProvider(); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 data.SetKeyword(updated_keyword); | 1178 data.SetKeyword(updated_keyword); |
1177 TemplateURL new_turl(existing_turl->profile(), data); | 1179 TemplateURL new_turl(existing_turl->profile(), data); |
1178 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); | 1180 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); |
1179 if (UpdateNoNotify(existing_turl, new_turl, search_terms_data)) | 1181 if (UpdateNoNotify(existing_turl, new_turl, search_terms_data)) |
1180 NotifyObservers(); | 1182 NotifyObservers(); |
1181 | 1183 |
1182 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); | 1184 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); |
1183 new_changes.push_back(syncer::SyncChange(FROM_HERE, | 1185 new_changes.push_back(syncer::SyncChange(FROM_HERE, |
1184 syncer::SyncChange::ACTION_ADD, | 1186 syncer::SyncChange::ACTION_ADD, |
1185 sync_data)); | 1187 sync_data)); |
1186 // Ignore the delete attempt. This means we never end up reseting the | 1188 // Ignore the delete attempt. This means we never end up resetting the |
1187 // default search provider due to an ACTION_DELETE from sync. | 1189 // default search provider due to an ACTION_DELETE from sync. |
1188 continue; | 1190 continue; |
1189 } | 1191 } |
1190 | 1192 |
1191 Remove(existing_turl); | 1193 Remove(existing_turl); |
1192 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { | 1194 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { |
1193 if (existing_turl) { | 1195 if (existing_turl) { |
1194 error = sync_error_factory_->CreateAndUploadError( | 1196 error = sync_error_factory_->CreateAndUploadError( |
1195 FROM_HERE, | 1197 FROM_HERE, |
1196 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); | 1198 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); |
(...skipping 1474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2671 // Don't log anything if the user has a NULL default search provider. | 2673 // Don't log anything if the user has a NULL default search provider. |
2672 if (default_search_provider_) { | 2674 if (default_search_provider_) { |
2673 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType", | 2675 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType", |
2674 TemplateURLPrepopulateData::GetEngineType(*default_search_provider_), | 2676 TemplateURLPrepopulateData::GetEngineType(*default_search_provider_), |
2675 SEARCH_ENGINE_MAX); | 2677 SEARCH_ENGINE_MAX); |
2676 } | 2678 } |
2677 } | 2679 } |
2678 } | 2680 } |
2679 | 2681 |
2680 TemplateURL* TemplateURLService::CreateTemplateURLForExtension( | 2682 TemplateURL* TemplateURLService::CreateTemplateURLForExtension( |
2681 const ExtensionKeyword& extension_keyword) const { | 2683 const ExtensionKeyword& extension_keyword) { |
2682 TemplateURLData data; | 2684 TemplateURLData data; |
2683 data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name); | 2685 data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name); |
2684 data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword)); | 2686 data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword)); |
2685 // This URL is not actually used for navigation. It holds the extension's | 2687 // This URL is not actually used for navigation. It holds the extension's |
2686 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 2688 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
2687 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + | 2689 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + |
2688 extension_keyword.extension_id + "/?q={searchTerms}"); | 2690 extension_keyword.extension_id + "/?q={searchTerms}"); |
2689 return new TemplateURL(profile_, data); | 2691 return new TemplateURL(profile_, data); |
2690 } | 2692 } |
2691 | 2693 |
2692 TemplateURL* TemplateURLService::FindTemplateURLForExtension( | 2694 TemplateURL* TemplateURLService::FindTemplateURLForExtension( |
2693 const std::string& extension_id, | 2695 const std::string& extension_id, |
2694 TemplateURL::Type type) const { | 2696 TemplateURL::Type type) { |
2695 DCHECK_NE(TemplateURL::NORMAL, type); | 2697 DCHECK_NE(TemplateURL::NORMAL, type); |
2696 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2698 for (TemplateURLVector::const_iterator i = template_urls_.begin(); |
2697 i != template_urls_.end(); ++i) { | 2699 i != template_urls_.end(); ++i) { |
2698 if ((*i)->GetType() == type && | 2700 if ((*i)->GetType() == type && |
2699 (*i)->GetExtensionId() == extension_id) | 2701 (*i)->GetExtensionId() == extension_id) |
2700 return *i; | 2702 return *i; |
2701 } | 2703 } |
2702 | 2704 |
2703 return NULL; | 2705 return NULL; |
2704 } | 2706 } |
(...skipping 30 matching lines...) Expand all Loading... |
2735 new_dse = *i; | 2737 new_dse = *i; |
2736 break; | 2738 break; |
2737 } | 2739 } |
2738 } | 2740 } |
2739 } | 2741 } |
2740 } | 2742 } |
2741 if (!new_dse) | 2743 if (!new_dse) |
2742 new_dse = FindNewDefaultSearchProvider(); | 2744 new_dse = FindNewDefaultSearchProvider(); |
2743 SetDefaultSearchProviderNoNotify(new_dse); | 2745 SetDefaultSearchProviderNoNotify(new_dse); |
2744 } | 2746 } |
OLD | NEW |