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

Unified Diff: chrome/browser/ui/webui/settings/search_engines_handler.cc

Issue 2507363002: MD Settings: Add Hotword (OK Google) section to search_page (Closed)
Patch Set: Self Review Created 4 years, 1 month 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/ui/webui/settings/search_engines_handler.cc
diff --git a/chrome/browser/ui/webui/settings/search_engines_handler.cc b/chrome/browser/ui/webui/settings/search_engines_handler.cc
index 9d20b307db1cf9b6e032bd125fd6f417fed18a9e..74ed0eaf266153d6bfbb64304723aa4f43ba70e1 100644
--- a/chrome/browser/ui/webui/settings/search_engines_handler.cc
+++ b/chrome/browser/ui/webui/settings/search_engines_handler.cc
@@ -11,16 +11,25 @@
#include "base/values.h"
#include "chrome/browser/extensions/extension_util.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/browser/search_engines/ui_thread_search_terms_data.h"
+#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/search_engines/template_url_table_model.h"
+#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
+#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
+#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_ui.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
+#include "ui/base/l10n/l10n_util.h"
namespace {
// The following strings need to match with the IDs of the paper-input elements
@@ -29,14 +38,43 @@ const char kSearchEngineField[] = "searchEngine";
const char kKeywordField[] = "keyword";
const char kQueryUrlField[] = "queryUrl";
+// Fields for hotwordUpdateInfo result.
+const char kHotwordSatusAllowed[] = "allowed";
+const char kHotwordSatusEnabled[] = "enabled";
+const char kHotwordStatusAlwaysOn[] = "alwaysOn";
+const char kHotwordSatusErrorMessage[] = "errorMessage";
+const char kHotwordSatusUserUserName[] = "userName";
+const char kHotwordSatusHistoryEnabled[] = "historyEnabled";
+
// Dummy number used for indicating that a new search engine is added.
const int kNewSearchEngineIndex = -1;
+
+bool IsGoogleDefaultSearch(Profile* profile) {
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(profile);
+ if (!template_url_service)
+ return false;
+ const TemplateURL* url_template =
+ template_url_service->GetDefaultSearchProvider();
+ return url_template &&
+ url_template->url_ref().HasGoogleBaseURLs(
+ template_url_service->search_terms_data());
+}
+
+bool GetHotwordAlwaysOn(Profile* profile) {
+ SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
+ return signin && signin->IsAuthenticated() &&
+ HotwordServiceFactory::IsAlwaysOnAvailable();
+}
+
} // namespace
namespace settings {
SearchEnginesHandler::SearchEnginesHandler(Profile* profile)
- : profile_(profile), list_controller_(profile) {}
+ : profile_(profile), list_controller_(profile), weak_ptr_factory_(this) {
+ pref_change_registrar_.Init(profile_->GetPrefs());
+}
SearchEnginesHandler::~SearchEnginesHandler() {
// TODO(tommycli): Refactor KeywordEditorController to be compatible with
@@ -73,20 +111,32 @@ void SearchEnginesHandler::RegisterMessages() {
"searchEngineEditCompleted",
base::Bind(&SearchEnginesHandler::HandleSearchEngineEditCompleted,
base::Unretained(this)));
+ web_ui()->RegisterMessageCallback(
+ "getHotwordInfo", base::Bind(&SearchEnginesHandler::HandleGetHotwordInfo,
+ base::Unretained(this)));
+ web_ui()->RegisterMessageCallback(
+ "setHotwordSearchEnabled",
+ base::Bind(&SearchEnginesHandler::HandleSetHotwordSearchEnabled,
+ base::Unretained(this)));
}
void SearchEnginesHandler::OnJavascriptAllowed() {
list_controller_.table_model()->SetObserver(this);
+ pref_change_registrar_.Add(prefs::kHotwordSearchEnabled,
+ base::Bind(&SearchEnginesHandler::SendHotwordInfo,
+ base::Unretained(this)));
+ pref_change_registrar_.Add(prefs::kHotwordAlwaysOnSearchEnabled,
+ base::Bind(&SearchEnginesHandler::SendHotwordInfo,
+ base::Unretained(this)));
}
void SearchEnginesHandler::OnJavascriptDisallowed() {
list_controller_.table_model()->SetObserver(nullptr);
+ pref_change_registrar_.RemoveAll();
}
std::unique_ptr<base::DictionaryValue>
SearchEnginesHandler::GetSearchEnginesList() {
- AllowJavascript();
-
// Find the default engine.
const TemplateURL* default_engine =
list_controller_.GetDefaultSearchProvider();
@@ -130,6 +180,7 @@ SearchEnginesHandler::GetSearchEnginesList() {
}
void SearchEnginesHandler::OnModelChanged() {
+ AllowJavascript();
CallJavascriptFunction("cr.webUIListenerCallback",
base::StringValue("search-engines-changed"),
*GetSearchEnginesList());
@@ -200,6 +251,7 @@ void SearchEnginesHandler::HandleGetSearchEnginesList(
CHECK_EQ(1U, args->GetSize());
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
+ AllowJavascript();
ResolveJavascriptCallback(*callback_id, *GetSearchEnginesList());
}
@@ -331,4 +383,141 @@ void SearchEnginesHandler::HandleSearchEngineEditCompleted(
}
}
+// TODO: Update hotword enabled and call DisableHotwordPreferences.
michaelpg 2016/11/27 22:25:36 TODO(name)
stevenjb 2016/11/28 20:53:37 Oops. I meant to do this in this CL. I've added th
+
+void SearchEnginesHandler::HandleGetHotwordInfo(const base::ListValue* args) {
+ AllowJavascript();
+
+ std::unique_ptr<base::Value> callback_id;
+ if (args) {
+ CHECK_EQ(1U, args->GetSize());
+ const base::Value* id;
+ CHECK(args->Get(0, &id));
+ callback_id = id->CreateDeepCopy();
+ }
+
+ std::unique_ptr<base::DictionaryValue> status = GetHotwordInfo();
+ bool enabled = false;
+ status->GetBoolean(kHotwordSatusEnabled, &enabled);
+ bool always_on = false;
+ status->GetBoolean(kHotwordStatusAlwaysOn, &always_on);
+ if (!enabled || !always_on) {
+ HotwordInfoComplete(callback_id.get(), *status);
+ return;
+ }
+
+ // OnGetHotwordAudioHistoryEnabled will call HotwordInfoComplete()..
michaelpg 2016/11/27 22:25:36 remove '.'
stevenjb 2016/11/28 20:53:37 Done.
+ HotwordServiceFactory::GetForProfile(profile_)
+ ->GetAudioHistoryHandler()
+ ->GetAudioHistoryEnabled(
+ base::Bind(&SearchEnginesHandler::OnGetHotwordAudioHistoryEnabled,
+ weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback_id),
+ base::Passed(&status)));
+}
+
+std::unique_ptr<base::DictionaryValue> SearchEnginesHandler::GetHotwordInfo() {
+ auto status = base::MakeUnique<base::DictionaryValue>();
+ if (!IsGoogleDefaultSearch(profile_)) {
+ status->SetBoolean(kHotwordSatusAllowed, false);
+ return status;
+ }
+
+ status->SetBoolean(kHotwordSatusAllowed,
+ HotwordServiceFactory::IsHotwordAllowed(profile_));
+
+ HotwordServiceFactory::IsServiceAvailable(profile_); // Update error value.
+ int hotword_error = HotwordServiceFactory::GetCurrentError(profile_);
+ if (hotword_error) {
+ base::string16 hotword_error_message;
+ if (hotword_error != IDS_HOTWORD_GENERIC_ERROR_MESSAGE) {
+ hotword_error_message = l10n_util::GetStringUTF16(hotword_error);
+ } else {
+ hotword_error_message = l10n_util::GetStringFUTF16(
+ hotword_error, base::ASCIIToUTF16(chrome::kHotwordLearnMoreURL));
+ }
+ status->SetString(kHotwordSatusErrorMessage, hotword_error_message);
+ }
+
+ if (!HotwordServiceFactory::GetForProfile(profile_)) {
+ status->SetBoolean(kHotwordSatusEnabled, false);
+ return status;
+ }
+
+ bool always_on = GetHotwordAlwaysOn(profile_);
+ status->SetBoolean(kHotwordStatusAlwaysOn, always_on);
+
+ std::string pref_name = always_on ? prefs::kHotwordAlwaysOnSearchEnabled
+ : prefs::kHotwordSearchEnabled;
+ bool enabled = profile_->GetPrefs()->GetBoolean(pref_name);
+ status->SetBoolean(kHotwordSatusEnabled, enabled);
+ if (!enabled)
+ return status;
+
+ SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile_);
+ std::string user_display_name =
+ signin ? signin->GetAuthenticatedAccountInfo().email : "";
+ status->SetString(kHotwordSatusUserUserName, user_display_name);
+ return status;
+}
+
+void SearchEnginesHandler::OnGetHotwordAudioHistoryEnabled(
+ std::unique_ptr<base::Value> callback_id,
+ std::unique_ptr<base::DictionaryValue> status,
+ bool success,
+ bool logging_enabled) {
+ if (success)
+ status->SetBoolean(kHotwordSatusHistoryEnabled, logging_enabled);
+ HotwordInfoComplete(callback_id.get(), *status);
+}
+
+void SearchEnginesHandler::HotwordInfoComplete(
+ const base::Value* callback_id,
+ const base::DictionaryValue& status) {
+ if (callback_id) {
+ ResolveJavascriptCallback(*callback_id, status);
+ } else {
+ CallJavascriptFunction("cr.webUIListenerCallback",
+ base::StringValue("hotword-info-update"), status);
+ }
+}
+
+void SearchEnginesHandler::SendHotwordInfo() {
+ HandleGetHotwordInfo(nullptr);
+}
+
+void SearchEnginesHandler::HandleSetHotwordSearchEnabled(
+ const base::ListValue* args) {
+ CHECK_EQ(1U, args->GetSize());
+ bool enabled;
+ CHECK(args->GetBoolean(0, &enabled));
+
+ bool always_on = GetHotwordAlwaysOn(profile_);
+ if (!always_on) {
+ profile_->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, enabled);
+ return;
+ }
+
+ HotwordService* hotword_service =
+ HotwordServiceFactory::GetForProfile(profile_);
+ std::string pref_name = always_on ? prefs::kHotwordAlwaysOnSearchEnabled
michaelpg 2016/11/27 22:25:37 but, always_on cannot be false due to the early re
stevenjb 2016/11/28 20:53:36 Good point. Removed |pref_name|.
+ : prefs::kHotwordSearchEnabled;
+ if (!enabled || !hotword_service) {
+ profile_->GetPrefs()->SetBoolean(pref_name, false);
+ return;
+ }
+
+ bool was_enabled = profile_->GetPrefs()->GetBoolean(pref_name);
+ HotwordService::LaunchMode launch_mode;
+ if (was_enabled) {
+ launch_mode = HotwordService::RETRAIN;
+ } else {
+ bool logging_enabled =
+ profile_->GetPrefs()->GetBoolean(prefs::kHotwordAudioLoggingEnabled);
+ launch_mode = logging_enabled ? HotwordService::HOTWORD_ONLY
+ : HotwordService::HOTWORD_AND_AUDIO_HISTORY;
+ }
+ hotword_service->OptIntoHotwording(launch_mode);
+ SendHotwordInfo();
+}
+
} // namespace settings

Powered by Google App Engine
This is Rietveld 408576698