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

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: Remove DisableHotwordPreferences call 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 }
212 }
213
214 if (!HotwordServiceFactory::IsHotwordAllowed(profile) ||
215 !is_search_provider_google) {
216 return RespondNow(OneArgument(state->ToValue().release()));
stevenjb 2015/04/28 22:27:12 We can now eliminate is_search_provider_google and
Oren Blasberg 2015/04/29 00:24:59 Done.
217 }
218
219 state->availability.push_back(
220 search_engines_private::HotwordFeature::HOTWORD_FEATURE_SEARCH);
221
222 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
223 bool authenticated = signin && signin->IsAuthenticated();
224 bool always_on =
225 HotwordServiceFactory::IsAlwaysOnAvailable() && authenticated;
226
227 if (always_on) {
228 state->availability.push_back(
229 search_engines_private::HotwordFeature::HOTWORD_FEATURE_ALWAYS_ON);
230 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
231 state->availability.push_back(
232 search_engines_private::HotwordFeature::HOTWORD_FEATURE_RETRAIN_LINK);
233 }
234 }
235
236 int error = HotwordServiceFactory::GetCurrentError(profile);
237 if (error) {
238 base::string16 hotword_help_url =
239 base::ASCIIToUTF16(chrome::kHotwordLearnMoreURL);
240 std::string error_message(l10n_util::GetStringUTF8(error));
241 if (error == IDS_HOTWORD_GENERIC_ERROR_MESSAGE) {
242 error_message = l10n_util::GetStringFUTF8(error, hotword_help_url);
243 }
244 state->error_msg.reset(new std::string(error_message));
245 }
246
247 // Audio history should be displayed if it's enabled regardless of the
248 // hotword error state if the user is signed in. If the user is not signed
249 // in, audio history is meaningless. This is only displayed if always-on
250 // hotwording is available.
251 if (authenticated && always_on) {
252 std::string user_display_name = signin->GetAuthenticatedUsername();
253 DCHECK(!user_display_name.empty());
254 base::string16 audio_history_state =
255 l10n_util::GetStringFUTF16(IDS_HOTWORD_AUDIO_HISTORY_ENABLED,
256 base::ASCIIToUTF16(user_display_name));
257 HotwordService* hotword_service =
258 HotwordServiceFactory::GetForProfile(profile);
259 if (hotword_service) {
260 hotword_service->GetAudioHistoryHandler()->GetAudioHistoryEnabled(
261 base::Bind(&SearchEnginesPrivateGetHotwordStateFunction::
262 OnAudioHistoryChecked,
263 weak_ptr_factory_.GetWeakPtr(), base::Passed(&state),
264 audio_history_state));
265 return RespondLater();
266 }
267 }
268
269 return RespondNow(OneArgument(state->ToValue().release()));
270 }
271
272 void SearchEnginesPrivateGetHotwordStateFunction::OnAudioHistoryChecked(
273 scoped_ptr<search_engines_private::HotwordState> state,
274 const base::string16& audio_history_state,
275 bool success,
276 bool logging_enabled) {
277 if (success && logging_enabled) {
278 state->availability.push_back(
279 search_engines_private::HotwordFeature::HOTWORD_FEATURE_AUDIO_HISTORY);
280 std::string audio_history_state_str(
281 base::UTF16ToASCII(audio_history_state));
282 state->audio_history_state = make_scoped_ptr(&audio_history_state_str);
283 }
284 Respond(OneArgument(state->ToValue().release()));
285 }
286
287 ////////////////////////////////////////////////////////////////////////////////
288 // SearchEnginesPrivateOptIntoHotwordingFunction
289
290 SearchEnginesPrivateOptIntoHotwordingFunction::
291 SearchEnginesPrivateOptIntoHotwordingFunction()
292 : chrome_details_(this) {
293 }
294
295 SearchEnginesPrivateOptIntoHotwordingFunction::
296 ~SearchEnginesPrivateOptIntoHotwordingFunction() {
297 }
298
299 ExtensionFunction::ResponseAction
300 SearchEnginesPrivateOptIntoHotwordingFunction::Run() {
301 scoped_ptr<search_engines_private::OptIntoHotwording::Params> parameters =
302 search_engines_private::OptIntoHotwording::Params::Create(*args_);
303 EXTENSION_FUNCTION_VALIDATE(parameters.get());
304
305 Profile* profile = chrome_details_.GetProfile();
306
307 HotwordService::LaunchMode launch_mode =
308 HotwordService::HOTWORD_AND_AUDIO_HISTORY;
309
310 if (parameters->retrain) {
311 if (!profile->GetPrefs()->GetBoolean(
312 prefs::kHotwordAlwaysOnSearchEnabled) ||
313 !profile->GetPrefs()->GetBoolean(
314 prefs::kHotwordAudioLoggingEnabled)) {
315 return RespondNow(Error(
316 "Cannot retrain without always search and audio logging enabled"));
317 }
318
319 launch_mode = HotwordService::RETRAIN;
320 } else if (profile->GetPrefs()->GetBoolean(
321 prefs::kHotwordAudioLoggingEnabled)) {
322 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
323 return RespondNow(Error(
324 "Cannot opt in with audio logging but also with always on enabled"));
325
326 }
327 launch_mode = HotwordService::HOTWORD_ONLY;
328 } else {
329 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled))
330 return RespondNow(Error(
331 "Cannot opt in if already opted in"));
332 }
333
334 HotwordService* hotword_service =
335 HotwordServiceFactory::GetForProfile(profile);
336 if (hotword_service)
337 hotword_service->OptIntoHotwording(launch_mode);
338 return RespondNow(NoArguments());
339 }
340
79 } // namespace extensions 341 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698