Chromium Code Reviews| 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 "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/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/browser/protector/base_setting_change.h" | 9 #include "chrome/browser/protector/base_setting_change.h" |
| 10 #include "chrome/browser/protector/histograms.h" | 10 #include "chrome/browser/protector/histograms.h" |
| 11 #include "chrome/browser/protector/protector.h" | 11 #include "chrome/browser/protector/protector.h" |
| 12 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
| 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
| 13 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_observer.h" | 15 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 15 #include "chrome/browser/webdata/keyword_table.h" | 16 #include "chrome/browser/webdata/keyword_table.h" |
| 16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 18 #include "grit/chromium_strings.h" | 19 #include "grit/chromium_strings.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 | 22 |
| 22 namespace protector { | 23 namespace protector { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Maximum length of the search engine name to be displayed. | 27 // Maximum length of the search engine name to be displayed. |
| 27 const size_t kMaxDisplayedNameLength = 10; | 28 const size_t kMaxDisplayedNameLength = 10; |
| 28 | 29 |
| 30 // Predicate that matches a TemplateURL with given ID. | |
| 31 class TemplateURLHasId { | |
| 32 public: | |
| 33 explicit TemplateURLHasId(int id) : id_(id) { | |
| 34 } | |
| 35 | |
| 36 bool operator()(const TemplateURL* url) { | |
| 37 return url->id() == id_; | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 int id_; | |
|
sky
2011/11/28 17:03:11
int -> TemplateURLID
Ivan Korotkov
2011/11/29 10:13:47
Done.
| |
| 42 }; | |
| 43 | |
| 44 // Matcher for TemplateURLs based on their search and instant URLs. | |
|
sky
2011/11/28 17:03:11
The code doesn't look at instant url.
Ivan Korotkov
2011/11/29 10:13:47
Hmm, I though that suggest_url is actually the ins
| |
| 45 class TemplateURLMatches { | |
| 46 public: | |
| 47 // Creates a matcher based on |other|. | |
| 48 explicit TemplateURLMatches(const TemplateURL* other) : other_(other) { | |
| 49 } | |
| 50 | |
| 51 // Returns true if both |other| and |url| are NULL or have the same search | |
| 52 // and instant URLs. | |
| 53 bool operator()(const TemplateURL* url) { | |
| 54 if (url == other_ ) | |
| 55 return true; | |
| 56 if (!url || !other_) | |
| 57 return false; | |
| 58 return TemplateURLRef::SameUrlRefs(url->url(), other_->url()) && | |
| 59 TemplateURLRef::SameUrlRefs(url->suggestions_url(), | |
| 60 other_->suggestions_url()); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 const TemplateURL* other_; | |
| 65 }; | |
| 66 | |
| 29 } // namespace | 67 } // namespace |
| 30 | 68 |
| 31 class DefaultSearchProviderChange : public BaseSettingChange, | 69 class DefaultSearchProviderChange : public BaseSettingChange, |
| 32 public TemplateURLServiceObserver { | 70 public TemplateURLServiceObserver { |
| 33 public: | 71 public: |
| 34 DefaultSearchProviderChange(const TemplateURL* old_url, | 72 DefaultSearchProviderChange(const TemplateURL* old_url, |
| 35 const TemplateURL* new_url); | 73 const TemplateURL* new_url); |
| 36 | 74 |
| 37 // BaseSettingChange overrides: | 75 // BaseSettingChange overrides: |
| 38 virtual bool Init(Protector* protector) OVERRIDE; | 76 virtual bool Init(Protector* protector) OVERRIDE; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 UMA_HISTOGRAM_ENUMERATION( | 146 UMA_HISTOGRAM_ENUMERATION( |
| 109 kProtectorHistogramNewSearchProvider, | 147 kProtectorHistogramNewSearchProvider, |
| 110 new_histogram_id_, | 148 new_histogram_id_, |
| 111 kProtectorMaxSearchProviderID); | 149 kProtectorMaxSearchProviderID); |
| 112 | 150 |
| 113 // Initially reset the search engine to its previous setting. | 151 // Initially reset the search engine to its previous setting. |
| 114 default_search_provider_ = SetDefaultSearchProvider(old_id_, true); | 152 default_search_provider_ = SetDefaultSearchProvider(old_id_, true); |
| 115 if (!default_search_provider_) | 153 if (!default_search_provider_) |
| 116 return false; | 154 return false; |
| 117 | 155 |
| 156 int restored_histogram_id = | |
| 157 GetSearchProviderHistogramID(default_search_provider_); | |
| 158 UMA_HISTOGRAM_ENUMERATION( | |
| 159 kProtectorHistogramSearchProviderRestored, | |
| 160 restored_histogram_id, | |
| 161 kProtectorMaxSearchProviderID); | |
| 162 | |
| 118 if (!old_id_ || default_search_provider_->id() != old_id_) { | 163 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 | 164 // Old settings is lost or invalid, so we had to fall back to one of the |
| 120 // prepopulated search engines. | 165 // prepopulated search engines. |
| 121 fallback_id_ = default_search_provider_->id(); | 166 fallback_id_ = default_search_provider_->id(); |
| 122 fallback_name_ = default_search_provider_->short_name(); | 167 fallback_name_ = default_search_provider_->short_name(); |
| 123 VLOG(1) << "Fallback to " << fallback_name_; | 168 |
| 169 VLOG(1) << "Fallback to search provider: " << fallback_name_; | |
| 170 UMA_HISTOGRAM_ENUMERATION( | |
| 171 kProtectorHistogramSearchProviderFallback, | |
| 172 restored_histogram_id, | |
| 173 kProtectorMaxSearchProviderID); | |
| 124 } | 174 } |
| 125 | 175 |
| 126 // This must be called after the initial |SetDefaultSearchProvider| call | |
| 127 // because the latter will remove the observer. | |
| 128 protector->GetTemplateURLService()->AddObserver(this); | 176 protector->GetTemplateURLService()->AddObserver(this); |
| 129 | 177 |
| 130 return true; | 178 return true; |
| 131 } | 179 } |
| 132 | 180 |
| 133 void DefaultSearchProviderChange::Apply() { | 181 void DefaultSearchProviderChange::Apply() { |
| 134 UMA_HISTOGRAM_ENUMERATION( | 182 UMA_HISTOGRAM_ENUMERATION( |
| 135 kProtectorHistogramSearchProviderApplied, | 183 kProtectorHistogramSearchProviderApplied, |
| 136 new_histogram_id_, | 184 new_histogram_id_, |
| 137 kProtectorMaxSearchProviderID); | 185 kProtectorMaxSearchProviderID); |
| 138 | 186 |
| 187 protector()->GetTemplateURLService()->RemoveObserver(this); | |
| 139 if (!new_id_) { | 188 if (!new_id_) { |
| 140 // Open settings page in case the new setting is invalid. | 189 // Open settings page in case the new setting is invalid. |
| 141 OpenSearchEngineSettings(); | 190 OpenSearchEngineSettings(); |
| 142 } else { | 191 } else { |
| 143 SetDefaultSearchProvider(new_id_, false); | 192 SetDefaultSearchProvider(new_id_, false); |
| 144 } | 193 } |
| 145 } | 194 } |
| 146 | 195 |
| 147 void DefaultSearchProviderChange::Discard() { | 196 void DefaultSearchProviderChange::Discard() { |
| 148 UMA_HISTOGRAM_ENUMERATION( | 197 UMA_HISTOGRAM_ENUMERATION( |
| 149 kProtectorHistogramSearchProviderDiscarded, | 198 kProtectorHistogramSearchProviderDiscarded, |
| 150 new_histogram_id_, | 199 new_histogram_id_, |
| 151 kProtectorMaxSearchProviderID); | 200 kProtectorMaxSearchProviderID); |
| 152 | 201 |
| 202 protector()->GetTemplateURLService()->RemoveObserver(this); | |
| 153 if (!old_id_) { | 203 if (!old_id_) { |
| 154 // Open settings page in case the old setting is invalid. | 204 // Open settings page in case the old setting is invalid. |
| 155 OpenSearchEngineSettings(); | 205 OpenSearchEngineSettings(); |
| 156 } | 206 } |
| 157 // Nothing to do otherwise since we have already set the search engine | 207 // Nothing to do otherwise since we have already set the search engine |
| 158 // to |old_id_| in |Init|. | 208 // to |old_id_| in |Init|. |
| 159 } | 209 } |
| 160 | 210 |
| 161 void DefaultSearchProviderChange::Timeout() { | 211 void DefaultSearchProviderChange::Timeout() { |
| 162 UMA_HISTOGRAM_ENUMERATION( | 212 UMA_HISTOGRAM_ENUMERATION( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); | 257 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); |
| 208 else | 258 else |
| 209 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); | 259 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); |
| 210 } else { | 260 } else { |
| 211 // Old setting is lost, offer to go to settings. | 261 // Old setting is lost, offer to go to settings. |
| 212 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); | 262 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); |
| 213 } | 263 } |
| 214 } | 264 } |
| 215 | 265 |
| 216 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() { | 266 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() { |
| 217 if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() != | 267 TemplateURLService* url_service = protector()->GetTemplateURLService(); |
| 218 default_search_provider_) { | 268 if (url_service->GetDefaultSearchProvider() != default_search_provider_) { |
| 269 VLOG(1) << "Default search provider has been changed by user"; | |
| 219 default_search_provider_ = NULL; | 270 default_search_provider_ = NULL; |
| 220 VLOG(1) << "Default search provider has been changed by user"; | 271 url_service->RemoveObserver(this); |
| 221 // This will delete the Protector instance and |this|. | 272 // This will delete the Protector instance and |this|. |
| 222 protector()->DismissChange(); | 273 protector()->DismissChange(); |
| 223 } | 274 } |
| 224 } | 275 } |
| 225 | 276 |
| 226 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( | 277 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( |
| 227 int64 id, | 278 int64 id, |
| 228 bool allow_fallback) { | 279 bool allow_fallback) { |
| 229 TemplateURLService* url_service = protector()->GetTemplateURLService(); | 280 TemplateURLService* url_service = protector()->GetTemplateURLService(); |
| 230 if (!url_service) { | 281 if (!url_service) { |
| 231 NOTREACHED() << "Can't get TemplateURLService object."; | 282 NOTREACHED() << "Can't get TemplateURLService object."; |
| 232 return NULL; | 283 return NULL; |
| 233 } | 284 } |
| 285 | |
| 286 TemplateURLService::TemplateURLVector urls = url_service->GetTemplateURLs(); | |
| 234 const TemplateURL* url = NULL; | 287 const TemplateURL* url = NULL; |
| 235 if (id) { | 288 if (id) { |
| 236 const TemplateURLService::TemplateURLVector& urls = | 289 TemplateURLService::TemplateURLVector::const_iterator i = |
| 237 url_service->GetTemplateURLs(); | 290 find_if(urls.begin(), urls.end(), TemplateURLHasId(id)); |
| 238 for (size_t i = 0; i < urls.size(); ++i) { | 291 if (i != urls.end()) |
| 239 if (urls[i]->id() == id) { | 292 url = *i; |
| 240 url = urls[i]; | 293 } |
| 241 break; | 294 if (!url && allow_fallback) { |
| 242 } | 295 // Fallback to the prepopulated default search provider. |
| 296 TemplateURL* new_url = | |
|
sky
2011/11/28 17:03:11
Put this in a scoped_ptr and use release so that i
Ivan Korotkov
2011/11/29 10:13:47
Done.
| |
| 297 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch( | |
| 298 NULL // Ignore any overrides in prefs. | |
| 299 ); | |
| 300 DCHECK(new_url); | |
| 301 VLOG(1) << "Prepopulated search provider: " << new_url->short_name(); | |
| 302 | |
| 303 // Check if a matching (see |TemplateURLMatches|) provider already exists | |
| 304 // and update it from this prepopulated template then. | |
| 305 TemplateURLService::TemplateURLVector::const_iterator i = | |
|
sky
2011/11/29 16:44:59
This worries me. What are we hoping to do with thi
Ivan Korotkov
2011/11/29 19:10:15
There are examples of malware that just remove the
| |
| 306 find_if(urls.begin(), urls.end(), TemplateURLMatches(new_url)); | |
| 307 if (i != urls.end()) { | |
| 308 VLOG(1) << "Provider already exists, updating"; | |
| 309 url = *i; | |
| 310 url_service->Update(url, *new_url); | |
| 311 delete new_url; | |
| 312 } else { | |
| 313 VLOG(1) << "No match, adding new provider"; | |
| 314 url = new_url; | |
| 315 url_service->Add(new_url); | |
| 316 UMA_HISTOGRAM_ENUMERATION( | |
| 317 kProtectorHistogramSearchProviderMissing, | |
| 318 GetSearchProviderHistogramID(url), | |
| 319 kProtectorMaxSearchProviderID); | |
| 243 } | 320 } |
| 244 } | 321 } |
| 245 if (!url && allow_fallback) { | 322 |
| 246 url = url_service->FindNewDefaultSearchProvider(); | |
| 247 DCHECK(url); | |
| 248 } | |
| 249 if (url) { | 323 if (url) { |
| 250 // Remove ourselves from the observer list to prevent from catching our own | 324 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); | 325 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 } | 326 } |
| 258 return url; | 327 return url; |
| 259 } | 328 } |
| 260 | 329 |
| 261 void DefaultSearchProviderChange::OpenSearchEngineSettings() { | 330 void DefaultSearchProviderChange::OpenSearchEngineSettings() { |
| 262 protector()->OpenTab( | 331 protector()->OpenTab( |
| 263 GURL(std::string(chrome::kChromeUISettingsURL) + | 332 GURL(std::string(chrome::kChromeUISettingsURL) + |
| 264 chrome::kSearchEnginesSubPage)); | 333 chrome::kSearchEnginesSubPage)); |
| 265 } | 334 } |
| 266 | 335 |
| 267 BaseSettingChange* CreateDefaultSearchProviderChange( | 336 BaseSettingChange* CreateDefaultSearchProviderChange( |
| 268 const TemplateURL* actual, | 337 const TemplateURL* actual, |
| 269 const TemplateURL* backup) { | 338 const TemplateURL* backup) { |
| 270 return new DefaultSearchProviderChange(backup, actual); | 339 return new DefaultSearchProviderChange(backup, actual); |
| 271 } | 340 } |
| 272 | 341 |
| 273 } // namespace protector | 342 } // namespace protector |
| OLD | NEW |