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