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

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: Address comments Created 5 years, 7 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 // SearchEnginesPrivateGetSearchEnginesFunction
20 31
21 SearchEnginesPrivateGetDefaultSearchEnginesFunction:: 32 SearchEnginesPrivateGetSearchEnginesFunction::
22 SearchEnginesPrivateGetDefaultSearchEnginesFunction() 33 SearchEnginesPrivateGetSearchEnginesFunction()
23 : chrome_details_(this) { 34 : chrome_details_(this) {
24 } 35 }
25 36
26 SearchEnginesPrivateGetDefaultSearchEnginesFunction:: 37 SearchEnginesPrivateGetSearchEnginesFunction::
27 ~SearchEnginesPrivateGetDefaultSearchEnginesFunction() { 38 ~SearchEnginesPrivateGetSearchEnginesFunction() {
28 } 39 }
29 40
30 ExtensionFunction::ResponseAction 41 ExtensionFunction::ResponseAction
31 SearchEnginesPrivateGetDefaultSearchEnginesFunction::Run() { 42 SearchEnginesPrivateGetSearchEnginesFunction::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 search_engines_private::SearchEngine engine;
41 engine.guid = urls[i]->sync_guid(); 52 engine.guid = urls[i]->sync_guid();
42 engine.name = base::UTF16ToASCII(urls[i]->short_name()); 53 engine.name = base::UTF16ToASCII(urls[i]->short_name());
54 engine.keyword = base::UTF16ToASCII(urls[i]->keyword());
55 engine.url = urls[i]->url();
56 engine.type = urls[i]->show_in_default_list()
57 ? search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_DEFAULT
58 : search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_OTHER;
59
43 if (urls[i] == default_url) 60 if (urls[i] == default_url)
44 engine.is_selected = scoped_ptr<bool>(new bool(true)); 61 engine.is_selected = scoped_ptr<bool>(new bool(true));
45 62
46 engines->Append(engine.ToValue().release()); 63 engines->Append(engine.ToValue().release());
47 } 64 }
48 65
49 return RespondNow(OneArgument(engines)); 66 return RespondNow(OneArgument(engines));
50 } 67 }
51 68
52 //////////////////////////////////////////////////////////////////////////////// 69 ////////////////////////////////////////////////////////////////////////////////
53 // SearchEnginesPrivateSetSelectedSearchEngineFunction 70 // SearchEnginesPrivateSetSelectedSearchEngineFunction
54 71
55 SearchEnginesPrivateSetSelectedSearchEngineFunction:: 72 SearchEnginesPrivateSetSelectedSearchEngineFunction::
56 SearchEnginesPrivateSetSelectedSearchEngineFunction() 73 SearchEnginesPrivateSetSelectedSearchEngineFunction()
57 : chrome_details_(this) { 74 : chrome_details_(this) {
58 } 75 }
59 76
60 SearchEnginesPrivateSetSelectedSearchEngineFunction:: 77 SearchEnginesPrivateSetSelectedSearchEngineFunction::
61 ~SearchEnginesPrivateSetSelectedSearchEngineFunction() { 78 ~SearchEnginesPrivateSetSelectedSearchEngineFunction() {
62 } 79 }
63 80
64 ExtensionFunction::ResponseAction 81 ExtensionFunction::ResponseAction
65 SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() { 82 SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() {
66 scoped_ptr<api::search_engines_private::SetSelectedSearchEngine::Params> 83 scoped_ptr<search_engines_private::SetSelectedSearchEngine::Params>
67 parameters = 84 parameters =
68 api::search_engines_private::SetSelectedSearchEngine::Params::Create( 85 search_engines_private::SetSelectedSearchEngine::Params::Create(
69 *args_); 86 *args_);
70 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 87 EXTENSION_FUNCTION_VALIDATE(parameters.get());
71 88
72 TemplateURLService* template_url_service = 89 TemplateURLService* template_url_service =
73 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); 90 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
74 template_url_service->SetUserSelectedDefaultSearchProvider( 91 template_url_service->SetUserSelectedDefaultSearchProvider(
75 template_url_service->GetTemplateURLForGUID(parameters->guid)); 92 template_url_service->GetTemplateURLForGUID(parameters->guid));
76 return RespondNow(NoArguments()); 93 return RespondNow(NoArguments());
77 } 94 }
78 95
96 ////////////////////////////////////////////////////////////////////////////////
97 // SearchEnginesPrivateAddOtherSearchEngineFunction
98
99 SearchEnginesPrivateAddOtherSearchEngineFunction::
100 SearchEnginesPrivateAddOtherSearchEngineFunction()
101 : chrome_details_(this) {
102 }
103
104 SearchEnginesPrivateAddOtherSearchEngineFunction::
105 ~SearchEnginesPrivateAddOtherSearchEngineFunction() {
106 }
107
108 ExtensionFunction::ResponseAction
109 SearchEnginesPrivateAddOtherSearchEngineFunction::Run() {
110 scoped_ptr<search_engines_private::AddOtherSearchEngine::Params> parameters =
111 search_engines_private::AddOtherSearchEngine::Params::Create(*args_);
112 EXTENSION_FUNCTION_VALIDATE(parameters.get());
113
114 TemplateURLData data;
115 data.short_name = base::ASCIIToUTF16(parameters->name);
116 data.SetKeyword(base::ASCIIToUTF16(parameters->keyword));
117 data.SetURL(parameters->url);
118 TemplateURL* turl = new TemplateURL(data);
119
120 TemplateURLService* template_url_service =
121 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
122 template_url_service->Add(turl);
123 return RespondNow(NoArguments());
124 }
125
126 ////////////////////////////////////////////////////////////////////////////////
127 // SearchEnginesPrivateUpdateSearchEngineFunction
128
129 SearchEnginesPrivateUpdateSearchEngineFunction::
130 SearchEnginesPrivateUpdateSearchEngineFunction()
131 : chrome_details_(this) {
132 }
133
134 SearchEnginesPrivateUpdateSearchEngineFunction::
135 ~SearchEnginesPrivateUpdateSearchEngineFunction() {
136 }
137
138 ExtensionFunction::ResponseAction
139 SearchEnginesPrivateUpdateSearchEngineFunction::Run() {
140 scoped_ptr<search_engines_private::UpdateSearchEngine::Params> parameters =
141 search_engines_private::UpdateSearchEngine::Params::Create(*args_);
142 EXTENSION_FUNCTION_VALIDATE(parameters.get());
143
144 TemplateURLService* template_url_service =
145 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
146 TemplateURL* turl =
147 template_url_service->GetTemplateURLForGUID(parameters->guid);
148
149 template_url_service->ResetTemplateURL(
150 turl, base::ASCIIToUTF16(parameters->name),
151 base::ASCIIToUTF16(parameters->keyword), parameters->url);
152
153 return RespondNow(NoArguments());
154 }
155
156 ////////////////////////////////////////////////////////////////////////////////
157 // SearchEnginesPrivateRemoveSearchEngineFunction
158
159 SearchEnginesPrivateRemoveSearchEngineFunction::
160 SearchEnginesPrivateRemoveSearchEngineFunction()
161 : chrome_details_(this) {
162 }
163
164 SearchEnginesPrivateRemoveSearchEngineFunction::
165 ~SearchEnginesPrivateRemoveSearchEngineFunction() {
166 }
167
168 ExtensionFunction::ResponseAction
169 SearchEnginesPrivateRemoveSearchEngineFunction::Run() {
170 scoped_ptr<search_engines_private::RemoveSearchEngine::Params> parameters =
171 search_engines_private::RemoveSearchEngine::Params::Create(*args_);
172 EXTENSION_FUNCTION_VALIDATE(parameters.get());
173
174 TemplateURLService* template_url_service =
175 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
176 TemplateURL* turl =
177 template_url_service->GetTemplateURLForGUID(parameters->guid);
178 template_url_service->Remove(turl);
179 return RespondNow(NoArguments());
180 }
181
182 ////////////////////////////////////////////////////////////////////////////////
183 // SearchEnginesPrivateGetHotwordStateFunction
184
185 SearchEnginesPrivateGetHotwordStateFunction::
186 SearchEnginesPrivateGetHotwordStateFunction()
187 : chrome_details_(this), weak_ptr_factory_(this) {
188 }
189
190 SearchEnginesPrivateGetHotwordStateFunction::
191 ~SearchEnginesPrivateGetHotwordStateFunction() {
192 }
193
194 ExtensionFunction::ResponseAction
195 SearchEnginesPrivateGetHotwordStateFunction::Run() {
196 Profile* profile = chrome_details_.GetProfile();
197 TemplateURLService* template_url_service =
198 TemplateURLServiceFactory::GetForProfile(profile);
199
200 scoped_ptr<search_engines_private::HotwordState> state(
201 new search_engines_private::HotwordState());
202
203 bool is_search_provider_google = false;
204 if (template_url_service && template_url_service->loaded()) {
205 const TemplateURL* default_url =
206 template_url_service->GetDefaultSearchProvider();
207 if (default_url &&
208 default_url->HasGoogleBaseURLs(
209 template_url_service->search_terms_data())) {
210 is_search_provider_google = true;
211 } else {
212 // If the user has chosen a default search provide other than Google, turn
213 // off hotwording since other providers don't provide that functionality.
214 HotwordService* hotword_service =
215 HotwordServiceFactory::GetForProfile(profile);
216 if (hotword_service)
217 hotword_service->DisableHotwordPreferences();
218 }
219 }
220
221 if (!HotwordServiceFactory::IsHotwordAllowed(profile) ||
222 !is_search_provider_google) {
223 return RespondNow(OneArgument(state->ToValue().release()));
224 }
225
226 state->availability.push_back(
227 search_engines_private::HotwordFeature::HOTWORD_FEATURE_SEARCH);
228
229 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
230 bool authenticated = signin && signin->IsAuthenticated();
231 bool always_on =
232 HotwordServiceFactory::IsAlwaysOnAvailable() && authenticated;
233
234 if (always_on) {
235 state->availability.push_back(
236 search_engines_private::HotwordFeature::HOTWORD_FEATURE_ALWAYS_ON);
237 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
238 state->availability.push_back(
239 search_engines_private::HotwordFeature::HOTWORD_FEATURE_RETRAIN_LINK);
240 }
241 }
242
243 int error = HotwordServiceFactory::GetCurrentError(profile);
244 if (error) {
245 base::string16 hotword_help_url =
246 base::ASCIIToUTF16(chrome::kHotwordLearnMoreURL);
stevenjb 2015/04/28 22:27:11 Since this is only used inside the if() below, we
Oren Blasberg 2015/04/29 00:24:59 Done.
247 std::string error_message(l10n_util::GetStringUTF8(error));
248 if (error == IDS_HOTWORD_GENERIC_ERROR_MESSAGE) {
249 error_message = l10n_util::GetStringFUTF8(error, hotword_help_url);
250 }
251 state->error_msg.reset(new std::string(error_message));
252 }
253
254 // Audio history should be displayed if it's enabled regardless of the
255 // hotword error state if the user is signed in. If the user is not signed
256 // in, audio history is meaningless. This is only displayed if always-on
257 // hotwording is available.
258 if (authenticated && always_on) {
259 std::string user_display_name = signin->GetAuthenticatedUsername();
260 DCHECK(!user_display_name.empty());
stevenjb 2015/04/28 22:27:11 I realize this was mostly copied from browser_opti
Oren Blasberg 2015/04/29 00:24:59 Thanks for reviewing this code thoroughly :) Done.
261 base::string16 audio_history_state =
262 l10n_util::GetStringFUTF16(IDS_HOTWORD_AUDIO_HISTORY_ENABLED,
263 base::ASCIIToUTF16(user_display_name));
stevenjb 2015/04/28 22:27:11 Here also, declare where it is used or inline. No
Oren Blasberg 2015/04/29 00:24:59 Done.
264 HotwordService* hotword_service =
265 HotwordServiceFactory::GetForProfile(profile);
266 if (hotword_service) {
267 hotword_service->GetAudioHistoryHandler()->GetAudioHistoryEnabled(
268 base::Bind(&SearchEnginesPrivateGetHotwordStateFunction::
269 OnAudioHistoryChecked,
270 weak_ptr_factory_.GetWeakPtr(), base::Passed(&state),
271 audio_history_state));
272 return RespondLater();
273 }
274 }
275
276 return RespondNow(OneArgument(state->ToValue().release()));
277 }
278
279 void SearchEnginesPrivateGetHotwordStateFunction::OnAudioHistoryChecked(
280 scoped_ptr<search_engines_private::HotwordState> state,
281 const base::string16& audio_history_state,
282 bool success,
283 bool logging_enabled) {
284 if (success && logging_enabled) {
285 state->availability.push_back(
286 search_engines_private::HotwordFeature::HOTWORD_FEATURE_AUDIO_HISTORY);
287 std::string audio_history_state_str(
288 base::UTF16ToASCII(audio_history_state));
289 state->audio_history_state = make_scoped_ptr(&audio_history_state_str);
stevenjb 2015/04/28 22:27:11 state->audio_history_state(new std::string(base::U
Oren Blasberg 2015/04/29 00:24:59 Done. (assuming you meant to have a .reset in ther
290 }
291 Respond(OneArgument(state->ToValue().release()));
292 }
293
294 ////////////////////////////////////////////////////////////////////////////////
295 // SearchEnginesPrivateOptIntoHotwordingFunction
296
297 SearchEnginesPrivateOptIntoHotwordingFunction::
298 SearchEnginesPrivateOptIntoHotwordingFunction()
299 : chrome_details_(this) {
300 }
301
302 SearchEnginesPrivateOptIntoHotwordingFunction::
303 ~SearchEnginesPrivateOptIntoHotwordingFunction() {
304 }
305
306 ExtensionFunction::ResponseAction
307 SearchEnginesPrivateOptIntoHotwordingFunction::Run() {
308 scoped_ptr<search_engines_private::OptIntoHotwording::Params> parameters =
309 search_engines_private::OptIntoHotwording::Params::Create(*args_);
310 EXTENSION_FUNCTION_VALIDATE(parameters.get());
311
312 Profile* profile = chrome_details_.GetProfile();
313
314 HotwordService::LaunchMode launch_mode =
315 HotwordService::HOTWORD_AND_AUDIO_HISTORY;
316
317 if (parameters->retrain) {
318 if (!profile->GetPrefs()->GetBoolean(
319 prefs::kHotwordAlwaysOnSearchEnabled) ||
320 !profile->GetPrefs()->GetBoolean(
stevenjb 2015/04/28 22:27:11 nit: we call profile->GetPrefs() a bunch in this m
Oren Blasberg 2015/04/29 00:24:59 Done.
321 prefs::kHotwordAudioLoggingEnabled)) {
322 return RespondNow(Error(
323 "Cannot retrain without always search and audio logging enabled"));
stevenjb 2015/04/28 22:27:11 For error messages passed in an API we should use
Oren Blasberg 2015/04/29 00:24:59 Done.
324 }
325
326 launch_mode = HotwordService::RETRAIN;
327 } else if (profile->GetPrefs()->GetBoolean(
328 prefs::kHotwordAudioLoggingEnabled)) {
329 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
330 return RespondNow(Error(
331 "Cannot opt in with audio logging but also with always on enabled"));
332
333 }
334 launch_mode = HotwordService::HOTWORD_ONLY;
335 } else {
336 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled))
337 return RespondNow(Error(
338 "Cannot opt in if already opted in"));
339 }
340
341 HotwordService* hotword_service =
342 HotwordServiceFactory::GetForProfile(profile);
343 if (hotword_service)
stevenjb 2015/04/28 22:27:11 nit: if (!hotword_service) all of the above is poi
Oren Blasberg 2015/04/29 00:24:59 Done.
344 hotword_service->OptIntoHotwording(launch_mode);
345 return RespondNow(NoArguments());
346 }
347
79 } // namespace extensions 348 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698