| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/profile_resetter/automatic_profile_resetter_delegate.h" | 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 11 #include "base/callback.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/md5.h" | 13 #include "base/md5.h" |
| 12 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/app/chrome_command_ids.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/google/google_util.h" |
| 21 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h" |
| 22 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" |
| 23 #include "chrome/browser/profile_resetter/profile_resetter.h" |
| 24 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 26 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 19 #include "chrome/browser/search_engines/template_url_service.h" | 27 #include "chrome/browser/search_engines/template_url_service.h" |
| 20 #include "chrome/browser/search_engines/template_url_service_factory.h" | 28 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 29 #include "chrome/browser/ui/browser.h" |
| 30 #include "chrome/browser/ui/browser_finder.h" |
| 31 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 32 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 21 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 23 | 35 |
| 24 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
| 25 #include "chrome/browser/enumerate_modules_model_win.h" | 37 #include "chrome/browser/enumerate_modules_model_win.h" |
| 26 #endif | 38 #endif |
| 27 | 39 |
| 28 namespace { | 40 namespace { |
| 29 | 41 |
| 30 scoped_ptr<base::DictionaryValue> BuildSubTreeFromTemplateURL( | 42 scoped_ptr<base::DictionaryValue> BuildSubTreeFromTemplateURL( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 100 } |
| 89 } | 101 } |
| 90 #endif | 102 #endif |
| 91 | 103 |
| 92 } // namespace | 104 } // namespace |
| 93 | 105 |
| 94 | 106 |
| 95 // AutomaticProfileResetterDelegateImpl -------------------------------------- | 107 // AutomaticProfileResetterDelegateImpl -------------------------------------- |
| 96 | 108 |
| 97 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( | 109 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( |
| 98 TemplateURLService* template_url_service) | 110 Profile* profile, |
| 99 : template_url_service_(template_url_service) { | 111 ProfileResetter::ResettableFlags resettable_aspects) |
| 112 : profile_(profile), |
| 113 global_error_service_(GlobalErrorServiceFactory::GetForProfile(profile_)), |
| 114 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
| 115 resettable_aspects_(resettable_aspects) { |
| 116 DCHECK(profile_); |
| 100 if (template_url_service_) { | 117 if (template_url_service_) { |
| 101 template_url_service_->AddObserver(this); | 118 template_url_service_->AddObserver(this); |
| 102 // Needed so that |template_url_service_ready_event_| will be signaled even | 119 // Needed so that |template_url_service_ready_event_| will be signaled even |
| 103 // when TemplateURLService had been already initialized before this point. | 120 // when TemplateURLService had been already initialized before this point. |
| 104 OnTemplateURLServiceChanged(); | 121 OnTemplateURLServiceChanged(); |
| 105 } | 122 } |
| 106 | 123 |
| 107 #if defined(OS_WIN) | 124 #if defined(OS_WIN) |
| 108 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | 125 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| 109 #endif | 126 #endif |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 template_url_service_->Load(); // Safe to call even if it has loaded already. | 161 template_url_service_->Load(); // Safe to call even if it has loaded already. |
| 145 } | 162 } |
| 146 | 163 |
| 147 void AutomaticProfileResetterDelegateImpl:: | 164 void AutomaticProfileResetterDelegateImpl:: |
| 148 RequestCallbackWhenTemplateURLServiceIsLoaded( | 165 RequestCallbackWhenTemplateURLServiceIsLoaded( |
| 149 const base::Closure& ready_callback) const { | 166 const base::Closure& ready_callback) const { |
| 150 DCHECK(!ready_callback.is_null()); | 167 DCHECK(!ready_callback.is_null()); |
| 151 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); | 168 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); |
| 152 } | 169 } |
| 153 | 170 |
| 171 void AutomaticProfileResetterDelegateImpl:: |
| 172 FetchBrandcodedDefaultSettingsIfNeeded() { |
| 173 if (brandcoded_config_fetcher_ || |
| 174 brandcoded_defaults_fetched_event_.is_signaled()) |
| 175 return; |
| 176 |
| 177 std::string brandcode; |
| 178 google_util::GetBrand(&brandcode); |
| 179 if (brandcode.empty()) { |
| 180 brandcoded_defaults_.reset(new BrandcodedDefaultSettings); |
| 181 brandcoded_defaults_fetched_event_.Signal(); |
| 182 } else { |
| 183 brandcoded_config_fetcher_.reset(new BrandcodeConfigFetcher( |
| 184 base::Bind( |
| 185 &AutomaticProfileResetterDelegateImpl::OnBrandcodedDefaultsFetched, |
| 186 base::Unretained(this)), |
| 187 GURL("https://tools.google.com/service/update2"), |
| 188 brandcode)); |
| 189 } |
| 190 } |
| 191 |
| 192 void AutomaticProfileResetterDelegateImpl:: |
| 193 RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 194 const base::Closure& ready_callback) const { |
| 195 DCHECK(!ready_callback.is_null()); |
| 196 brandcoded_defaults_fetched_event_.Post(FROM_HERE, ready_callback); |
| 197 } |
| 198 |
| 154 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | 199 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: |
| 155 GetLoadedModuleNameDigests() const { | 200 GetLoadedModuleNameDigests() const { |
| 156 DCHECK(modules_have_been_enumerated_event_.is_signaled()); | 201 DCHECK(modules_have_been_enumerated_event_.is_signaled()); |
| 157 scoped_ptr<base::ListValue> result(new base::ListValue); | 202 scoped_ptr<base::ListValue> result(new base::ListValue); |
| 158 #if defined(OS_WIN) | 203 #if defined(OS_WIN) |
| 159 if (module_list_) | 204 if (module_list_) |
| 160 ExtractLoadedModuleNameDigests(*module_list_, result.get()); | 205 ExtractLoadedModuleNameDigests(*module_list_, result.get()); |
| 161 #endif | 206 #endif |
| 162 return result.Pass(); | 207 return result.Pass(); |
| 163 } | 208 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 181 | 226 |
| 182 bool AutomaticProfileResetterDelegateImpl:: | 227 bool AutomaticProfileResetterDelegateImpl:: |
| 183 IsDefaultSearchProviderManaged() const { | 228 IsDefaultSearchProviderManaged() const { |
| 184 DCHECK(template_url_service_); | 229 DCHECK(template_url_service_); |
| 185 DCHECK(template_url_service_->loaded()); | 230 DCHECK(template_url_service_->loaded()); |
| 186 return template_url_service_->is_default_search_managed(); | 231 return template_url_service_->is_default_search_managed(); |
| 187 } | 232 } |
| 188 | 233 |
| 189 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | 234 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: |
| 190 GetPrepopulatedSearchProvidersDetails() const { | 235 GetPrepopulatedSearchProvidersDetails() const { |
| 191 DCHECK(template_url_service_); | |
| 192 size_t default_search_index = 0; | 236 size_t default_search_index = 0; |
| 193 ScopedVector<TemplateURL> engines( | 237 ScopedVector<TemplateURL> engines( |
| 194 TemplateURLPrepopulateData::GetPrepopulatedEngines( | 238 TemplateURLPrepopulateData::GetPrepopulatedEngines( |
| 195 template_url_service_->profile(), &default_search_index)); | 239 template_url_service_->profile(), &default_search_index)); |
| 196 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); | 240 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); |
| 197 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); | 241 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); |
| 198 it != engines.end(); ++it) | 242 it != engines.end(); ++it) |
| 199 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); | 243 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); |
| 200 return engines_details_list.Pass(); | 244 return engines_details_list.Pass(); |
| 201 } | 245 } |
| 202 | 246 |
| 203 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { | 247 bool AutomaticProfileResetterDelegateImpl::TriggerPrompt() { |
| 204 // TODO(engedy): Call the UI from here once we have it. | 248 DCHECK(global_error_service_); |
| 249 |
| 250 if (!ProfileResetGlobalError::IsSupportedOnPlatform()) |
| 251 return false; |
| 252 |
| 253 ProfileResetGlobalError* global_error = new ProfileResetGlobalError(profile_); |
| 254 global_error_service_->AddGlobalError(global_error); |
| 255 |
| 256 // Do not try to show bubble if another GlobalError is already showing one. |
| 257 const GlobalErrorService::GlobalErrorList& global_errors( |
| 258 global_error_service_->errors()); |
| 259 GlobalErrorService::GlobalErrorList::const_iterator it; |
| 260 for (it = global_errors.begin(); it != global_errors.end(); ++it) { |
| 261 if ((*it)->GetBubbleView()) |
| 262 break; |
| 263 } |
| 264 if (it == global_errors.end()) { |
| 265 Browser* browser = chrome::FindTabbedBrowser( |
| 266 profile_, |
| 267 false /*match_original_profiles*/, |
| 268 chrome::GetActiveDesktop()); |
| 269 if (browser) |
| 270 global_error->ShowBubbleView(browser); |
| 271 } |
| 272 return true; |
| 273 } |
| 274 |
| 275 void AutomaticProfileResetterDelegateImpl::TriggerProfileSettingsReset( |
| 276 bool send_feedback, |
| 277 const base::Closure& completion) { |
| 278 DCHECK(!profile_resetter_); |
| 279 DCHECK(!completion.is_null()); |
| 280 |
| 281 profile_resetter_.reset(new ProfileResetter(profile_)); |
| 282 FetchBrandcodedDefaultSettingsIfNeeded(); |
| 283 RequestCallbackWhenBrandcodedDefaultsAreFetched(base::Bind( |
| 284 &AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset, |
| 285 AsWeakPtr(), |
| 286 send_feedback, |
| 287 completion)); |
| 205 } | 288 } |
| 206 | 289 |
| 207 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { | 290 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { |
| 208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 291 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 209 DCHECK(template_url_service_); | 292 DCHECK(template_url_service_); |
| 210 if (template_url_service_->loaded() && | 293 if (template_url_service_->loaded() && |
| 211 !template_url_service_ready_event_.is_signaled()) | 294 !template_url_service_ready_event_.is_signaled()) |
| 212 template_url_service_ready_event_.Signal(); | 295 template_url_service_ready_event_.Signal(); |
| 213 } | 296 } |
| 214 | 297 |
| 298 void AutomaticProfileResetterDelegateImpl::DismissPrompt() { |
| 299 DCHECK(global_error_service_); |
| 300 GlobalError* global_error = |
| 301 global_error_service_->GetGlobalErrorByMenuItemCommandID( |
| 302 IDC_SHOW_SETTINGS_RESET_BUBBLE); |
| 303 if (global_error) { |
| 304 // This will also close/destroy the Bubble UI if it is currently shown. |
| 305 global_error_service_->RemoveGlobalError(global_error); |
| 306 delete global_error; |
| 307 } |
| 308 } |
| 309 |
| 215 void AutomaticProfileResetterDelegateImpl::Observe( | 310 void AutomaticProfileResetterDelegateImpl::Observe( |
| 216 int type, | 311 int type, |
| 217 const content::NotificationSource& source, | 312 const content::NotificationSource& source, |
| 218 const content::NotificationDetails& details) { | 313 const content::NotificationDetails& details) { |
| 219 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 314 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 220 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && | 315 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && |
| 221 !modules_have_been_enumerated_event_.is_signaled()) { | 316 !modules_have_been_enumerated_event_.is_signaled()) { |
| 222 #if defined(OS_WIN) | 317 #if defined(OS_WIN) |
| 223 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | 318 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| 224 #endif | 319 #endif |
| 225 modules_have_been_enumerated_event_.Signal(); | 320 modules_have_been_enumerated_event_.Signal(); |
| 226 } | 321 } |
| 227 } | 322 } |
| 323 |
| 324 void AutomaticProfileResetterDelegateImpl::SendFeedback( |
| 325 const std::string& report) const { |
| 326 SendSettingsFeedback(report, profile_, PROFILE_RESET_PROMPT); |
| 327 } |
| 328 |
| 329 void AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset( |
| 330 bool send_feedback, |
| 331 const base::Closure& completion) { |
| 332 DCHECK(brandcoded_defaults_); |
| 333 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot( |
| 334 send_feedback ? new ResettableSettingsSnapshot(profile_) : NULL); |
| 335 profile_resetter_->Reset( |
| 336 resettable_aspects_, |
| 337 brandcoded_defaults_.Pass(), |
| 338 base::Bind(&AutomaticProfileResetterDelegateImpl:: |
| 339 OnProfileSettingsResetCompleted, |
| 340 AsWeakPtr(), |
| 341 completion, |
| 342 base::Passed(&old_settings_snapshot))); |
| 343 } |
| 344 |
| 345 void AutomaticProfileResetterDelegateImpl:: |
| 346 OnBrandcodedDefaultsFetched() { |
| 347 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 348 DCHECK(brandcoded_config_fetcher_); |
| 349 DCHECK(!brandcoded_config_fetcher_->IsActive()); |
| 350 brandcoded_defaults_ = brandcoded_config_fetcher_->GetSettings(); |
| 351 if (!brandcoded_defaults_) |
| 352 brandcoded_defaults_.reset(new BrandcodedDefaultSettings); |
| 353 brandcoded_defaults_fetched_event_.Signal(); |
| 354 } |
| 355 |
| 356 void AutomaticProfileResetterDelegateImpl::OnProfileSettingsResetCompleted( |
| 357 const base::Closure& user_callback, |
| 358 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot) { |
| 359 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 360 if (old_settings_snapshot) { |
| 361 ResettableSettingsSnapshot new_settings_snapshot(profile_); |
| 362 int difference = |
| 363 old_settings_snapshot->FindDifferentFields(new_settings_snapshot); |
| 364 if (difference) { |
| 365 old_settings_snapshot->Subtract(new_settings_snapshot); |
| 366 std::string report = |
| 367 SerializeSettingsReport(*old_settings_snapshot, difference); |
| 368 SendFeedback(report); |
| 369 } |
| 370 } |
| 371 content::BrowserThread::PostTask( |
| 372 content::BrowserThread::UI, FROM_HERE, user_callback); |
| 373 } |
| OLD | NEW |