| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/threading/simple_thread.h" | 15 #include "base/threading/simple_thread.h" |
| 16 #include "mojo/public/cpp/bindings/binding_set.h" | 16 #include "mojo/public/cpp/bindings/binding_set.h" |
| 17 #include "services/service_manager/public/c/main.h" | 17 #include "services/service_manager/public/c/main.h" |
| 18 #include "services/service_manager/public/cpp/connector.h" | 18 #include "services/service_manager/public/cpp/connector.h" |
| 19 #include "services/service_manager/public/cpp/interface_factory.h" | 19 #include "services/service_manager/public/cpp/interface_factory.h" |
| 20 #include "services/service_manager/public/cpp/interface_registry.h" |
| 20 #include "services/service_manager/public/cpp/service.h" | 21 #include "services/service_manager/public/cpp/service.h" |
| 22 #include "services/service_manager/public/cpp/service_context.h" |
| 21 #include "services/service_manager/public/cpp/service_runner.h" | 23 #include "services/service_manager/public/cpp/service_runner.h" |
| 22 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 24 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 23 #include "services/service_manager/tests/connect/connect_test.mojom.h" | 25 #include "services/service_manager/tests/connect/connect_test.mojom.h" |
| 24 | 26 |
| 25 // Tests that multiple services can be packaged in a single service by | 27 // Tests that multiple services can be packaged in a single service by |
| 26 // implementing ServiceFactory; that these services can be specified by | 28 // implementing ServiceFactory; that these services can be specified by |
| 27 // the package's manifest and are thus registered with the PackageManager. | 29 // the package's manifest and are thus registered with the PackageManager. |
| 28 | 30 |
| 29 namespace service_manager { | 31 namespace service_manager { |
| 30 | 32 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 class ProvidedService | 43 class ProvidedService |
| 42 : public Service, | 44 : public Service, |
| 43 public InterfaceFactory<test::mojom::ConnectTestService>, | 45 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 44 public InterfaceFactory<test::mojom::BlockedInterface>, | 46 public InterfaceFactory<test::mojom::BlockedInterface>, |
| 45 public InterfaceFactory<test::mojom::UserIdTest>, | 47 public InterfaceFactory<test::mojom::UserIdTest>, |
| 46 public test::mojom::ConnectTestService, | 48 public test::mojom::ConnectTestService, |
| 47 public test::mojom::BlockedInterface, | 49 public test::mojom::BlockedInterface, |
| 48 public test::mojom::UserIdTest, | 50 public test::mojom::UserIdTest, |
| 49 public base::SimpleThread { | 51 public base::SimpleThread { |
| 50 public: | 52 public: |
| 51 ProvidedService(const std::string& title, | 53 ProvidedService(const std::string& title, mojom::ServiceRequest request) |
| 52 mojom::ServiceRequest request) | |
| 53 : base::SimpleThread(title), | 54 : base::SimpleThread(title), |
| 54 title_(title), | 55 title_(title), |
| 55 request_(std::move(request)) { | 56 request_(std::move(request)) { |
| 56 Start(); | 57 Start(); |
| 57 } | 58 } |
| 58 ~ProvidedService() override { | 59 ~ProvidedService() override { |
| 59 Join(); | 60 Join(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 private: | 63 private: |
| 63 // service_manager::Service: | 64 // service_manager::Service: |
| 64 void OnStart(const ServiceInfo& info) override { | 65 void OnStart(ServiceContext* context) override { |
| 65 identity_ = info.identity; | 66 context_ = context; |
| 66 bindings_.set_connection_error_handler( | 67 bindings_.set_connection_error_handler( |
| 67 base::Bind(&ProvidedService::OnConnectionError, | 68 base::Bind(&ProvidedService::OnConnectionError, |
| 68 base::Unretained(this))); | 69 base::Unretained(this))); |
| 69 } | 70 } |
| 71 |
| 70 bool OnConnect(const ServiceInfo& remote_info, | 72 bool OnConnect(const ServiceInfo& remote_info, |
| 71 InterfaceRegistry* registry) override { | 73 InterfaceRegistry* registry) override { |
| 72 registry->AddInterface<test::mojom::ConnectTestService>(this); | 74 registry->AddInterface<test::mojom::ConnectTestService>(this); |
| 73 registry->AddInterface<test::mojom::BlockedInterface>(this); | 75 registry->AddInterface<test::mojom::BlockedInterface>(this); |
| 74 registry->AddInterface<test::mojom::UserIdTest>(this); | 76 registry->AddInterface<test::mojom::UserIdTest>(this); |
| 75 | 77 |
| 76 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | 78 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); |
| 77 state->connection_remote_name = remote_info.identity.name(); | 79 state->connection_remote_name = remote_info.identity.name(); |
| 78 state->connection_remote_userid = remote_info.identity.user_id(); | 80 state->connection_remote_userid = remote_info.identity.user_id(); |
| 79 state->initialize_local_name = identity_.name(); | 81 state->initialize_local_name = context_->identity().name(); |
| 80 state->initialize_userid = identity_.user_id(); | 82 state->initialize_userid = context_->identity().user_id(); |
| 81 | 83 |
| 82 connector()->ConnectToInterface(remote_info.identity, &caller_); | 84 context_->connector()->ConnectToInterface(remote_info.identity, &caller_); |
| 83 caller_->ConnectionAccepted(std::move(state)); | 85 caller_->ConnectionAccepted(std::move(state)); |
| 84 | 86 |
| 85 return true; | 87 return true; |
| 86 } | 88 } |
| 87 | 89 |
| 88 // InterfaceFactory<test::mojom::ConnectTestService>: | 90 // InterfaceFactory<test::mojom::ConnectTestService>: |
| 89 void Create(const Identity& remote_identity, | 91 void Create(const Identity& remote_identity, |
| 90 test::mojom::ConnectTestServiceRequest request) override { | 92 test::mojom::ConnectTestServiceRequest request) override { |
| 91 bindings_.AddBinding(this, std::move(request)); | 93 bindings_.AddBinding(this, std::move(request)); |
| 92 } | 94 } |
| 93 | 95 |
| 94 // InterfaceFactory<test::mojom::BlockedInterface>: | 96 // InterfaceFactory<test::mojom::BlockedInterface>: |
| 95 void Create(const Identity& remote_identity, | 97 void Create(const Identity& remote_identity, |
| 96 test::mojom::BlockedInterfaceRequest request) override { | 98 test::mojom::BlockedInterfaceRequest request) override { |
| 97 blocked_bindings_.AddBinding(this, std::move(request)); | 99 blocked_bindings_.AddBinding(this, std::move(request)); |
| 98 } | 100 } |
| 99 | 101 |
| 100 // InterfaceFactory<test::mojom::UserIdTest>: | 102 // InterfaceFactory<test::mojom::UserIdTest>: |
| 101 void Create(const Identity& remote_identity, | 103 void Create(const Identity& remote_identity, |
| 102 test::mojom::UserIdTestRequest request) override { | 104 test::mojom::UserIdTestRequest request) override { |
| 103 user_id_test_bindings_.AddBinding(this, std::move(request)); | 105 user_id_test_bindings_.AddBinding(this, std::move(request)); |
| 104 } | 106 } |
| 105 | 107 |
| 106 // test::mojom::ConnectTestService: | 108 // test::mojom::ConnectTestService: |
| 107 void GetTitle(const GetTitleCallback& callback) override { | 109 void GetTitle(const GetTitleCallback& callback) override { |
| 108 callback.Run(title_); | 110 callback.Run(title_); |
| 109 } | 111 } |
| 110 void GetInstance(const GetInstanceCallback& callback) override { | 112 void GetInstance(const GetInstanceCallback& callback) override { |
| 111 callback.Run(identity_.instance()); | 113 callback.Run(context_->identity().instance()); |
| 112 } | 114 } |
| 113 | 115 |
| 114 // test::mojom::BlockedInterface: | 116 // test::mojom::BlockedInterface: |
| 115 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 117 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
| 116 callback.Run("Called Blocked Interface!"); | 118 callback.Run("Called Blocked Interface!"); |
| 117 } | 119 } |
| 118 | 120 |
| 119 // test::mojom::UserIdTest: | 121 // test::mojom::UserIdTest: |
| 120 void ConnectToClassAppAsDifferentUser( | 122 void ConnectToClassAppAsDifferentUser( |
| 121 const service_manager::Identity& target, | 123 const service_manager::Identity& target, |
| 122 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 124 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
| 123 Connector::ConnectParams params(target); | 125 Connector::ConnectParams params(target); |
| 124 std::unique_ptr<Connection> connection = | 126 std::unique_ptr<Connection> connection = |
| 125 connector()->Connect(¶ms); | 127 context_->connector()->Connect(¶ms); |
| 126 { | 128 { |
| 127 base::RunLoop loop; | 129 base::RunLoop loop; |
| 128 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 130 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 129 base::MessageLoop::ScopedNestableTaskAllower allow( | 131 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 130 base::MessageLoop::current()); | 132 base::MessageLoop::current()); |
| 131 loop.Run(); | 133 loop.Run(); |
| 132 } | 134 } |
| 133 callback.Run(static_cast<int32_t>(connection->GetResult()), | 135 callback.Run(static_cast<int32_t>(connection->GetResult()), |
| 134 connection->GetRemoteIdentity()); | 136 connection->GetRemoteIdentity()); |
| 135 } | 137 } |
| 136 | 138 |
| 137 // base::SimpleThread: | 139 // base::SimpleThread: |
| 138 void Run() override { | 140 void Run() override { |
| 139 ServiceRunner(this).Run(request_.PassMessagePipe().release().value(), | 141 ServiceRunner(this).Run(request_.PassMessagePipe().release().value(), |
| 140 false); | 142 false); |
| 141 delete this; | 143 delete this; |
| 142 } | 144 } |
| 143 | 145 |
| 144 void OnConnectionError() { | 146 void OnConnectionError() { |
| 145 if (bindings_.empty()) | 147 if (bindings_.empty()) |
| 146 base::MessageLoop::current()->QuitWhenIdle(); | 148 base::MessageLoop::current()->QuitWhenIdle(); |
| 147 } | 149 } |
| 148 | 150 |
| 149 Identity identity_; | 151 ServiceContext* context_ = nullptr; |
| 150 const std::string title_; | 152 const std::string title_; |
| 151 mojom::ServiceRequest request_; | 153 mojom::ServiceRequest request_; |
| 152 test::mojom::ExposedInterfacePtr caller_; | 154 test::mojom::ExposedInterfacePtr caller_; |
| 153 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 155 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 154 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 156 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
| 155 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 157 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
| 156 | 158 |
| 157 DISALLOW_COPY_AND_ASSIGN(ProvidedService); | 159 DISALLOW_COPY_AND_ASSIGN(ProvidedService); |
| 158 }; | 160 }; |
| 159 | 161 |
| 160 class ConnectTestService | 162 class ConnectTestService |
| 161 : public Service, | 163 : public Service, |
| 162 public InterfaceFactory<mojom::ServiceFactory>, | 164 public InterfaceFactory<mojom::ServiceFactory>, |
| 163 public InterfaceFactory<test::mojom::ConnectTestService>, | 165 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 164 public mojom::ServiceFactory, | 166 public mojom::ServiceFactory, |
| 165 public test::mojom::ConnectTestService { | 167 public test::mojom::ConnectTestService { |
| 166 public: | 168 public: |
| 167 ConnectTestService() {} | 169 ConnectTestService() {} |
| 168 ~ConnectTestService() override {} | 170 ~ConnectTestService() override {} |
| 169 | 171 |
| 170 private: | 172 private: |
| 171 // service_manager::Service: | 173 // service_manager::Service: |
| 172 void OnStart(const ServiceInfo& info) override { | 174 void OnStart(ServiceContext* context) override { |
| 173 identity_ = info.identity; | 175 identity_ = context->identity(); |
| 174 bindings_.set_connection_error_handler( | 176 bindings_.set_connection_error_handler( |
| 175 base::Bind(&ConnectTestService::OnConnectionError, | 177 base::Bind(&ConnectTestService::OnConnectionError, |
| 176 base::Unretained(this))); | 178 base::Unretained(this))); |
| 177 } | 179 } |
| 178 bool OnConnect(const ServiceInfo& remote_info, | 180 bool OnConnect(const ServiceInfo& remote_info, |
| 179 InterfaceRegistry* registry) override { | 181 InterfaceRegistry* registry) override { |
| 180 registry->AddInterface<ServiceFactory>(this); | 182 registry->AddInterface<ServiceFactory>(this); |
| 181 registry->AddInterface<test::mojom::ConnectTestService>(this); | 183 registry->AddInterface<test::mojom::ConnectTestService>(this); |
| 182 return true; | 184 return true; |
| 183 } | 185 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); | 226 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); |
| 225 }; | 227 }; |
| 226 | 228 |
| 227 } // namespace service_manager | 229 } // namespace service_manager |
| 228 | 230 |
| 229 MojoResult ServiceMain(MojoHandle service_request_handle) { | 231 MojoResult ServiceMain(MojoHandle service_request_handle) { |
| 230 service_manager::ServiceRunner runner( | 232 service_manager::ServiceRunner runner( |
| 231 new service_manager::ConnectTestService); | 233 new service_manager::ConnectTestService); |
| 232 return runner.Run(service_request_handle); | 234 return runner.Run(service_request_handle); |
| 233 } | 235 } |
| OLD | NEW |