Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc

Issue 1109563003: Implement remaining chrome.searchEnginesPrivate methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the event router bug so my test passes now Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/search_engines_private/search_engines_pr ivate_api.h" 5 #include "chrome/browser/extensions/api/search_engines_private/search_engines_pr ivate_api.h"
6 6
7 #include "base/prefs/pref_service.h"
7 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h" 9 #include "base/values.h"
9 #include "chrome/browser/extensions/chrome_extension_function.h" 10 #include "chrome/browser/extensions/chrome_extension_function.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/hotword_audio_history_handler.h"
13 #include "chrome/browser/search/hotword_service.h"
14 #include "chrome/browser/search/hotword_service_factory.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h" 15 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/common/extensions/api/search_engines_private.h" 16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
13 #include "components/search_engines/template_url_service.h" 20 #include "components/search_engines/template_url_service.h"
21 #include "components/signin/core/browser/signin_manager_base.h"
14 #include "extensions/browser/extension_function_registry.h" 22 #include "extensions/browser/extension_function_registry.h"
23 #include "ui/base/l10n/l10n_util.h"
15 24
16 namespace extensions { 25 namespace extensions {
17 26
27 namespace search_engines_private = api::search_engines_private;
28
18 //////////////////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////////////////
19 // SearchEnginesPrivateGetDefaultSearchEnginesFunction 30 // SearchEnginesPrivateGetDefaultSearchEnginesFunction
20 31
21 SearchEnginesPrivateGetDefaultSearchEnginesFunction:: 32 SearchEnginesPrivateGetDefaultSearchEnginesFunction::
22 SearchEnginesPrivateGetDefaultSearchEnginesFunction() 33 SearchEnginesPrivateGetDefaultSearchEnginesFunction()
23 : chrome_details_(this) { 34 : chrome_details_(this) {
24 } 35 }
25 36
26 SearchEnginesPrivateGetDefaultSearchEnginesFunction:: 37 SearchEnginesPrivateGetDefaultSearchEnginesFunction::
27 ~SearchEnginesPrivateGetDefaultSearchEnginesFunction() { 38 ~SearchEnginesPrivateGetDefaultSearchEnginesFunction() {
28 } 39 }
29 40
30 ExtensionFunction::ResponseAction 41 ExtensionFunction::ResponseAction
31 SearchEnginesPrivateGetDefaultSearchEnginesFunction::Run() { 42 SearchEnginesPrivateGetDefaultSearchEnginesFunction::Run() {
32 TemplateURLService* template_url_service = 43 TemplateURLService* template_url_service =
33 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); 44 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
34 base::ListValue* engines = new base::ListValue(); 45 base::ListValue* engines = new base::ListValue();
35 46
36 const TemplateURL* default_url = 47 const TemplateURL* default_url =
37 template_url_service->GetDefaultSearchProvider(); 48 template_url_service->GetDefaultSearchProvider();
38 std::vector<TemplateURL*> urls = template_url_service->GetTemplateURLs(); 49 std::vector<TemplateURL*> urls = template_url_service->GetTemplateURLs();
39 for (size_t i = 0; i < urls.size(); i++) { 50 for (size_t i = 0; i < urls.size(); i++) {
40 api::search_engines_private::SearchEngine engine; 51 if (!urls[i]->show_in_default_list())
52 continue;
stevenjb 2015/04/28 17:16:38 If the data model differentiates between "default"
Oren Blasberg 2015/04/28 21:58:30 Done.
53
54 search_engines_private::SearchEngine engine;
41 engine.guid = urls[i]->sync_guid(); 55 engine.guid = urls[i]->sync_guid();
42 engine.name = base::UTF16ToASCII(urls[i]->short_name()); 56 engine.name = base::UTF16ToASCII(urls[i]->short_name());
57 engine.keyword = base::UTF16ToASCII(urls[i]->keyword());
58 engine.url = urls[i]->url();
43 if (urls[i] == default_url) 59 if (urls[i] == default_url)
44 engine.is_selected = scoped_ptr<bool>(new bool(true)); 60 engine.is_selected = scoped_ptr<bool>(new bool(true));
45 61
46 engines->Append(engine.ToValue().release()); 62 engines->Append(engine.ToValue().release());
47 } 63 }
48 64
49 return RespondNow(OneArgument(engines)); 65 return RespondNow(OneArgument(engines));
50 } 66 }
51 67
52 //////////////////////////////////////////////////////////////////////////////// 68 ////////////////////////////////////////////////////////////////////////////////
53 // SearchEnginesPrivateSetSelectedSearchEngineFunction 69 // SearchEnginesPrivateSetSelectedSearchEngineFunction
54 70
55 SearchEnginesPrivateSetSelectedSearchEngineFunction:: 71 SearchEnginesPrivateSetSelectedSearchEngineFunction::
56 SearchEnginesPrivateSetSelectedSearchEngineFunction() 72 SearchEnginesPrivateSetSelectedSearchEngineFunction()
57 : chrome_details_(this) { 73 : chrome_details_(this) {
58 } 74 }
59 75
60 SearchEnginesPrivateSetSelectedSearchEngineFunction:: 76 SearchEnginesPrivateSetSelectedSearchEngineFunction::
61 ~SearchEnginesPrivateSetSelectedSearchEngineFunction() { 77 ~SearchEnginesPrivateSetSelectedSearchEngineFunction() {
62 } 78 }
63 79
64 ExtensionFunction::ResponseAction 80 ExtensionFunction::ResponseAction
65 SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() { 81 SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() {
66 scoped_ptr<api::search_engines_private::SetSelectedSearchEngine::Params> 82 scoped_ptr<search_engines_private::SetSelectedSearchEngine::Params>
67 parameters = 83 parameters =
68 api::search_engines_private::SetSelectedSearchEngine::Params::Create( 84 search_engines_private::SetSelectedSearchEngine::Params::Create(
69 *args_); 85 *args_);
70 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 86 EXTENSION_FUNCTION_VALIDATE(parameters.get());
71 87
72 TemplateURLService* template_url_service = 88 TemplateURLService* template_url_service =
73 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); 89 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
74 template_url_service->SetUserSelectedDefaultSearchProvider( 90 template_url_service->SetUserSelectedDefaultSearchProvider(
75 template_url_service->GetTemplateURLForGUID(parameters->guid)); 91 template_url_service->GetTemplateURLForGUID(parameters->guid));
76 return RespondNow(NoArguments()); 92 return RespondNow(NoArguments());
77 } 93 }
78 94
95 ////////////////////////////////////////////////////////////////////////////////
96 // SearchEnginesPrivateGetOtherSearchEnginesFunction
97
98 SearchEnginesPrivateGetOtherSearchEnginesFunction::
99 SearchEnginesPrivateGetOtherSearchEnginesFunction()
100 : chrome_details_(this) {
101 }
102
103 SearchEnginesPrivateGetOtherSearchEnginesFunction::
104 ~SearchEnginesPrivateGetOtherSearchEnginesFunction() {
105 }
106
107 ExtensionFunction::ResponseAction
108 SearchEnginesPrivateGetOtherSearchEnginesFunction::Run() {
109 TemplateURLService* template_url_service =
110 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
111 base::ListValue* engines = new base::ListValue();
112
113 std::vector<TemplateURL*> urls = template_url_service->GetTemplateURLs();
114 for (size_t i = 0; i < urls.size(); i++) {
115 if (urls[i]->show_in_default_list())
116 continue;
stevenjb 2015/04/28 17:16:39 If we do decide to keep this as two separate APIs
Oren Blasberg 2015/04/28 21:58:30 Ended up combining them.
117
118 search_engines_private::SearchEngine engine;
119 engine.guid = urls[i]->sync_guid();
120 engine.name = base::UTF16ToASCII(urls[i]->short_name());
121 engine.keyword = base::UTF16ToASCII(urls[i]->keyword());
122 engine.url = urls[i]->url();
123
124 engines->Append(engine.ToValue().release());
125 }
126
127 return RespondNow(OneArgument(engines));
128 }
129
130 ////////////////////////////////////////////////////////////////////////////////
131 // SearchEnginesPrivateAddOtherSearchEngineFunction
132
133 SearchEnginesPrivateAddOtherSearchEngineFunction::
134 SearchEnginesPrivateAddOtherSearchEngineFunction()
135 : chrome_details_(this) {
136 }
137
138 SearchEnginesPrivateAddOtherSearchEngineFunction::
139 ~SearchEnginesPrivateAddOtherSearchEngineFunction() {
140 }
141
142 ExtensionFunction::ResponseAction
143 SearchEnginesPrivateAddOtherSearchEngineFunction::Run() {
144 scoped_ptr<search_engines_private::AddOtherSearchEngine::Params> parameters =
145 search_engines_private::AddOtherSearchEngine::Params::Create(*args_);
146 EXTENSION_FUNCTION_VALIDATE(parameters.get());
147
148 TemplateURLData data;
149 data.short_name = base::ASCIIToUTF16(parameters->name);
150 data.SetKeyword(base::ASCIIToUTF16(parameters->keyword));
151 data.SetURL(parameters->url);
152 TemplateURL* turl = new TemplateURL(data);
153
154 TemplateURLService* template_url_service =
155 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
156 template_url_service->Add(turl);
157 return RespondNow(NoArguments());
158 }
159
160 ////////////////////////////////////////////////////////////////////////////////
161 // SearchEnginesPrivateUpdateSearchEngineFunction
162
163 SearchEnginesPrivateUpdateSearchEngineFunction::
164 SearchEnginesPrivateUpdateSearchEngineFunction()
165 : chrome_details_(this) {
166 }
167
168 SearchEnginesPrivateUpdateSearchEngineFunction::
169 ~SearchEnginesPrivateUpdateSearchEngineFunction() {
170 }
171
172 ExtensionFunction::ResponseAction
173 SearchEnginesPrivateUpdateSearchEngineFunction::Run() {
174 scoped_ptr<search_engines_private::UpdateSearchEngine::Params> parameters =
175 search_engines_private::UpdateSearchEngine::Params::Create(*args_);
176 EXTENSION_FUNCTION_VALIDATE(parameters.get());
177
178 TemplateURLService* template_url_service =
179 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
180 TemplateURL* turl =
181 template_url_service->GetTemplateURLForGUID(parameters->guid);
182
183 template_url_service->ResetTemplateURL(
184 turl, base::ASCIIToUTF16(parameters->name),
185 base::ASCIIToUTF16(parameters->keyword), parameters->url);
186
187 return RespondNow(NoArguments());
188 }
189
190 ////////////////////////////////////////////////////////////////////////////////
191 // SearchEnginesPrivateRemoveSearchEngineFunction
192
193 SearchEnginesPrivateRemoveSearchEngineFunction::
194 SearchEnginesPrivateRemoveSearchEngineFunction()
195 : chrome_details_(this) {
196 }
197
198 SearchEnginesPrivateRemoveSearchEngineFunction::
199 ~SearchEnginesPrivateRemoveSearchEngineFunction() {
200 }
201
202 ExtensionFunction::ResponseAction
203 SearchEnginesPrivateRemoveSearchEngineFunction::Run() {
204 scoped_ptr<search_engines_private::RemoveSearchEngine::Params> parameters =
205 search_engines_private::RemoveSearchEngine::Params::Create(*args_);
206 EXTENSION_FUNCTION_VALIDATE(parameters.get());
207
208 TemplateURLService* template_url_service =
209 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
210 TemplateURL* turl =
211 template_url_service->GetTemplateURLForGUID(parameters->guid);
212 template_url_service->Remove(turl);
213 return RespondNow(NoArguments());
214 }
215
216 ////////////////////////////////////////////////////////////////////////////////
217 // SearchEnginesPrivateGetHotwordStateFunction
218
219 SearchEnginesPrivateGetHotwordStateFunction::
220 SearchEnginesPrivateGetHotwordStateFunction()
221 : chrome_details_(this), weak_ptr_factory_(this) {
222 }
223
224 SearchEnginesPrivateGetHotwordStateFunction::
225 ~SearchEnginesPrivateGetHotwordStateFunction() {
226 }
227
228 ExtensionFunction::ResponseAction
229 SearchEnginesPrivateGetHotwordStateFunction::Run() {
230 Profile* profile = chrome_details_.GetProfile();
231 TemplateURLService* template_url_service =
232 TemplateURLServiceFactory::GetForProfile(profile);
233
234 scoped_ptr<search_engines_private::HotwordState> state(
235 new search_engines_private::HotwordState());
236
237 bool is_search_provider_google = false;
238 if (template_url_service && template_url_service->loaded()) {
239 const TemplateURL* default_url =
240 template_url_service->GetDefaultSearchProvider();
241 if (default_url &&
242 default_url->HasGoogleBaseURLs(
243 template_url_service->search_terms_data())) {
244 is_search_provider_google = true;
245 } else {
246 // If the user has chosen a default search provide other than Google, turn
247 // off hotwording since other providers don't provide that functionality.
248 HotwordService* hotword_service =
249 HotwordServiceFactory::GetForProfile(profile);
250 if (hotword_service)
251 hotword_service->DisableHotwordPreferences();
stevenjb 2015/04/28 17:16:38 This seems like a strange place to do this; effect
Oren Blasberg 2015/04/28 18:54:26 disclaimer: Lots of this code is copied/adapted fr
252 }
253 }
254
255 if (!HotwordServiceFactory::IsHotwordAllowed(profile) ||
256 !is_search_provider_google) {
257 return RespondNow(OneArgument(state->ToValue().release()));
stevenjb 2015/04/28 17:16:38 It looks like we could replace the clause above wi
Oren Blasberg 2015/04/28 21:58:30 I don't quite see how this is equivalent. Which cl
258 }
259
260 state->availability.push_back(
261 search_engines_private::HotwordFeature::HOTWORD_FEATURE_SEARCH);
262
263 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
264 bool authenticated = signin && signin->IsAuthenticated();
265 bool always_on =
266 HotwordServiceFactory::IsAlwaysOnAvailable() && authenticated;
267
268 if (always_on) {
269 state->availability.push_back(
270 search_engines_private::HotwordFeature::HOTWORD_FEATURE_ALWAYS_ON);
271 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
272 state->availability.push_back(
273 search_engines_private::HotwordFeature::HOTWORD_FEATURE_RETRAIN_LINK);
274 }
275 }
276
277 int error = HotwordServiceFactory::GetCurrentError(profile);
278 if (error) {
279 base::string16 hotword_help_url =
280 base::ASCIIToUTF16(chrome::kHotwordLearnMoreURL);
281 std::string error_message(l10n_util::GetStringUTF8(error));
282 if (error == IDS_HOTWORD_GENERIC_ERROR_MESSAGE) {
283 error_message = l10n_util::GetStringFUTF8(error, hotword_help_url);
284 }
285 state->error_msg = scoped_ptr<std::string>(&error_message);
stevenjb 2015/04/28 17:16:38 state->error_msg.reset(new std::string(error_messa
Oren Blasberg 2015/04/28 18:44:00 Done.
286 }
287
288 // Audio history should be displayed if it's enabled regardless of the
289 // hotword error state if the user is signed in. If the user is not signed
290 // in, audio history is meaningless. This is only displayed if always-on
291 // hotwording is available.
292 if (authenticated && always_on) {
293 std::string user_display_name = signin->GetAuthenticatedUsername();
294 DCHECK(!user_display_name.empty());
295 base::string16 audio_history_state =
296 l10n_util::GetStringFUTF16(IDS_HOTWORD_AUDIO_HISTORY_ENABLED,
297 base::ASCIIToUTF16(user_display_name));
298 HotwordService* hotword_service =
299 HotwordServiceFactory::GetForProfile(profile);
300 if (hotword_service) {
301 hotword_service->GetAudioHistoryHandler()->GetAudioHistoryEnabled(
302 base::Bind(&SearchEnginesPrivateGetHotwordStateFunction::
303 OnAudioHistoryChecked,
304 weak_ptr_factory_.GetWeakPtr(), base::Passed(&state),
305 audio_history_state));
306 return RespondLater();
307 }
308 }
309
310 return RespondNow(OneArgument(state->ToValue().release()));
311 }
312
313 void SearchEnginesPrivateGetHotwordStateFunction::OnAudioHistoryChecked(
314 scoped_ptr<search_engines_private::HotwordState> state,
315 const base::string16& audio_history_state,
316 bool success,
317 bool logging_enabled) {
318 if (success && logging_enabled) {
319 state->availability.push_back(
320 search_engines_private::HotwordFeature::HOTWORD_FEATURE_AUDIO_HISTORY);
321 std::string audio_history_state_str(
322 base::UTF16ToASCII(audio_history_state));
323 state->audio_history_state = make_scoped_ptr(&audio_history_state_str);
324 }
325 Respond(OneArgument(state->ToValue().release()));
326 }
327
328 ////////////////////////////////////////////////////////////////////////////////
329 // SearchEnginesPrivateOptIntoHotwordingFunction
330
331 SearchEnginesPrivateOptIntoHotwordingFunction::
332 SearchEnginesPrivateOptIntoHotwordingFunction()
333 : chrome_details_(this) {
334 }
335
336 SearchEnginesPrivateOptIntoHotwordingFunction::
337 ~SearchEnginesPrivateOptIntoHotwordingFunction() {
338 }
339
340 ExtensionFunction::ResponseAction
341 SearchEnginesPrivateOptIntoHotwordingFunction::Run() {
342 scoped_ptr<search_engines_private::OptIntoHotwording::Params> parameters =
343 search_engines_private::OptIntoHotwording::Params::Create(*args_);
344 EXTENSION_FUNCTION_VALIDATE(parameters.get());
345
346 Profile* profile = chrome_details_.GetProfile();
347
348 HotwordService::LaunchMode launch_mode =
349 HotwordService::HOTWORD_AND_AUDIO_HISTORY;
350
351 if (parameters->retrain) {
352 DCHECK(
353 profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled));
354 DCHECK(profile->GetPrefs()->GetBoolean(prefs::kHotwordAudioLoggingEnabled));
stevenjb 2015/04/28 17:16:39 Since this is an API we should set an error and fa
Oren Blasberg 2015/04/28 18:44:00 Done (setting error) -- Q: How do I "fail"? I can'
stevenjb 2015/04/28 19:06:03 I think you just set 'error_ = errordesc' then Sen
Oren Blasberg 2015/04/28 21:58:30 Done.
355
356 launch_mode = HotwordService::RETRAIN;
357 } else if (profile->GetPrefs()->GetBoolean(
358 prefs::kHotwordAudioLoggingEnabled)) {
359 DCHECK(
360 !profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled));
stevenjb 2015/04/28 17:16:38 Sane here.
Oren Blasberg 2015/04/28 21:58:30 Done.
361 launch_mode = HotwordService::HOTWORD_ONLY;
362 } else {
363 DCHECK(
364 !profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled));
stevenjb 2015/04/28 17:16:38 And here.
Oren Blasberg 2015/04/28 21:58:30 Done.
365 }
366
367 HotwordService* hotword_service =
368 HotwordServiceFactory::GetForProfile(profile);
369 if (hotword_service)
370 hotword_service->OptIntoHotwording(launch_mode);
371 return RespondNow(NoArguments());
372 }
373
79 } // namespace extensions 374 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698