Commit 7a8abf35 authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Commit Bot

Convert Model from model.mojom to new Mojo types

This CL converts Model from model.mojom to new Mojo types using
PendingReceiver, ReceiverSet, and Remote.

Bug: 955171
Change-Id: Ie0ac1f426d46d8efe22bdb52c99d6c8e43051efc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1851367Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Commit-Queue: Julie Kim <jkim@igalia.com>
Cr-Commit-Position: refs/heads/master@{#706328}
parent b52d0651
...@@ -22,10 +22,10 @@ MachineLearningInternalsPageHandler::~MachineLearningInternalsPageHandler() = ...@@ -22,10 +22,10 @@ MachineLearningInternalsPageHandler::~MachineLearningInternalsPageHandler() =
void MachineLearningInternalsPageHandler::LoadBuiltinModel( void MachineLearningInternalsPageHandler::LoadBuiltinModel(
mojom::BuiltinModelSpecPtr spec, mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
LoadBuiltinModelCallback callback) { LoadBuiltinModelCallback callback) {
ServiceConnection::GetInstance()->LoadBuiltinModel( ServiceConnection::GetInstance()->LoadBuiltinModel(
std::move(spec), std::move(request), std::move(callback)); std::move(spec), std::move(receiver), std::move(callback));
} }
} // namespace machine_learning } // namespace machine_learning
......
...@@ -26,7 +26,7 @@ class MachineLearningInternalsPageHandler : public mojom::PageHandler { ...@@ -26,7 +26,7 @@ class MachineLearningInternalsPageHandler : public mojom::PageHandler {
private: private:
// mojom::PageHandler: // mojom::PageHandler:
void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec, void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
LoadBuiltinModelCallback callback) override; LoadBuiltinModelCallback callback) override;
mojo::Receiver<mojom::PageHandler> receiver_; mojo::Receiver<mojom::PageHandler> receiver_;
......
...@@ -11,6 +11,6 @@ import "chromeos/services/machine_learning/public/mojom/graph_executor.mojom"; ...@@ -11,6 +11,6 @@ import "chromeos/services/machine_learning/public/mojom/graph_executor.mojom";
interface PageHandler { interface PageHandler {
// Loads the specified builtin model by forwarding it to Chrome OS ML // Loads the specified builtin model by forwarding it to Chrome OS ML
// Service. // Service.
LoadBuiltinModel(BuiltinModelSpec spec, Model& request) LoadBuiltinModel(BuiltinModelSpec spec, pending_receiver<Model> receiver)
=> (LoadModelResult result); => (LoadModelResult result);
}; };
\ No newline at end of file
...@@ -16,17 +16,17 @@ FakeServiceConnectionImpl::~FakeServiceConnectionImpl() {} ...@@ -16,17 +16,17 @@ FakeServiceConnectionImpl::~FakeServiceConnectionImpl() {}
void FakeServiceConnectionImpl::LoadBuiltinModel( void FakeServiceConnectionImpl::LoadBuiltinModel(
mojom::BuiltinModelSpecPtr spec, mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadBuiltinModelCallback callback) { mojom::MachineLearningService::LoadBuiltinModelCallback callback) {
model_bindings_.AddBinding(this, std::move(request)); model_receivers_.Add(this, std::move(receiver));
std::move(callback).Run(mojom::LoadModelResult::OK); std::move(callback).Run(mojom::LoadModelResult::OK);
} }
void FakeServiceConnectionImpl::LoadFlatBufferModel( void FakeServiceConnectionImpl::LoadFlatBufferModel(
mojom::FlatBufferModelSpecPtr spec, mojom::FlatBufferModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadFlatBufferModelCallback callback) { mojom::MachineLearningService::LoadFlatBufferModelCallback callback) {
model_bindings_.AddBinding(this, std::move(request)); model_receivers_.Add(this, std::move(receiver));
std::move(callback).Run(mojom::LoadModelResult::OK); std::move(callback).Run(mojom::LoadModelResult::OK);
} }
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "chromeos/services/machine_learning/public/mojom/model.mojom.h" #include "chromeos/services/machine_learning/public/mojom/model.mojom.h"
#include "chromeos/services/machine_learning/public/mojom/tensor.mojom.h" #include "chromeos/services/machine_learning/public/mojom/tensor.mojom.h"
#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
namespace chromeos { namespace chromeos {
namespace machine_learning { namespace machine_learning {
...@@ -31,14 +33,14 @@ class FakeServiceConnectionImpl : public ServiceConnection, ...@@ -31,14 +33,14 @@ class FakeServiceConnectionImpl : public ServiceConnection,
~FakeServiceConnectionImpl() override; ~FakeServiceConnectionImpl() override;
// It's safe to execute LoadBuiltinModel and LoadFlatBufferModel for multi // It's safe to execute LoadBuiltinModel and LoadFlatBufferModel for multi
// times, but all the requests will be bound to the same instance. // times, but all the receivers will be bound to the same instance.
void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec, void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadBuiltinModelCallback mojom::MachineLearningService::LoadBuiltinModelCallback
callback) override; callback) override;
void LoadFlatBufferModel( void LoadFlatBufferModel(
mojom::FlatBufferModelSpecPtr spec, mojom::FlatBufferModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadFlatBufferModelCallback callback) mojom::MachineLearningService::LoadFlatBufferModelCallback callback)
override; override;
...@@ -58,7 +60,7 @@ class FakeServiceConnectionImpl : public ServiceConnection, ...@@ -58,7 +60,7 @@ class FakeServiceConnectionImpl : public ServiceConnection,
const std::vector<double>& value); const std::vector<double>& value);
private: private:
mojo::BindingSet<mojom::Model> model_bindings_; mojo::ReceiverSet<mojom::Model> model_receivers_;
mojo::BindingSet<mojom::GraphExecutor> graph_bindings_; mojo::BindingSet<mojom::GraphExecutor> graph_bindings_;
mojom::TensorPtr execute_result_; mojom::TensorPtr execute_result_;
......
...@@ -28,13 +28,13 @@ class ServiceConnectionImpl : public ServiceConnection { ...@@ -28,13 +28,13 @@ class ServiceConnectionImpl : public ServiceConnection {
~ServiceConnectionImpl() override = default; ~ServiceConnectionImpl() override = default;
void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec, void LoadBuiltinModel(mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadBuiltinModelCallback mojom::MachineLearningService::LoadBuiltinModelCallback
result_callback) override; result_callback) override;
void LoadFlatBufferModel( void LoadFlatBufferModel(
mojom::FlatBufferModelSpecPtr spec, mojom::FlatBufferModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadFlatBufferModelCallback mojom::MachineLearningService::LoadFlatBufferModelCallback
result_callback) override; result_callback) override;
...@@ -60,23 +60,23 @@ class ServiceConnectionImpl : public ServiceConnection { ...@@ -60,23 +60,23 @@ class ServiceConnectionImpl : public ServiceConnection {
void ServiceConnectionImpl::LoadBuiltinModel( void ServiceConnectionImpl::LoadBuiltinModel(
mojom::BuiltinModelSpecPtr spec, mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadBuiltinModelCallback result_callback) { mojom::MachineLearningService::LoadBuiltinModelCallback result_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BindMachineLearningServiceIfNeeded(); BindMachineLearningServiceIfNeeded();
machine_learning_service_->LoadBuiltinModel( machine_learning_service_->LoadBuiltinModel(
std::move(spec), std::move(request), std::move(result_callback)); std::move(spec), std::move(receiver), std::move(result_callback));
} }
void ServiceConnectionImpl::LoadFlatBufferModel( void ServiceConnectionImpl::LoadFlatBufferModel(
mojom::FlatBufferModelSpecPtr spec, mojom::FlatBufferModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadFlatBufferModelCallback mojom::MachineLearningService::LoadFlatBufferModelCallback
result_callback) { result_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BindMachineLearningServiceIfNeeded(); BindMachineLearningServiceIfNeeded();
machine_learning_service_->LoadFlatBufferModel( machine_learning_service_->LoadFlatBufferModel(
std::move(spec), std::move(request), std::move(result_callback)); std::move(spec), std::move(receiver), std::move(result_callback));
} }
void ServiceConnectionImpl::BindMachineLearningServiceIfNeeded() { void ServiceConnectionImpl::BindMachineLearningServiceIfNeeded() {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROMEOS_SERVICES_MACHINE_LEARNING_PUBLIC_CPP_SERVICE_CONNECTION_H_ #define CHROMEOS_SERVICES_MACHINE_LEARNING_PUBLIC_CPP_SERVICE_CONNECTION_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"
#include "mojo/public/cpp/bindings/pending_receiver.h"
namespace chromeos { namespace chromeos {
namespace machine_learning { namespace machine_learning {
...@@ -13,16 +14,16 @@ namespace machine_learning { ...@@ -13,16 +14,16 @@ namespace machine_learning {
// Encapsulates a connection to the Chrome OS ML Service daemon via its Mojo // Encapsulates a connection to the Chrome OS ML Service daemon via its Mojo
// interface. // interface.
// Usage for Built-in models: // Usage for Built-in models:
// chromeos::machine_learning::mojom::ModelPtr model; // mojo::Remote<chromeos::machine_learning::mojom::Model> model;
// chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec = // chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec =
// chromeos::machine_learning::mojom::BuiltinModelSpec::New(); // chromeos::machine_learning::mojom::BuiltinModelSpec::New();
// spec->id = ...; // spec->id = ...;
// chromeos::machine_learning::ServiceConnection::GetInstance() // chromeos::machine_learning::ServiceConnection::GetInstance()
// ->LoadBuiltinModel(std::move(spec), mojom::MakeRequest(&model), // ->LoadBuiltinModel(std::move(spec), model.BindNewPipeAndPassReceiver(),
// base::BindOnce(&MyCallBack)); // base::BindOnce(&MyCallBack));
// // Use |model| or wait for |MyCallBack|. // // Use |model| or wait for |MyCallBack|.
// Usage for Flatbuffer models: // Usage for Flatbuffer models:
// chromeos::machine_learning::mojom::ModelPtr model; // mojo::Remote<chromeos::machine_learning::mojom::Model> model;
// chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec = // chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec =
// chromeos::machine_learning::mojom::FlatBufferModelSpec::New(); // chromeos::machine_learning::mojom::FlatBufferModelSpec::New();
// spec->model_string = ...; // spec->model_string = ...;
...@@ -30,7 +31,8 @@ namespace machine_learning { ...@@ -30,7 +31,8 @@ namespace machine_learning {
// spec->outputs = ...; // spec->outputs = ...;
// spec->metrics_model_name = ...; // spec->metrics_model_name = ...;
// chromeos::machine_learning::ServiceConnection::GetInstance() // chromeos::machine_learning::ServiceConnection::GetInstance()
// ->LoadFlatBufferModel(std::move(spec), mojom::MakeRequest(&model), // ->LoadFlatBufferModel(std::move(spec),
// model.BindNewPipeAndPassReceiver(),
// base::BindOnce(&MyCallBack)); // base::BindOnce(&MyCallBack));
// //
// Sequencing: Must be used on a single sequence (may be created on another). // Sequencing: Must be used on a single sequence (may be created on another).
...@@ -43,20 +45,20 @@ class ServiceConnection { ...@@ -43,20 +45,20 @@ class ServiceConnection {
ServiceConnection* fake_service_connection); ServiceConnection* fake_service_connection);
// Instruct ML daemon to load the builtin model specified in |spec|, binding a // Instruct ML daemon to load the builtin model specified in |spec|, binding a
// Model implementation to |request|. Bootstraps the initial Mojo connection // Model implementation to |receiver|. Bootstraps the initial Mojo connection
// to the daemon if necessary. // to the daemon if necessary.
virtual void LoadBuiltinModel( virtual void LoadBuiltinModel(
mojom::BuiltinModelSpecPtr spec, mojom::BuiltinModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadBuiltinModelCallback mojom::MachineLearningService::LoadBuiltinModelCallback
result_callback) = 0; result_callback) = 0;
// Instruct ML daemon to load the flatbuffer model specified in |spec|, // Instruct ML daemon to load the flatbuffer model specified in |spec|,
// binding a Model implementation to |request|. Bootstraps the initial Mojo // binding a Model implementation to |receiver|. Bootstraps the initial Mojo
// connection to the daemon if necessary. // connection to the daemon if necessary.
virtual void LoadFlatBufferModel( virtual void LoadFlatBufferModel(
mojom::FlatBufferModelSpecPtr spec, mojom::FlatBufferModelSpecPtr spec,
mojom::ModelRequest request, mojo::PendingReceiver<mojom::Model> receiver,
mojom::MachineLearningService::LoadFlatBufferModelCallback mojom::MachineLearningService::LoadFlatBufferModelCallback
result_callback) = 0; result_callback) = 0;
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "mojo/core/embedder/embedder.h" #include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/scoped_ipc_support.h" #include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { namespace chromeos {
...@@ -55,27 +56,27 @@ class ServiceConnectionTest : public testing::Test { ...@@ -55,27 +56,27 @@ class ServiceConnectionTest : public testing::Test {
// Tests that LoadBuiltinModel runs OK (no crash) in a basic Mojo // Tests that LoadBuiltinModel runs OK (no crash) in a basic Mojo
// environment. // environment.
TEST_F(ServiceConnectionTest, LoadBuiltinModel) { TEST_F(ServiceConnectionTest, LoadBuiltinModel) {
mojom::ModelPtr model; mojo::Remote<mojom::Model> model;
mojom::BuiltinModelSpecPtr spec = mojom::BuiltinModelSpecPtr spec =
mojom::BuiltinModelSpec::New(mojom::BuiltinModelId::TEST_MODEL); mojom::BuiltinModelSpec::New(mojom::BuiltinModelId::TEST_MODEL);
ServiceConnection::GetInstance()->LoadBuiltinModel( ServiceConnection::GetInstance()->LoadBuiltinModel(
std::move(spec), mojo::MakeRequest(&model), std::move(spec), model.BindNewPipeAndPassReceiver(),
base::BindOnce([](mojom::LoadModelResult result) {})); base::BindOnce([](mojom::LoadModelResult result) {}));
} }
// Tests that LoadFlatBufferModel runs OK (no crash) in a basic Mojo // Tests that LoadFlatBufferModel runs OK (no crash) in a basic Mojo
// environment. // environment.
TEST_F(ServiceConnectionTest, LoadFlatBufferModel) { TEST_F(ServiceConnectionTest, LoadFlatBufferModel) {
mojom::ModelPtr model; mojo::Remote<mojom::Model> model;
mojom::FlatBufferModelSpecPtr spec = mojom::FlatBufferModelSpec::New(); mojom::FlatBufferModelSpecPtr spec = mojom::FlatBufferModelSpec::New();
ServiceConnection::GetInstance()->LoadFlatBufferModel( ServiceConnection::GetInstance()->LoadFlatBufferModel(
std::move(spec), mojo::MakeRequest(&model), std::move(spec), model.BindNewPipeAndPassReceiver(),
base::BindOnce([](mojom::LoadModelResult result) {})); base::BindOnce([](mojom::LoadModelResult result) {}));
} }
// Tests the fake ML service for builtin model. // Tests the fake ML service for builtin model.
TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) { TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) {
mojom::ModelPtr model; mojo::Remote<mojom::Model> model;
bool callback_done = false; bool callback_done = false;
FakeServiceConnectionImpl fake_service_connection; FakeServiceConnectionImpl fake_service_connection;
ServiceConnection::UseFakeServiceConnectionForTesting( ServiceConnection::UseFakeServiceConnectionForTesting(
...@@ -86,7 +87,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) { ...@@ -86,7 +87,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) {
std::vector<double>{expected_value}); std::vector<double>{expected_value});
ServiceConnection::GetInstance()->LoadBuiltinModel( ServiceConnection::GetInstance()->LoadBuiltinModel(
mojom::BuiltinModelSpec::New(mojom::BuiltinModelId::TEST_MODEL), mojom::BuiltinModelSpec::New(mojom::BuiltinModelId::TEST_MODEL),
mojo::MakeRequest(&model), model.BindNewPipeAndPassReceiver(),
base::BindOnce( base::BindOnce(
[](bool* callback_done, mojom::LoadModelResult result) { [](bool* callback_done, mojom::LoadModelResult result) {
EXPECT_EQ(result, mojom::LoadModelResult::OK); EXPECT_EQ(result, mojom::LoadModelResult::OK);
...@@ -136,7 +137,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) { ...@@ -136,7 +137,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForBuiltinModel) {
// Tests the fake ML service for flatbuffer model. // Tests the fake ML service for flatbuffer model.
TEST_F(ServiceConnectionTest, FakeServiceConnectionForFlatBufferModel) { TEST_F(ServiceConnectionTest, FakeServiceConnectionForFlatBufferModel) {
mojom::ModelPtr model; mojo::Remote<mojom::Model> model;
bool callback_done = false; bool callback_done = false;
FakeServiceConnectionImpl fake_service_connection; FakeServiceConnectionImpl fake_service_connection;
ServiceConnection::UseFakeServiceConnectionForTesting( ServiceConnection::UseFakeServiceConnectionForTesting(
...@@ -147,7 +148,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForFlatBufferModel) { ...@@ -147,7 +148,7 @@ TEST_F(ServiceConnectionTest, FakeServiceConnectionForFlatBufferModel) {
std::vector<double>{expected_value}); std::vector<double>{expected_value});
ServiceConnection::GetInstance()->LoadFlatBufferModel( ServiceConnection::GetInstance()->LoadFlatBufferModel(
mojom::FlatBufferModelSpec::New(), mojo::MakeRequest(&model), mojom::FlatBufferModelSpec::New(), model.BindNewPipeAndPassReceiver(),
base::BindOnce( base::BindOnce(
[](bool* callback_done, mojom::LoadModelResult result) { [](bool* callback_done, mojom::LoadModelResult result) {
EXPECT_EQ(result, mojom::LoadModelResult::OK); EXPECT_EQ(result, mojom::LoadModelResult::OK);
......
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