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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "mojo/public/cpp/bindings/binding_set.h" | 12 #include "mojo/public/cpp/bindings/binding_set.h" |
13 #include "services/service_manager/public/c/main.h" | 13 #include "services/service_manager/public/c/main.h" |
| 14 #include "services/service_manager/public/cpp/binder_registry.h" |
14 #include "services/service_manager/public/cpp/connector.h" | 15 #include "services/service_manager/public/cpp/connector.h" |
15 #include "services/service_manager/public/cpp/interface_factory.h" | 16 #include "services/service_manager/public/cpp/interface_factory.h" |
16 #include "services/service_manager/public/cpp/interface_registry.h" | |
17 #include "services/service_manager/public/cpp/service.h" | 17 #include "services/service_manager/public/cpp/service.h" |
18 #include "services/service_manager/public/cpp/service_context.h" | 18 #include "services/service_manager/public/cpp/service_context.h" |
19 #include "services/service_manager/public/cpp/service_runner.h" | 19 #include "services/service_manager/public/cpp/service_runner.h" |
20 #include "services/service_manager/public/interfaces/connector.mojom.h" | 20 #include "services/service_manager/public/interfaces/connector.mojom.h" |
21 #include "services/service_manager/tests/connect/connect_test.mojom.h" | 21 #include "services/service_manager/tests/connect/connect_test.mojom.h" |
22 | 22 |
23 namespace service_manager { | 23 namespace service_manager { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 void QuitLoop(base::RunLoop* loop) { | 27 void QuitLoop(base::RunLoop* loop, |
| 28 mojom::ConnectResult* out_result, |
| 29 Identity* out_resolved_identity, |
| 30 mojom::ConnectResult result, |
| 31 const Identity& resolved_identity) { |
28 loop->Quit(); | 32 loop->Quit(); |
| 33 *out_result = result; |
| 34 *out_resolved_identity = resolved_identity; |
29 } | 35 } |
30 | 36 |
31 void ReceiveString(std::string* string, | 37 void ReceiveString(std::string* string, |
32 base::RunLoop* loop, | 38 base::RunLoop* loop, |
33 const std::string& response) { | 39 const std::string& response) { |
34 *string = response; | 40 *string = response; |
35 loop->Quit(); | 41 loop->Quit(); |
36 } | 42 } |
37 | 43 |
38 } // namespace | 44 } // namespace |
(...skipping 15 matching lines...) Loading... |
54 | 60 |
55 private: | 61 private: |
56 // service_manager::Service: | 62 // service_manager::Service: |
57 void OnStart() override { | 63 void OnStart() override { |
58 bindings_.set_connection_error_handler( | 64 bindings_.set_connection_error_handler( |
59 base::Bind(&ConnectTestApp::OnConnectionError, | 65 base::Bind(&ConnectTestApp::OnConnectionError, |
60 base::Unretained(this))); | 66 base::Unretained(this))); |
61 standalone_bindings_.set_connection_error_handler( | 67 standalone_bindings_.set_connection_error_handler( |
62 base::Bind(&ConnectTestApp::OnConnectionError, | 68 base::Bind(&ConnectTestApp::OnConnectionError, |
63 base::Unretained(this))); | 69 base::Unretained(this))); |
| 70 registry_.AddInterface<test::mojom::ConnectTestService>(this); |
| 71 registry_.AddInterface<test::mojom::StandaloneApp>(this); |
| 72 registry_.AddInterface<test::mojom::BlockedInterface>(this); |
| 73 registry_.AddInterface<test::mojom::UserIdTest>(this); |
64 } | 74 } |
65 | 75 void OnBindInterface(const ServiceInfo& source_info, |
66 bool OnConnect(const ServiceInfo& remote_info, | 76 const std::string& interface_name, |
67 InterfaceRegistry* registry) override { | 77 mojo::ScopedMessagePipeHandle interface_pipe) override { |
68 registry->AddInterface<test::mojom::ConnectTestService>(this); | 78 registry_.BindInterface(source_info.identity, interface_name, |
69 registry->AddInterface<test::mojom::StandaloneApp>(this); | 79 std::move(interface_pipe)); |
70 registry->AddInterface<test::mojom::BlockedInterface>(this); | |
71 registry->AddInterface<test::mojom::UserIdTest>(this); | |
72 | |
73 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | |
74 state->connection_remote_name = remote_info.identity.name(); | |
75 state->connection_remote_userid = remote_info.identity.user_id(); | |
76 state->initialize_local_name = context()->identity().name(); | |
77 state->initialize_userid = context()->identity().user_id(); | |
78 | |
79 context()->connector()->BindInterface(remote_info.identity, &caller_); | |
80 caller_->ConnectionAccepted(std::move(state)); | |
81 | |
82 return true; | |
83 } | 80 } |
84 | 81 |
85 // InterfaceFactory<test::mojom::ConnectTestService>: | 82 // InterfaceFactory<test::mojom::ConnectTestService>: |
86 void Create(const Identity& remote_identity, | 83 void Create(const Identity& remote_identity, |
87 test::mojom::ConnectTestServiceRequest request) override { | 84 test::mojom::ConnectTestServiceRequest request) override { |
88 bindings_.AddBinding(this, std::move(request)); | 85 bindings_.AddBinding(this, std::move(request)); |
| 86 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); |
| 87 state->connection_remote_name = remote_identity.name(); |
| 88 state->connection_remote_userid = remote_identity.user_id(); |
| 89 state->initialize_local_name = context()->identity().name(); |
| 90 state->initialize_userid = context()->identity().user_id(); |
| 91 |
| 92 context()->connector()->BindInterface(remote_identity, &caller_); |
| 93 caller_->ConnectionAccepted(std::move(state)); |
89 } | 94 } |
90 | 95 |
91 // InterfaceFactory<test::mojom::StandaloneApp>: | 96 // InterfaceFactory<test::mojom::StandaloneApp>: |
92 void Create(const Identity& remote_identity, | 97 void Create(const Identity& remote_identity, |
93 test::mojom::StandaloneAppRequest request) override { | 98 test::mojom::StandaloneAppRequest request) override { |
94 standalone_bindings_.AddBinding(this, std::move(request)); | 99 standalone_bindings_.AddBinding(this, std::move(request)); |
95 } | 100 } |
96 | 101 |
97 // InterfaceFactory<test::mojom::BlockedInterface>: | 102 // InterfaceFactory<test::mojom::BlockedInterface>: |
98 void Create(const Identity& remote_identity, | 103 void Create(const Identity& remote_identity, |
(...skipping 12 matching lines...) Loading... |
111 callback.Run("APP"); | 116 callback.Run("APP"); |
112 } | 117 } |
113 void GetInstance(const GetInstanceCallback& callback) override { | 118 void GetInstance(const GetInstanceCallback& callback) override { |
114 callback.Run(context()->identity().instance()); | 119 callback.Run(context()->identity().instance()); |
115 } | 120 } |
116 | 121 |
117 // test::mojom::StandaloneApp: | 122 // test::mojom::StandaloneApp: |
118 void ConnectToAllowedAppInBlockedPackage( | 123 void ConnectToAllowedAppInBlockedPackage( |
119 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { | 124 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { |
120 base::RunLoop run_loop; | 125 base::RunLoop run_loop; |
121 std::unique_ptr<Connection> connection = | |
122 context()->connector()->Connect("connect_test_a"); | |
123 connection->SetConnectionLostClosure( | |
124 base::Bind(&ConnectTestApp::OnConnectionBlocked, | |
125 base::Unretained(this), callback, &run_loop)); | |
126 test::mojom::ConnectTestServicePtr test_service; | 126 test::mojom::ConnectTestServicePtr test_service; |
127 connection->GetInterface(&test_service); | 127 context()->connector()->BindInterface("connect_test_a", &test_service); |
| 128 test_service.set_connection_error_handler( |
| 129 base::Bind(&ConnectTestApp::OnConnectionBlocked, base::Unretained(this), |
| 130 callback, &run_loop)); |
128 test_service->GetTitle( | 131 test_service->GetTitle( |
129 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), | 132 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), |
130 callback, &run_loop)); | 133 callback, &run_loop)); |
131 { | 134 { |
132 // This message is dispatched as a task on the same run loop, so we need | 135 // This message is dispatched as a task on the same run loop, so we need |
133 // to allow nesting in order to pump additional signals. | 136 // to allow nesting in order to pump additional signals. |
134 base::MessageLoop::ScopedNestableTaskAllower allow( | 137 base::MessageLoop::ScopedNestableTaskAllower allow( |
135 base::MessageLoop::current()); | 138 base::MessageLoop::current()); |
136 run_loop.Run(); | 139 run_loop.Run(); |
137 } | 140 } |
138 } | 141 } |
139 void ConnectToClassInterface( | 142 void ConnectToClassInterface( |
140 const ConnectToClassInterfaceCallback& callback) override { | 143 const ConnectToClassInterfaceCallback& callback) override { |
141 std::unique_ptr<Connection> connection = | |
142 context()->connector()->Connect("connect_test_class_app"); | |
143 test::mojom::ClassInterfacePtr class_interface; | 144 test::mojom::ClassInterfacePtr class_interface; |
144 connection->GetInterface(&class_interface); | 145 context()->connector()->BindInterface("connect_test_class_app", |
| 146 &class_interface); |
145 std::string ping_response; | 147 std::string ping_response; |
146 { | 148 { |
147 base::RunLoop loop; | 149 base::RunLoop loop; |
148 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); | 150 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); |
149 base::MessageLoop::ScopedNestableTaskAllower allow( | 151 base::MessageLoop::ScopedNestableTaskAllower allow( |
150 base::MessageLoop::current()); | 152 base::MessageLoop::current()); |
151 loop.Run(); | 153 loop.Run(); |
152 } | 154 } |
153 test::mojom::ConnectTestServicePtr service; | 155 test::mojom::ConnectTestServicePtr service; |
154 connection->GetInterface(&service); | 156 context()->connector()->BindInterface("connect_test_class_app", &service); |
155 std::string title_response; | 157 std::string title_response; |
156 { | 158 { |
157 base::RunLoop loop; | 159 base::RunLoop loop; |
158 service->GetTitle(base::Bind(&ReceiveString, &title_response, &loop)); | 160 service->GetTitle(base::Bind(&ReceiveString, &title_response, &loop)); |
159 base::MessageLoop::ScopedNestableTaskAllower allow( | 161 base::MessageLoop::ScopedNestableTaskAllower allow( |
160 base::MessageLoop::current()); | 162 base::MessageLoop::current()); |
161 loop.Run(); | 163 loop.Run(); |
162 } | 164 } |
163 callback.Run(ping_response, title_response); | 165 callback.Run(ping_response, title_response); |
164 } | 166 } |
165 | 167 |
166 // test::mojom::BlockedInterface: | 168 // test::mojom::BlockedInterface: |
167 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 169 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
168 callback.Run("Called Blocked Interface!"); | 170 callback.Run("Called Blocked Interface!"); |
169 } | 171 } |
170 | 172 |
171 // test::mojom::UserIdTest: | 173 // test::mojom::UserIdTest: |
172 void ConnectToClassAppAsDifferentUser( | 174 void ConnectToClassAppAsDifferentUser( |
173 const service_manager::Identity& target, | 175 const service_manager::Identity& target, |
174 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 176 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
175 std::unique_ptr<Connection> connection = | 177 context()->connector()->StartService(target); |
176 context()->connector()->Connect(target); | 178 mojom::ConnectResult result; |
| 179 Identity resolved_identity; |
177 { | 180 { |
178 base::RunLoop loop; | 181 base::RunLoop loop; |
179 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 182 Connector::TestApi test_api(context()->connector()); |
| 183 test_api.SetStartServiceCallback( |
| 184 base::Bind(&QuitLoop, &loop, &result, &resolved_identity)); |
180 base::MessageLoop::ScopedNestableTaskAllower allow( | 185 base::MessageLoop::ScopedNestableTaskAllower allow( |
181 base::MessageLoop::current()); | 186 base::MessageLoop::current()); |
182 loop.Run(); | 187 loop.Run(); |
183 } | 188 } |
184 callback.Run(static_cast<int32_t>(connection->GetResult()), | 189 callback.Run(static_cast<int32_t>(result), resolved_identity); |
185 connection->GetRemoteIdentity()); | |
186 } | 190 } |
187 | 191 |
188 void OnConnectionBlocked( | 192 void OnConnectionBlocked( |
189 const ConnectToAllowedAppInBlockedPackageCallback& callback, | 193 const ConnectToAllowedAppInBlockedPackageCallback& callback, |
190 base::RunLoop* run_loop) { | 194 base::RunLoop* run_loop) { |
191 callback.Run("uninitialized"); | 195 callback.Run("uninitialized"); |
192 run_loop->Quit(); | 196 run_loop->Quit(); |
193 } | 197 } |
194 | 198 |
195 void OnGotTitle( | 199 void OnGotTitle( |
196 const ConnectToAllowedAppInBlockedPackageCallback& callback, | 200 const ConnectToAllowedAppInBlockedPackageCallback& callback, |
197 base::RunLoop* run_loop, | 201 base::RunLoop* run_loop, |
198 const std::string& title) { | 202 const std::string& title) { |
199 callback.Run(title); | 203 callback.Run(title); |
200 run_loop->Quit(); | 204 run_loop->Quit(); |
201 } | 205 } |
202 | 206 |
203 void OnConnectionError() { | 207 void OnConnectionError() { |
204 if (bindings_.empty() && standalone_bindings_.empty()) | 208 if (bindings_.empty() && standalone_bindings_.empty()) |
205 base::MessageLoop::current()->QuitWhenIdle(); | 209 base::MessageLoop::current()->QuitWhenIdle(); |
206 } | 210 } |
207 | 211 |
| 212 BinderRegistry registry_; |
208 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 213 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
209 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_; | 214 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_; |
210 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 215 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
211 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 216 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
212 test::mojom::ExposedInterfacePtr caller_; | 217 test::mojom::ExposedInterfacePtr caller_; |
213 | 218 |
214 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); | 219 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); |
215 }; | 220 }; |
216 | 221 |
217 } // namespace service_manager | 222 } // namespace service_manager |
218 | 223 |
219 MojoResult ServiceMain(MojoHandle service_request_handle) { | 224 MojoResult ServiceMain(MojoHandle service_request_handle) { |
220 service_manager::ServiceRunner runner(new service_manager::ConnectTestApp); | 225 service_manager::ServiceRunner runner(new service_manager::ConnectTestApp); |
221 return runner.Run(service_request_handle); | 226 return runner.Run(service_request_handle); |
222 } | 227 } |
OLD | NEW |