Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_vector.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #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.
| |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
| 17 #include "chrome/browser/search_engines/template_url_service.h" | |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/notification_service.h" | |
| 21 | |
| 22 #if defined(OS_WIN) | |
| 23 #include "chrome/browser/enumerate_modules_model_win.h" | |
| 24 #include "chrome/browser/install_module_verifier_win.h" | |
| 25 #endif | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 base::DictionaryValue* BuildSubTreeFromTemplateURL( | |
| 30 const TemplateURL* template_url) { | |
| 31 base::DictionaryValue* tree = new base::DictionaryValue; | |
| 32 tree->SetString("search_url", template_url->url()); | |
| 33 tree->SetString("search_terms_replacement_key", | |
| 34 template_url->search_terms_replacement_key()); | |
| 35 tree->SetString("suggest_url", template_url->suggestions_url()); | |
| 36 tree->SetString("instant_url", template_url->instant_url()); | |
| 37 tree->SetString("image_url", template_url->image_url()); | |
| 38 tree->SetString("new_tab_url", template_url->new_tab_url()); | |
| 39 tree->SetString("search_url_post_params", | |
| 40 template_url->search_url_post_params()); | |
| 41 tree->SetString("suggest_url_post_params", | |
| 42 template_url->suggestions_url_post_params()); | |
| 43 tree->SetString("instant_url_post_params", | |
| 44 template_url->instant_url_post_params()); | |
| 45 tree->SetString("image_url_post_params", | |
| 46 template_url->image_url_post_params()); | |
| 47 tree->SetString("icon_url", template_url->favicon_url().spec()); | |
| 48 tree->SetString("name", template_url->short_name()); | |
| 49 tree->SetString("keyword", template_url->keyword()); | |
| 50 // For consistency with Prefs. | |
| 51 tree->SetString("encodings", | |
| 52 JoinString(template_url->input_encodings(), ';')); | |
| 53 tree->SetString("prepopulate_id", | |
| 54 base::IntToString(template_url->prepopulate_id())); | |
| 55 base::ListValue* alternate_urls = new base::ListValue; | |
| 56 alternate_urls->AppendStrings(template_url->alternate_urls()); | |
| 57 tree->Set("alternate_urls", alternate_urls); | |
| 58 return tree; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 // AutomaticProfileResetterDelegateImpl -------------------------------------- | |
| 64 | |
| 65 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( | |
| 66 Profile* profile) | |
| 67 : profile_(profile) { | |
| 68 TemplateURLService* template_url_service = | |
| 69 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 70 if (template_url_service) { | |
| 71 template_url_service->AddObserver(this); | |
| 72 // Needed so that |template_url_service_ready_event_| will be signaled even | |
| 73 // when TemplateURLService had been already initialized before this point. | |
| 74 OnTemplateURLServiceChanged(); | |
| 75 } | |
| 76 | |
| 77 #if defined(OS_WIN) | |
| 78 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
| 79 #endif | |
| 80 if (module_list_) { | |
| 81 // Having a non-empty module list proves that enumeration had been already | |
| 82 // performed before this point. | |
| 83 modules_have_been_enumerated_event_.Signal(); | |
| 84 } | |
| 85 registrar_.Add(this, | |
| 86 chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | |
| 87 content::NotificationService::AllSources()); | |
| 88 } | |
| 89 | |
| 90 AutomaticProfileResetterDelegateImpl::~AutomaticProfileResetterDelegateImpl() { | |
| 91 TemplateURLService* template_url_service = | |
| 92 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 93 if (template_url_service) { | |
| 94 template_url_service->RemoveObserver(this); | |
| 95 } | |
| 96 registrar_.RemoveAll(); | |
|
vasilii
2013/10/14 16:43:33
registrar_ is design to do this implicitly.
engedy
2013/10/14 19:35:50
Done.
| |
| 97 } | |
| 98 | |
| 99 void AutomaticProfileResetterDelegateImpl::EnumerateLoadedModulesIfNeeded() { | |
| 100 if (!modules_have_been_enumerated_event_.is_signaled()) { | |
| 101 #if defined(OS_WIN) | |
| 102 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
| |
| 103 #else | |
| 104 modules_have_been_enumerated_event_.Signal(); | |
| 105 #endif | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void AutomaticProfileResetterDelegateImpl::WaitOnEnumerationOfLoadedModules( | |
| 110 const base::Closure& ready_callback) const { | |
| 111 DCHECK(!ready_callback.is_null()); | |
| 112 modules_have_been_enumerated_event_.Post(FROM_HERE, ready_callback); | |
| 113 } | |
| 114 | |
| 115 void AutomaticProfileResetterDelegateImpl::LoadTemplateURLServiceIfNeeded() { | |
| 116 TemplateURLService* template_url_service = | |
| 117 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 118 DCHECK(template_url_service); | |
| 119 template_url_service->Load(); // Safe to call even if it has loaded already. | |
| 120 } | |
| 121 | |
| 122 void AutomaticProfileResetterDelegateImpl::WaitOnTemplateURLService( | |
| 123 const base::Closure& ready_callback) const { | |
| 124 DCHECK(!ready_callback.is_null()); | |
| 125 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); | |
| 126 } | |
| 127 | |
| 128 base::ListValue* | |
| 129 AutomaticProfileResetterDelegateImpl::GetLoadedModuleNameDigests() const { | |
| 130 DCHECK(modules_have_been_enumerated_event_.is_signaled()); | |
| 131 std::set<std::string> module_name_digests; | |
| 132 #if defined(OS_WIN) | |
| 133 if (module_list_) { | |
| 134 DLOG(INFO) << *module_list_; | |
| 135 ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); | |
| 136 } | |
| 137 #endif | |
| 138 base::ListValue* result = new base::ListValue; | |
| 139 for (std::set<std::string>::const_iterator it = module_name_digests.begin(); | |
| 140 it != module_name_digests.end(); | |
| 141 ++it) { | |
| 142 result->AppendString(*it); | |
| 143 } | |
| 144 return result; | |
| 145 } | |
| 146 | |
| 147 base::DictionaryValue* | |
| 148 AutomaticProfileResetterDelegateImpl::GetDefaultSearchProviderDetails() const { | |
| 149 TemplateURLService* template_url_service = | |
| 150 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 151 DCHECK(template_url_service); | |
| 152 DCHECK(template_url_service->loaded()); | |
| 153 | |
| 154 TemplateURL* default_search_provider = | |
| 155 template_url_service->GetDefaultSearchProvider(); | |
| 156 | |
| 157 // Having a NULL default search provider is due to either: | |
| 158 // 1.) default search providers being disabled by policy, | |
| 159 // 2.) directly tampering the Preferences and/or the SQLite DBs. | |
| 160 // In this state, Omnibox non-keyword search functionality is disabled. | |
| 161 // Unfortunately, we cannot really tell apart the two underlying causes. | |
| 162 if (default_search_provider) | |
| 163 return BuildSubTreeFromTemplateURL(default_search_provider); | |
| 164 else | |
| 165 return NULL; | |
| 166 } | |
| 167 | |
| 168 bool AutomaticProfileResetterDelegateImpl::IsDefaultSearchProviderManaged() | |
| 169 const { | |
| 170 TemplateURLService* template_url_service = | |
| 171 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 172 DCHECK(template_url_service); | |
| 173 DCHECK(template_url_service->loaded()); | |
| 174 return template_url_service->is_default_search_managed(); | |
| 175 } | |
| 176 | |
| 177 base::ListValue* | |
| 178 AutomaticProfileResetterDelegateImpl::GetPrepopulatedSearchProvidersDetails() | |
| 179 const { | |
| 180 ScopedVector<TemplateURL> engines; | |
| 181 size_t default_search_index = 0; | |
| 182 engines = TemplateURLPrepopulateData::GetPrepopulatedEngines( | |
| 183 profile_, &default_search_index); | |
| 184 base::ListValue* engines_details_list = new base::ListValue; | |
| 185 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); | |
| 186 it != engines.end(); | |
| 187 ++it) { | |
| 188 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it)); | |
| 189 } | |
| 190 return engines_details_list; | |
| 191 } | |
| 192 | |
| 193 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { | |
| 194 // TODO(engedy): Call the UI from here once we have it. | |
| 195 } | |
| 196 | |
| 197 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
| |
| 198 uint32 satisfied_criteria_mask, | |
| 199 uint32 combined_status_mask) { | |
| 200 UMA_HISTOGRAM_ENUMERATION( | |
| 201 "AutomaticProfileReset.SatisfiedCriteriaMask", | |
| 202 satisfied_criteria_mask, | |
| 203 AutomaticProfileResetter::kSatisfiedCriteriaMaskMaximumValue); | |
| 204 UMA_HISTOGRAM_ENUMERATION( | |
| 205 "AutomaticProfileReset.CombinedStatusMask", | |
| 206 combined_status_mask, | |
| 207 AutomaticProfileResetter::kCombinedStatusMaskMaximumValue); | |
| 208 } | |
| 209 | |
| 210 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { | |
| 211 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 212 TemplateURLService* template_url_service = | |
| 213 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 214 DCHECK(template_url_service); | |
| 215 if (template_url_service->loaded() && | |
| 216 !template_url_service_ready_event_.is_signaled()) { | |
| 217 template_url_service_ready_event_.Signal(); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 void AutomaticProfileResetterDelegateImpl::Observe( | |
| 222 int type, | |
| 223 const content::NotificationSource& source, | |
| 224 const content::NotificationDetails& details) { | |
| 225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 226 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && | |
| 227 !modules_have_been_enumerated_event_.is_signaled()) { | |
| 228 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.
| |
| 229 #if defined(OS_WIN) | |
| 230 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
| 231 #endif | |
| 232 } | |
| 233 } | |
| OLD | NEW |