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