Commit 4abd6dbb authored by Jeroen Dhollander's avatar Jeroen Dhollander Committed by Chromium LUCI CQ

Separate ServiceController Initialize() and Start()

A lot of things are configured between the creation of the
|AssistantManager| and the call of |AssistantManager::Start()|.

Right now this is done through an initialize callback (which circumvents
the Mojom API).
To get rid of this initialize callback (and configure things properly
through Mojom APIs), we need to separate the creation and start of
|AssistantManager| in 2 separate mojom calls.

This CL also renames all |ServiceController| tests to
|AssistantServiceControllerTest| so we can run all assistant tests using
the "Assistant*" test filter.

Bug: b/176851446
Test: chromeos_unittests --gtest_filter="Assistant*"
Change-Id: I37c11439446f52fcc209d0612fd73cf8b0478de0
Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2617119Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Jeroen Dhollander <jeroendh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842124}
parent c5f9e587
...@@ -159,7 +159,8 @@ void ServiceControllerProxy::Start( ...@@ -159,7 +159,8 @@ void ServiceControllerProxy::Start(
assistant::features::IsMediaSessionIntegrationEnabled()); assistant::features::IsMediaSessionIntegrationEnabled());
// The mojom service will create the |AssistantManager|. // The mojom service will create the |AssistantManager|.
service_controller_remote_->Start(libassistant_config); service_controller_remote_->Initialize(libassistant_config);
service_controller_remote_->Start();
// We need to initialize the |AssistantManager| once it's created and before // We need to initialize the |AssistantManager| once it's created and before
// it's started, so we register a callback to do just that. // it's started, so we register a callback to do just that.
......
...@@ -49,9 +49,11 @@ void FakeServiceController::UnblockStartCalls() { ...@@ -49,9 +49,11 @@ void FakeServiceController::UnblockStartCalls() {
start_mutex_.unlock(); start_mutex_.unlock();
} }
void FakeServiceController::Start(const std::string& libassistant_config) { void FakeServiceController::Initialize(const std::string& libassistant_config) {
libassistant_config_ = libassistant_config; libassistant_config_ = libassistant_config;
}
void FakeServiceController::Start() {
// Will block if |BlockStartCalls| was invoked. // Will block if |BlockStartCalls| was invoked.
std::lock_guard<std::mutex> lock(start_mutex_); std::lock_guard<std::mutex> lock(start_mutex_);
......
...@@ -40,7 +40,7 @@ class FakeServiceController : public libassistant::mojom::ServiceController { ...@@ -40,7 +40,7 @@ class FakeServiceController : public libassistant::mojom::ServiceController {
void SetState(State new_state); void SetState(State new_state);
State state() const { return state_; } State state() const { return state_; }
// Returns the Libassistant config that was passed to the last Start() call. // Returns the Libassistant config that was passed to Initialize().
std::string libassistant_config() { return libassistant_config_; } std::string libassistant_config() { return libassistant_config_; }
void Bind(mojo::PendingReceiver<libassistant::mojom::ServiceController> void Bind(mojo::PendingReceiver<libassistant::mojom::ServiceController>
...@@ -57,7 +57,8 @@ class FakeServiceController : public libassistant::mojom::ServiceController { ...@@ -57,7 +57,8 @@ class FakeServiceController : public libassistant::mojom::ServiceController {
void UnblockStartCalls(); void UnblockStartCalls();
// mojom::ServiceController implementation: // mojom::ServiceController implementation:
void Start(const std::string& libassistant_config) override; void Initialize(const std::string& libassistant_config) override;
void Start() override;
void Stop() override; void Stop() override;
void AddAndFireStateObserver( void AddAndFireStateObserver(
mojo::PendingRemote<libassistant::mojom::StateObserver> pending_observer) mojo::PendingRemote<libassistant::mojom::StateObserver> pending_observer)
......
...@@ -25,12 +25,19 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) AssistantManagerObserver ...@@ -25,12 +25,19 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) AssistantManagerObserver
~AssistantManagerObserver() override = default; ~AssistantManagerObserver() override = default;
// Called when the |AssistantManager| and |AssistantManagerInternal| have // Called when the |AssistantManager| and |AssistantManagerInternal| have
// been created. The pointers are guaranteed to remain valid until after // been created, but not started yet.
// The pointers are guaranteed to remain valid until after
// OnDestroyingAssistantManager() is called. // OnDestroyingAssistantManager() is called.
virtual void OnAssistantManagerCreated( virtual void OnAssistantManagerCreated(
assistant_client::AssistantManager* assistant_manager, assistant_client::AssistantManager* assistant_manager,
assistant_client::AssistantManagerInternal* assistant_client::AssistantManagerInternal* assistant_manager_internal) {}
assistant_manager_internal) = 0;
// Called when Start() has been called on the |AssistantManager|.
// The pointers are guaranteed to remain valid until after
// OnDestroyingAssistantManager() is called.
virtual void OnAssistantManagerStarted(
assistant_client::AssistantManager* assistant_manager,
assistant_client::AssistantManagerInternal* assistant_manager_internal) {}
// Called just before the |AssistantManager| and |AssistantManagerInternal| // Called just before the |AssistantManager| and |AssistantManagerInternal|
// will be destroyed. They should not be used anymore after this has been // will be destroyed. They should not be used anymore after this has been
...@@ -39,8 +46,7 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) AssistantManagerObserver ...@@ -39,8 +46,7 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) AssistantManagerObserver
// for the implementer's convenience). // for the implementer's convenience).
virtual void OnDestroyingAssistantManager( virtual void OnDestroyingAssistantManager(
assistant_client::AssistantManager* assistant_manager, assistant_client::AssistantManager* assistant_manager,
assistant_client::AssistantManagerInternal* assistant_client::AssistantManagerInternal* assistant_manager_internal) {}
assistant_manager_internal) = 0;
}; };
} // namespace libassistant } // namespace libassistant
......
...@@ -16,13 +16,18 @@ enum ServiceState { ...@@ -16,13 +16,18 @@ enum ServiceState {
// Interface managing the lifecycle of Libassistant, // Interface managing the lifecycle of Libassistant,
// exposing methods to start/stop and configure Libassistant. // exposing methods to start/stop and configure Libassistant.
interface ServiceController { 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);
// Start the service. Can be called multiple times, and will be a noop if // Start the service. Can be called multiple times, and will be a noop if
// the service is already started or running. // the service is already started or running.
// TODO(jeroendh): I feel the libassistant config is something internal to Start();
// the mojom service, so we should not pass that in here. Instead, have
// setters on the |ServiceController| for the fields that are non-static
// like the s3 server URI.
Start(string libassistant_config);
// Stop the service. Will be a noop if the service is already stopped. // Stop the service. Will be a noop if the service is already stopped.
Stop(); Stop();
......
...@@ -36,16 +36,28 @@ void ServiceController::SetInitializeCallback(InitializeCallback callback) { ...@@ -36,16 +36,28 @@ void ServiceController::SetInitializeCallback(InitializeCallback callback) {
initialize_callback_ = std::move(callback); initialize_callback_ = std::move(callback);
} }
void ServiceController::Start(const std::string& libassistant_config) { void ServiceController::Initialize(const std::string& libassistant_config) {
if (state_ != ServiceState::kStopped) if (assistant_manager_ != nullptr) {
LOG(ERROR) << "Initialize() should only be called once.";
return; return;
}
assistant_manager_ = assistant_manager_ =
delegate_->CreateAssistantManager(platform_api_, libassistant_config); delegate_->CreateAssistantManager(platform_api_, libassistant_config);
assistant_manager_internal_ = assistant_manager_internal_ =
delegate_->UnwrapAssistantManagerInternal(assistant_manager_.get()); delegate_->UnwrapAssistantManagerInternal(assistant_manager_.get());
libassistant_v1_api_ = std::make_unique<assistant::LibassistantV1Api>(
assistant_manager_.get(), assistant_manager_internal_); for (auto& observer : assistant_manager_observers_) {
observer.OnAssistantManagerCreated(assistant_manager(),
assistant_manager_internal());
}
}
void ServiceController::Start() {
if (state_ != ServiceState::kStopped)
return;
DCHECK(IsInitialized()) << "Initialize() must be called before Start()";
if (initialize_callback_) { if (initialize_callback_) {
std::move(initialize_callback_) std::move(initialize_callback_)
...@@ -54,10 +66,13 @@ void ServiceController::Start(const std::string& libassistant_config) { ...@@ -54,10 +66,13 @@ void ServiceController::Start(const std::string& libassistant_config) {
assistant_manager()->Start(); assistant_manager()->Start();
libassistant_v1_api_ = std::make_unique<assistant::LibassistantV1Api>(
assistant_manager_.get(), assistant_manager_internal_);
SetStateAndInformObservers(ServiceState::kStarted); SetStateAndInformObservers(ServiceState::kStarted);
for (auto& observer : assistant_manager_observers_) { for (auto& observer : assistant_manager_observers_) {
observer.OnAssistantManagerCreated(assistant_manager(), observer.OnAssistantManagerStarted(assistant_manager(),
assistant_manager_internal()); assistant_manager_internal());
} }
} }
...@@ -93,10 +108,14 @@ void ServiceController::AddAndFireAssistantManagerObserver( ...@@ -93,10 +108,14 @@ void ServiceController::AddAndFireAssistantManagerObserver(
assistant_manager_observers_.AddObserver(observer); assistant_manager_observers_.AddObserver(observer);
if (IsStarted()) { if (IsInitialized()) {
observer->OnAssistantManagerCreated(assistant_manager(), observer->OnAssistantManagerCreated(assistant_manager(),
assistant_manager_internal()); assistant_manager_internal());
} }
if (IsStarted()) {
observer->OnAssistantManagerStarted(assistant_manager(),
assistant_manager_internal());
}
} }
void ServiceController::RemoveAssistantManagerObserver( void ServiceController::RemoveAssistantManagerObserver(
...@@ -108,6 +127,10 @@ bool ServiceController::IsStarted() const { ...@@ -108,6 +127,10 @@ bool ServiceController::IsStarted() const {
return state_ != mojom::ServiceState::kStopped; return state_ != mojom::ServiceState::kStopped;
} }
bool ServiceController::IsInitialized() const {
return assistant_manager_ != nullptr;
}
assistant_client::AssistantManager* ServiceController::assistant_manager() { assistant_client::AssistantManager* ServiceController::assistant_manager() {
return assistant_manager_.get(); return assistant_manager_.get();
} }
......
...@@ -62,7 +62,8 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) ServiceController ...@@ -62,7 +62,8 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) ServiceController
void SetInitializeCallback(InitializeCallback callback); void SetInitializeCallback(InitializeCallback callback);
// mojom::ServiceController implementation: // mojom::ServiceController implementation:
void Start(const std::string& libassistant_config) override; void Initialize(const std::string& libassistant_config) override;
void Start() override;
void Stop() override; void Stop() override;
void AddAndFireStateObserver( void AddAndFireStateObserver(
mojo::PendingRemote<mojom::StateObserver> observer) override; mojo::PendingRemote<mojom::StateObserver> observer) override;
...@@ -70,6 +71,7 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) ServiceController ...@@ -70,6 +71,7 @@ class COMPONENT_EXPORT(LIBASSISTANT_SERVICE) ServiceController
void AddAndFireAssistantManagerObserver(AssistantManagerObserver* observer); void AddAndFireAssistantManagerObserver(AssistantManagerObserver* observer);
void RemoveAssistantManagerObserver(AssistantManagerObserver* observer); void RemoveAssistantManagerObserver(AssistantManagerObserver* observer);
bool IsInitialized() const;
bool IsStarted() const; bool IsStarted() const;
// Will return nullptr if the service is stopped. // Will return nullptr if the service is stopped.
......
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