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/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/threading/simple_thread.h" | 16 #include "base/threading/simple_thread.h" |
17 #include "mojo/public/cpp/bindings/binding_set.h" | 17 #include "mojo/public/cpp/bindings/binding_set.h" |
18 #include "services/service_manager/public/c/main.h" | 18 #include "services/service_manager/public/c/main.h" |
| 19 #include "services/service_manager/public/cpp/binder_registry.h" |
19 #include "services/service_manager/public/cpp/connector.h" | 20 #include "services/service_manager/public/cpp/connector.h" |
20 #include "services/service_manager/public/cpp/interface_factory.h" | 21 #include "services/service_manager/public/cpp/interface_factory.h" |
21 #include "services/service_manager/public/cpp/interface_registry.h" | |
22 #include "services/service_manager/public/cpp/service.h" | 22 #include "services/service_manager/public/cpp/service.h" |
23 #include "services/service_manager/public/cpp/service_context.h" | 23 #include "services/service_manager/public/cpp/service_context.h" |
24 #include "services/service_manager/public/cpp/service_runner.h" | 24 #include "services/service_manager/public/cpp/service_runner.h" |
25 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 25 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
26 #include "services/service_manager/tests/connect/connect_test.mojom.h" | 26 #include "services/service_manager/tests/connect/connect_test.mojom.h" |
27 | 27 |
28 // Tests that multiple services can be packaged in a single service by | 28 // Tests that multiple services can be packaged in a single service by |
29 // implementing ServiceFactory; that these services can be specified by | 29 // implementing ServiceFactory; that these services can be specified by |
30 // the package's manifest and are thus registered with the PackageManager. | 30 // the package's manifest and are thus registered with the PackageManager. |
31 | 31 |
32 namespace service_manager { | 32 namespace service_manager { |
| 33 namespace { |
| 34 |
| 35 void QuitLoop(base::RunLoop* loop, |
| 36 mojom::ConnectResult* out_result, |
| 37 Identity* out_resolved_identity, |
| 38 mojom::ConnectResult result, |
| 39 const Identity& resolved_identity) { |
| 40 loop->Quit(); |
| 41 *out_result = result; |
| 42 *out_resolved_identity = resolved_identity; |
| 43 } |
| 44 |
| 45 } // namespace |
33 | 46 |
34 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | 47 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; |
35 | 48 |
36 class ProvidedService | 49 class ProvidedService |
37 : public Service, | 50 : public Service, |
38 public InterfaceFactory<test::mojom::ConnectTestService>, | 51 public InterfaceFactory<test::mojom::ConnectTestService>, |
39 public InterfaceFactory<test::mojom::BlockedInterface>, | 52 public InterfaceFactory<test::mojom::BlockedInterface>, |
40 public InterfaceFactory<test::mojom::UserIdTest>, | 53 public InterfaceFactory<test::mojom::UserIdTest>, |
41 public test::mojom::ConnectTestService, | 54 public test::mojom::ConnectTestService, |
42 public test::mojom::BlockedInterface, | 55 public test::mojom::BlockedInterface, |
(...skipping 10 matching lines...) Expand all Loading... |
53 ~ProvidedService() override { | 66 ~ProvidedService() override { |
54 Join(); | 67 Join(); |
55 } | 68 } |
56 | 69 |
57 private: | 70 private: |
58 // service_manager::Service: | 71 // service_manager::Service: |
59 void OnStart() override { | 72 void OnStart() override { |
60 bindings_.set_connection_error_handler( | 73 bindings_.set_connection_error_handler( |
61 base::Bind(&ProvidedService::OnConnectionError, | 74 base::Bind(&ProvidedService::OnConnectionError, |
62 base::Unretained(this))); | 75 base::Unretained(this))); |
| 76 registry_.AddInterface<test::mojom::ConnectTestService>(this); |
| 77 registry_.AddInterface<test::mojom::BlockedInterface>(this); |
| 78 registry_.AddInterface<test::mojom::UserIdTest>(this); |
63 } | 79 } |
64 | 80 void OnBindInterface(const ServiceInfo& source_info, |
65 bool OnConnect(const ServiceInfo& remote_info, | 81 const std::string& interface_name, |
66 InterfaceRegistry* registry) override { | 82 mojo::ScopedMessagePipeHandle interface_pipe) override { |
67 registry->AddInterface<test::mojom::ConnectTestService>(this); | 83 registry_.BindInterface(source_info.identity, interface_name, |
68 registry->AddInterface<test::mojom::BlockedInterface>(this); | 84 std::move(interface_pipe)); |
69 registry->AddInterface<test::mojom::UserIdTest>(this); | |
70 | |
71 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | |
72 state->connection_remote_name = remote_info.identity.name(); | |
73 state->connection_remote_userid = remote_info.identity.user_id(); | |
74 state->initialize_local_name = context()->identity().name(); | |
75 state->initialize_userid = context()->identity().user_id(); | |
76 | |
77 context()->connector()->BindInterface(remote_info.identity, &caller_); | |
78 caller_->ConnectionAccepted(std::move(state)); | |
79 | |
80 return true; | |
81 } | 85 } |
82 | 86 |
83 // InterfaceFactory<test::mojom::ConnectTestService>: | 87 // InterfaceFactory<test::mojom::ConnectTestService>: |
84 void Create(const Identity& remote_identity, | 88 void Create(const Identity& remote_identity, |
85 test::mojom::ConnectTestServiceRequest request) override { | 89 test::mojom::ConnectTestServiceRequest request) override { |
86 bindings_.AddBinding(this, std::move(request)); | 90 bindings_.AddBinding(this, std::move(request)); |
| 91 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); |
| 92 state->connection_remote_name = remote_identity.name(); |
| 93 state->connection_remote_userid = remote_identity.user_id(); |
| 94 state->initialize_local_name = context()->identity().name(); |
| 95 state->initialize_userid = context()->identity().user_id(); |
| 96 |
| 97 context()->connector()->BindInterface(remote_identity, &caller_); |
| 98 caller_->ConnectionAccepted(std::move(state)); |
87 } | 99 } |
88 | 100 |
89 // InterfaceFactory<test::mojom::BlockedInterface>: | 101 // InterfaceFactory<test::mojom::BlockedInterface>: |
90 void Create(const Identity& remote_identity, | 102 void Create(const Identity& remote_identity, |
91 test::mojom::BlockedInterfaceRequest request) override { | 103 test::mojom::BlockedInterfaceRequest request) override { |
92 blocked_bindings_.AddBinding(this, std::move(request)); | 104 blocked_bindings_.AddBinding(this, std::move(request)); |
93 } | 105 } |
94 | 106 |
95 // InterfaceFactory<test::mojom::UserIdTest>: | 107 // InterfaceFactory<test::mojom::UserIdTest>: |
96 void Create(const Identity& remote_identity, | 108 void Create(const Identity& remote_identity, |
(...skipping 12 matching lines...) Expand all Loading... |
109 | 121 |
110 // test::mojom::BlockedInterface: | 122 // test::mojom::BlockedInterface: |
111 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 123 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
112 callback.Run("Called Blocked Interface!"); | 124 callback.Run("Called Blocked Interface!"); |
113 } | 125 } |
114 | 126 |
115 // test::mojom::UserIdTest: | 127 // test::mojom::UserIdTest: |
116 void ConnectToClassAppAsDifferentUser( | 128 void ConnectToClassAppAsDifferentUser( |
117 const service_manager::Identity& target, | 129 const service_manager::Identity& target, |
118 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 130 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
119 std::unique_ptr<Connection> connection = | 131 context()->connector()->StartService(target); |
120 context()->connector()->Connect(target); | 132 mojom::ConnectResult result; |
| 133 Identity resolved_identity; |
121 { | 134 { |
122 base::RunLoop loop; | 135 base::RunLoop loop; |
123 connection->AddConnectionCompletedClosure(loop.QuitClosure()); | 136 Connector::TestApi test_api(context()->connector()); |
| 137 test_api.SetStartServiceCallback( |
| 138 base::Bind(&QuitLoop, &loop, &result, &resolved_identity)); |
124 base::MessageLoop::ScopedNestableTaskAllower allow( | 139 base::MessageLoop::ScopedNestableTaskAllower allow( |
125 base::MessageLoop::current()); | 140 base::MessageLoop::current()); |
126 loop.Run(); | 141 loop.Run(); |
127 } | 142 } |
128 callback.Run(static_cast<int32_t>(connection->GetResult()), | 143 callback.Run(static_cast<int32_t>(result), resolved_identity); |
129 connection->GetRemoteIdentity()); | |
130 } | 144 } |
131 | 145 |
132 // base::SimpleThread: | 146 // base::SimpleThread: |
133 void Run() override { | 147 void Run() override { |
134 ServiceRunner(new ForwardingService(this)).Run( | 148 ServiceRunner(new ForwardingService(this)).Run( |
135 request_.PassMessagePipe().release().value(), false); | 149 request_.PassMessagePipe().release().value(), false); |
136 caller_.reset(); | 150 caller_.reset(); |
137 bindings_.CloseAllBindings(); | 151 bindings_.CloseAllBindings(); |
138 blocked_bindings_.CloseAllBindings(); | 152 blocked_bindings_.CloseAllBindings(); |
139 user_id_test_bindings_.CloseAllBindings(); | 153 user_id_test_bindings_.CloseAllBindings(); |
140 } | 154 } |
141 | 155 |
142 void OnConnectionError() { | 156 void OnConnectionError() { |
143 if (bindings_.empty()) | 157 if (bindings_.empty()) |
144 base::MessageLoop::current()->QuitWhenIdle(); | 158 base::MessageLoop::current()->QuitWhenIdle(); |
145 } | 159 } |
146 | 160 |
147 const std::string title_; | 161 const std::string title_; |
148 mojom::ServiceRequest request_; | 162 mojom::ServiceRequest request_; |
149 test::mojom::ExposedInterfacePtr caller_; | 163 test::mojom::ExposedInterfacePtr caller_; |
| 164 BinderRegistry registry_; |
150 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 165 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
151 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 166 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
152 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 167 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
153 | 168 |
154 DISALLOW_COPY_AND_ASSIGN(ProvidedService); | 169 DISALLOW_COPY_AND_ASSIGN(ProvidedService); |
155 }; | 170 }; |
156 | 171 |
157 class ConnectTestService | 172 class ConnectTestService |
158 : public Service, | 173 : public Service, |
159 public InterfaceFactory<mojom::ServiceFactory>, | 174 public InterfaceFactory<mojom::ServiceFactory>, |
160 public InterfaceFactory<test::mojom::ConnectTestService>, | 175 public InterfaceFactory<test::mojom::ConnectTestService>, |
161 public mojom::ServiceFactory, | 176 public mojom::ServiceFactory, |
162 public test::mojom::ConnectTestService { | 177 public test::mojom::ConnectTestService { |
163 public: | 178 public: |
164 ConnectTestService() {} | 179 ConnectTestService() {} |
165 ~ConnectTestService() override {} | 180 ~ConnectTestService() override {} |
166 | 181 |
167 private: | 182 private: |
168 // service_manager::Service: | 183 // service_manager::Service: |
169 void OnStart() override { | 184 void OnStart() override { |
170 base::Closure error_handler = | 185 base::Closure error_handler = |
171 base::Bind(&ConnectTestService::OnConnectionError, | 186 base::Bind(&ConnectTestService::OnConnectionError, |
172 base::Unretained(this)); | 187 base::Unretained(this)); |
173 bindings_.set_connection_error_handler(error_handler); | 188 bindings_.set_connection_error_handler(error_handler); |
174 service_factory_bindings_.set_connection_error_handler(error_handler); | 189 service_factory_bindings_.set_connection_error_handler(error_handler); |
| 190 registry_.AddInterface<ServiceFactory>(this); |
| 191 registry_.AddInterface<test::mojom::ConnectTestService>(this); |
175 } | 192 } |
176 | 193 void OnBindInterface(const ServiceInfo& source_info, |
177 bool OnConnect(const ServiceInfo& remote_info, | 194 const std::string& interface_name, |
178 InterfaceRegistry* registry) override { | 195 mojo::ScopedMessagePipeHandle interface_pipe) override { |
179 registry->AddInterface<ServiceFactory>(this); | 196 registry_.BindInterface(source_info.identity, interface_name, |
180 registry->AddInterface<test::mojom::ConnectTestService>(this); | 197 std::move(interface_pipe)); |
181 return true; | |
182 } | 198 } |
183 | 199 |
184 bool OnServiceManagerConnectionLost() override { | 200 bool OnServiceManagerConnectionLost() override { |
185 provided_services_.clear(); | 201 provided_services_.clear(); |
186 return true; | 202 return true; |
187 } | 203 } |
188 | 204 |
189 // InterfaceFactory<mojom::ServiceFactory>: | 205 // InterfaceFactory<mojom::ServiceFactory>: |
190 void Create(const Identity& remote_identity, | 206 void Create(const Identity& remote_identity, |
191 mojom::ServiceFactoryRequest request) override { | 207 mojom::ServiceFactoryRequest request) override { |
(...skipping 27 matching lines...) Expand all Loading... |
219 callback.Run(context()->identity().instance()); | 235 callback.Run(context()->identity().instance()); |
220 } | 236 } |
221 | 237 |
222 void OnConnectionError() { | 238 void OnConnectionError() { |
223 if (bindings_.empty() && service_factory_bindings_.empty()) | 239 if (bindings_.empty() && service_factory_bindings_.empty()) |
224 context()->RequestQuit(); | 240 context()->RequestQuit(); |
225 } | 241 } |
226 | 242 |
227 std::vector<std::unique_ptr<Service>> delegates_; | 243 std::vector<std::unique_ptr<Service>> delegates_; |
228 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; | 244 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; |
| 245 BinderRegistry registry_; |
229 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 246 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
230 std::list<std::unique_ptr<ProvidedService>> provided_services_; | 247 std::list<std::unique_ptr<ProvidedService>> provided_services_; |
231 | 248 |
232 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); | 249 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); |
233 }; | 250 }; |
234 | 251 |
235 } // namespace service_manager | 252 } // namespace service_manager |
236 | 253 |
237 MojoResult ServiceMain(MojoHandle service_request_handle) { | 254 MojoResult ServiceMain(MojoHandle service_request_handle) { |
238 service_manager::ServiceRunner runner( | 255 service_manager::ServiceRunner runner( |
239 new service_manager::ConnectTestService); | 256 new service_manager::ConnectTestService); |
240 return runner.Run(service_request_handle); | 257 return runner.Run(service_request_handle); |
241 } | 258 } |
OLD | NEW |