OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdint.h> | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/at_exit.h" | |
11 #include "base/base_paths.h" | |
12 #include "base/base_switches.h" | |
13 #include "base/bind.h" | |
14 #include "base/command_line.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/message_loop/message_loop.h" | |
18 #include "base/path_service.h" | |
19 #include "base/process/process.h" | |
20 #include "base/threading/thread_task_runner_handle.h" | |
21 #include "mojo/edk/embedder/embedder.h" | |
22 #include "mojo/edk/embedder/platform_channel_pair.h" | |
23 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
24 #include "mojo/public/cpp/bindings/binding_set.h" | |
25 #include "services/service_manager/public/cpp/connection.h" | |
26 #include "services/service_manager/public/cpp/connector.h" | |
27 #include "services/service_manager/public/cpp/interface_factory.h" | |
28 #include "services/service_manager/public/cpp/service.h" | |
29 #include "services/service_manager/public/interfaces/connector.mojom.h" | |
30 #include "services/service_manager/public/interfaces/service_manager.mojom.h" | |
31 #include "services/service_manager/runner/child/test_native_main.h" | |
32 #include "services/service_manager/runner/common/client_util.h" | |
33 #include "services/service_manager/runner/common/switches.h" | |
34 #include "services/service_manager/runner/init.h" | |
35 #include "services/service_manager/tests/shell/shell_unittest.mojom.h" | |
36 | |
37 namespace { | |
38 | |
39 class Driver : public service_manager::Service, | |
40 public service_manager::InterfaceFactory< | |
41 service_manager::test::mojom::Driver>, | |
42 public service_manager::test::mojom::Driver { | |
43 public: | |
44 Driver() : weak_factory_(this) {} | |
45 ~Driver() override {} | |
46 | |
47 private: | |
48 // service_manager::Service: | |
49 void OnStart(const service_manager::Identity& identity) override { | |
50 base::FilePath target_path; | |
51 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); | |
52 #if defined(OS_WIN) | |
53 target_path = target_path.Append( | |
54 FILE_PATH_LITERAL("shell_unittest_target.exe")); | |
55 #else | |
56 target_path = target_path.Append( | |
57 FILE_PATH_LITERAL("shell_unittest_target")); | |
58 #endif | |
59 | |
60 base::CommandLine child_command_line(target_path); | |
61 // Forward the wait-for-debugger flag but nothing else - we don't want to | |
62 // stamp on the platform-channel flag. | |
63 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
64 switches::kWaitForDebugger)) { | |
65 child_command_line.AppendSwitch(switches::kWaitForDebugger); | |
66 } | |
67 | |
68 // Create the channel to be shared with the target process. Pass one end | |
69 // on the command line. | |
70 mojo::edk::PlatformChannelPair platform_channel_pair; | |
71 mojo::edk::HandlePassingInformation handle_passing_info; | |
72 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | |
73 &child_command_line, &handle_passing_info); | |
74 | |
75 std::string child_token = mojo::edk::GenerateRandomToken(); | |
76 service_manager::mojom::ServicePtr client = | |
77 service_manager::PassServiceRequestOnCommandLine(&child_command_line, | |
78 child_token); | |
79 service_manager::mojom::PIDReceiverPtr receiver; | |
80 | |
81 service_manager::Identity target("exe:shell_unittest_target", | |
82 service_manager::mojom::kInheritUserID); | |
83 service_manager::Connector::ConnectParams params(target); | |
84 params.set_client_process_connection(std::move(client), | |
85 GetProxy(&receiver)); | |
86 std::unique_ptr<service_manager::Connection> connection = | |
87 connector()->Connect(¶ms); | |
88 connection->AddConnectionCompletedClosure( | |
89 base::Bind(&Driver::OnConnectionCompleted, base::Unretained(this))); | |
90 | |
91 base::LaunchOptions options; | |
92 #if defined(OS_WIN) | |
93 options.handles_to_inherit = &handle_passing_info; | |
94 #elif defined(OS_POSIX) | |
95 options.fds_to_remap = &handle_passing_info; | |
96 #endif | |
97 target_ = base::LaunchProcess(child_command_line, options); | |
98 DCHECK(target_.IsValid()); | |
99 receiver->SetPID(target_.Pid()); | |
100 mojo::edk::ChildProcessLaunched(target_.Handle(), | |
101 platform_channel_pair.PassServerHandle(), | |
102 child_token); | |
103 } | |
104 | |
105 bool OnConnect(const service_manager::Identity& remote_identity, | |
106 service_manager::InterfaceRegistry* registry) override { | |
107 registry->AddInterface<service_manager::test::mojom::Driver>(this); | |
108 return true; | |
109 } | |
110 | |
111 // service_manager::InterfaceFactory<Driver>: | |
112 void Create(const service_manager::Identity& remote_identity, | |
113 service_manager::test::mojom::DriverRequest request) override { | |
114 bindings_.AddBinding(this, std::move(request)); | |
115 } | |
116 | |
117 // Driver: | |
118 void QuitDriver() override { | |
119 target_.Terminate(0, false); | |
120 base::MessageLoop::current()->QuitWhenIdle(); | |
121 } | |
122 | |
123 void OnConnectionCompleted() {} | |
124 | |
125 base::Process target_; | |
126 mojo::BindingSet<service_manager::test::mojom::Driver> bindings_; | |
127 base::WeakPtrFactory<Driver> weak_factory_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(Driver); | |
130 }; | |
131 | |
132 } // namespace | |
133 | |
134 int main(int argc, char** argv) { | |
135 base::AtExitManager at_exit; | |
136 base::CommandLine::Init(argc, argv); | |
137 | |
138 service_manager::InitializeLogging(); | |
139 | |
140 Driver driver; | |
141 return service_manager::TestNativeMain(&driver); | |
142 } | |
OLD | NEW |