| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/search_engines_private/search_engines_pr
ivate_api.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/extensions/chrome_extension_function.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/search/hotword_audio_history_handler.h" | |
| 14 #include "chrome/browser/search/hotword_service.h" | |
| 15 #include "chrome/browser/search/hotword_service_factory.h" | |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 18 #include "chrome/common/pref_names.h" | |
| 19 #include "chrome/common/url_constants.h" | |
| 20 #include "chrome/grit/generated_resources.h" | |
| 21 #include "components/prefs/pref_service.h" | |
| 22 #include "components/search_engines/template_url_service.h" | |
| 23 #include "components/signin/core/browser/signin_manager.h" | |
| 24 #include "components/signin/core/browser/signin_manager_base.h" | |
| 25 #include "extensions/browser/extension_function_registry.h" | |
| 26 #include "ui/base/l10n/l10n_util.h" | |
| 27 | |
| 28 namespace extensions { | |
| 29 | |
| 30 namespace search_engines_private = api::search_engines_private; | |
| 31 | |
| 32 const char kHotwordServiceMissing[] = "Cannot retrieve hotword service"; | |
| 33 const char kRetrainWithoutAlwaysOnEnabledError[] = | |
| 34 "Cannot retrain without always search and audio logging enabled"; | |
| 35 const char kOptInWithAudioLoggingError[] = | |
| 36 "Cannot opt in with audio logging but also with always on enabled"; | |
| 37 const char kAlreadyOptedInError[] = "Cannot opt in if already opted in"; | |
| 38 | |
| 39 //////////////////////////////////////////////////////////////////////////////// | |
| 40 // SearchEnginesPrivateGetSearchEnginesFunction | |
| 41 | |
| 42 SearchEnginesPrivateGetSearchEnginesFunction:: | |
| 43 SearchEnginesPrivateGetSearchEnginesFunction() | |
| 44 : chrome_details_(this) { | |
| 45 } | |
| 46 | |
| 47 SearchEnginesPrivateGetSearchEnginesFunction:: | |
| 48 ~SearchEnginesPrivateGetSearchEnginesFunction() { | |
| 49 } | |
| 50 | |
| 51 ExtensionFunction::ResponseAction | |
| 52 SearchEnginesPrivateGetSearchEnginesFunction::Run() { | |
| 53 TemplateURLService* template_url_service = | |
| 54 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 55 base::ListValue* engines = new base::ListValue(); | |
| 56 | |
| 57 const TemplateURL* default_url = | |
| 58 template_url_service->GetDefaultSearchProvider(); | |
| 59 std::vector<TemplateURL*> urls = template_url_service->GetTemplateURLs(); | |
| 60 for (size_t i = 0; i < urls.size(); i++) { | |
| 61 search_engines_private::SearchEngine engine; | |
| 62 engine.guid = urls[i]->sync_guid(); | |
| 63 engine.name = base::UTF16ToUTF8(urls[i]->short_name()); | |
| 64 engine.keyword = base::UTF16ToUTF8(urls[i]->keyword()); | |
| 65 engine.url = urls[i]->url(); | |
| 66 engine.type = urls[i]->show_in_default_list() | |
| 67 ? search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_DEFAULT | |
| 68 : search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_OTHER; | |
| 69 | |
| 70 if (urls[i] == default_url) | |
| 71 engine.is_selected = scoped_ptr<bool>(new bool(true)); | |
| 72 | |
| 73 engines->Append(engine.ToValue().release()); | |
| 74 } | |
| 75 | |
| 76 return RespondNow(OneArgument(engines)); | |
| 77 } | |
| 78 | |
| 79 //////////////////////////////////////////////////////////////////////////////// | |
| 80 // SearchEnginesPrivateSetSelectedSearchEngineFunction | |
| 81 | |
| 82 SearchEnginesPrivateSetSelectedSearchEngineFunction:: | |
| 83 SearchEnginesPrivateSetSelectedSearchEngineFunction() | |
| 84 : chrome_details_(this) { | |
| 85 } | |
| 86 | |
| 87 SearchEnginesPrivateSetSelectedSearchEngineFunction:: | |
| 88 ~SearchEnginesPrivateSetSelectedSearchEngineFunction() { | |
| 89 } | |
| 90 | |
| 91 ExtensionFunction::ResponseAction | |
| 92 SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() { | |
| 93 scoped_ptr<search_engines_private::SetSelectedSearchEngine::Params> | |
| 94 parameters = | |
| 95 search_engines_private::SetSelectedSearchEngine::Params::Create( | |
| 96 *args_); | |
| 97 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | |
| 98 | |
| 99 TemplateURLService* template_url_service = | |
| 100 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 101 template_url_service->SetUserSelectedDefaultSearchProvider( | |
| 102 template_url_service->GetTemplateURLForGUID(parameters->guid)); | |
| 103 return RespondNow(NoArguments()); | |
| 104 } | |
| 105 | |
| 106 //////////////////////////////////////////////////////////////////////////////// | |
| 107 // SearchEnginesPrivateAddOtherSearchEngineFunction | |
| 108 | |
| 109 SearchEnginesPrivateAddOtherSearchEngineFunction:: | |
| 110 SearchEnginesPrivateAddOtherSearchEngineFunction() | |
| 111 : chrome_details_(this) { | |
| 112 } | |
| 113 | |
| 114 SearchEnginesPrivateAddOtherSearchEngineFunction:: | |
| 115 ~SearchEnginesPrivateAddOtherSearchEngineFunction() { | |
| 116 } | |
| 117 | |
| 118 ExtensionFunction::ResponseAction | |
| 119 SearchEnginesPrivateAddOtherSearchEngineFunction::Run() { | |
| 120 scoped_ptr<search_engines_private::AddOtherSearchEngine::Params> parameters = | |
| 121 search_engines_private::AddOtherSearchEngine::Params::Create(*args_); | |
| 122 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | |
| 123 | |
| 124 TemplateURLData data; | |
| 125 data.SetShortName(base::UTF8ToUTF16(parameters->name)); | |
| 126 data.SetKeyword(base::UTF8ToUTF16(parameters->keyword)); | |
| 127 data.SetURL(parameters->url); | |
| 128 TemplateURL* turl = new TemplateURL(data); | |
| 129 | |
| 130 TemplateURLService* template_url_service = | |
| 131 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 132 template_url_service->Add(turl); | |
| 133 return RespondNow(NoArguments()); | |
| 134 } | |
| 135 | |
| 136 //////////////////////////////////////////////////////////////////////////////// | |
| 137 // SearchEnginesPrivateUpdateSearchEngineFunction | |
| 138 | |
| 139 SearchEnginesPrivateUpdateSearchEngineFunction:: | |
| 140 SearchEnginesPrivateUpdateSearchEngineFunction() | |
| 141 : chrome_details_(this) { | |
| 142 } | |
| 143 | |
| 144 SearchEnginesPrivateUpdateSearchEngineFunction:: | |
| 145 ~SearchEnginesPrivateUpdateSearchEngineFunction() { | |
| 146 } | |
| 147 | |
| 148 ExtensionFunction::ResponseAction | |
| 149 SearchEnginesPrivateUpdateSearchEngineFunction::Run() { | |
| 150 scoped_ptr<search_engines_private::UpdateSearchEngine::Params> parameters = | |
| 151 search_engines_private::UpdateSearchEngine::Params::Create(*args_); | |
| 152 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | |
| 153 | |
| 154 TemplateURLService* template_url_service = | |
| 155 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 156 TemplateURL* turl = | |
| 157 template_url_service->GetTemplateURLForGUID(parameters->guid); | |
| 158 | |
| 159 template_url_service->ResetTemplateURL( | |
| 160 turl, base::UTF8ToUTF16(parameters->name), | |
| 161 base::UTF8ToUTF16(parameters->keyword), parameters->url); | |
| 162 | |
| 163 return RespondNow(NoArguments()); | |
| 164 } | |
| 165 | |
| 166 //////////////////////////////////////////////////////////////////////////////// | |
| 167 // SearchEnginesPrivateRemoveSearchEngineFunction | |
| 168 | |
| 169 SearchEnginesPrivateRemoveSearchEngineFunction:: | |
| 170 SearchEnginesPrivateRemoveSearchEngineFunction() | |
| 171 : chrome_details_(this) { | |
| 172 } | |
| 173 | |
| 174 SearchEnginesPrivateRemoveSearchEngineFunction:: | |
| 175 ~SearchEnginesPrivateRemoveSearchEngineFunction() { | |
| 176 } | |
| 177 | |
| 178 ExtensionFunction::ResponseAction | |
| 179 SearchEnginesPrivateRemoveSearchEngineFunction::Run() { | |
| 180 scoped_ptr<search_engines_private::RemoveSearchEngine::Params> parameters = | |
| 181 search_engines_private::RemoveSearchEngine::Params::Create(*args_); | |
| 182 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | |
| 183 | |
| 184 TemplateURLService* template_url_service = | |
| 185 TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile()); | |
| 186 TemplateURL* turl = | |
| 187 template_url_service->GetTemplateURLForGUID(parameters->guid); | |
| 188 template_url_service->Remove(turl); | |
| 189 return RespondNow(NoArguments()); | |
| 190 } | |
| 191 | |
| 192 //////////////////////////////////////////////////////////////////////////////// | |
| 193 // SearchEnginesPrivateGetHotwordStateFunction | |
| 194 | |
| 195 SearchEnginesPrivateGetHotwordStateFunction:: | |
| 196 SearchEnginesPrivateGetHotwordStateFunction() | |
| 197 : chrome_details_(this), weak_ptr_factory_(this) { | |
| 198 } | |
| 199 | |
| 200 SearchEnginesPrivateGetHotwordStateFunction:: | |
| 201 ~SearchEnginesPrivateGetHotwordStateFunction() { | |
| 202 } | |
| 203 | |
| 204 ExtensionFunction::ResponseAction | |
| 205 SearchEnginesPrivateGetHotwordStateFunction::Run() { | |
| 206 Profile* profile = chrome_details_.GetProfile(); | |
| 207 TemplateURLService* template_url_service = | |
| 208 TemplateURLServiceFactory::GetForProfile(profile); | |
| 209 | |
| 210 scoped_ptr<search_engines_private::HotwordState> state( | |
| 211 new search_engines_private::HotwordState()); | |
| 212 | |
| 213 if (template_url_service && template_url_service->loaded()) { | |
| 214 const TemplateURL* default_url = | |
| 215 template_url_service->GetDefaultSearchProvider(); | |
| 216 if (!default_url || | |
| 217 !default_url->HasGoogleBaseURLs( | |
| 218 template_url_service->search_terms_data()) || | |
| 219 !HotwordServiceFactory::IsHotwordAllowed(profile)) { | |
| 220 return RespondNow(OneArgument(state->ToValue().release())); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 state->availability.push_back( | |
| 225 search_engines_private::HotwordFeature::HOTWORD_FEATURE_SEARCH); | |
| 226 | |
| 227 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
| 228 bool authenticated = signin && signin->IsAuthenticated(); | |
| 229 bool always_on = | |
| 230 HotwordServiceFactory::IsAlwaysOnAvailable() && authenticated; | |
| 231 | |
| 232 if (always_on) { | |
| 233 state->availability.push_back( | |
| 234 search_engines_private::HotwordFeature::HOTWORD_FEATURE_ALWAYS_ON); | |
| 235 if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) { | |
| 236 state->availability.push_back( | |
| 237 search_engines_private::HotwordFeature::HOTWORD_FEATURE_RETRAIN_LINK); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 int error = HotwordServiceFactory::GetCurrentError(profile); | |
| 242 if (error) { | |
| 243 std::string error_message(l10n_util::GetStringUTF8(error)); | |
| 244 if (error == IDS_HOTWORD_GENERIC_ERROR_MESSAGE) { | |
| 245 error_message = l10n_util::GetStringFUTF8( | |
| 246 error, base::UTF8ToUTF16(chrome::kHotwordLearnMoreURL)); | |
| 247 } | |
| 248 state->error_msg.reset(new std::string(error_message)); | |
| 249 } | |
| 250 | |
| 251 // Audio history should be displayed if it's enabled regardless of the | |
| 252 // hotword error state if the user is signed in. If the user is not signed | |
| 253 // in, audio history is meaningless. This is only displayed if always-on | |
| 254 // hotwording is available. | |
| 255 if (authenticated && always_on) { | |
| 256 std::string user_display_name = signin->GetAuthenticatedAccountInfo().email; | |
| 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::UTF8ToUTF16(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::UTF16ToUTF8(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 | |
| 340 } // namespace extensions | |
| OLD | NEW |