OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/base_switches.h" | 11 #include "base/base_switches.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/process/process.h" | 19 #include "base/process/process.h" |
20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
22 #include "mojo/edk/embedder/embedder.h" | 22 #include "mojo/edk/embedder/embedder.h" |
23 #include "mojo/edk/embedder/pending_process_connection.h" | 23 #include "mojo/edk/embedder/pending_process_connection.h" |
24 #include "mojo/edk/embedder/platform_channel_pair.h" | 24 #include "mojo/edk/embedder/platform_channel_pair.h" |
25 #include "mojo/edk/embedder/scoped_platform_handle.h" | 25 #include "mojo/edk/embedder/scoped_platform_handle.h" |
26 #include "mojo/public/cpp/bindings/binding_set.h" | 26 #include "mojo/public/cpp/bindings/binding_set.h" |
| 27 #include "services/service_manager/public/cpp/binder_registry.h" |
27 #include "services/service_manager/public/cpp/interface_factory.h" | 28 #include "services/service_manager/public/cpp/interface_factory.h" |
28 #include "services/service_manager/public/cpp/interface_registry.h" | |
29 #include "services/service_manager/public/cpp/service.h" | 29 #include "services/service_manager/public/cpp/service.h" |
30 #include "services/service_manager/public/cpp/service_test.h" | 30 #include "services/service_manager/public/cpp/service_test.h" |
31 #include "services/service_manager/public/interfaces/constants.mojom.h" | 31 #include "services/service_manager/public/interfaces/constants.mojom.h" |
32 #include "services/service_manager/public/interfaces/service_manager.mojom.h" | 32 #include "services/service_manager/public/interfaces/service_manager.mojom.h" |
33 #include "services/service_manager/runner/common/client_util.h" | 33 #include "services/service_manager/runner/common/client_util.h" |
34 #include "services/service_manager/tests/service_manager/service_manager_unittes
t.mojom.h" | 34 #include "services/service_manager/tests/service_manager/service_manager_unittes
t.mojom.h" |
35 | 35 |
36 namespace service_manager { | 36 namespace service_manager { |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 class ServiceManagerTestClient | 40 class ServiceManagerTestClient |
41 : public test::ServiceTestClient, | 41 : public test::ServiceTestClient, |
42 public InterfaceFactory<test::mojom::CreateInstanceTest>, | 42 public InterfaceFactory<test::mojom::CreateInstanceTest>, |
43 public test::mojom::CreateInstanceTest { | 43 public test::mojom::CreateInstanceTest { |
44 public: | 44 public: |
45 explicit ServiceManagerTestClient(test::ServiceTest* test) | 45 explicit ServiceManagerTestClient(test::ServiceTest* test) |
46 : test::ServiceTestClient(test), binding_(this) {} | 46 : test::ServiceTestClient(test), binding_(this) { |
| 47 registry_.AddInterface<test::mojom::CreateInstanceTest>(this); |
| 48 } |
47 ~ServiceManagerTestClient() override {} | 49 ~ServiceManagerTestClient() override {} |
48 | 50 |
49 const Identity& target_identity() const { return target_identity_; } | 51 const Identity& target_identity() const { return target_identity_; } |
50 | 52 |
51 void WaitForTargetIdentityCall() { | 53 void WaitForTargetIdentityCall() { |
52 wait_for_target_identity_loop_ = base::MakeUnique<base::RunLoop>(); | 54 wait_for_target_identity_loop_ = base::MakeUnique<base::RunLoop>(); |
53 wait_for_target_identity_loop_->Run(); | 55 wait_for_target_identity_loop_->Run(); |
54 } | 56 } |
55 | 57 |
56 private: | 58 private: |
57 // test::ServiceTestClient: | 59 // test::ServiceTestClient: |
58 bool OnConnect(const ServiceInfo& remote_info, | 60 void OnBindInterface(const ServiceInfo& source_info, |
59 InterfaceRegistry* registry) override { | 61 const std::string& interface_name, |
60 registry->AddInterface<test::mojom::CreateInstanceTest>(this); | 62 mojo::ScopedMessagePipeHandle interface_pipe) override { |
61 return true; | 63 registry_.BindInterface(source_info.identity, interface_name, |
| 64 std::move(interface_pipe)); |
62 } | 65 } |
63 | 66 |
64 // InterfaceFactory<test::mojom::CreateInstanceTest>: | 67 // InterfaceFactory<test::mojom::CreateInstanceTest>: |
65 void Create(const Identity& remote_identity, | 68 void Create(const Identity& remote_identity, |
66 test::mojom::CreateInstanceTestRequest request) override { | 69 test::mojom::CreateInstanceTestRequest request) override { |
67 binding_.Bind(std::move(request)); | 70 binding_.Bind(std::move(request)); |
68 } | 71 } |
69 | 72 |
70 // test::mojom::CreateInstanceTest: | 73 // test::mojom::CreateInstanceTest: |
71 void SetTargetIdentity(const service_manager::Identity& identity) override { | 74 void SetTargetIdentity(const service_manager::Identity& identity) override { |
72 target_identity_ = identity; | 75 target_identity_ = identity; |
73 if (!wait_for_target_identity_loop_) | 76 if (!wait_for_target_identity_loop_) |
74 LOG(ERROR) << "SetTargetIdentity call received when not waiting for it."; | 77 LOG(ERROR) << "SetTargetIdentity call received when not waiting for it."; |
75 else | 78 else |
76 wait_for_target_identity_loop_->Quit(); | 79 wait_for_target_identity_loop_->Quit(); |
77 } | 80 } |
78 | 81 |
79 service_manager::Identity target_identity_; | 82 service_manager::Identity target_identity_; |
80 std::unique_ptr<base::RunLoop> wait_for_target_identity_loop_; | 83 std::unique_ptr<base::RunLoop> wait_for_target_identity_loop_; |
81 | 84 |
| 85 BinderRegistry registry_; |
82 mojo::Binding<test::mojom::CreateInstanceTest> binding_; | 86 mojo::Binding<test::mojom::CreateInstanceTest> binding_; |
83 | 87 |
84 DISALLOW_COPY_AND_ASSIGN(ServiceManagerTestClient); | 88 DISALLOW_COPY_AND_ASSIGN(ServiceManagerTestClient); |
85 }; | 89 }; |
86 | 90 |
87 } // namespace | 91 } // namespace |
88 | 92 |
89 class ServiceManagerTest : public test::ServiceTest, | 93 class ServiceManagerTest : public test::ServiceTest, |
90 public mojom::ServiceManagerListener { | 94 public mojom::ServiceManagerListener { |
91 public: | 95 public: |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 mojo::edk::PendingProcessConnection process_connection; | 184 mojo::edk::PendingProcessConnection process_connection; |
181 service_manager::mojom::ServicePtr client = | 185 service_manager::mojom::ServicePtr client = |
182 service_manager::PassServiceRequestOnCommandLine(&process_connection, | 186 service_manager::PassServiceRequestOnCommandLine(&process_connection, |
183 &child_command_line); | 187 &child_command_line); |
184 service_manager::mojom::PIDReceiverPtr receiver; | 188 service_manager::mojom::PIDReceiverPtr receiver; |
185 | 189 |
186 service_manager::Identity target("service_manager_unittest_target", | 190 service_manager::Identity target("service_manager_unittest_target", |
187 service_manager::mojom::kInheritUserID); | 191 service_manager::mojom::kInheritUserID); |
188 connector()->StartService(target, std::move(client), | 192 connector()->StartService(target, std::move(client), |
189 MakeRequest(&receiver)); | 193 MakeRequest(&receiver)); |
190 std::unique_ptr<service_manager::Connection> connection = | 194 Connector::TestApi test_api(connector()); |
191 connector()->Connect(target); | 195 test_api.SetStartServiceCallback(base::Bind( |
192 connection->AddConnectionCompletedClosure( | 196 &ServiceManagerTest::OnConnectionCompleted, base::Unretained(this))); |
193 base::Bind(&ServiceManagerTest::OnConnectionCompleted, | |
194 base::Unretained(this))); | |
195 | 197 |
196 base::LaunchOptions options; | 198 base::LaunchOptions options; |
197 #if defined(OS_WIN) | 199 #if defined(OS_WIN) |
198 options.handles_to_inherit = &handle_passing_info; | 200 options.handles_to_inherit = &handle_passing_info; |
199 #elif defined(OS_POSIX) | 201 #elif defined(OS_POSIX) |
200 options.fds_to_remap = &handle_passing_info; | 202 options.fds_to_remap = &handle_passing_info; |
201 #endif | 203 #endif |
202 target_ = base::LaunchProcess(child_command_line, options); | 204 target_ = base::LaunchProcess(child_command_line, options); |
203 DCHECK(target_.IsValid()); | 205 DCHECK(target_.IsValid()); |
204 receiver->SetPID(target_.Pid()); | 206 receiver->SetPID(target_.Pid()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 void OnServiceStopped(const service_manager::Identity& identity) override { | 250 void OnServiceStopped(const service_manager::Identity& identity) override { |
249 for (auto it = instances_.begin(); it != instances_.end(); ++it) { | 251 for (auto it = instances_.begin(); it != instances_.end(); ++it) { |
250 auto& instance = *it; | 252 auto& instance = *it; |
251 if (instance.identity == identity) { | 253 if (instance.identity == identity) { |
252 instances_.erase(it); | 254 instances_.erase(it); |
253 break; | 255 break; |
254 } | 256 } |
255 } | 257 } |
256 } | 258 } |
257 | 259 |
258 void OnConnectionCompleted() {} | 260 void OnConnectionCompleted(mojom::ConnectResult, const Identity&) {} |
259 | 261 |
260 ServiceManagerTestClient* service_; | 262 ServiceManagerTestClient* service_; |
261 mojo::Binding<mojom::ServiceManagerListener> binding_; | 263 mojo::Binding<mojom::ServiceManagerListener> binding_; |
262 std::vector<InstanceInfo> instances_; | 264 std::vector<InstanceInfo> instances_; |
263 std::vector<InstanceInfo> initial_instances_; | 265 std::vector<InstanceInfo> initial_instances_; |
264 std::unique_ptr<base::RunLoop> wait_for_instances_loop_; | 266 std::unique_ptr<base::RunLoop> wait_for_instances_loop_; |
265 ServiceStartedCallback service_started_callback_; | 267 ServiceStartedCallback service_started_callback_; |
266 ServiceFailedToStartCallback service_failed_to_start_callback_; | 268 ServiceFailedToStartCallback service_failed_to_start_callback_; |
267 base::Process target_; | 269 base::Process target_; |
268 | 270 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 int start_count = 0; | 328 int start_count = 0; |
327 std::string service_name; | 329 std::string service_name; |
328 set_service_started_callback(base::BindRepeating( | 330 set_service_started_callback(base::BindRepeating( |
329 &OnServiceStartedCallback, | 331 &OnServiceStartedCallback, |
330 &start_count, &service_name, loop.QuitClosure())); | 332 &start_count, &service_name, loop.QuitClosure())); |
331 bool failed_to_start = false; | 333 bool failed_to_start = false; |
332 set_service_failed_to_start_callback(base::BindRepeating( | 334 set_service_failed_to_start_callback(base::BindRepeating( |
333 &OnServiceFailedToStartCallback, | 335 &OnServiceFailedToStartCallback, |
334 &failed_to_start, loop.QuitClosure())); | 336 &failed_to_start, loop.QuitClosure())); |
335 | 337 |
336 std::unique_ptr<Connection> embedder_connection = | 338 connector()->StartService("service_manager_unittest_embedder"); |
337 connector()->Connect("service_manager_unittest_embedder"); | |
338 loop.Run(); | 339 loop.Run(); |
339 EXPECT_FALSE(failed_to_start); | 340 EXPECT_FALSE(failed_to_start); |
340 EXPECT_FALSE(embedder_connection->IsPending()); | |
341 EXPECT_EQ(1, start_count); | 341 EXPECT_EQ(1, start_count); |
342 EXPECT_EQ("service_manager_unittest_embedder", service_name); | 342 EXPECT_EQ("service_manager_unittest_embedder", service_name); |
343 } | 343 } |
344 | 344 |
345 { | 345 { |
346 base::RunLoop loop; | 346 base::RunLoop loop; |
347 int start_count = 0; | 347 int start_count = 0; |
348 std::string service_name; | 348 std::string service_name; |
349 set_service_started_callback(base::BindRepeating( | 349 set_service_started_callback(base::BindRepeating( |
350 &OnServiceStartedCallback, | 350 &OnServiceStartedCallback, |
351 &start_count, &service_name, loop.QuitClosure())); | 351 &start_count, &service_name, loop.QuitClosure())); |
352 bool failed_to_start = false; | 352 bool failed_to_start = false; |
353 set_service_failed_to_start_callback(base::BindRepeating( | 353 set_service_failed_to_start_callback(base::BindRepeating( |
354 &OnServiceFailedToStartCallback, | 354 &OnServiceFailedToStartCallback, |
355 &failed_to_start, loop.QuitClosure())); | 355 &failed_to_start, loop.QuitClosure())); |
356 | 356 |
357 // Connect to the packaged singleton service. | 357 // Connect to the packaged singleton service. |
358 std::unique_ptr<Connection> singleton_connection = | 358 connector()->StartService("service_manager_unittest_singleton"); |
359 connector()->Connect("service_manager_unittest_singleton"); | |
360 loop.Run(); | 359 loop.Run(); |
361 EXPECT_FALSE(failed_to_start); | 360 EXPECT_FALSE(failed_to_start); |
362 EXPECT_FALSE(singleton_connection->IsPending()); | |
363 EXPECT_EQ(1, start_count); | 361 EXPECT_EQ(1, start_count); |
364 EXPECT_EQ("service_manager_unittest_singleton", service_name); | 362 EXPECT_EQ("service_manager_unittest_singleton", service_name); |
365 } | 363 } |
366 } | 364 } |
367 | 365 |
368 } // namespace service_manager | 366 } // namespace service_manager |
OLD | NEW |