Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/protector/default_search_provider_change.cc

Issue 8704007: Protector adds the default prepopulated engine if it was removed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/protector/histograms.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
9 #include "chrome/browser/protector/base_setting_change.h" 10 #include "chrome/browser/protector/base_setting_change.h"
10 #include "chrome/browser/protector/histograms.h" 11 #include "chrome/browser/protector/histograms.h"
11 #include "chrome/browser/protector/protector.h" 12 #include "chrome/browser/protector/protector.h"
12 #include "chrome/browser/search_engines/template_url.h" 13 #include "chrome/browser/search_engines/template_url.h"
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
13 #include "chrome/browser/search_engines/template_url_service.h" 15 #include "chrome/browser/search_engines/template_url_service.h"
14 #include "chrome/browser/search_engines/template_url_service_observer.h" 16 #include "chrome/browser/search_engines/template_url_service_observer.h"
15 #include "chrome/browser/webdata/keyword_table.h" 17 #include "chrome/browser/webdata/keyword_table.h"
16 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
17 #include "googleurl/src/gurl.h" 19 #include "googleurl/src/gurl.h"
18 #include "grit/chromium_strings.h" 20 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
21 23
22 namespace protector { 24 namespace protector {
23 25
24 namespace { 26 namespace {
25 27
26 // Maximum length of the search engine name to be displayed. 28 // Maximum length of the search engine name to be displayed.
27 const size_t kMaxDisplayedNameLength = 10; 29 const size_t kMaxDisplayedNameLength = 10;
28 30
31 // Predicate that matches a TemplateURL with given ID.
32 class TemplateURLHasId {
33 public:
34 explicit TemplateURLHasId(TemplateURLID id) : id_(id) {
35 }
36
37 bool operator()(const TemplateURL* url) {
38 return url->id() == id_;
39 }
40
41 private:
42 TemplateURLID id_;
43 };
44
45 // Matches TemplateURL with all fields set from the prepopulated data equal
46 // to fields in another TemplateURL.
47 class TemplateURLIsSame {
48 public:
49 // Creates a matcher based on |other|.
50 explicit TemplateURLIsSame(const TemplateURL* other) : other_(other) {
51 }
52
53 // Returns true if both |other| and |url| are NULL or have same field values.
54 bool operator()(const TemplateURL* url) {
55 if (url == other_ )
56 return true;
57 if (!url || !other_)
58 return false;
59 return url->short_name() == other_->short_name() &&
60 AreKeywordsSame(url, other_) &&
61 TemplateURLRef::SameUrlRefs(url->url(), other_->url()) &&
62 TemplateURLRef::SameUrlRefs(url->suggestions_url(),
63 other_->suggestions_url()) &&
64 TemplateURLRef::SameUrlRefs(url->instant_url(),
65 other_->instant_url()) &&
66 url->GetFaviconURL() == other_->GetFaviconURL() &&
67 url->safe_for_autoreplace() == other_->safe_for_autoreplace() &&
68 url->show_in_default_list() == other_->show_in_default_list() &&
69 url->input_encodings() == other_->input_encodings() &&
70 url->logo_id() == other_->logo_id() &&
71 url->prepopulate_id() == other_->prepopulate_id();
72 }
73
74 private:
75 // Returns true if both |url1| and |url2| have autogenerated keywords
76 // or if their keywords are identical.
77 bool AreKeywordsSame(const TemplateURL* url1, const TemplateURL* url2) {
78 return (url1->autogenerate_keyword() && url2->autogenerate_keyword()) ||
79 url1->keyword() == url2->keyword();
80 }
81
82 const TemplateURL* other_;
83 };
84
29 } // namespace 85 } // namespace
30 86
31 class DefaultSearchProviderChange : public BaseSettingChange, 87 class DefaultSearchProviderChange : public BaseSettingChange,
32 public TemplateURLServiceObserver { 88 public TemplateURLServiceObserver {
33 public: 89 public:
34 DefaultSearchProviderChange(const TemplateURL* old_url, 90 DefaultSearchProviderChange(const TemplateURL* old_url,
35 const TemplateURL* new_url); 91 const TemplateURL* new_url);
36 92
37 // BaseSettingChange overrides: 93 // BaseSettingChange overrides:
38 virtual bool Init(Protector* protector) OVERRIDE; 94 virtual bool Init(Protector* protector) OVERRIDE;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 UMA_HISTOGRAM_ENUMERATION( 164 UMA_HISTOGRAM_ENUMERATION(
109 kProtectorHistogramNewSearchProvider, 165 kProtectorHistogramNewSearchProvider,
110 new_histogram_id_, 166 new_histogram_id_,
111 kProtectorMaxSearchProviderID); 167 kProtectorMaxSearchProviderID);
112 168
113 // Initially reset the search engine to its previous setting. 169 // Initially reset the search engine to its previous setting.
114 default_search_provider_ = SetDefaultSearchProvider(old_id_, true); 170 default_search_provider_ = SetDefaultSearchProvider(old_id_, true);
115 if (!default_search_provider_) 171 if (!default_search_provider_)
116 return false; 172 return false;
117 173
174 int restored_histogram_id =
175 GetSearchProviderHistogramID(default_search_provider_);
176 UMA_HISTOGRAM_ENUMERATION(
177 kProtectorHistogramSearchProviderRestored,
178 restored_histogram_id,
179 kProtectorMaxSearchProviderID);
180
118 if (!old_id_ || default_search_provider_->id() != old_id_) { 181 if (!old_id_ || default_search_provider_->id() != old_id_) {
119 // Old settings is lost or invalid, so we had to fall back to one of the 182 // Old settings is lost or invalid, so we had to fall back to one of the
120 // prepopulated search engines. 183 // prepopulated search engines.
121 fallback_id_ = default_search_provider_->id(); 184 fallback_id_ = default_search_provider_->id();
122 fallback_name_ = default_search_provider_->short_name(); 185 fallback_name_ = default_search_provider_->short_name();
123 VLOG(1) << "Fallback to " << fallback_name_; 186
187 VLOG(1) << "Fallback to search provider: " << fallback_name_;
188 UMA_HISTOGRAM_ENUMERATION(
189 kProtectorHistogramSearchProviderFallback,
190 restored_histogram_id,
191 kProtectorMaxSearchProviderID);
124 } 192 }
125 193
126 // This must be called after the initial |SetDefaultSearchProvider| call
127 // because the latter will remove the observer.
128 protector->GetTemplateURLService()->AddObserver(this); 194 protector->GetTemplateURLService()->AddObserver(this);
129 195
130 return true; 196 return true;
131 } 197 }
132 198
133 void DefaultSearchProviderChange::Apply() { 199 void DefaultSearchProviderChange::Apply() {
134 UMA_HISTOGRAM_ENUMERATION( 200 UMA_HISTOGRAM_ENUMERATION(
135 kProtectorHistogramSearchProviderApplied, 201 kProtectorHistogramSearchProviderApplied,
136 new_histogram_id_, 202 new_histogram_id_,
137 kProtectorMaxSearchProviderID); 203 kProtectorMaxSearchProviderID);
138 204
205 protector()->GetTemplateURLService()->RemoveObserver(this);
139 if (!new_id_) { 206 if (!new_id_) {
140 // Open settings page in case the new setting is invalid. 207 // Open settings page in case the new setting is invalid.
141 OpenSearchEngineSettings(); 208 OpenSearchEngineSettings();
142 } else { 209 } else {
143 SetDefaultSearchProvider(new_id_, false); 210 SetDefaultSearchProvider(new_id_, false);
144 } 211 }
145 } 212 }
146 213
147 void DefaultSearchProviderChange::Discard() { 214 void DefaultSearchProviderChange::Discard() {
148 UMA_HISTOGRAM_ENUMERATION( 215 UMA_HISTOGRAM_ENUMERATION(
149 kProtectorHistogramSearchProviderDiscarded, 216 kProtectorHistogramSearchProviderDiscarded,
150 new_histogram_id_, 217 new_histogram_id_,
151 kProtectorMaxSearchProviderID); 218 kProtectorMaxSearchProviderID);
152 219
220 protector()->GetTemplateURLService()->RemoveObserver(this);
153 if (!old_id_) { 221 if (!old_id_) {
154 // Open settings page in case the old setting is invalid. 222 // Open settings page in case the old setting is invalid.
155 OpenSearchEngineSettings(); 223 OpenSearchEngineSettings();
156 } 224 }
157 // Nothing to do otherwise since we have already set the search engine 225 // Nothing to do otherwise since we have already set the search engine
158 // to |old_id_| in |Init|. 226 // to |old_id_| in |Init|.
159 } 227 }
160 228
161 void DefaultSearchProviderChange::Timeout() { 229 void DefaultSearchProviderChange::Timeout() {
162 UMA_HISTOGRAM_ENUMERATION( 230 UMA_HISTOGRAM_ENUMERATION(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); 275 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING);
208 else 276 else
209 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); 277 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_);
210 } else { 278 } else {
211 // Old setting is lost, offer to go to settings. 279 // Old setting is lost, offer to go to settings.
212 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); 280 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
213 } 281 }
214 } 282 }
215 283
216 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() { 284 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() {
217 if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() != 285 TemplateURLService* url_service = protector()->GetTemplateURLService();
218 default_search_provider_) { 286 if (url_service->GetDefaultSearchProvider() != default_search_provider_) {
287 VLOG(1) << "Default search provider has been changed by user";
219 default_search_provider_ = NULL; 288 default_search_provider_ = NULL;
220 VLOG(1) << "Default search provider has been changed by user"; 289 url_service->RemoveObserver(this);
221 // This will delete the Protector instance and |this|. 290 // This will delete the Protector instance and |this|.
222 protector()->DismissChange(); 291 protector()->DismissChange();
223 } 292 }
224 } 293 }
225 294
226 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( 295 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
227 int64 id, 296 int64 id,
228 bool allow_fallback) { 297 bool allow_fallback) {
229 TemplateURLService* url_service = protector()->GetTemplateURLService(); 298 TemplateURLService* url_service = protector()->GetTemplateURLService();
230 if (!url_service) { 299 if (!url_service) {
231 NOTREACHED() << "Can't get TemplateURLService object."; 300 NOTREACHED() << "Can't get TemplateURLService object.";
232 return NULL; 301 return NULL;
233 } 302 }
303
304 TemplateURLService::TemplateURLVector urls = url_service->GetTemplateURLs();
234 const TemplateURL* url = NULL; 305 const TemplateURL* url = NULL;
235 if (id) { 306 if (id) {
236 const TemplateURLService::TemplateURLVector& urls = 307 TemplateURLService::TemplateURLVector::const_iterator i =
237 url_service->GetTemplateURLs(); 308 find_if(urls.begin(), urls.end(), TemplateURLHasId(id));
238 for (size_t i = 0; i < urls.size(); ++i) { 309 if (i != urls.end())
239 if (urls[i]->id() == id) { 310 url = *i;
240 url = urls[i];
241 break;
242 }
243 }
244 } 311 }
245 if (!url && allow_fallback) { 312 if (!url && allow_fallback) {
246 url = url_service->FindNewDefaultSearchProvider(); 313 // Fallback to the prepopulated default search provider.
247 DCHECK(url); 314 scoped_ptr<TemplateURL> new_url(
315 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(
316 NULL // Ignore any overrides in prefs.
317 ));
318 DCHECK(new_url.get());
319 VLOG(1) << "Prepopulated search provider: " << new_url->short_name();
320
321 // Check if this provider already exists and add it otherwise.
322 TemplateURLService::TemplateURLVector::const_iterator i =
323 find_if(urls.begin(), urls.end(), TemplateURLIsSame(new_url.get()));
324 if (i != urls.end()) {
325 VLOG(1) << "Provider already exists";
326 url = *i;
327 } else {
328 VLOG(1) << "No match, adding new provider";
329 url = new_url.get();
330 url_service->Add(new_url.release());
331 UMA_HISTOGRAM_ENUMERATION(
332 kProtectorHistogramSearchProviderMissing,
333 GetSearchProviderHistogramID(url),
334 kProtectorMaxSearchProviderID);
335 }
336 // TODO(ivankr): handle keyword conflicts with existing providers.
248 } 337 }
338
249 if (url) { 339 if (url) {
250 // Remove ourselves from the observer list to prevent from catching our own 340 VLOG(1) << "Default search provider set to: " << url->short_name();
251 // change. It is safe to do this multiple times or before adding ourselves.
252 url_service->RemoveObserver(this);
253 url_service->SetDefaultSearchProvider(url); 341 url_service->SetDefaultSearchProvider(url);
254 VLOG(1) << "Default search provider set to: " << url->short_name();
255 // No need to re-add observer again because any further changes to the
256 // default search provider are of no interest.
257 } 342 }
258 return url; 343 return url;
259 } 344 }
260 345
261 void DefaultSearchProviderChange::OpenSearchEngineSettings() { 346 void DefaultSearchProviderChange::OpenSearchEngineSettings() {
262 protector()->OpenTab( 347 protector()->OpenTab(
263 GURL(std::string(chrome::kChromeUISettingsURL) + 348 GURL(std::string(chrome::kChromeUISettingsURL) +
264 chrome::kSearchEnginesSubPage)); 349 chrome::kSearchEnginesSubPage));
265 } 350 }
266 351
267 BaseSettingChange* CreateDefaultSearchProviderChange( 352 BaseSettingChange* CreateDefaultSearchProviderChange(
268 const TemplateURL* actual, 353 const TemplateURL* actual,
269 const TemplateURL* backup) { 354 const TemplateURL* backup) {
270 return new DefaultSearchProviderChange(backup, actual); 355 return new DefaultSearchProviderChange(backup, actual);
271 } 356 }
272 357
273 } // namespace protector 358 } // namespace protector
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/protector/histograms.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698