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()) { |
Peter Kasting
2014/05/08 17:36:43
Nit: Can this be moved onto the previous line? (2
erikwright (departed)
2014/05/08 18:24:20
Done.
| |
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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
802 return !is_default_search_managed() && | 806 return !is_default_search_managed() && |
803 !IsExtensionControlledDefaultSearch() && | 807 !IsExtensionControlledDefaultSearch() && |
804 (url != GetDefaultSearchProvider()) && | 808 (url != GetDefaultSearchProvider()) && |
805 url->url_ref().SupportsReplacement() && | 809 url->url_ref().SupportsReplacement() && |
806 (url->GetType() == TemplateURL::NORMAL); | 810 (url->GetType() == TemplateURL::NORMAL); |
807 } | 811 } |
808 | 812 |
809 void TemplateURLService::SetUserSelectedDefaultSearchProvider( | 813 void TemplateURLService::SetUserSelectedDefaultSearchProvider( |
810 TemplateURL* url) { | 814 TemplateURL* url) { |
811 SetDefaultSearchProvider(url); | 815 SetDefaultSearchProvider(url); |
812 if (default_search_manager_) { | 816 if (url) |
813 if (url) | 817 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data()); |
814 default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); | 818 else |
815 else | 819 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
816 default_search_manager_->ClearUserSelectedDefaultSearchEngine(); | |
817 } | |
818 } | 820 } |
819 | 821 |
820 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 822 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
821 return (loaded_ && !load_failed_) ? | 823 return (loaded_ && !load_failed_) ? |
822 default_search_provider_ : initial_default_search_provider_.get(); | 824 default_search_provider_ : initial_default_search_provider_.get(); |
823 } | 825 } |
824 | 826 |
825 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( | 827 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( |
826 const GURL& url) { | 828 const GURL& url) { |
827 TemplateURL* default_provider = GetDefaultSearchProvider(); | 829 TemplateURL* default_provider = GetDefaultSearchProvider(); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1160 data.SetKeyword(updated_keyword); | 1162 data.SetKeyword(updated_keyword); |
1161 TemplateURL new_turl(existing_turl->profile(), data); | 1163 TemplateURL new_turl(existing_turl->profile(), data); |
1162 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); | 1164 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); |
1163 if (UpdateNoNotify(existing_turl, new_turl, search_terms_data)) | 1165 if (UpdateNoNotify(existing_turl, new_turl, search_terms_data)) |
1164 NotifyObservers(); | 1166 NotifyObservers(); |
1165 | 1167 |
1166 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); | 1168 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); |
1167 new_changes.push_back(syncer::SyncChange(FROM_HERE, | 1169 new_changes.push_back(syncer::SyncChange(FROM_HERE, |
1168 syncer::SyncChange::ACTION_ADD, | 1170 syncer::SyncChange::ACTION_ADD, |
1169 sync_data)); | 1171 sync_data)); |
1170 // Ignore the delete attempt. This means we never end up reseting the | 1172 // Ignore the delete attempt. This means we never end up resetting the |
1171 // default search provider due to an ACTION_DELETE from sync. | 1173 // default search provider due to an ACTION_DELETE from sync. |
1172 continue; | 1174 continue; |
1173 } | 1175 } |
1174 | 1176 |
1175 Remove(existing_turl); | 1177 Remove(existing_turl); |
1176 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { | 1178 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { |
1177 if (existing_turl) { | 1179 if (existing_turl) { |
1178 error = sync_error_factory_->CreateAndUploadError( | 1180 error = sync_error_factory_->CreateAndUploadError( |
1179 FROM_HERE, | 1181 FROM_HERE, |
1180 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); | 1182 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); |
(...skipping 1498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2679 // Don't log anything if the user has a NULL default search provider. | 2681 // Don't log anything if the user has a NULL default search provider. |
2680 if (default_search_provider_) { | 2682 if (default_search_provider_) { |
2681 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType", | 2683 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType", |
2682 TemplateURLPrepopulateData::GetEngineType(*default_search_provider_), | 2684 TemplateURLPrepopulateData::GetEngineType(*default_search_provider_), |
2683 SEARCH_ENGINE_MAX); | 2685 SEARCH_ENGINE_MAX); |
2684 } | 2686 } |
2685 } | 2687 } |
2686 } | 2688 } |
2687 | 2689 |
2688 TemplateURL* TemplateURLService::CreateTemplateURLForExtension( | 2690 TemplateURL* TemplateURLService::CreateTemplateURLForExtension( |
2689 const ExtensionKeyword& extension_keyword) const { | 2691 const ExtensionKeyword& extension_keyword) { |
2690 TemplateURLData data; | 2692 TemplateURLData data; |
2691 data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name); | 2693 data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name); |
2692 data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword)); | 2694 data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword)); |
2693 // This URL is not actually used for navigation. It holds the extension's | 2695 // This URL is not actually used for navigation. It holds the extension's |
2694 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 2696 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
2695 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + | 2697 data.SetURL(std::string(extensions::kExtensionScheme) + "://" + |
2696 extension_keyword.extension_id + "/?q={searchTerms}"); | 2698 extension_keyword.extension_id + "/?q={searchTerms}"); |
2697 return new TemplateURL(profile_, data); | 2699 return new TemplateURL(profile_, data); |
2698 } | 2700 } |
2699 | 2701 |
2700 TemplateURL* TemplateURLService::FindTemplateURLForExtension( | 2702 TemplateURL* TemplateURLService::FindTemplateURLForExtension( |
2701 const std::string& extension_id, | 2703 const std::string& extension_id, |
2702 TemplateURL::Type type) const { | 2704 TemplateURL::Type type) { |
2703 DCHECK_NE(TemplateURL::NORMAL, type); | 2705 DCHECK_NE(TemplateURL::NORMAL, type); |
2704 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2706 for (TemplateURLVector::const_iterator i = template_urls_.begin(); |
2705 i != template_urls_.end(); ++i) { | 2707 i != template_urls_.end(); ++i) { |
2706 if ((*i)->GetType() == type && | 2708 if ((*i)->GetType() == type && |
2707 (*i)->GetExtensionId() == extension_id) | 2709 (*i)->GetExtensionId() == extension_id) |
2708 return *i; | 2710 return *i; |
2709 } | 2711 } |
2710 | 2712 |
2711 return NULL; | 2713 return NULL; |
2712 } | 2714 } |
(...skipping 30 matching lines...) Expand all Loading... | |
2743 new_dse = *i; | 2745 new_dse = *i; |
2744 break; | 2746 break; |
2745 } | 2747 } |
2746 } | 2748 } |
2747 } | 2749 } |
2748 } | 2750 } |
2749 if (!new_dse) | 2751 if (!new_dse) |
2750 new_dse = FindNewDefaultSearchProvider(); | 2752 new_dse = FindNewDefaultSearchProvider(); |
2751 SetDefaultSearchProviderNoNotify(new_dse); | 2753 SetDefaultSearchProviderNoNotify(new_dse); |
2752 } | 2754 } |
OLD | NEW |