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

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: First draft. 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 : profile_(profile),
80 global_error_service_(GlobalErrorServiceFactory::GetForProfile(profile_)),
81 template_url_service_(
82 TemplateURLServiceFactory::GetForProfile(profile_)) {
83 DCHECK(profile_);
69 if (template_url_service_) { 84 if (template_url_service_) {
70 template_url_service_->AddObserver(this); 85 template_url_service_->AddObserver(this);
71 // Needed so that |template_url_service_ready_event_| will be signaled even 86 // Needed so that |template_url_service_ready_event_| will be signaled even
72 // when TemplateURLService had been already initialized before this point. 87 // when TemplateURLService had been already initialized before this point.
73 OnTemplateURLServiceChanged(); 88 OnTemplateURLServiceChanged();
74 } 89 }
75 90
76 #if defined(OS_WIN) 91 #if defined(OS_WIN)
77 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); 92 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList());
78 #endif 93 #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. 128 template_url_service_->Load(); // Safe to call even if it has loaded already.
114 } 129 }
115 130
116 void AutomaticProfileResetterDelegateImpl:: 131 void AutomaticProfileResetterDelegateImpl::
117 RequestCallbackWhenTemplateURLServiceIsLoaded( 132 RequestCallbackWhenTemplateURLServiceIsLoaded(
118 const base::Closure& ready_callback) const { 133 const base::Closure& ready_callback) const {
119 DCHECK(!ready_callback.is_null()); 134 DCHECK(!ready_callback.is_null());
120 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); 135 template_url_service_ready_event_.Post(FROM_HERE, ready_callback);
121 } 136 }
122 137
138 void AutomaticProfileResetterDelegateImpl::
139 FetchBrandcodedDefaultSettingsIfNeeded() {
140 if (brandcoded_config_fetcher_ ||
141 brandcoded_defaults_fetched_event_.is_signaled())
142 return;
143
144 std::string brandcode;
145 google_util::GetBrand(&brandcode);
146 if (brandcode.empty()) {
147 brandcoded_defaults_.reset(new BrandcodedDefaultSettings);
148 brandcoded_defaults_fetched_event_.Signal();
149 return;
150 }
151
152 brandcoded_config_fetcher_.reset(new BrandcodeConfigFetcher(
153 base::Bind(
154 &AutomaticProfileResetterDelegateImpl::OnBrandcodedDefaultsFetched,
155 base::Unretained(this)),
156 GURL("https://tools.google.com/service/update2"),
157 brandcode));
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
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 void AutomaticProfileResetterDelegateImpl::ShowPrompt() {
177 // TODO(engedy): Call the UI from here once we have it. 220 DCHECK(global_error_service_);
221 ProfileResetGlobalError* global_error = new ProfileResetGlobalError(profile_);
222 global_error_service_->AddGlobalError(global_error);
223
224 // Do not try to show bubble if another GlobalError is already showing one.
225 const GlobalErrorService::GlobalErrorList& global_errors(
226 global_error_service_->errors());
227 for (GlobalErrorService::GlobalErrorList::const_iterator
228 it = global_errors.begin(); it != global_errors.end(); ++it) {
229 if ((*it)->GetBubbleView())
230 return;
231 }
232 Browser* browser = chrome::FindTabbedBrowser(
233 profile_, false /*match_original_profiles*/, chrome::GetActiveDesktop());
234 if (browser)
235 global_error->ShowBubbleView(browser);
236 }
237
238 void AutomaticProfileResetterDelegateImpl::TriggerProfileSettingsReset(
239 bool send_feedback,
240 const base::Closure& completion) {
241 DCHECK(!profile_resetter_);
242 DCHECK(!completion.is_null());
243
244 profile_resetter_.reset(new ProfileResetter(profile_));
245 FetchBrandcodedDefaultSettingsIfNeeded();
246 RequestCallbackWhenBrandcodedDefaultsAreFetched(base::Bind(
247 &AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset,
248 AsWeakPtr(),
249 send_feedback,
250 completion));
178 } 251 }
179 252
180 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { 253 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() {
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
182 DCHECK(template_url_service_); 255 DCHECK(template_url_service_);
183 if (template_url_service_->loaded() && 256 if (template_url_service_->loaded() &&
184 !template_url_service_ready_event_.is_signaled()) 257 !template_url_service_ready_event_.is_signaled())
185 template_url_service_ready_event_.Signal(); 258 template_url_service_ready_event_.Signal();
186 } 259 }
187 260
261 void AutomaticProfileResetterDelegateImpl::DismissPrompt() {
262 DCHECK(global_error_service_);
263 GlobalError* global_error =
264 global_error_service_->GetGlobalErrorByMenuItemCommandID(
265 IDC_SHOW_SETTINGS_RESET_BUBBLE);
266 if (global_error) {
267 // This will also close/destroy the Bubble UI if it is currently shown.
268 global_error_service_->RemoveGlobalError(global_error);
269 delete global_error;
270 }
271 }
272
188 void AutomaticProfileResetterDelegateImpl::Observe( 273 void AutomaticProfileResetterDelegateImpl::Observe(
189 int type, 274 int type,
190 const content::NotificationSource& source, 275 const content::NotificationSource& source,
191 const content::NotificationDetails& details) { 276 const content::NotificationDetails& details) {
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && 278 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED &&
194 !modules_have_been_enumerated_event_.is_signaled()) { 279 !modules_have_been_enumerated_event_.is_signaled()) {
195 #if defined(OS_WIN) 280 #if defined(OS_WIN)
196 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); 281 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList());
197 #endif 282 #endif
198 modules_have_been_enumerated_event_.Signal(); 283 modules_have_been_enumerated_event_.Signal();
199 } 284 }
200 } 285 }
286
287 void AutomaticProfileResetterDelegateImpl::RunProfileSettingsReset(
288 bool send_feedback,
289 const base::Closure& completion) {
290 DCHECK(brandcoded_defaults_);
291 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot(
292 send_feedback ? new ResettableSettingsSnapshot(profile_) : NULL);
293 profile_resetter_->Reset(
294 ProfileResetter::ALL,
295 brandcoded_defaults_.Pass(),
296 base::Bind(&AutomaticProfileResetterDelegateImpl::
297 OnProfileSettingsResetCompleted,
298 AsWeakPtr(),
299 completion,
300 base::Passed(&old_settings_snapshot)));
301 }
302
303 void AutomaticProfileResetterDelegateImpl::
304 OnBrandcodedDefaultsFetched() {
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
306 DCHECK(brandcoded_config_fetcher_);
307 DCHECK(!brandcoded_config_fetcher_->IsActive());
308 brandcoded_defaults_ = brandcoded_config_fetcher_->GetSettings();
309 brandcoded_defaults_fetched_event_.Signal();
310 }
311
312 void AutomaticProfileResetterDelegateImpl::OnProfileSettingsResetCompleted(
313 const base::Closure& user_callback,
314 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot) {
315 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
316 if (old_settings_snapshot) {
317 ResettableSettingsSnapshot new_settings_snapshot(profile_);
318 int difference =
319 old_settings_snapshot->FindDifferentFields(new_settings_snapshot);
320 if (difference) {
321 old_settings_snapshot->Subtract(new_settings_snapshot);
322 std::string report =
323 SerializeSettingsReport(*old_settings_snapshot, difference);
324 SendSettingsFeedback(report, profile_, PROFILE_RESET_PROMPT);
325 }
326 }
327 if (!user_callback.is_null())
328 content::BrowserThread::PostTask(
329 content::BrowserThread::UI, FROM_HERE, user_callback);
330 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698