Commit 227509c0 authored by Jeroen Dhollander's avatar Jeroen Dhollander Committed by Chromium LUCI CQ

Create Libassistant bootup config in Libassistant mojom service

Previously the Libassistant bootup config was created on the browser
thread and passed to the mojom service, but that just adds unnecessary
dependencies of the browser thread on Libassistant internal stuff (i.e.
the structure of the bootup config).

Bug: b/176851446
Test: chromeos_unittests && deployed
Change-Id: I3617943bc1dff483a1745d03194b9565cd1ad653
Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618546
Commit-Queue: Jeroen Dhollander <jeroendh@chromium.org>
Auto-Submit: Jeroen Dhollander <jeroendh@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843825}
parent 618cb00b
......@@ -35,7 +35,6 @@ component("lib") {
deps = [
"//base",
"//build/util:webkit_version",
"//chromeos/assistant:buildflags",
"//chromeos/audio",
"//chromeos/constants",
......
......@@ -16,6 +16,7 @@
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
......@@ -153,6 +154,15 @@ const char* ToTriggerSource(AssistantEntryPoint entry_point) {
}
}
bool ShouldPutLogsInHomeDirectory() {
// If this command line flag is specified, the logs should *not* be put in
// the home directory.
const bool redirect_logging =
base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kRedirectLibassistantLogging);
return !redirect_logging;
}
} // namespace
AssistantManagerServiceImpl::AssistantManagerServiceImpl(
......@@ -174,8 +184,10 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl(
assistant_proxy_(std::make_unique<AssistantProxy>()),
context_(context),
delegate_(std::move(delegate)),
libassistant_config_(
CreateLibAssistantConfig(s3_server_uri_override, device_id_override)),
bootup_config_(ServiceControllerProxy::BootupConfig::New(
s3_server_uri_override,
device_id_override,
ShouldPutLogsInHomeDirectory())),
weak_factory_(this) {
platform_api_ = delegate_->CreatePlatformApi(
media_session_.get(),
......@@ -1017,7 +1029,7 @@ void AssistantManagerServiceImpl::InitAssistant(
/*assistant_manager_delegate=*/this,
/*conversation_state_listener=*/this,
/*device_state_listener=*/this,
/*event_observer=*/this, libassistant_config_, locale,
/*event_observer=*/this, std::move(bootup_config_), locale,
GetLocaleOrDefault(assistant_state()->locale().value()),
spoken_feedback_enabled_, ToAuthTokensOrEmpty(user),
base::BindOnce(&AssistantManagerServiceImpl::PostInitAssistant,
......
......@@ -27,6 +27,7 @@
#include "chromeos/services/assistant/proxy/assistant_proxy.h"
#include "chromeos/services/assistant/proxy/conversation_controller_proxy.h"
#include "chromeos/services/assistant/proxy/libassistant_service_host.h"
#include "chromeos/services/assistant/proxy/service_controller_proxy.h"
#include "chromeos/services/assistant/public/cpp/assistant_notification.h"
#include "chromeos/services/assistant/public/cpp/assistant_service.h"
#include "chromeos/services/assistant/public/cpp/device_actions.h"
......@@ -363,7 +364,7 @@ class COMPONENT_EXPORT(ASSISTANT_SERVICE) AssistantManagerServiceImpl
base::UnguessableToken::Null();
// Configuration passed to libassistant.
std::string libassistant_config_;
ServiceControllerProxy::BootupConfigPtr bootup_config_;
base::TimeDelta stop_interactioin_delay_ =
base::TimeDelta::FromMilliseconds(500);
......
......@@ -205,7 +205,8 @@ class AssistantManagerServiceImplTest : public testing::Test {
}
void CreateAssistantManagerServiceImpl(
base::Optional<std::string> s3_server_uri_override = base::nullopt) {
base::Optional<std::string> s3_server_uri_override = base::nullopt,
base::Optional<std::string> device_id_override = base::nullopt) {
// We can not have 2 instances of |AssistantManagerServiceImpl| at the same
// time, so we must destroy the old one before creating a new one.
assistant_manager_service_.reset();
......@@ -214,7 +215,7 @@ class AssistantManagerServiceImplTest : public testing::Test {
service_context_.get(),
std::make_unique<FakeAssistantManagerServiceDelegate>(),
shared_url_loader_factory_->Clone(), s3_server_uri_override,
/*device_id_override=*/base::nullopt,
device_id_override,
std::make_unique<FakeLibassistantServiceHost>(&libassistant_service_));
}
......@@ -355,19 +356,6 @@ class AssistantManagerServiceImplTest : public testing::Test {
DISALLOW_COPY_AND_ASSIGN(AssistantManagerServiceImplTest);
};
// Tests if the JSON string contains the given path with the given value
#define EXPECT_HAS_PATH_WITH_VALUE(config_string, path, expected_value) \
({ \
base::Optional<base::Value> config = \
base::JSONReader::Read(config_string); \
ASSERT_TRUE(config.has_value()); \
const base::Value* actual = config->FindPath(path); \
base::Value expected = base::Value(expected_value); \
ASSERT_NE(actual, nullptr) \
<< "Path '" << path << "' not found in config: " << config_string; \
EXPECT_EQ(*actual, expected); \
})
} // namespace
TEST_F(AssistantManagerServiceImplTest, StateShouldStartAsStopped) {
......@@ -479,14 +467,30 @@ TEST_F(AssistantManagerServiceImplTest,
}
TEST_F(AssistantManagerServiceImplTest,
ShouldPassS3ServerUriOverrideToAssistantManager) {
ShouldPassS3ServerUriOverrideToMojomService) {
CreateAssistantManagerServiceImpl("the-uri-override");
Start();
WaitForState(AssistantManagerService::STARTED);
EXPECT_HAS_PATH_WITH_VALUE(mojom_service_controller().libassistant_config(),
"testing.s3_grpc_server_uri", "the-uri-override");
EXPECT_EQ(mojom_service_controller()
.libassistant_config()
.s3_server_uri_override.value_or("<none>"),
"the-uri-override");
}
TEST_F(AssistantManagerServiceImplTest,
ShouldPassDeviceIdOverrideToMojomService) {
CreateAssistantManagerServiceImpl(
/*s3_server_uri_override=*/base::nullopt, "the-device-id-override");
Start();
WaitForState(AssistantManagerService::STARTED);
EXPECT_EQ(mojom_service_controller()
.libassistant_config()
.device_id_override.value_or("<none>"),
"the-device-id-override");
}
TEST_F(AssistantManagerServiceImplTest, ShouldPauseMediaManagerOnPause) {
......
......@@ -16,6 +16,7 @@
#include "chromeos/services/assistant/public/cpp/migration/assistant_manager_service_delegate.h"
#include "chromeos/services/assistant/public/cpp/migration/libassistant_v1_api.h"
#include "chromeos/services/libassistant/libassistant_service.h"
#include "chromeos/services/libassistant/public/mojom/service_controller.mojom-forward.h"
#include "libassistant/shared/internal_api/assistant_manager_internal.h"
#include "libassistant/shared/internal_api/fuchsia_api_helper.h"
......@@ -129,7 +130,7 @@ void ServiceControllerProxy::Start(
assistant_client::ConversationStateListener* conversation_state_listener,
assistant_client::DeviceStateListener* device_state_listener,
AssistantEventObserver* event_observer,
const std::string& libassistant_config,
BootupConfigPtr bootup_config,
const std::string& locale,
const std::string& locale_override,
bool spoken_feedback_enabled,
......@@ -144,7 +145,7 @@ void ServiceControllerProxy::Start(
assistant::features::IsMediaSessionIntegrationEnabled());
// The mojom service will create the |AssistantManager|.
service_controller_remote_->Initialize(libassistant_config);
service_controller_remote_->Initialize(std::move(bootup_config));
service_controller_remote_->SetLocaleOverride(locale_override);
UpdateInternalOptions(locale, spoken_feedback_enabled);
SetAuthTokens(auth_tokens);
......
......@@ -40,6 +40,8 @@ class ServiceControllerProxy : private libassistant::mojom::StateObserver {
public:
// Each authentication token exists of a [gaia_id, access_token] tuple.
using AuthTokens = std::vector<std::pair<std::string, std::string>>;
using BootupConfig = libassistant::mojom::BootupConfig;
using BootupConfigPtr = libassistant::mojom::BootupConfigPtr;
ServiceControllerProxy(
LibassistantServiceHost* host,
......@@ -68,7 +70,7 @@ class ServiceControllerProxy : private libassistant::mojom::StateObserver {
assistant_client::ConversationStateListener* conversation_state_listener,
assistant_client::DeviceStateListener* device_state_listener,
AssistantEventObserver* event_observer,
const std::string& libassistant_config,
BootupConfigPtr bootup_config,
const std::string& locale,
const std::string& locale_override,
bool spoken_feedback_enabled,
......
......@@ -63,8 +63,9 @@ std::string FakeServiceController::gaia_id() {
return kNoValue;
}
void FakeServiceController::Initialize(const std::string& libassistant_config) {
libassistant_config_ = libassistant_config;
void FakeServiceController::Initialize(
libassistant::mojom::BootupConfigPtr config) {
libassistant_config_ = std::move(*config);
}
void FakeServiceController::Start() {
......
......@@ -50,7 +50,9 @@ class FakeServiceController : public libassistant::mojom::ServiceController {
State state() const { return state_; }
// Returns the Libassistant config that was passed to Initialize().
std::string libassistant_config() { return libassistant_config_; }
const libassistant::mojom::BootupConfig& libassistant_config() {
return libassistant_config_;
}
void Bind(mojo::PendingReceiver<libassistant::mojom::ServiceController>
pending_receiver);
......@@ -73,7 +75,7 @@ class FakeServiceController : public libassistant::mojom::ServiceController {
std::string gaia_id();
// mojom::ServiceController implementation:
void Initialize(const std::string& libassistant_config) override;
void Initialize(libassistant::mojom::BootupConfigPtr config) override;
void Start() override;
void Stop() override;
void AddAndFireStateObserver(
......@@ -91,7 +93,7 @@ class FakeServiceController : public libassistant::mojom::ServiceController {
std::mutex start_mutex_;
// Config passed to LibAssistant when it was started.
std::string libassistant_config_;
libassistant::mojom::BootupConfig libassistant_config_;
// Authentication tokens passed to SetAuthenticationTokens().
std::vector<libassistant::mojom::AuthenticationTokenPtr>
......
......@@ -7,41 +7,12 @@
#include <utility>
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/values.h"
#include "build/util/webkit_version.h"
#include "chromeos/assistant/internal/internal_constants.h"
#include "chromeos/constants/chromeos_switches.h"
#include "chromeos/dbus/util/version_loader.h"
#include "chromeos/services/assistant/public/cpp/features.h"
namespace chromeos {
namespace assistant {
namespace {
void CreateUserAgent(std::string* user_agent) {
DCHECK(user_agent->empty());
base::StringAppendF(user_agent,
"Mozilla/5.0 (X11; CrOS %s %s; %s) "
"AppleWebKit/%d.%d (KHTML, like Gecko)",
base::SysInfo::OperatingSystemArchitecture().c_str(),
base::SysInfo::OperatingSystemVersion().c_str(),
base::SysInfo::GetLsbReleaseBoard().c_str(),
WEBKIT_VERSION_MAJOR, WEBKIT_VERSION_MINOR);
std::string arc_version = chromeos::version_loader::GetARCVersion();
if (!arc_version.empty())
base::StringAppendF(user_agent, " ARC/%s", arc_version.c_str());
}
} // namespace
// Get the root path for assistant files.
base::FilePath GetRootPath() {
base::FilePath home_dir;
......@@ -55,118 +26,5 @@ base::FilePath GetBaseAssistantDir() {
return GetRootPath().Append(FILE_PATH_LITERAL("google-assistant-library"));
}
std::string CreateLibAssistantConfig(
base::Optional<std::string> s3_server_uri_override,
base::Optional<std::string> device_id_override) {
using Value = base::Value;
using Type = base::Value::Type;
Value config(Type::DICTIONARY);
Value device(Type::DICTIONARY);
device.SetKey("board_name", Value(base::SysInfo::GetLsbReleaseBoard()));
device.SetKey("board_revision", Value("1"));
device.SetKey("embedder_build_info",
Value(chromeos::version_loader::GetVersion(
chromeos::version_loader::VERSION_FULL)));
device.SetKey("model_id", Value(kModelId));
device.SetKey("model_revision", Value(1));
config.SetKey("device", std::move(device));
Value discovery(Type::DICTIONARY);
discovery.SetKey("enable_mdns", Value(false));
config.SetKey("discovery", std::move(discovery));
Value internal(Type::DICTIONARY);
internal.SetKey("surface_type", Value("OPA_CROS"));
std::string user_agent;
CreateUserAgent(&user_agent);
internal.SetKey("user_agent", Value(user_agent));
// Prevent LibAssistant from automatically playing ready message TTS during
// the startup sequence when the version of LibAssistant has been upgraded.
internal.SetKey("override_ready_message", Value(true));
// Set DeviceProperties.visibility to Visibility::PRIVATE.
// See //libassistant/shared/proto/device_properties.proto.
internal.SetKey("visibility", Value("PRIVATE"));
if (base::SysInfo::IsRunningOnChromeOS()) {
Value logging(Type::DICTIONARY);
// Redirect libassistant logging to /var/log/chrome/ if has the switch,
// otherwise log to 'log' sub dir in user's home dir.
const bool redirect_logging =
base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kRedirectLibassistantLogging);
const std::string log_dir =
redirect_logging
? "/var/log/chrome/"
: GetRootPath().Append(FILE_PATH_LITERAL("log")).value();
logging.SetKey("directory", Value(log_dir));
// Maximum disk space consumed by all log files. There are 5 rotating log
// files on disk.
logging.SetKey("max_size_kb", Value(3 * 1024));
// Empty "output_type" disables logging to stderr.
logging.SetKey("output_type", Value(Type::LIST));
config.SetKey("logging", std::move(logging));
} else {
// Print logs to console if running in desktop mode.
internal.SetKey("disable_log_files", Value(true));
}
// Enable logging.
internal.SetBoolKey("enable_logging", true);
// This only enables logging to local disk combined with the flag above. When
// user choose to file a Feedback report, user can examine the log and choose
// to upload the log with the report or not.
internal.SetBoolKey("logging_opt_in", true);
// Allows libassistant to automatically toggle signed-out mode depending on
// whether it has auth_tokens.
internal.SetBoolKey("enable_signed_out_mode", true);
config.SetKey("internal", std::move(internal));
Value audio_input(Type::DICTIONARY);
// Skip sending speaker ID selection info to disable user verification.
audio_input.SetKey("should_send_speaker_id_selection_info", Value(false));
Value sources(Type::LIST);
Value dict(Type::DICTIONARY);
dict.SetKey("enable_eraser", Value(features::IsAudioEraserEnabled()));
dict.SetKey("enable_eraser_toggling",
Value(features::IsAudioEraserEnabled()));
sources.Append(std::move(dict));
audio_input.SetKey("sources", std::move(sources));
config.SetKey("audio_input", std::move(audio_input));
if (features::IsLibAssistantBetaBackendEnabled())
config.SetStringPath("internal.backend_type", "BETA_DOGFOOD");
// Use http unless we're using the fake s3 server, which requires grpc.
if (s3_server_uri_override)
config.SetStringPath("internal.transport_type", "GRPC");
else
config.SetStringPath("internal.transport_type", "HTTP");
if (device_id_override)
config.SetStringPath("internal.cast_device_id", device_id_override.value());
config.SetBoolPath("internal.enable_on_device_assistant_tts_as_text", true);
// Finally add in the server uri override.
if (s3_server_uri_override) {
config.SetStringPath("testing.s3_grpc_server_uri",
s3_server_uri_override.value());
}
std::string json;
base::JSONWriter::Write(config, &json);
return json;
}
} // namespace assistant
} // namespace chromeos
......@@ -39,11 +39,15 @@ source_set("internal") {
"platform_api.h",
"service_controller.cc",
"service_controller.h",
"util.cc",
"util.h",
]
deps = [
"//build/util:webkit_version",
"//chromeos/assistant/internal",
"//chromeos/assistant/internal/proto/google3",
"//chromeos/dbus",
"//chromeos/services/assistant/public/cpp",
"//chromeos/services/assistant/public/cpp/migration",
"//chromeos/services/libassistant/public/mojom",
......
......@@ -16,14 +16,13 @@ enum ServiceState {
// Interface managing the lifecycle of Libassistant,
// exposing methods to start/stop and configure Libassistant.
interface ServiceController {
// Initialize the service. This must be called before Start() and before
// restarting the service (so between all Stop() and Start() calls).
// Will be a noop if the service is started or running.
// Note that calling Initialize() will not cause any change in the service
// state, as the service will remain in state |kStopped| until Start() is
// called.
Initialize(string libassistant_config);
Initialize(BootupConfig bootup_config);
// Start the service. Can be called multiple times, and will be a noop if
// the service is already started or running.
......@@ -61,3 +60,15 @@ struct AuthenticationToken {
string gaia_id;
string access_token;
};
// Configuration settings that must be set to allow Libassistant to boot.
struct BootupConfig {
// A custom URI for the S3 Server. Used to use a fake S3 server during
// unittests.
string? s3_server_uri_override;
// A custom device id. Used during unittests.
string? device_id_override;
// If true, Libassistant logs will appear in the user's home directory.
// If false, the logs will end up in /var/log/chrome/.
bool log_in_home_dir;
};
......@@ -11,6 +11,7 @@
#include "chromeos/services/assistant/public/cpp/features.h"
#include "chromeos/services/assistant/public/cpp/migration/assistant_manager_service_delegate.h"
#include "chromeos/services/assistant/public/cpp/migration/libassistant_v1_api.h"
#include "chromeos/services/libassistant/util.h"
#include "libassistant/shared/internal_api/assistant_manager_internal.h"
namespace chromeos {
......@@ -30,6 +31,12 @@ std::vector<std::pair<std::string, std::string>> ToAuthTokens(
return result;
}
std::string ToLibassistantConfig(const mojom::BootupConfig& bootup_config) {
return CreateLibAssistantConfig(bootup_config.s3_server_uri_override,
bootup_config.device_id_override,
bootup_config.log_in_home_dir);
}
} // namespace
ServiceController::ServiceController(
......@@ -53,14 +60,14 @@ void ServiceController::SetInitializeCallback(InitializeCallback callback) {
initialize_callback_ = std::move(callback);
}
void ServiceController::Initialize(const std::string& libassistant_config) {
void ServiceController::Initialize(mojom::BootupConfigPtr config) {
if (assistant_manager_ != nullptr) {
LOG(ERROR) << "Initialize() should only be called once.";
return;
}
assistant_manager_ =
delegate_->CreateAssistantManager(platform_api_, libassistant_config);
assistant_manager_ = delegate_->CreateAssistantManager(
platform_api_, ToLibassistantConfig(*config));
assistant_manager_internal_ =
delegate_->UnwrapAssistantManagerInternal(assistant_manager_.get());
......
......@@ -10,6 +10,7 @@
#include "base/scoped_observation.h"
#include "chromeos/services/libassistant/assistant_manager_observer.h"
#include "chromeos/services/libassistant/public/mojom/service.mojom.h"
#include "chromeos/services/libassistant/public/mojom/service_controller.mojom-forward.h"
#include "chromeos/services/libassistant/public/mojom/service_controller.mojom-shared.h"
#include "libassistant/shared/public/assistant_manager.h"
#include "mojo/public/cpp/bindings/receiver.h"
......@@ -62,7 +63,7 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) ServiceController
void SetInitializeCallback(InitializeCallback callback);
// mojom::ServiceController implementation:
void Initialize(const std::string& libassistant_config) override;
void Initialize(mojom::BootupConfigPtr libassistant_config) override;
void Start() override;
void Stop() override;
void AddAndFireStateObserver(
......
......@@ -6,6 +6,7 @@
#include <memory>
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
......@@ -30,6 +31,19 @@ using ::testing::StrictMock;
#define EXPECT_NO_CALLS(args...) EXPECT_CALL(args).Times(0)
// Tests if the JSON string contains the given path with the given value
#define EXPECT_HAS_PATH_WITH_VALUE(config_string, path, expected_value) \
({ \
base::Optional<base::Value> config = \
base::JSONReader::Read(config_string); \
ASSERT_TRUE(config.has_value()); \
const base::Value* actual = config->FindPath(path); \
base::Value expected = base::Value(expected_value); \
ASSERT_NE(actual, nullptr) \
<< "Path '" << path << "' not found in config: " << config_string; \
EXPECT_EQ(*actual, expected); \
})
class StateObserverMock : public mojom::StateObserver {
public:
StateObserverMock() : receiver_(this) {}
......@@ -111,8 +125,8 @@ class AssistantServiceControllerTest : public testing::Test {
RunUntilIdle();
}
void Initialize(const std::string& libassistant_config = std::string("")) {
service_controller().Initialize(libassistant_config);
void Initialize(mojom::BootupConfigPtr config = mojom::BootupConfig::New()) {
service_controller().Initialize(std::move(config));
}
void Start() {
......@@ -347,11 +361,14 @@ TEST_F(AssistantServiceControllerTest,
}
TEST_F(AssistantServiceControllerTest,
ShouldPassLibassistantConfigToAssistantManager) {
Initialize(/*libassistant_config=*/"the-libassistant-config");
Start();
EXPECT_EQ("the-libassistant-config", delegate().libassistant_config());
ShouldPassS3ServerUriOverrideToMojomService) {
auto bootup_config = mojom::BootupConfig::New();
bootup_config->s3_server_uri_override = "the-s3-server-uri-override";
Initialize(std::move(bootup_config));
EXPECT_HAS_PATH_WITH_VALUE(delegate().libassistant_config(),
"testing.s3_grpc_server_uri",
"the-s3-server-uri-override");
}
TEST_F(AssistantServiceControllerTest,
......
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/libassistant/util.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/values.h"
#include "build/util/webkit_version.h"
#include "chromeos/assistant/internal/internal_constants.h"
#include "chromeos/dbus/util/version_loader.h"
#include "chromeos/services/assistant/public/cpp/features.h"
namespace chromeos {
namespace libassistant {
namespace {
void CreateUserAgent(std::string* user_agent) {
DCHECK(user_agent->empty());
base::StringAppendF(user_agent,
"Mozilla/5.0 (X11; CrOS %s %s; %s) "
"AppleWebKit/%d.%d (KHTML, like Gecko)",
base::SysInfo::OperatingSystemArchitecture().c_str(),
base::SysInfo::OperatingSystemVersion().c_str(),
base::SysInfo::GetLsbReleaseBoard().c_str(),
WEBKIT_VERSION_MAJOR, WEBKIT_VERSION_MINOR);
std::string arc_version = chromeos::version_loader::GetARCVersion();
if (!arc_version.empty())
base::StringAppendF(user_agent, " ARC/%s", arc_version.c_str());
}
// Get the root path for assistant files.
base::FilePath GetRootPath() {
base::FilePath home_dir;
CHECK(base::PathService::Get(base::DIR_HOME, &home_dir));
// Ensures DIR_HOME is overridden after primary user sign-in.
CHECK_NE(base::GetHomeDir(), home_dir);
return home_dir;
}
} // namespace
base::FilePath GetBaseAssistantDir() {
return GetRootPath().Append(FILE_PATH_LITERAL("google-assistant-library"));
}
std::string CreateLibAssistantConfig(
base::Optional<std::string> s3_server_uri_override,
base::Optional<std::string> device_id_override,
bool log_in_home_dir) {
using Value = base::Value;
using Type = base::Value::Type;
Value config(Type::DICTIONARY);
Value device(Type::DICTIONARY);
device.SetKey("board_name", Value(base::SysInfo::GetLsbReleaseBoard()));
device.SetKey("board_revision", Value("1"));
device.SetKey("embedder_build_info",
Value(chromeos::version_loader::GetVersion(
chromeos::version_loader::VERSION_FULL)));
device.SetKey("model_id", Value(assistant::kModelId));
device.SetKey("model_revision", Value(1));
config.SetKey("device", std::move(device));
Value discovery(Type::DICTIONARY);
discovery.SetKey("enable_mdns", Value(false));
config.SetKey("discovery", std::move(discovery));
Value internal(Type::DICTIONARY);
internal.SetKey("surface_type", Value("OPA_CROS"));
std::string user_agent;
CreateUserAgent(&user_agent);
internal.SetKey("user_agent", Value(user_agent));
// Prevent LibAssistant from automatically playing ready message TTS during
// the startup sequence when the version of LibAssistant has been upgraded.
internal.SetKey("override_ready_message", Value(true));
// Set DeviceProperties.visibility to Visibility::PRIVATE.
// See //libassistant/shared/proto/device_properties.proto.
internal.SetKey("visibility", Value("PRIVATE"));
if (base::SysInfo::IsRunningOnChromeOS()) {
Value logging(Type::DICTIONARY);
const std::string log_dir =
log_in_home_dir ? GetRootPath().Append(FILE_PATH_LITERAL("log")).value()
: "/var/log/chrome/";
logging.SetKey("directory", Value(log_dir));
// Maximum disk space consumed by all log files. There are 5 rotating log
// files on disk.
logging.SetKey("max_size_kb", Value(3 * 1024));
// Empty "output_type" disables logging to stderr.
logging.SetKey("output_type", Value(Type::LIST));
config.SetKey("logging", std::move(logging));
} else {
// Print logs to console if running in desktop mode.
internal.SetKey("disable_log_files", Value(true));
}
// Enable logging.
internal.SetBoolKey("enable_logging", true);
// This only enables logging to local disk combined with the flag above. When
// user choose to file a Feedback report, user can examine the log and choose
// to upload the log with the report or not.
internal.SetBoolKey("logging_opt_in", true);
// Allows libassistant to automatically toggle signed-out mode depending on
// whether it has auth_tokens.
internal.SetBoolKey("enable_signed_out_mode", true);
config.SetKey("internal", std::move(internal));
Value audio_input(Type::DICTIONARY);
// Skip sending speaker ID selection info to disable user verification.
audio_input.SetKey("should_send_speaker_id_selection_info", Value(false));
Value sources(Type::LIST);
Value dict(Type::DICTIONARY);
dict.SetKey("enable_eraser",
Value(assistant::features::IsAudioEraserEnabled()));
dict.SetKey("enable_eraser_toggling",
Value(assistant::features::IsAudioEraserEnabled()));
sources.Append(std::move(dict));
audio_input.SetKey("sources", std::move(sources));
config.SetKey("audio_input", std::move(audio_input));
if (assistant::features::IsLibAssistantBetaBackendEnabled())
config.SetStringPath("internal.backend_type", "BETA_DOGFOOD");
// Use http unless we're using the fake s3 server, which requires grpc.
if (s3_server_uri_override)
config.SetStringPath("internal.transport_type", "GRPC");
else
config.SetStringPath("internal.transport_type", "HTTP");
if (device_id_override)
config.SetStringPath("internal.cast_device_id", device_id_override.value());
config.SetBoolPath("internal.enable_on_device_assistant_tts_as_text", true);
// Finally add in the server uri override.
if (s3_server_uri_override) {
config.SetStringPath("testing.s3_grpc_server_uri",
s3_server_uri_override.value());
}
std::string json;
base::JSONWriter::Write(config, &json);
return json;
}
} // namespace libassistant
} // namespace chromeos
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_SERVICES_LIBASSISTANT_UTIL_H_
#define CHROMEOS_SERVICES_LIBASSISTANT_UTIL_H_
#include <string>
#include "base/optional.h"
namespace base {
class FilePath;
} // namespace base
namespace chromeos {
namespace libassistant {
// Creates the configuration for libassistant.
std::string CreateLibAssistantConfig(
base::Optional<std::string> s3_server_uri_override,
base::Optional<std::string> device_id_override,
bool log_in_home_dir);
// Returns the path where all downloaded LibAssistant resources are stored.
base::FilePath GetBaseAssistantDir();
} // namespace libassistant
} // namespace chromeos
#endif // CHROMEOS_SERVICES_LIBASSISTANT_UTIL_H_
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