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

Unified 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, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc
diff --git a/chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc b/chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc
index 6fc7e43bd37986da7b7e0778f079fdc4a1fc09a1..2b5b32e45642d94ad19a1f3ca33102c050f65b3e 100644
--- a/chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc
+++ b/chrome/browser/extensions/api/search_engines_private/search_engines_private_api.cc
@@ -4,31 +4,42 @@
#include "chrome/browser/extensions/api/search_engines_private/search_engines_private_api.h"
+#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/search/hotword_audio_history_handler.h"
+#include "chrome/browser/search/hotword_service.h"
+#include "chrome/browser/search/hotword_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
-#include "chrome/common/extensions/api/search_engines_private.h"
+#include "chrome/browser/signin/signin_manager_factory.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/url_constants.h"
+#include "chrome/grit/generated_resources.h"
#include "components/search_engines/template_url_service.h"
+#include "components/signin/core/browser/signin_manager_base.h"
#include "extensions/browser/extension_function_registry.h"
+#include "ui/base/l10n/l10n_util.h"
namespace extensions {
+namespace search_engines_private = api::search_engines_private;
+
////////////////////////////////////////////////////////////////////////////////
-// SearchEnginesPrivateGetDefaultSearchEnginesFunction
+// SearchEnginesPrivateGetSearchEnginesFunction
-SearchEnginesPrivateGetDefaultSearchEnginesFunction::
- SearchEnginesPrivateGetDefaultSearchEnginesFunction()
+SearchEnginesPrivateGetSearchEnginesFunction::
+ SearchEnginesPrivateGetSearchEnginesFunction()
: chrome_details_(this) {
}
-SearchEnginesPrivateGetDefaultSearchEnginesFunction::
- ~SearchEnginesPrivateGetDefaultSearchEnginesFunction() {
+SearchEnginesPrivateGetSearchEnginesFunction::
+ ~SearchEnginesPrivateGetSearchEnginesFunction() {
}
ExtensionFunction::ResponseAction
-SearchEnginesPrivateGetDefaultSearchEnginesFunction::Run() {
+SearchEnginesPrivateGetSearchEnginesFunction::Run() {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
base::ListValue* engines = new base::ListValue();
@@ -37,9 +48,15 @@ SearchEnginesPrivateGetDefaultSearchEnginesFunction::Run() {
template_url_service->GetDefaultSearchProvider();
std::vector<TemplateURL*> urls = template_url_service->GetTemplateURLs();
for (size_t i = 0; i < urls.size(); i++) {
- api::search_engines_private::SearchEngine engine;
+ search_engines_private::SearchEngine engine;
engine.guid = urls[i]->sync_guid();
engine.name = base::UTF16ToASCII(urls[i]->short_name());
+ engine.keyword = base::UTF16ToASCII(urls[i]->keyword());
+ engine.url = urls[i]->url();
+ engine.type = urls[i]->show_in_default_list()
+ ? search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_DEFAULT
+ : search_engines_private::SearchEngineType::SEARCH_ENGINE_TYPE_OTHER;
+
if (urls[i] == default_url)
engine.is_selected = scoped_ptr<bool>(new bool(true));
@@ -63,9 +80,9 @@ SearchEnginesPrivateSetSelectedSearchEngineFunction::
ExtensionFunction::ResponseAction
SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() {
- scoped_ptr<api::search_engines_private::SetSelectedSearchEngine::Params>
+ scoped_ptr<search_engines_private::SetSelectedSearchEngine::Params>
parameters =
- api::search_engines_private::SetSelectedSearchEngine::Params::Create(
+ search_engines_private::SetSelectedSearchEngine::Params::Create(
*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
@@ -76,4 +93,249 @@ SearchEnginesPrivateSetSelectedSearchEngineFunction::Run() {
return RespondNow(NoArguments());
}
+////////////////////////////////////////////////////////////////////////////////
+// SearchEnginesPrivateAddOtherSearchEngineFunction
+
+SearchEnginesPrivateAddOtherSearchEngineFunction::
+ SearchEnginesPrivateAddOtherSearchEngineFunction()
+ : chrome_details_(this) {
+}
+
+SearchEnginesPrivateAddOtherSearchEngineFunction::
+ ~SearchEnginesPrivateAddOtherSearchEngineFunction() {
+}
+
+ExtensionFunction::ResponseAction
+SearchEnginesPrivateAddOtherSearchEngineFunction::Run() {
+ scoped_ptr<search_engines_private::AddOtherSearchEngine::Params> parameters =
+ search_engines_private::AddOtherSearchEngine::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+
+ TemplateURLData data;
+ data.short_name = base::ASCIIToUTF16(parameters->name);
+ data.SetKeyword(base::ASCIIToUTF16(parameters->keyword));
+ data.SetURL(parameters->url);
+ TemplateURL* turl = new TemplateURL(data);
+
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
+ template_url_service->Add(turl);
+ return RespondNow(NoArguments());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// SearchEnginesPrivateUpdateSearchEngineFunction
+
+SearchEnginesPrivateUpdateSearchEngineFunction::
+ SearchEnginesPrivateUpdateSearchEngineFunction()
+ : chrome_details_(this) {
+}
+
+SearchEnginesPrivateUpdateSearchEngineFunction::
+ ~SearchEnginesPrivateUpdateSearchEngineFunction() {
+}
+
+ExtensionFunction::ResponseAction
+SearchEnginesPrivateUpdateSearchEngineFunction::Run() {
+ scoped_ptr<search_engines_private::UpdateSearchEngine::Params> parameters =
+ search_engines_private::UpdateSearchEngine::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
+ TemplateURL* turl =
+ template_url_service->GetTemplateURLForGUID(parameters->guid);
+
+ template_url_service->ResetTemplateURL(
+ turl, base::ASCIIToUTF16(parameters->name),
+ base::ASCIIToUTF16(parameters->keyword), parameters->url);
+
+ return RespondNow(NoArguments());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// SearchEnginesPrivateRemoveSearchEngineFunction
+
+SearchEnginesPrivateRemoveSearchEngineFunction::
+ SearchEnginesPrivateRemoveSearchEngineFunction()
+ : chrome_details_(this) {
+}
+
+SearchEnginesPrivateRemoveSearchEngineFunction::
+ ~SearchEnginesPrivateRemoveSearchEngineFunction() {
+}
+
+ExtensionFunction::ResponseAction
+SearchEnginesPrivateRemoveSearchEngineFunction::Run() {
+ scoped_ptr<search_engines_private::RemoveSearchEngine::Params> parameters =
+ search_engines_private::RemoveSearchEngine::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(chrome_details_.GetProfile());
+ TemplateURL* turl =
+ template_url_service->GetTemplateURLForGUID(parameters->guid);
+ template_url_service->Remove(turl);
+ return RespondNow(NoArguments());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// SearchEnginesPrivateGetHotwordStateFunction
+
+SearchEnginesPrivateGetHotwordStateFunction::
+ SearchEnginesPrivateGetHotwordStateFunction()
+ : chrome_details_(this), weak_ptr_factory_(this) {
+}
+
+SearchEnginesPrivateGetHotwordStateFunction::
+ ~SearchEnginesPrivateGetHotwordStateFunction() {
+}
+
+ExtensionFunction::ResponseAction
+SearchEnginesPrivateGetHotwordStateFunction::Run() {
+ Profile* profile = chrome_details_.GetProfile();
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(profile);
+
+ scoped_ptr<search_engines_private::HotwordState> state(
+ new search_engines_private::HotwordState());
+
+ bool is_search_provider_google = false;
+ if (template_url_service && template_url_service->loaded()) {
+ const TemplateURL* default_url =
+ template_url_service->GetDefaultSearchProvider();
+ if (default_url &&
+ default_url->HasGoogleBaseURLs(
+ template_url_service->search_terms_data())) {
+ is_search_provider_google = true;
+ }
+ }
+
+ if (!HotwordServiceFactory::IsHotwordAllowed(profile) ||
+ !is_search_provider_google) {
+ 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.
+ }
+
+ state->availability.push_back(
+ search_engines_private::HotwordFeature::HOTWORD_FEATURE_SEARCH);
+
+ SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
+ bool authenticated = signin && signin->IsAuthenticated();
+ bool always_on =
+ HotwordServiceFactory::IsAlwaysOnAvailable() && authenticated;
+
+ if (always_on) {
+ state->availability.push_back(
+ search_engines_private::HotwordFeature::HOTWORD_FEATURE_ALWAYS_ON);
+ if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
+ state->availability.push_back(
+ search_engines_private::HotwordFeature::HOTWORD_FEATURE_RETRAIN_LINK);
+ }
+ }
+
+ int error = HotwordServiceFactory::GetCurrentError(profile);
+ if (error) {
+ base::string16 hotword_help_url =
+ base::ASCIIToUTF16(chrome::kHotwordLearnMoreURL);
+ std::string error_message(l10n_util::GetStringUTF8(error));
+ if (error == IDS_HOTWORD_GENERIC_ERROR_MESSAGE) {
+ error_message = l10n_util::GetStringFUTF8(error, hotword_help_url);
+ }
+ state->error_msg.reset(new std::string(error_message));
+ }
+
+ // Audio history should be displayed if it's enabled regardless of the
+ // hotword error state if the user is signed in. If the user is not signed
+ // in, audio history is meaningless. This is only displayed if always-on
+ // hotwording is available.
+ if (authenticated && always_on) {
+ std::string user_display_name = signin->GetAuthenticatedUsername();
+ DCHECK(!user_display_name.empty());
+ base::string16 audio_history_state =
+ l10n_util::GetStringFUTF16(IDS_HOTWORD_AUDIO_HISTORY_ENABLED,
+ base::ASCIIToUTF16(user_display_name));
+ HotwordService* hotword_service =
+ HotwordServiceFactory::GetForProfile(profile);
+ if (hotword_service) {
+ hotword_service->GetAudioHistoryHandler()->GetAudioHistoryEnabled(
+ base::Bind(&SearchEnginesPrivateGetHotwordStateFunction::
+ OnAudioHistoryChecked,
+ weak_ptr_factory_.GetWeakPtr(), base::Passed(&state),
+ audio_history_state));
+ return RespondLater();
+ }
+ }
+
+ return RespondNow(OneArgument(state->ToValue().release()));
+}
+
+void SearchEnginesPrivateGetHotwordStateFunction::OnAudioHistoryChecked(
+ scoped_ptr<search_engines_private::HotwordState> state,
+ const base::string16& audio_history_state,
+ bool success,
+ bool logging_enabled) {
+ if (success && logging_enabled) {
+ state->availability.push_back(
+ search_engines_private::HotwordFeature::HOTWORD_FEATURE_AUDIO_HISTORY);
+ std::string audio_history_state_str(
+ base::UTF16ToASCII(audio_history_state));
+ state->audio_history_state = make_scoped_ptr(&audio_history_state_str);
+ }
+ Respond(OneArgument(state->ToValue().release()));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// SearchEnginesPrivateOptIntoHotwordingFunction
+
+SearchEnginesPrivateOptIntoHotwordingFunction::
+ SearchEnginesPrivateOptIntoHotwordingFunction()
+ : chrome_details_(this) {
+}
+
+SearchEnginesPrivateOptIntoHotwordingFunction::
+ ~SearchEnginesPrivateOptIntoHotwordingFunction() {
+}
+
+ExtensionFunction::ResponseAction
+SearchEnginesPrivateOptIntoHotwordingFunction::Run() {
+ scoped_ptr<search_engines_private::OptIntoHotwording::Params> parameters =
+ search_engines_private::OptIntoHotwording::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+
+ Profile* profile = chrome_details_.GetProfile();
+
+ HotwordService::LaunchMode launch_mode =
+ HotwordService::HOTWORD_AND_AUDIO_HISTORY;
+
+ if (parameters->retrain) {
+ if (!profile->GetPrefs()->GetBoolean(
+ prefs::kHotwordAlwaysOnSearchEnabled) ||
+ !profile->GetPrefs()->GetBoolean(
+ prefs::kHotwordAudioLoggingEnabled)) {
+ return RespondNow(Error(
+ "Cannot retrain without always search and audio logging enabled"));
+ }
+
+ launch_mode = HotwordService::RETRAIN;
+ } else if (profile->GetPrefs()->GetBoolean(
+ prefs::kHotwordAudioLoggingEnabled)) {
+ if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
+ return RespondNow(Error(
+ "Cannot opt in with audio logging but also with always on enabled"));
+
+ }
+ launch_mode = HotwordService::HOTWORD_ONLY;
+ } else {
+ if (profile->GetPrefs()->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled))
+ return RespondNow(Error(
+ "Cannot opt in if already opted in"));
+ }
+
+ HotwordService* hotword_service =
+ HotwordServiceFactory::GetForProfile(profile);
+ if (hotword_service)
+ hotword_service->OptIntoHotwording(launch_mode);
+ return RespondNow(NoArguments());
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698