| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "mojo/public/cpp/bindings/binding_set.h" | 9 #include "mojo/public/cpp/bindings/binding_set.h" |
| 10 #include "services/service_manager/public/c/main.h" | 10 #include "services/service_manager/public/c/main.h" |
| 11 #include "services/service_manager/public/cpp/binder_registry.h" |
| 11 #include "services/service_manager/public/cpp/connector.h" | 12 #include "services/service_manager/public/cpp/connector.h" |
| 12 #include "services/service_manager/public/cpp/interface_factory.h" | 13 #include "services/service_manager/public/cpp/interface_factory.h" |
| 13 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 14 #include "services/service_manager/public/cpp/service.h" | 14 #include "services/service_manager/public/cpp/service.h" |
| 15 #include "services/service_manager/public/cpp/service_context.h" | 15 #include "services/service_manager/public/cpp/service_context.h" |
| 16 #include "services/service_manager/public/cpp/service_context_ref.h" |
| 16 #include "services/service_manager/public/cpp/service_runner.h" | 17 #include "services/service_manager/public/cpp/service_runner.h" |
| 17 #include "services/service_manager/public/interfaces/connector.mojom.h" | 18 #include "services/service_manager/public/interfaces/connector.mojom.h" |
| 18 #include "services/service_manager/tests/connect/connect_test.mojom.h" | 19 #include "services/service_manager/tests/connect/connect_test.mojom.h" |
| 19 | 20 |
| 20 namespace service_manager { | 21 namespace service_manager { |
| 21 | 22 |
| 22 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | 23 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; |
| 23 | 24 |
| 24 class ConnectTestClassApp | 25 class ConnectTestClassApp |
| 25 : public Service, | 26 : public Service, |
| 26 public InterfaceFactory<test::mojom::ConnectTestService>, | 27 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 27 public InterfaceFactory<test::mojom::ClassInterface>, | 28 public InterfaceFactory<test::mojom::ClassInterface>, |
| 28 public test::mojom::ConnectTestService, | 29 public test::mojom::ConnectTestService, |
| 29 public test::mojom::ClassInterface { | 30 public test::mojom::ClassInterface { |
| 30 public: | 31 public: |
| 31 ConnectTestClassApp() {} | 32 ConnectTestClassApp() |
| 33 : ref_factory_(base::Bind(&ConnectTestClassApp::HandleQuit, |
| 34 base::Unretained(this))) { |
| 35 bindings_.set_connection_error_handler(base::Bind( |
| 36 &ConnectTestClassApp::HandleInterfaceClose, base::Unretained(this))); |
| 37 class_interface_bindings_.set_connection_error_handler(base::Bind( |
| 38 &ConnectTestClassApp::HandleInterfaceClose, base::Unretained(this))); |
| 39 registry_.AddInterface<test::mojom::ConnectTestService>(this); |
| 40 registry_.AddInterface<test::mojom::ClassInterface>(this); |
| 41 } |
| 32 ~ConnectTestClassApp() override {} | 42 ~ConnectTestClassApp() override {} |
| 33 | 43 |
| 34 private: | 44 private: |
| 35 // service_manager::Service: | 45 // service_manager::Service: |
| 36 bool OnConnect(const ServiceInfo& remote_info, | 46 void OnBindInterface(const ServiceInfo& source_info, |
| 37 InterfaceRegistry* registry) override { | 47 const std::string& interface_name, |
| 38 registry->AddInterface<test::mojom::ConnectTestService>(this); | 48 mojo::ScopedMessagePipeHandle interface_pipe) override { |
| 39 registry->AddInterface<test::mojom::ClassInterface>(this); | 49 registry_.BindInterface(source_info.identity, interface_name, |
| 40 inbound_connections_.insert(registry); | 50 std::move(interface_pipe)); |
| 41 registry->AddConnectionLostClosure( | |
| 42 base::Bind(&ConnectTestClassApp::OnConnectionError, | |
| 43 base::Unretained(this), registry)); | |
| 44 return true; | |
| 45 } | 51 } |
| 46 | 52 |
| 47 // InterfaceFactory<test::mojom::ConnectTestService>: | 53 // InterfaceFactory<test::mojom::ConnectTestService>: |
| 48 void Create(const Identity& remote_identity, | 54 void Create(const Identity& remote_identity, |
| 49 test::mojom::ConnectTestServiceRequest request) override { | 55 test::mojom::ConnectTestServiceRequest request) override { |
| 56 refs_.push_back(ref_factory_.CreateRef()); |
| 50 bindings_.AddBinding(this, std::move(request)); | 57 bindings_.AddBinding(this, std::move(request)); |
| 51 } | 58 } |
| 52 | 59 |
| 53 // InterfaceFactory<test::mojom::ClassInterface>: | 60 // InterfaceFactory<test::mojom::ClassInterface>: |
| 54 void Create(const Identity& remote_identity, | 61 void Create(const Identity& remote_identity, |
| 55 test::mojom::ClassInterfaceRequest request) override { | 62 test::mojom::ClassInterfaceRequest request) override { |
| 63 refs_.push_back(ref_factory_.CreateRef()); |
| 56 class_interface_bindings_.AddBinding(this, std::move(request)); | 64 class_interface_bindings_.AddBinding(this, std::move(request)); |
| 57 } | 65 } |
| 58 | 66 |
| 59 // test::mojom::ConnectTestService: | 67 // test::mojom::ConnectTestService: |
| 60 void GetTitle(const GetTitleCallback& callback) override { | 68 void GetTitle(const GetTitleCallback& callback) override { |
| 61 callback.Run("CLASS APP"); | 69 callback.Run("CLASS APP"); |
| 62 } | 70 } |
| 63 void GetInstance(const GetInstanceCallback& callback) override { | 71 void GetInstance(const GetInstanceCallback& callback) override { |
| 64 callback.Run(context()->identity().instance()); | 72 callback.Run(context()->identity().instance()); |
| 65 } | 73 } |
| 66 | 74 |
| 67 // test::mojom::ClassInterface: | 75 // test::mojom::ClassInterface: |
| 68 void Ping(const PingCallback& callback) override { | 76 void Ping(const PingCallback& callback) override { |
| 69 callback.Run("PONG"); | 77 callback.Run("PONG"); |
| 70 } | 78 } |
| 71 | 79 |
| 72 void OnConnectionError(InterfaceRegistry* registry) { | 80 void HandleQuit() { context()->QuitNow(); } |
| 73 auto it = inbound_connections_.find(registry); | |
| 74 DCHECK(it != inbound_connections_.end()); | |
| 75 inbound_connections_.erase(it); | |
| 76 if (inbound_connections_.empty()) | |
| 77 context()->QuitNow(); | |
| 78 } | |
| 79 | 81 |
| 80 std::set<InterfaceRegistry*> inbound_connections_; | 82 void HandleInterfaceClose() { refs_.pop_back(); } |
| 83 |
| 84 BinderRegistry registry_; |
| 81 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 85 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 82 mojo::BindingSet<test::mojom::ClassInterface> class_interface_bindings_; | 86 mojo::BindingSet<test::mojom::ClassInterface> class_interface_bindings_; |
| 87 ServiceContextRefFactory ref_factory_; |
| 88 std::vector<std::unique_ptr<ServiceContextRef>> refs_; |
| 83 | 89 |
| 84 DISALLOW_COPY_AND_ASSIGN(ConnectTestClassApp); | 90 DISALLOW_COPY_AND_ASSIGN(ConnectTestClassApp); |
| 85 }; | 91 }; |
| 86 | 92 |
| 87 } // namespace service_manager | 93 } // namespace service_manager |
| 88 | 94 |
| 89 MojoResult ServiceMain(MojoHandle service_request_handle) { | 95 MojoResult ServiceMain(MojoHandle service_request_handle) { |
| 90 service_manager::ServiceRunner runner( | 96 service_manager::ServiceRunner runner( |
| 91 new service_manager::ConnectTestClassApp); | 97 new service_manager::ConnectTestClassApp); |
| 92 return runner.Run(service_request_handle); | 98 return runner.Run(service_request_handle); |
| 93 } | 99 } |
| OLD | NEW |