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