| 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 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 title_(title), | 47 title_(title), |
| 48 request_(std::move(request)) { | 48 request_(std::move(request)) { |
| 49 Start(); | 49 Start(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 ~ProvidedService() override { | 52 ~ProvidedService() override { |
| 53 Join(); | 53 Join(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 class ForwardingServiceImpl : public Service { | |
| 58 public: | |
| 59 explicit ForwardingServiceImpl(Service* service) | |
| 60 : service_(service) {} | |
| 61 ~ForwardingServiceImpl() override {} | |
| 62 | |
| 63 // Service: | |
| 64 void OnStart(ServiceContext* context) override { | |
| 65 service_->OnStart(context); | |
| 66 } | |
| 67 | |
| 68 bool OnConnect(const ServiceInfo& remote_info, | |
| 69 InterfaceRegistry* registry) override { | |
| 70 return service_->OnConnect(remote_info, registry); | |
| 71 } | |
| 72 | |
| 73 bool OnStop() override { return service_->OnStop(); } | |
| 74 | |
| 75 private: | |
| 76 Service* const service_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(ForwardingServiceImpl); | |
| 79 }; | |
| 80 | |
| 81 // service_manager::Service: | 57 // service_manager::Service: |
| 82 void OnStart(ServiceContext* context) override { | 58 void OnStart() override { |
| 83 context_ = context; | |
| 84 bindings_.set_connection_error_handler( | 59 bindings_.set_connection_error_handler( |
| 85 base::Bind(&ProvidedService::OnConnectionError, | 60 base::Bind(&ProvidedService::OnConnectionError, |
| 86 base::Unretained(this))); | 61 base::Unretained(this))); |
| 87 } | 62 } |
| 88 | 63 |
| 89 bool OnConnect(const ServiceInfo& remote_info, | 64 bool OnConnect(const ServiceInfo& remote_info, |
| 90 InterfaceRegistry* registry) override { | 65 InterfaceRegistry* registry) override { |
| 91 registry->AddInterface<test::mojom::ConnectTestService>(this); | 66 registry->AddInterface<test::mojom::ConnectTestService>(this); |
| 92 registry->AddInterface<test::mojom::BlockedInterface>(this); | 67 registry->AddInterface<test::mojom::BlockedInterface>(this); |
| 93 registry->AddInterface<test::mojom::UserIdTest>(this); | 68 registry->AddInterface<test::mojom::UserIdTest>(this); |
| 94 | 69 |
| 95 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | 70 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); |
| 96 state->connection_remote_name = remote_info.identity.name(); | 71 state->connection_remote_name = remote_info.identity.name(); |
| 97 state->connection_remote_userid = remote_info.identity.user_id(); | 72 state->connection_remote_userid = remote_info.identity.user_id(); |
| 98 state->initialize_local_name = context_->identity().name(); | 73 state->initialize_local_name = context()->identity().name(); |
| 99 state->initialize_userid = context_->identity().user_id(); | 74 state->initialize_userid = context()->identity().user_id(); |
| 100 | 75 |
| 101 context_->connector()->ConnectToInterface(remote_info.identity, &caller_); | 76 context()->connector()->ConnectToInterface(remote_info.identity, &caller_); |
| 102 caller_->ConnectionAccepted(std::move(state)); | 77 caller_->ConnectionAccepted(std::move(state)); |
| 103 | 78 |
| 104 return true; | 79 return true; |
| 105 } | 80 } |
| 106 | 81 |
| 107 // InterfaceFactory<test::mojom::ConnectTestService>: | 82 // InterfaceFactory<test::mojom::ConnectTestService>: |
| 108 void Create(const Identity& remote_identity, | 83 void Create(const Identity& remote_identity, |
| 109 test::mojom::ConnectTestServiceRequest request) override { | 84 test::mojom::ConnectTestServiceRequest request) override { |
| 110 bindings_.AddBinding(this, std::move(request)); | 85 bindings_.AddBinding(this, std::move(request)); |
| 111 } | 86 } |
| 112 | 87 |
| 113 // InterfaceFactory<test::mojom::BlockedInterface>: | 88 // InterfaceFactory<test::mojom::BlockedInterface>: |
| 114 void Create(const Identity& remote_identity, | 89 void Create(const Identity& remote_identity, |
| 115 test::mojom::BlockedInterfaceRequest request) override { | 90 test::mojom::BlockedInterfaceRequest request) override { |
| 116 blocked_bindings_.AddBinding(this, std::move(request)); | 91 blocked_bindings_.AddBinding(this, std::move(request)); |
| 117 } | 92 } |
| 118 | 93 |
| 119 // InterfaceFactory<test::mojom::UserIdTest>: | 94 // InterfaceFactory<test::mojom::UserIdTest>: |
| 120 void Create(const Identity& remote_identity, | 95 void Create(const Identity& remote_identity, |
| 121 test::mojom::UserIdTestRequest request) override { | 96 test::mojom::UserIdTestRequest request) override { |
| 122 user_id_test_bindings_.AddBinding(this, std::move(request)); | 97 user_id_test_bindings_.AddBinding(this, std::move(request)); |
| 123 } | 98 } |
| 124 | 99 |
| 125 // test::mojom::ConnectTestService: | 100 // test::mojom::ConnectTestService: |
| 126 void GetTitle(const GetTitleCallback& callback) override { | 101 void GetTitle(const GetTitleCallback& callback) override { |
| 127 callback.Run(title_); | 102 callback.Run(title_); |
| 128 } | 103 } |
| 129 | 104 |
| 130 void GetInstance(const GetInstanceCallback& callback) override { | 105 void GetInstance(const GetInstanceCallback& callback) override { |
| 131 callback.Run(context_->identity().instance()); | 106 callback.Run(context()->identity().instance()); |
| 132 } | 107 } |
| 133 | 108 |
| 134 // test::mojom::BlockedInterface: | 109 // test::mojom::BlockedInterface: |
| 135 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 110 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
| 136 callback.Run("Called Blocked Interface!"); | 111 callback.Run("Called Blocked Interface!"); |
| 137 } | 112 } |
| 138 | 113 |
| 139 // test::mojom::UserIdTest: | 114 // test::mojom::UserIdTest: |
| 140 void ConnectToClassAppAsDifferentUser( | 115 void ConnectToClassAppAsDifferentUser( |
| 141 const service_manager::Identity& target, | 116 const service_manager::Identity& target, |
| 142 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 117 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
| 143 Connector::ConnectParams params(target); | 118 Connector::ConnectParams params(target); |
| 144 std::unique_ptr<Connection> connection = | 119 std::unique_ptr<Connection> connection = |
| 145 context_->connector()->Connect(¶ms); | 120 context()->connector()->Connect(¶ms); |
| 146 { | 121 { |
| 147 base::RunLoop loop; | 122 base::RunLoop loop; |
| 148 connection->AddConnectionCompletedClosure(loop.QuitClosure()); | 123 connection->AddConnectionCompletedClosure(loop.QuitClosure()); |
| 149 base::MessageLoop::ScopedNestableTaskAllower allow( | 124 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 150 base::MessageLoop::current()); | 125 base::MessageLoop::current()); |
| 151 loop.Run(); | 126 loop.Run(); |
| 152 } | 127 } |
| 153 callback.Run(static_cast<int32_t>(connection->GetResult()), | 128 callback.Run(static_cast<int32_t>(connection->GetResult()), |
| 154 connection->GetRemoteIdentity()); | 129 connection->GetRemoteIdentity()); |
| 155 } | 130 } |
| 156 | 131 |
| 157 // base::SimpleThread: | 132 // base::SimpleThread: |
| 158 void Run() override { | 133 void Run() override { |
| 159 ServiceRunner(new ForwardingServiceImpl(this)).Run( | 134 ServiceRunner(new ForwardingService(this)).Run( |
| 160 request_.PassMessagePipe().release().value(), false); | 135 request_.PassMessagePipe().release().value(), false); |
| 161 caller_.reset(); | 136 caller_.reset(); |
| 162 bindings_.CloseAllBindings(); | 137 bindings_.CloseAllBindings(); |
| 163 blocked_bindings_.CloseAllBindings(); | 138 blocked_bindings_.CloseAllBindings(); |
| 164 user_id_test_bindings_.CloseAllBindings(); | 139 user_id_test_bindings_.CloseAllBindings(); |
| 165 } | 140 } |
| 166 | 141 |
| 167 void OnConnectionError() { | 142 void OnConnectionError() { |
| 168 if (bindings_.empty()) | 143 if (bindings_.empty()) |
| 169 base::MessageLoop::current()->QuitWhenIdle(); | 144 base::MessageLoop::current()->QuitWhenIdle(); |
| 170 } | 145 } |
| 171 | 146 |
| 172 ServiceContext* context_ = nullptr; | |
| 173 const std::string title_; | 147 const std::string title_; |
| 174 mojom::ServiceRequest request_; | 148 mojom::ServiceRequest request_; |
| 175 test::mojom::ExposedInterfacePtr caller_; | 149 test::mojom::ExposedInterfacePtr caller_; |
| 176 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 150 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 177 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 151 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
| 178 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 152 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
| 179 | 153 |
| 180 DISALLOW_COPY_AND_ASSIGN(ProvidedService); | 154 DISALLOW_COPY_AND_ASSIGN(ProvidedService); |
| 181 }; | 155 }; |
| 182 | 156 |
| 183 class ConnectTestService | 157 class ConnectTestService |
| 184 : public Service, | 158 : public Service, |
| 185 public InterfaceFactory<mojom::ServiceFactory>, | 159 public InterfaceFactory<mojom::ServiceFactory>, |
| 186 public InterfaceFactory<test::mojom::ConnectTestService>, | 160 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 187 public mojom::ServiceFactory, | 161 public mojom::ServiceFactory, |
| 188 public test::mojom::ConnectTestService { | 162 public test::mojom::ConnectTestService { |
| 189 public: | 163 public: |
| 190 ConnectTestService() {} | 164 ConnectTestService() {} |
| 191 ~ConnectTestService() override {} | 165 ~ConnectTestService() override {} |
| 192 | 166 |
| 193 private: | 167 private: |
| 194 // service_manager::Service: | 168 // service_manager::Service: |
| 195 void OnStart(ServiceContext* context) override { | 169 void OnStart() override { |
| 196 context_ = context; | |
| 197 | |
| 198 base::Closure error_handler = | 170 base::Closure error_handler = |
| 199 base::Bind(&ConnectTestService::OnConnectionError, | 171 base::Bind(&ConnectTestService::OnConnectionError, |
| 200 base::Unretained(this)); | 172 base::Unretained(this)); |
| 201 bindings_.set_connection_error_handler(error_handler); | 173 bindings_.set_connection_error_handler(error_handler); |
| 202 service_factory_bindings_.set_connection_error_handler(error_handler); | 174 service_factory_bindings_.set_connection_error_handler(error_handler); |
| 203 } | 175 } |
| 204 | 176 |
| 205 bool OnConnect(const ServiceInfo& remote_info, | 177 bool OnConnect(const ServiceInfo& remote_info, |
| 206 InterfaceRegistry* registry) override { | 178 InterfaceRegistry* registry) override { |
| 207 registry->AddInterface<ServiceFactory>(this); | 179 registry->AddInterface<ServiceFactory>(this); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 237 base::MakeUnique<ProvidedService>("B", std::move(request))); | 209 base::MakeUnique<ProvidedService>("B", std::move(request))); |
| 238 } | 210 } |
| 239 } | 211 } |
| 240 | 212 |
| 241 // test::mojom::ConnectTestService: | 213 // test::mojom::ConnectTestService: |
| 242 void GetTitle(const GetTitleCallback& callback) override { | 214 void GetTitle(const GetTitleCallback& callback) override { |
| 243 callback.Run("ROOT"); | 215 callback.Run("ROOT"); |
| 244 } | 216 } |
| 245 | 217 |
| 246 void GetInstance(const GetInstanceCallback& callback) override { | 218 void GetInstance(const GetInstanceCallback& callback) override { |
| 247 callback.Run(context_->identity().instance()); | 219 callback.Run(context()->identity().instance()); |
| 248 } | 220 } |
| 249 | 221 |
| 250 void OnConnectionError() { | 222 void OnConnectionError() { |
| 251 if (bindings_.empty() && service_factory_bindings_.empty()) | 223 if (bindings_.empty() && service_factory_bindings_.empty()) |
| 252 context_->RequestQuit(); | 224 context()->RequestQuit(); |
| 253 } | 225 } |
| 254 | 226 |
| 255 ServiceContext* context_ = nullptr; | |
| 256 std::vector<std::unique_ptr<Service>> delegates_; | 227 std::vector<std::unique_ptr<Service>> delegates_; |
| 257 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; | 228 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; |
| 258 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 229 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 259 std::list<std::unique_ptr<ProvidedService>> provided_services_; | 230 std::list<std::unique_ptr<ProvidedService>> provided_services_; |
| 260 | 231 |
| 261 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); | 232 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); |
| 262 }; | 233 }; |
| 263 | 234 |
| 264 } // namespace service_manager | 235 } // namespace service_manager |
| 265 | 236 |
| 266 MojoResult ServiceMain(MojoHandle service_request_handle) { | 237 MojoResult ServiceMain(MojoHandle service_request_handle) { |
| 267 service_manager::ServiceRunner runner( | 238 service_manager::ServiceRunner runner( |
| 268 new service_manager::ConnectTestService); | 239 new service_manager::ConnectTestService); |
| 269 return runner.Run(service_request_handle); | 240 return runner.Run(service_request_handle); |
| 270 } | 241 } |
| OLD | NEW |