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()) | |
MAD
2013/11/11 16:38:32
add {} when condition is more than 1 line.
engedy
2013/11/13 00:40:44
Hmm. Other code in this directory seems to followi
| |
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 return; | |
151 } | |
152 | |
153 brandcoded_config_fetcher_.reset(new BrandcodeConfigFetcher( | |
154 base::Bind( | |
155 &AutomaticProfileResetterDelegateImpl::OnBrandcodedDefaultsFetched, | |
156 base::Unretained(this)), | |
157 GURL("https://tools.google.com/service/update2"), | |
158 brandcode)); | |
159 } | |
160 | |
161 void AutomaticProfileResetterDelegateImpl:: | |
162 RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
163 const base::Closure& ready_callback) const { | |
164 DCHECK(!ready_callback.is_null()); | |
165 brandcoded_defaults_fetched_event_.Post(FROM_HERE, ready_callback); | |
166 } | |
167 | |
123 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | 168 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: |
124 GetLoadedModuleNameDigests() const { | 169 GetLoadedModuleNameDigests() const { |
125 DCHECK(modules_have_been_enumerated_event_.is_signaled()); | 170 DCHECK(modules_have_been_enumerated_event_.is_signaled()); |
126 std::set<std::string> module_name_digests; | 171 std::set<std::string> module_name_digests; |
127 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
128 if (module_list_) | 173 if (module_list_) |
129 ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); | 174 ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); |
130 #endif | 175 #endif |
131 scoped_ptr<base::ListValue> result(new base::ListValue); | 176 scoped_ptr<base::ListValue> result(new base::ListValue); |
132 for (std::set<std::string>::const_iterator it = module_name_digests.begin(); | 177 for (std::set<std::string>::const_iterator it = module_name_digests.begin(); |
(...skipping 21 matching lines...) Expand all Loading... | |
154 | 199 |
155 bool AutomaticProfileResetterDelegateImpl:: | 200 bool AutomaticProfileResetterDelegateImpl:: |
156 IsDefaultSearchProviderManaged() const { | 201 IsDefaultSearchProviderManaged() const { |
157 DCHECK(template_url_service_); | 202 DCHECK(template_url_service_); |
158 DCHECK(template_url_service_->loaded()); | 203 DCHECK(template_url_service_->loaded()); |
159 return template_url_service_->is_default_search_managed(); | 204 return template_url_service_->is_default_search_managed(); |
160 } | 205 } |
161 | 206 |
162 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | 207 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: |
163 GetPrepopulatedSearchProvidersDetails() const { | 208 GetPrepopulatedSearchProvidersDetails() const { |
164 DCHECK(template_url_service_); | |
165 size_t default_search_index = 0; | 209 size_t default_search_index = 0; |
166 ScopedVector<TemplateURL> engines( | 210 ScopedVector<TemplateURL> engines( |
167 TemplateURLPrepopulateData::GetPrepopulatedEngines( | 211 TemplateURLPrepopulateData::GetPrepopulatedEngines( |
168 template_url_service_->profile(), &default_search_index)); | 212 template_url_service_->profile(), &default_search_index)); |
169 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); | 213 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); |
170 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); | 214 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); |
171 it != engines.end(); ++it) | 215 it != engines.end(); ++it) |
172 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); | 216 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); |
173 return engines_details_list.Pass(); | 217 return engines_details_list.Pass(); |
174 } | 218 } |
175 | 219 |
176 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { | 220 bool AutomaticProfileResetterDelegateImpl::ShowPrompt() { |
177 // TODO(engedy): Call the UI from here once we have it. | 221 DCHECK(global_error_service_); |
222 | |
223 if (!ProfileResetGlobalError::IsSupportedOnPlatform()) | |
224 return false; | |
225 | |
226 ProfileResetGlobalError* global_error = new ProfileResetGlobalError(profile_); | |
227 global_error_service_->AddGlobalError(global_error); | |
228 | |
229 // Do not try to show bubble if another GlobalError is already showing one. | |
230 const GlobalErrorService::GlobalErrorList& global_errors( | |
231 global_error_service_->errors()); | |
232 GlobalErrorService::GlobalErrorList::const_iterator it; | |
233 for (it = global_errors.begin(); it != global_errors.end(); ++it) { | |
234 if ((*it)->GetBubbleView()) | |
235 break; | |
236 } | |
237 if (it == global_errors.end()) { | |
238 Browser* browser = chrome::FindTabbedBrowser( | |
239 profile_, | |
240 false /*match_original_profiles*/, | |
241 chrome::GetActiveDesktop()); | |
242 if (browser) | |
243 global_error->ShowBubbleView(browser); | |
244 } | |
245 return true; | |
246 } | |
247 | |
248 void AutomaticProfileResetterDelegateImpl::TriggerProfileSettingsReset( | |
249 bool send_feedback, | |
250 const base::Closure& completion) { | |
251 DCHECK(!profile_resetter_); | |
252 DCHECK(!completion.is_null()); | |
253 | |
254 profile_resetter_.reset(new ProfileResetter(profile_)); | |
255 FetchBrandcodedDefaultSettingsIfNeeded(); | |
256 RequestCallbackWhenBrandcodedDefaultsAreFetched(base::Bind( | |
257 &AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset, | |
258 AsWeakPtr(), | |
259 send_feedback, | |
260 completion)); | |
178 } | 261 } |
179 | 262 |
180 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { | 263 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { |
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 264 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
182 DCHECK(template_url_service_); | 265 DCHECK(template_url_service_); |
183 if (template_url_service_->loaded() && | 266 if (template_url_service_->loaded() && |
184 !template_url_service_ready_event_.is_signaled()) | 267 !template_url_service_ready_event_.is_signaled()) |
185 template_url_service_ready_event_.Signal(); | 268 template_url_service_ready_event_.Signal(); |
186 } | 269 } |
187 | 270 |
271 void AutomaticProfileResetterDelegateImpl::DismissPrompt() { | |
272 DCHECK(global_error_service_); | |
273 GlobalError* global_error = | |
274 global_error_service_->GetGlobalErrorByMenuItemCommandID( | |
275 IDC_SHOW_SETTINGS_RESET_BUBBLE); | |
276 if (global_error) { | |
277 // This will also close/destroy the Bubble UI if it is currently shown. | |
278 global_error_service_->RemoveGlobalError(global_error); | |
279 delete global_error; | |
280 } | |
281 } | |
282 | |
188 void AutomaticProfileResetterDelegateImpl::Observe( | 283 void AutomaticProfileResetterDelegateImpl::Observe( |
189 int type, | 284 int type, |
190 const content::NotificationSource& source, | 285 const content::NotificationSource& source, |
191 const content::NotificationDetails& details) { | 286 const content::NotificationDetails& details) { |
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 287 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
193 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && | 288 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && |
194 !modules_have_been_enumerated_event_.is_signaled()) { | 289 !modules_have_been_enumerated_event_.is_signaled()) { |
195 #if defined(OS_WIN) | 290 #if defined(OS_WIN) |
196 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | 291 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
197 #endif | 292 #endif |
198 modules_have_been_enumerated_event_.Signal(); | 293 modules_have_been_enumerated_event_.Signal(); |
199 } | 294 } |
200 } | 295 } |
296 | |
297 void AutomaticProfileResetterDelegateImpl::SendFeedback( | |
298 const std::string& report) const { | |
299 SendSettingsFeedback(report, profile_, PROFILE_RESET_PROMPT); | |
300 } | |
301 | |
302 void AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset( | |
303 bool send_feedback, | |
304 const base::Closure& completion) { | |
305 DCHECK(brandcoded_defaults_); | |
306 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot( | |
307 send_feedback ? new ResettableSettingsSnapshot(profile_) : NULL); | |
308 profile_resetter_->Reset( | |
309 resettable_aspects_, | |
310 brandcoded_defaults_.Pass(), | |
311 base::Bind(&AutomaticProfileResetterDelegateImpl:: | |
312 OnProfileSettingsResetCompleted, | |
313 AsWeakPtr(), | |
314 completion, | |
315 base::Passed(&old_settings_snapshot))); | |
316 } | |
317 | |
318 void AutomaticProfileResetterDelegateImpl:: | |
319 OnBrandcodedDefaultsFetched() { | |
320 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
321 DCHECK(brandcoded_config_fetcher_); | |
322 DCHECK(!brandcoded_config_fetcher_->IsActive()); | |
323 brandcoded_defaults_ = brandcoded_config_fetcher_->GetSettings(); | |
324 if (!brandcoded_defaults_) | |
325 brandcoded_defaults_.reset(new BrandcodedDefaultSettings); | |
326 brandcoded_defaults_fetched_event_.Signal(); | |
327 } | |
328 | |
329 void AutomaticProfileResetterDelegateImpl::OnProfileSettingsResetCompleted( | |
330 const base::Closure& user_callback, | |
331 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot) { | |
332 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
333 if (old_settings_snapshot) { | |
334 ResettableSettingsSnapshot new_settings_snapshot(profile_); | |
335 int difference = | |
336 old_settings_snapshot->FindDifferentFields(new_settings_snapshot); | |
337 if (difference) { | |
338 old_settings_snapshot->Subtract(new_settings_snapshot); | |
339 std::string report = | |
340 SerializeSettingsReport(*old_settings_snapshot, difference); | |
341 SendFeedback(report); | |
342 } | |
343 } | |
344 content::BrowserThread::PostTask( | |
345 content::BrowserThread::UI, FROM_HERE, user_callback); | |
346 } | |
OLD | NEW |