Commit 0ab1d47f authored by Xiaohui Chen's avatar Xiaohui Chen Committed by Commit Bot

assistant: add autotest api to enable feature

Bug: b:79879622
Test: run autotest in chromeos chroot
Test: test_that test_samus assistant_Startup
Change-Id: Ia354f4a75fac904c813f8fb379ab88a95500f391
Reviewed-on: https://chromium-review.googlesource.com/c/1330698Reviewed-by: default avatarToni Baržić <tbarzic@chromium.org>
Reviewed-by: default avatarAchuith Bhandarkar <achuith@chromium.org>
Commit-Queue: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608117}
parent 51910fbb
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "chrome/browser/chromeos/extensions/autotest_private/autotest_private_api.h" #include "chrome/browser/chromeos/extensions/autotest_private/autotest_private_api.h"
#include <memory> #include <set>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/metrics/statistics_recorder.h" #include "base/metrics/statistics_recorder.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/task/post_task.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h" #include "base/values.h"
...@@ -978,6 +979,76 @@ void AutotestPrivateBootstrapMachineLearningServiceFunction::ConnectionError() { ...@@ -978,6 +979,76 @@ void AutotestPrivateBootstrapMachineLearningServiceFunction::ConnectionError() {
Respond(Error("ML Service connection error")); Respond(Error("ML Service connection error"));
} }
///////////////////////////////////////////////////////////////////////////////
// AutotestPrivateSetAssistantEnabled
///////////////////////////////////////////////////////////////////////////////
AutotestPrivateSetAssistantEnabledFunction::
AutotestPrivateSetAssistantEnabledFunction() {
auto* connection = content::ServiceManagerConnection::GetForProcess();
assistant_state_.Init(connection->GetConnector());
assistant_state_.AddObserver(this);
}
AutotestPrivateSetAssistantEnabledFunction::
~AutotestPrivateSetAssistantEnabledFunction() {
assistant_state_.RemoveObserver(this);
}
ExtensionFunction::ResponseAction
AutotestPrivateSetAssistantEnabledFunction::Run() {
DVLOG(1) << "AutotestPrivateSetAssistantEnabledFunction";
std::unique_ptr<api::autotest_private::SetAssistantEnabled::Params> params(
api::autotest_private::SetAssistantEnabled::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
Profile* profile = Profile::FromBrowserContext(browser_context());
if (arc::IsAssistantAllowedForProfile(profile) !=
ash::mojom::AssistantAllowedState::ALLOWED) {
return RespondNow(Error("Assistant is not available for the current user"));
}
profile->GetPrefs()->SetBoolean(arc::prefs::kVoiceInteractionEnabled,
params->enabled);
// |NOT_READY| means service not running
// |STOPPED| means service running but UI not shown
auto new_state = params->enabled
? ash::mojom::VoiceInteractionState::STOPPED
: ash::mojom::VoiceInteractionState::NOT_READY;
if (assistant_state_.voice_interaction_state() == new_state)
return RespondNow(NoArguments());
// Assistant service has not responded yet, set up a delayed timer to wait for
// it and holder a reference to |this|. Also make sure we stop and respond
// when timeout.
expected_state_ = new_state;
timeout_timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(params->timeout_ms),
base::BindOnce(&AutotestPrivateSetAssistantEnabledFunction::Timeout,
this));
return RespondLater();
}
void AutotestPrivateSetAssistantEnabledFunction::
OnVoiceInteractionStatusChanged(ash::mojom::VoiceInteractionState state) {
DCHECK(expected_state_);
// The service could go through |NOT_READY| then to |STOPPED| during enable
// flow if this API is called before the initial state is reported.
if (expected_state_ != state)
return;
Respond(NoArguments());
expected_state_.reset();
timeout_timer_.AbandonAndStop();
}
void AutotestPrivateSetAssistantEnabledFunction::Timeout() {
Respond(Error("Assistant service timed out"));
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// AutotestPrivateAPI // AutotestPrivateAPI
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
...@@ -5,10 +5,15 @@ ...@@ -5,10 +5,15 @@
#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_AUTOTEST_PRIVATE_AUTOTEST_PRIVATE_API_H_ #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_AUTOTEST_PRIVATE_AUTOTEST_PRIVATE_API_H_
#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_AUTOTEST_PRIVATE_AUTOTEST_PRIVATE_API_H_ #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_AUTOTEST_PRIVATE_AUTOTEST_PRIVATE_API_H_
#include <memory>
#include <string> #include <string>
#include <vector>
#include "ash/public/cpp/assistant/assistant_state_proxy.h"
#include "ash/public/cpp/assistant/default_voice_interaction_observer.h"
#include "ash/public/interfaces/ash_message_center_controller.mojom.h" #include "ash/public/interfaces/ash_message_center_controller.mojom.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/timer/timer.h"
#include "chrome/browser/chromeos/printing/cups_printers_manager.h" #include "chrome/browser/chromeos/printing/cups_printers_manager.h"
#include "chrome/browser/extensions/chrome_extension_function.h" #include "chrome/browser/extensions/chrome_extension_function.h"
#include "chromeos/services/machine_learning/public/mojom/machine_learning_service.mojom.h" #include "chromeos/services/machine_learning/public/mojom/machine_learning_service.mojom.h"
...@@ -360,6 +365,33 @@ class AutotestPrivateBootstrapMachineLearningServiceFunction ...@@ -360,6 +365,33 @@ class AutotestPrivateBootstrapMachineLearningServiceFunction
chromeos::machine_learning::mojom::ModelPtr model_; chromeos::machine_learning::mojom::ModelPtr model_;
}; };
// Enable/disable the Google Assistant feature. This toggles the Assistant user
// pref which will indirectly bring up or shut down the Assistant service.
class AutotestPrivateSetAssistantEnabledFunction
: public UIThreadExtensionFunction,
public ash::DefaultVoiceInteractionObserver {
public:
AutotestPrivateSetAssistantEnabledFunction();
DECLARE_EXTENSION_FUNCTION("autotestPrivate.setAssistantEnabled",
AUTOTESTPRIVATE_SETASSISTANTENABLED)
private:
~AutotestPrivateSetAssistantEnabledFunction() override;
ResponseAction Run() override;
// ash::DefaultVoiceInteractionObserver overrides:
void OnVoiceInteractionStatusChanged(
ash::mojom::VoiceInteractionState state) override;
// Called when the Assistant service does not respond in a timely fashion. We
// will respond with an error.
void Timeout();
ash::AssistantStateProxy assistant_state_;
base::Optional<ash::mojom::VoiceInteractionState> expected_state_;
base::OneShotTimer timeout_timer_;
};
// The profile-keyed service that manages the autotestPrivate extension API. // The profile-keyed service that manages the autotestPrivate extension API.
class AutotestPrivateAPI : public BrowserContextKeyedAPI { class AutotestPrivateAPI : public BrowserContextKeyedAPI {
public: public:
......
...@@ -217,5 +217,9 @@ namespace autotestPrivate { ...@@ -217,5 +217,9 @@ namespace autotestPrivate {
// daemon startup, and 2. the initial D-Bus -> Mojo IPC bootstrap. // daemon startup, and 2. the initial D-Bus -> Mojo IPC bootstrap.
// |callback|: Called when the operation has completed. // |callback|: Called when the operation has completed.
static void bootstrapMachineLearningService(VoidCallback callback); static void bootstrapMachineLearningService(VoidCallback callback);
// Enable/disable the Google Assistant
// |callback|: Called when the operation has completed.
static void setAssistantEnabled(boolean enabled, long timeout_ms, VoidCallback callback);
}; };
}; };
...@@ -206,5 +206,10 @@ chrome.test.runTests([ ...@@ -206,5 +206,10 @@ chrome.test.runTests([
chrome.autotestPrivate.getPrinterList(function(){ chrome.autotestPrivate.getPrinterList(function(){
chrome.test.succeed(); chrome.test.succeed();
}); });
} },
function setAssistantEnabled() {
chrome.autotestPrivate.setAssistantEnabled(true, 1000 /* timeout_ms */,
chrome.test.callbackFail(
'Assistant is not available for the current user'));
},
]); ]);
...@@ -1352,6 +1352,7 @@ enum HistogramValue { ...@@ -1352,6 +1352,7 @@ enum HistogramValue {
TABS_GOFORWARD = 1289, TABS_GOFORWARD = 1289,
TABS_GOBACK = 1290, TABS_GOBACK = 1290,
BRAILLEDISPLAYPRIVATE_UPDATEBLUETOOTHBRAILLEDISPLAYADDRESS = 1291, BRAILLEDISPLAYPRIVATE_UPDATEBLUETOOTHBRAILLEDISPLAYADDRESS = 1291,
AUTOTESTPRIVATE_SETASSISTANTENABLED = 1292,
// Last entry: Add new entries above, then run: // Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py // python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY ENUM_BOUNDARY
......
...@@ -17263,6 +17263,7 @@ Called by update_net_error_codes.py.--> ...@@ -17263,6 +17263,7 @@ Called by update_net_error_codes.py.-->
<int value="1290" label="TABS_GOBACK"/> <int value="1290" label="TABS_GOBACK"/>
<int value="1291" <int value="1291"
label="BRAILLEDISPLAYPRIVATE_UPDATEBLUETOOTHBRAILLEDISPLAYADDRESS"/> label="BRAILLEDISPLAYPRIVATE_UPDATEBLUETOOTHBRAILLEDISPLAYADDRESS"/>
<int value="1292" label="AUTOTESTPRIVATE_SETASSISTANTENABLED"/>
</enum> </enum>
<enum name="ExtensionIconState"> <enum name="ExtensionIconState">
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment