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

Side by Side Diff: chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc

Issue 62193002: Integrate UI and reset logic into AutomaticProfileResetter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests, some other smaller changes. Created 7 years, 1 month 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
OLDNEW
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
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
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 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
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 void AutomaticProfileResetterDelegateImpl::ShowPrompt() {
177 // TODO(engedy): Call the UI from here once we have it. 221 DCHECK(global_error_service_);
222 ProfileResetGlobalError* global_error = new ProfileResetGlobalError(profile_);
223 global_error_service_->AddGlobalError(global_error);
224
225 // Do not try to show bubble if another GlobalError is already showing one.
226 const GlobalErrorService::GlobalErrorList& global_errors(
227 global_error_service_->errors());
228 for (GlobalErrorService::GlobalErrorList::const_iterator
229 it = global_errors.begin(); it != global_errors.end(); ++it) {
230 if ((*it)->GetBubbleView())
231 return;
232 }
233 Browser* browser = chrome::FindTabbedBrowser(
234 profile_, false /*match_original_profiles*/, chrome::GetActiveDesktop());
235 if (browser)
236 global_error->ShowBubbleView(browser);
237 }
238
239 void AutomaticProfileResetterDelegateImpl::TriggerProfileSettingsReset(
240 bool send_feedback,
241 const base::Closure& completion) {
242 DCHECK(!profile_resetter_);
243 DCHECK(!completion.is_null());
244
245 profile_resetter_.reset(new ProfileResetter(profile_));
246 FetchBrandcodedDefaultSettingsIfNeeded();
247 RequestCallbackWhenBrandcodedDefaultsAreFetched(base::Bind(
248 &AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset,
249 AsWeakPtr(),
250 send_feedback,
251 completion));
178 } 252 }
179 253
180 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { 254 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() {
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 255 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
182 DCHECK(template_url_service_); 256 DCHECK(template_url_service_);
183 if (template_url_service_->loaded() && 257 if (template_url_service_->loaded() &&
184 !template_url_service_ready_event_.is_signaled()) 258 !template_url_service_ready_event_.is_signaled())
185 template_url_service_ready_event_.Signal(); 259 template_url_service_ready_event_.Signal();
186 } 260 }
187 261
262 void AutomaticProfileResetterDelegateImpl::DismissPrompt() {
263 DCHECK(global_error_service_);
264 GlobalError* global_error =
265 global_error_service_->GetGlobalErrorByMenuItemCommandID(
266 IDC_SHOW_SETTINGS_RESET_BUBBLE);
267 if (global_error) {
268 // This will also close/destroy the Bubble UI if it is currently shown.
269 global_error_service_->RemoveGlobalError(global_error);
270 delete global_error;
271 }
272 }
273
188 void AutomaticProfileResetterDelegateImpl::Observe( 274 void AutomaticProfileResetterDelegateImpl::Observe(
189 int type, 275 int type,
190 const content::NotificationSource& source, 276 const content::NotificationSource& source,
191 const content::NotificationDetails& details) { 277 const content::NotificationDetails& details) {
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && 279 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED &&
194 !modules_have_been_enumerated_event_.is_signaled()) { 280 !modules_have_been_enumerated_event_.is_signaled()) {
195 #if defined(OS_WIN) 281 #if defined(OS_WIN)
196 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); 282 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList());
197 #endif 283 #endif
198 modules_have_been_enumerated_event_.Signal(); 284 modules_have_been_enumerated_event_.Signal();
199 } 285 }
200 } 286 }
287
288 void AutomaticProfileResetterDelegateImpl::SendFeedback(
289 const std::string& report) const {
290 SendSettingsFeedback(report, profile_, PROFILE_RESET_PROMPT);
291 }
292
293 void AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset(
294 bool send_feedback,
295 const base::Closure& completion) {
296 DCHECK(brandcoded_defaults_);
297 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot(
298 send_feedback ? new ResettableSettingsSnapshot(profile_) : NULL);
299 profile_resetter_->Reset(
300 resettable_aspects_,
301 brandcoded_defaults_.Pass(),
302 base::Bind(&AutomaticProfileResetterDelegateImpl::
303 OnProfileSettingsResetCompleted,
304 AsWeakPtr(),
305 completion,
306 base::Passed(&old_settings_snapshot)));
307 }
308
309 void AutomaticProfileResetterDelegateImpl::
310 OnBrandcodedDefaultsFetched() {
311 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
312 DCHECK(brandcoded_config_fetcher_);
313 DCHECK(!brandcoded_config_fetcher_->IsActive());
314 brandcoded_defaults_ = brandcoded_config_fetcher_->GetSettings();
315 if (!brandcoded_defaults_)
316 brandcoded_defaults_.reset(new BrandcodedDefaultSettings);
317 brandcoded_defaults_fetched_event_.Signal();
318 }
319
320 void AutomaticProfileResetterDelegateImpl::OnProfileSettingsResetCompleted(
321 const base::Closure& user_callback,
322 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot) {
323 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
324 if (old_settings_snapshot) {
325 ResettableSettingsSnapshot new_settings_snapshot(profile_);
326 int difference =
327 old_settings_snapshot->FindDifferentFields(new_settings_snapshot);
328 if (difference) {
329 old_settings_snapshot->Subtract(new_settings_snapshot);
330 std::string report =
331 SerializeSettingsReport(*old_settings_snapshot, difference);
332 SendFeedback(report);
333 }
334 }
335 content::BrowserThread::PostTask(
336 content::BrowserThread::UI, FROM_HERE, user_callback);
337 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698