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 "base/values.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | |
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 scoped_ptr<base::DictionaryValue> BuildSubTreeFromTemplateURL( | |
30 const TemplateURL* template_url) { | |
31 scoped_ptr<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 base::ListValue* input_encodings = new base::ListValue; | |
51 input_encodings->AppendStrings(template_url->input_encodings()); | |
52 tree->Set("encodings", input_encodings); | |
53 tree->SetString("id", base::Int64ToString(template_url->id())); | |
54 tree->SetString("prepopulate_id", | |
55 base::IntToString(template_url->prepopulate_id())); | |
56 base::ListValue* alternate_urls = new base::ListValue; | |
57 alternate_urls->AppendStrings(template_url->alternate_urls()); | |
58 tree->Set("alternate_urls", alternate_urls); | |
59 return tree.Pass(); | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 // AutomaticProfileResetterDelegateImpl -------------------------------------- | |
65 | |
66 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( | |
67 Profile* profile) | |
68 : profile_(profile) { | |
69 TemplateURLService* template_url_service = | |
70 TemplateURLServiceFactory::GetForProfile(profile_); | |
71 if (template_url_service) { | |
72 template_url_service->AddObserver(this); | |
73 // Needed so that |template_url_service_ready_event_| will be signaled even | |
74 // when TemplateURLService had been already initialized before this point. | |
75 OnTemplateURLServiceChanged(); | |
76 } | |
77 | |
78 #if defined(OS_WIN) | |
79 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
80 #endif | |
81 if (module_list_) { | |
82 // Having a non-empty module list proves that enumeration had been already | |
83 // performed before this point. | |
84 modules_have_been_enumerated_event_.Signal(); | |
85 } | |
86 registrar_.Add(this, | |
87 chrome::NOTIFICATION_MODULE_LIST_ENUMERATED, | |
88 content::NotificationService::AllSources()); | |
89 } | |
90 | |
91 AutomaticProfileResetterDelegateImpl::~AutomaticProfileResetterDelegateImpl() { | |
92 TemplateURLService* template_url_service = | |
93 TemplateURLServiceFactory::GetForProfile(profile_); | |
94 if (template_url_service) | |
95 template_url_service->RemoveObserver(this); | |
96 } | |
97 | |
98 void AutomaticProfileResetterDelegateImpl::EnumerateLoadedModulesIfNeeded() { | |
99 if (!modules_have_been_enumerated_event_.is_signaled()) { | |
100 #if defined(OS_WIN) | |
101 EnumerateModulesModel::GetInstance()->ScanNow(); | |
102 #else | |
103 modules_have_been_enumerated_event_.Signal(); | |
104 #endif | |
105 } | |
106 } | |
107 | |
108 void AutomaticProfileResetterDelegateImpl:: | |
109 RequestCallbackWhenLoadedModulesAreEnumerated( | |
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:: | |
123 RequestCallbackWhenTemplateURLServiceIsLoaded( | |
124 const base::Closure& ready_callback) const { | |
125 DCHECK(!ready_callback.is_null()); | |
126 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); | |
127 } | |
128 | |
129 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | |
130 GetLoadedModuleNameDigests() const { | |
131 DCHECK(modules_have_been_enumerated_event_.is_signaled()); | |
132 std::set<std::string> module_name_digests; | |
133 #if defined(OS_WIN) | |
134 if (module_list_) | |
135 ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); | |
136 #endif | |
137 scoped_ptr<base::ListValue> result(new base::ListValue); | |
138 for (std::set<std::string>::const_iterator it = module_name_digests.begin(); | |
139 it != module_name_digests.end(); ++it) | |
140 result->AppendString(*it); | |
141 return result.Pass(); | |
142 } | |
143 | |
144 scoped_ptr<base::DictionaryValue> AutomaticProfileResetterDelegateImpl:: | |
145 GetDefaultSearchProviderDetails() const { | |
146 TemplateURLService* template_url_service = | |
147 TemplateURLServiceFactory::GetForProfile(profile_); | |
148 DCHECK(template_url_service); | |
149 DCHECK(template_url_service->loaded()); | |
150 | |
151 const TemplateURL* default_search_provider = | |
152 template_url_service->GetDefaultSearchProvider(); | |
153 | |
154 // Having a NULL default search provider is due to either: | |
155 // 1.) default search providers being disabled by policy, | |
156 // 2.) directly tampering the Preferences and/or the SQLite DBs. | |
157 // In this state, Omnibox non-keyword search functionality is disabled. | |
158 // Unfortunately, we cannot really tell apart the two underlying causes. | |
159 return default_search_provider | |
160 ? BuildSubTreeFromTemplateURL(default_search_provider) | |
Peter Kasting
2013/10/16 00:40:21
Nit: Operators go on the ends of lines, and this i
engedy
2013/10/16 11:13:54
Done.
| |
161 : scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); | |
Peter Kasting
2013/10/16 00:40:21
If you're not going to return NULL, places like Au
engedy
2013/10/16 11:13:54
Done. Not checking for NULLs anymore.
| |
162 } | |
163 | |
164 bool AutomaticProfileResetterDelegateImpl:: | |
165 IsDefaultSearchProviderManaged() const { | |
166 TemplateURLService* template_url_service = | |
167 TemplateURLServiceFactory::GetForProfile(profile_); | |
168 DCHECK(template_url_service); | |
169 DCHECK(template_url_service->loaded()); | |
170 return template_url_service->is_default_search_managed(); | |
171 } | |
172 | |
173 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | |
174 GetPrepopulatedSearchProvidersDetails() const { | |
175 size_t default_search_index = 0; | |
176 ScopedVector<TemplateURL> engines( | |
177 TemplateURLPrepopulateData::GetPrepopulatedEngines( | |
178 profile_, &default_search_index)); | |
179 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); | |
180 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); | |
181 it != engines.end(); ++it) | |
182 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); | |
183 return engines_details_list.Pass(); | |
184 } | |
185 | |
186 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { | |
187 // TODO(engedy): Call the UI from here once we have it. | |
188 } | |
189 | |
190 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { | |
191 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
192 TemplateURLService* template_url_service = | |
193 TemplateURLServiceFactory::GetForProfile(profile_); | |
194 DCHECK(template_url_service); | |
195 if (template_url_service->loaded() && | |
196 !template_url_service_ready_event_.is_signaled()) | |
197 template_url_service_ready_event_.Signal(); | |
198 } | |
199 | |
200 void AutomaticProfileResetterDelegateImpl::Observe( | |
201 int type, | |
202 const content::NotificationSource& source, | |
203 const content::NotificationDetails& details) { | |
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
205 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && | |
206 !modules_have_been_enumerated_event_.is_signaled()) { | |
207 #if defined(OS_WIN) | |
208 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
209 #endif | |
210 modules_have_been_enumerated_event_.Signal(); | |
211 } | |
212 } | |
OLD | NEW |