Commit 87625133 authored by Jeroen Dhollander's avatar Jeroen Dhollander Committed by Commit Bot

Expose Libassistant V1 classes through a singleton

This will allow us to move these classes (|AssistantManager| and
|AssistantManagerInternal|) to a new Libassistant mojom service, but
still access them from the legacy code while the transition is in
progress.

Bug: b/171748795
Test: chromeos_unittests
Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome
Change-Id: I784c57ea1b1e89fb5ce710e031eae560e2e1cf92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537784Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Jeroen Dhollander <jeroendh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828225}
parent e321167b
......@@ -39,6 +39,7 @@
#include "chromeos/services/assistant/public/cpp/assistant_client.h"
#include "chromeos/services/assistant/public/cpp/device_actions.h"
#include "chromeos/services/assistant/public/cpp/features.h"
#include "chromeos/services/assistant/public/cpp/libassistant_v1_api.h"
#include "chromeos/services/assistant/public/shared/utils.h"
#include "chromeos/services/assistant/service_context.h"
#include "chromeos/services/assistant/utils.h"
......@@ -1380,16 +1381,14 @@ CrosDisplayConnection* AssistantManagerServiceImpl::display_connection() {
assistant_client::AssistantManager*
AssistantManagerServiceImpl::assistant_manager() {
if (!IsServiceStarted())
return nullptr;
return service_controller().assistant_manager();
auto* api = LibassistantV1Api::Get();
return api ? api->assistant_manager() : nullptr;
}
assistant_client::AssistantManagerInternal*
AssistantManagerServiceImpl::assistant_manager_internal() {
if (!IsServiceStarted())
return nullptr;
return service_controller().assistant_manager_internal();
auto* api = LibassistantV1Api::Get();
return api ? api->assistant_manager_internal() : nullptr;
}
ServiceController& AssistantManagerServiceImpl::service_controller() {
......
......@@ -10,6 +10,7 @@
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/services/assistant/assistant_manager_service_delegate.h"
#include "chromeos/services/assistant/public/cpp/features.h"
#include "chromeos/services/assistant/public/cpp/libassistant_v1_api.h"
#include "libassistant/shared/internal_api/assistant_manager_internal.h"
#include "libassistant/shared/internal_api/fuchsia_api_helper.h"
......@@ -223,6 +224,7 @@ void ServiceController::Stop() {
display_connection_ = nullptr;
assistant_manager_ = nullptr;
assistant_manager_internal_ = nullptr;
libassistant_v1_api_ = nullptr;
}
void ServiceController::UpdateInternalOptions(const std::string& locale,
......@@ -255,6 +257,10 @@ void ServiceController::OnAssistantCreated(
assistant_manager_ = std::move(assistant_manager);
assistant_manager_internal_ = assistant_manager_internal;
// Expose the Libassistant V1 API to the legacy code.
libassistant_v1_api_ = std::make_unique<LibassistantV1Api>(
assistant_manager_.get(), assistant_manager_internal_);
std::move(done_callback).Run();
}
......
......@@ -36,6 +36,7 @@ namespace assistant {
class AssistantEventObserver;
class AssistantManagerServiceDelegate;
class CrosDisplayConnection;
class LibassistantV1Api;
class ServiceController {
public:
......@@ -58,18 +59,6 @@ class ServiceController {
return display_connection_.get();
}
// Can not be invoked before Start() has finished.
assistant_client::AssistantManager* assistant_manager() {
DCHECK(IsStarted());
return assistant_manager_.get();
}
// Can not be invoked before Start() has finished.
assistant_client::AssistantManagerInternal* assistant_manager_internal() {
DCHECK(IsStarted());
return assistant_manager_internal_;
}
// Initialize the |AssistantManager| and all related objects by creating
// them on a background task and by calling their Start() methods. Will signal
// the objects exist and can be accessed by calling the |done_callback|.
......@@ -120,6 +109,18 @@ class ServiceController {
kStopped,
};
// Can not be invoked before Start() has finished.
assistant_client::AssistantManager* assistant_manager() {
DCHECK(IsStarted());
return assistant_manager_.get();
}
// Can not be invoked before Start() has finished.
assistant_client::AssistantManagerInternal* assistant_manager_internal() {
DCHECK(IsStarted());
return assistant_manager_internal_;
}
void OnAssistantCreated(
base::OnceClosure done_callback,
std::unique_ptr<CrosDisplayConnection> display_connection,
......@@ -140,6 +141,8 @@ class ServiceController {
assistant_client::AssistantManagerInternal* assistant_manager_internal_ =
nullptr;
std::unique_ptr<LibassistantV1Api> libassistant_v1_api_;
base::WeakPtrFactory<ServiceController> weak_factory_;
};
} // namespace assistant
......
......@@ -24,6 +24,8 @@ component("cpp") {
"device_actions.h",
"features.cc",
"features.h",
"libassistant_v1_api.cc",
"libassistant_v1_api.h",
]
public_deps = [
......
// Copyright 2020 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/assistant/public/cpp/libassistant_v1_api.h"
#include "base/check.h"
#include "base/check_op.h"
namespace chromeos {
namespace assistant {
// static
LibassistantV1Api* LibassistantV1Api::instance_ = nullptr;
LibassistantV1Api::LibassistantV1Api(
assistant_client::AssistantManager* assistant_manager,
assistant_client::AssistantManagerInternal* assistant_manager_internal)
: assistant_manager_(assistant_manager),
assistant_manager_internal_(assistant_manager_internal) {
DCHECK(!instance_);
instance_ = this;
}
LibassistantV1Api::~LibassistantV1Api() {
instance_ = nullptr;
}
} // namespace assistant
} // namespace chromeos
// Copyright 2020 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_ASSISTANT_PUBLIC_CPP_LIBASSISTANT_V1_API_H_
#define CHROMEOS_SERVICES_ASSISTANT_PUBLIC_CPP_LIBASSISTANT_V1_API_H_
#include "base/component_export.h"
namespace assistant_client {
class AssistantManager;
class AssistantManagerInternal;
} // namespace assistant_client
namespace chromeos {
namespace assistant {
// Singleton access to the Libassistant V1 objects.
// Only a single instance of this class may exist at any given time.
// TODO(b/171748795): Remove once all Libassistant access has been moved in the
// //chromeos/services/libassistant service.
class COMPONENT_EXPORT(ASSISTANT_SERVICE_PUBLIC) LibassistantV1Api {
public:
LibassistantV1Api(
assistant_client::AssistantManager* assistant_manager,
assistant_client::AssistantManagerInternal* assistant_manager_internal);
~LibassistantV1Api();
// Return the registered instance. Can be null if no instance was registered.
static LibassistantV1Api* Get() { return instance_; }
assistant_client::AssistantManager* assistant_manager() {
return assistant_manager_;
}
assistant_client::AssistantManagerInternal* assistant_manager_internal() {
return assistant_manager_internal_;
}
private:
static LibassistantV1Api* instance_;
assistant_client::AssistantManager* const assistant_manager_;
assistant_client::AssistantManagerInternal* const assistant_manager_internal_;
};
} // namespace assistant
} // namespace chromeos
#endif // CHROMEOS_SERVICES_ASSISTANT_PUBLIC_CPP_LIBASSISTANT_V1_API_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