Chromium Code Reviews| Index: chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc |
| diff --git a/chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d7f712b25d2617062f2c8ad1db7cc3059b12864 |
| --- /dev/null |
| +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc |
| @@ -0,0 +1,221 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
| + |
| +#include "base/callback.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| +#include "chrome/browser/search_engines/template_url_service.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_service.h" |
| + |
| +#if defined(OS_WIN) |
| +#include "chrome/browser/enumerate_modules_model_win.h" |
| +#include "chrome/browser/install_module_verifier_win.h" |
| +#endif |
| + |
| +namespace { |
| + |
| +base::DictionaryValue* BuildSubTreeFromTemplateURL( |
|
Peter Kasting
2013/10/15 01:31:28
Is there similar code to this elsewhere in the bro
engedy
2013/10/15 22:13:07
This is somewhat similar to TemplateURLService::Sa
Peter Kasting
2013/10/16 00:40:21
Hmm. I wonder if there is some way we could write
|
| + const TemplateURL* template_url) { |
| + base::DictionaryValue* tree = new base::DictionaryValue; |
| + tree->SetString("search_url", template_url->url()); |
| + tree->SetString("search_terms_replacement_key", |
| + template_url->search_terms_replacement_key()); |
| + tree->SetString("suggest_url", template_url->suggestions_url()); |
| + tree->SetString("instant_url", template_url->instant_url()); |
| + tree->SetString("image_url", template_url->image_url()); |
| + tree->SetString("new_tab_url", template_url->new_tab_url()); |
| + tree->SetString("search_url_post_params", |
| + template_url->search_url_post_params()); |
| + tree->SetString("suggest_url_post_params", |
| + template_url->suggestions_url_post_params()); |
| + tree->SetString("instant_url_post_params", |
| + template_url->instant_url_post_params()); |
| + tree->SetString("image_url_post_params", |
| + template_url->image_url_post_params()); |
| + tree->SetString("icon_url", template_url->favicon_url().spec()); |
| + tree->SetString("name", template_url->short_name()); |
| + tree->SetString("keyword", template_url->keyword()); |
| + // For consistency with Prefs. |
|
Peter Kasting
2013/10/15 01:31:28
Nit: This comment is vague; what is for consistenc
engedy
2013/10/15 22:13:07
Reworked this part so the comment is no longer req
|
| + tree->SetString("encodings", |
| + JoinString(template_url->input_encodings(), ';')); |
| + tree->SetString("prepopulate_id", |
| + base::IntToString(template_url->prepopulate_id())); |
| + base::ListValue* alternate_urls = new base::ListValue; |
| + alternate_urls->AppendStrings(template_url->alternate_urls()); |
| + tree->Set("alternate_urls", alternate_urls); |
| + return tree; |
| +} |
| + |
| +} // namespace |
| + |
| +// AutomaticProfileResetterDelegateImpl -------------------------------------- |
| + |
| +AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( |
| + Profile* profile) |
| + : profile_(profile) { |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + if (template_url_service) { |
|
Peter Kasting
2013/10/15 01:31:28
Some functions in this class check whether a Templ
engedy
2013/10/15 22:13:07
I have added comments above the class declaration
|
| + template_url_service->AddObserver(this); |
| + // Needed so that |template_url_service_ready_event_| will be signaled even |
| + // when TemplateURLService had been already initialized before this point. |
| + OnTemplateURLServiceChanged(); |
| + } |
| + |
| +#if defined(OS_WIN) |
| + module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| +#endif |
| + if (module_list_) { |
| + // Having a non-empty module list proves that enumeration had been already |
| + // performed before this point. |
| + modules_have_been_enumerated_event_.Signal(); |
| + } |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, |
| + content::NotificationService::AllSources()); |
| +} |
| + |
| +AutomaticProfileResetterDelegateImpl::~AutomaticProfileResetterDelegateImpl() { |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + if (template_url_service) { |
|
Peter Kasting
2013/10/15 01:31:28
Nit: No need for {} on one-line bodies (3 places)
engedy
2013/10/15 22:13:07
Done. In all places in automatic_profile_resetter*
|
| + template_url_service->RemoveObserver(this); |
| + } |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::EnumerateLoadedModulesIfNeeded() { |
| + if (!modules_have_been_enumerated_event_.is_signaled()) { |
| +#if defined(OS_WIN) |
| + EnumerateModulesModel::GetInstance()->ScanNow(); |
| +#else |
| + modules_have_been_enumerated_event_.Signal(); |
| +#endif |
| + } |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl:: |
| + RequestCallbackWhenLoadedModulesAreEnumerated( |
| + const base::Closure& ready_callback) const { |
|
Peter Kasting
2013/10/15 01:31:28
Nit: Indent 4, not 8 (2 places)
engedy
2013/10/15 22:13:07
Done. In all places in automatic_profile_resetter*
|
| + DCHECK(!ready_callback.is_null()); |
| + modules_have_been_enumerated_event_.Post(FROM_HERE, ready_callback); |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::LoadTemplateURLServiceIfNeeded() { |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + DCHECK(template_url_service); |
| + template_url_service->Load(); // Safe to call even if it has loaded already. |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl:: |
| + RequestCallbackWhenTemplateURLServiceIsLoaded( |
| + const base::Closure& ready_callback) const { |
| + DCHECK(!ready_callback.is_null()); |
| + template_url_service_ready_event_.Post(FROM_HERE, ready_callback); |
| +} |
| + |
| +base::ListValue* |
| +AutomaticProfileResetterDelegateImpl::GetLoadedModuleNameDigests() const { |
|
Peter Kasting
2013/10/15 01:31:28
Nit: Indent 4 (3 places)
engedy
2013/10/15 22:13:07
Done. In all places in automatic_profile_resetter*
|
| + DCHECK(modules_have_been_enumerated_event_.is_signaled()); |
| + std::set<std::string> module_name_digests; |
| +#if defined(OS_WIN) |
| + if (module_list_) { |
| + DLOG(INFO) << *module_list_; |
| + ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); |
| + } |
| +#endif |
| + base::ListValue* result = new base::ListValue; |
| + for (std::set<std::string>::const_iterator it = module_name_digests.begin(); |
| + it != module_name_digests.end(); |
| + ++it) { |
|
Peter Kasting
2013/10/15 01:31:28
Nit: Commonly we don't linebreak before this, but
engedy
2013/10/15 22:13:07
Done. In all places in automatic_profile_resetter*
|
| + result->AppendString(*it); |
| + } |
| + return result; |
| +} |
| + |
| +base::DictionaryValue* |
| +AutomaticProfileResetterDelegateImpl::GetDefaultSearchProviderDetails() const { |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + DCHECK(template_url_service); |
| + DCHECK(template_url_service->loaded()); |
| + |
| + TemplateURL* default_search_provider = |
| + template_url_service->GetDefaultSearchProvider(); |
| + |
| + // Having a NULL default search provider is due to either: |
| + // 1.) default search providers being disabled by policy, |
| + // 2.) directly tampering the Preferences and/or the SQLite DBs. |
| + // In this state, Omnibox non-keyword search functionality is disabled. |
| + // Unfortunately, we cannot really tell apart the two underlying causes. |
| + if (default_search_provider) |
| + return BuildSubTreeFromTemplateURL(default_search_provider); |
| + else |
|
Peter Kasting
2013/10/15 01:31:28
Nit: No else after return. Consider:
return de
engedy
2013/10/15 22:13:07
Done.
|
| + return NULL; |
| +} |
| + |
| +bool AutomaticProfileResetterDelegateImpl::IsDefaultSearchProviderManaged() |
| + const { |
|
Peter Kasting
2013/10/15 01:31:28
Nit: I'd probably break after "::" instead (2 plac
engedy
2013/10/15 22:13:07
Done. In all places in automatic_profile_resetter*
|
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + DCHECK(template_url_service); |
| + DCHECK(template_url_service->loaded()); |
| + return template_url_service->is_default_search_managed(); |
| +} |
| + |
| +base::ListValue* |
| +AutomaticProfileResetterDelegateImpl::GetPrepopulatedSearchProvidersDetails() |
| + const { |
| + ScopedVector<TemplateURL> engines; |
|
Peter Kasting
2013/10/15 01:31:28
Nit: Declare this in the statement that inits it
engedy
2013/10/15 22:13:07
Done.
|
| + size_t default_search_index = 0; |
| + engines = TemplateURLPrepopulateData::GetPrepopulatedEngines( |
| + profile_, &default_search_index); |
| + base::ListValue* engines_details_list = new base::ListValue; |
| + for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); |
| + it != engines.end(); |
| + ++it) { |
| + engines_details_list->Append(BuildSubTreeFromTemplateURL(*it)); |
| + } |
| + return engines_details_list; |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::ShowPrompt() { |
| + // TODO(engedy): Call the UI from here once we have it. |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile_); |
| + DCHECK(template_url_service); |
| + if (template_url_service->loaded() && |
| + !template_url_service_ready_event_.is_signaled()) { |
| + template_url_service_ready_event_.Signal(); |
| + } |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && |
| + !modules_have_been_enumerated_event_.is_signaled()) { |
| +#if defined(OS_WIN) |
| + module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| +#endif |
| + modules_have_been_enumerated_event_.Signal(); |
| + } |
| +} |