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..bb57e4515fb079a2e6f17edb07c2b669979839ab |
| --- /dev/null |
| +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate.cc |
| @@ -0,0 +1,233 @@ |
| +// 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 "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/profile_resetter/automatic_profile_resetter.h" |
|
vasilii
2013/10/14 16:43:33
Logically AutomaticProfileResetterDelegateImpl doe
engedy
2013/10/14 19:35:50
Done.
|
| +#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( |
| + 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. |
| + 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) { |
| + 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) { |
| + template_url_service->RemoveObserver(this); |
| + } |
| + registrar_.RemoveAll(); |
|
vasilii
2013/10/14 16:43:33
registrar_ is design to do this implicitly.
engedy
2013/10/14 19:35:50
Done.
|
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::EnumerateLoadedModulesIfNeeded() { |
| + if (!modules_have_been_enumerated_event_.is_signaled()) { |
| +#if defined(OS_WIN) |
| + EnumerateModulesModel::GetInstance()->ScanNow(); |
|
Finnur
2013/10/14 13:08:23
Is there ever a chance we'll get to this point dur
engedy
2013/10/14 13:22:38
We will actually get to here during start-up. I as
engedy
2013/10/14 16:07:21
Done.
Finnur
2013/10/15 10:41:07
Where's the code that delays starting this?
engedy
2013/10/15 22:13:07
This is called from AutomaticProfileResetter::Prep
|
| +#else |
| + modules_have_been_enumerated_event_.Signal(); |
| +#endif |
| + } |
| +} |
| + |
| +void AutomaticProfileResetterDelegateImpl::WaitOnEnumerationOfLoadedModules( |
| + const base::Closure& ready_callback) const { |
| + 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::WaitOnTemplateURLService( |
| + 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 { |
| + 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) { |
| + 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 |
| + return NULL; |
| +} |
| + |
| +bool AutomaticProfileResetterDelegateImpl::IsDefaultSearchProviderManaged() |
| + const { |
| + 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; |
| + 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::ReportStatistics( |
|
vasilii
2013/10/14 16:43:33
This method seems to be in the wrong place. It sho
engedy
2013/10/14 19:35:50
Done. As discussed, I moved this into AutomaticPro
|
| + uint32 satisfied_criteria_mask, |
| + uint32 combined_status_mask) { |
| + UMA_HISTOGRAM_ENUMERATION( |
| + "AutomaticProfileReset.SatisfiedCriteriaMask", |
| + satisfied_criteria_mask, |
| + AutomaticProfileResetter::kSatisfiedCriteriaMaskMaximumValue); |
| + UMA_HISTOGRAM_ENUMERATION( |
| + "AutomaticProfileReset.CombinedStatusMask", |
| + combined_status_mask, |
| + AutomaticProfileResetter::kCombinedStatusMaskMaximumValue); |
| +} |
| + |
| +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()) { |
| + modules_have_been_enumerated_event_.Signal(); |
|
vasilii
2013/10/14 16:43:33
Move the line below Signal()
engedy
2013/10/14 19:35:50
Done.
|
| +#if defined(OS_WIN) |
| + module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| +#endif |
| + } |
| +} |