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