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()); | |
robertshield
2013/10/24 00:36:33
fwiw, these string values seem to be duplicated in
engedy
2013/10/24 09:23:36
Yes, we have been discussing just that with pkasti
| |
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 | |
65 // AutomaticProfileResetterDelegateImpl -------------------------------------- | |
66 | |
67 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( | |
68 TemplateURLService* template_url_service) | |
69 : template_url_service_(template_url_service) { | |
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 if (template_url_service_) | |
92 template_url_service_->RemoveObserver(this); | |
93 } | |
94 | |
95 void AutomaticProfileResetterDelegateImpl::EnumerateLoadedModulesIfNeeded() { | |
96 if (!modules_have_been_enumerated_event_.is_signaled()) { | |
97 #if defined(OS_WIN) | |
98 EnumerateModulesModel::GetInstance()->ScanNow(); | |
99 #else | |
100 modules_have_been_enumerated_event_.Signal(); | |
101 #endif | |
102 } | |
103 } | |
104 | |
105 void AutomaticProfileResetterDelegateImpl:: | |
106 RequestCallbackWhenLoadedModulesAreEnumerated( | |
107 const base::Closure& ready_callback) const { | |
108 DCHECK(!ready_callback.is_null()); | |
109 modules_have_been_enumerated_event_.Post(FROM_HERE, ready_callback); | |
110 } | |
111 | |
112 void AutomaticProfileResetterDelegateImpl::LoadTemplateURLServiceIfNeeded() { | |
113 DCHECK(template_url_service_); | |
114 template_url_service_->Load(); // Safe to call even if it has loaded already. | |
115 } | |
116 | |
117 void AutomaticProfileResetterDelegateImpl:: | |
118 RequestCallbackWhenTemplateURLServiceIsLoaded( | |
119 const base::Closure& ready_callback) const { | |
120 DCHECK(!ready_callback.is_null()); | |
121 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); | |
122 } | |
123 | |
124 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | |
125 GetLoadedModuleNameDigests() const { | |
126 DCHECK(modules_have_been_enumerated_event_.is_signaled()); | |
127 std::set<std::string> module_name_digests; | |
128 #if defined(OS_WIN) | |
129 if (module_list_) | |
130 ExtractLoadedModuleNameDigests(*module_list_, &module_name_digests); | |
131 #endif | |
132 scoped_ptr<base::ListValue> result(new base::ListValue); | |
133 for (std::set<std::string>::const_iterator it = module_name_digests.begin(); | |
134 it != module_name_digests.end(); ++it) | |
135 result->AppendString(*it); | |
136 return result.Pass(); | |
137 } | |
138 | |
139 scoped_ptr<base::DictionaryValue> AutomaticProfileResetterDelegateImpl:: | |
140 GetDefaultSearchProviderDetails() const { | |
141 DCHECK(template_url_service_); | |
142 DCHECK(template_url_service_->loaded()); | |
143 | |
144 const TemplateURL* default_search_provider = | |
145 template_url_service_->GetDefaultSearchProvider(); | |
146 | |
147 // Having a NULL default search provider is due to either: | |
148 // 1.) default search providers being disabled by policy, | |
149 // 2.) directly tampering the Preferences and/or the SQLite DBs. | |
robertshield
2013/10/24 00:36:33
nit: tampering with
engedy
2013/10/24 09:23:36
Done. Also removed the last line of the comment, a
| |
150 // In this state, Omnibox non-keyword search functionality is disabled. | |
151 // Unfortunately, we cannot really tell apart the two underlying causes. | |
152 return default_search_provider ? | |
153 BuildSubTreeFromTemplateURL(default_search_provider) : | |
154 scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); | |
155 } | |
156 | |
157 bool AutomaticProfileResetterDelegateImpl:: | |
158 IsDefaultSearchProviderManaged() const { | |
159 DCHECK(template_url_service_); | |
160 DCHECK(template_url_service_->loaded()); | |
161 return template_url_service_->is_default_search_managed(); | |
162 } | |
163 | |
164 scoped_ptr<base::ListValue> AutomaticProfileResetterDelegateImpl:: | |
165 GetPrepopulatedSearchProvidersDetails() const { | |
166 DCHECK(template_url_service_); | |
167 size_t default_search_index = 0; | |
168 ScopedVector<TemplateURL> engines( | |
169 TemplateURLPrepopulateData::GetPrepopulatedEngines( | |
170 template_url_service_->profile(), &default_search_index)); | |
171 scoped_ptr<base::ListValue> engines_details_list(new base::ListValue); | |
172 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); | |
173 it != engines.end(); ++it) | |
174 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it).release()); | |
175 return engines_details_list.Pass(); | |
176 } | |
177 | |
178 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { | |
179 // TODO(engedy): Call the UI from here once we have it. | |
180 } | |
181 | |
182 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { | |
183 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
184 DCHECK(template_url_service_); | |
185 if (template_url_service_->loaded() && | |
186 !template_url_service_ready_event_.is_signaled()) | |
187 template_url_service_ready_event_.Signal(); | |
188 } | |
189 | |
190 void AutomaticProfileResetterDelegateImpl::Observe( | |
191 int type, | |
192 const content::NotificationSource& source, | |
193 const content::NotificationDetails& details) { | |
194 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
195 if (type == chrome::NOTIFICATION_MODULE_LIST_ENUMERATED && | |
196 !modules_have_been_enumerated_event_.is_signaled()) { | |
197 #if defined(OS_WIN) | |
198 module_list_.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
199 #endif | |
200 modules_have_been_enumerated_event_.Signal(); | |
201 } | |
202 } | |
OLD | NEW |