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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
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/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.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/common/weak_binding_set.h" | 21 #include "mojo/common/weak_binding_set.h" |
22 #include "mojo/converters/network/network_type_converters.h" | 22 #include "mojo/converters/network/network_type_converters.h" |
23 #include "mojo/edk/embedder/embedder.h" | 23 #include "mojo/edk/embedder/embedder.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/runner/child/test_native_main.h" | 26 #include "mojo/runner/child/test_native_main.h" |
26 #include "mojo/runner/init.h" | 27 #include "mojo/runner/init.h" |
27 #include "mojo/shell/application_manager_apptests.mojom.h" | 28 #include "mojo/shell/application_manager_apptests.mojom.h" |
28 #include "mojo/shell/public/cpp/application_connection.h" | 29 #include "mojo/shell/public/cpp/application_connection.h" |
29 #include "mojo/shell/public/cpp/application_delegate.h" | 30 #include "mojo/shell/public/cpp/application_delegate.h" |
30 #include "mojo/shell/public/cpp/application_impl.h" | 31 #include "mojo/shell/public/cpp/application_impl.h" |
31 #include "mojo/shell/public/cpp/interface_factory.h" | 32 #include "mojo/shell/public/cpp/interface_factory.h" |
32 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 33 #include "mojo/shell/public/interfaces/application_manager.mojom.h" |
33 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
34 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | |
35 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | |
36 | 34 |
37 using mojo::shell::test::mojom::Driver; | 35 using mojo::shell::test::mojom::Driver; |
38 | 36 |
39 namespace { | 37 namespace { |
40 | 38 |
41 class TargetApplicationDelegate : public mojo::ApplicationDelegate, | 39 class TargetApplicationDelegate : public mojo::ApplicationDelegate, |
42 public mojo::InterfaceFactory<Driver>, | 40 public mojo::InterfaceFactory<Driver>, |
43 public Driver { | 41 public Driver { |
44 public: | 42 public: |
45 TargetApplicationDelegate() : app_(nullptr), weak_factory_(this) {} | 43 TargetApplicationDelegate() : app_(nullptr), weak_factory_(this) {} |
(...skipping 16 matching lines...) Expand all Loading... |
62 FILE_PATH_LITERAL("application_manager_apptest_target")); | 60 FILE_PATH_LITERAL("application_manager_apptest_target")); |
63 #endif | 61 #endif |
64 | 62 |
65 base::CommandLine child_command_line(target_path); | 63 base::CommandLine child_command_line(target_path); |
66 // Forward the wait-for-debugger flag but nothing else - we don't want to | 64 // Forward the wait-for-debugger flag but nothing else - we don't want to |
67 // stamp on the platform-channel flag. | 65 // stamp on the platform-channel flag. |
68 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 66 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
69 switches::kWaitForDebugger)) { | 67 switches::kWaitForDebugger)) { |
70 child_command_line.AppendSwitch(switches::kWaitForDebugger); | 68 child_command_line.AppendSwitch(switches::kWaitForDebugger); |
71 } | 69 } |
72 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) | |
73 child_command_line.AppendSwitch("use-new-edk"); | |
74 | 70 |
75 mojo::embedder::HandlePassingInformation handle_passing_info; | 71 mojo::edk::HandlePassingInformation handle_passing_info; |
76 | 72 |
77 // Create the channel to be shared with the target process. | 73 // Create the channel to be shared with the target process. |
78 mojo::embedder::PlatformChannelPair platform_channel_pair; | 74 mojo::edk::PlatformChannelPair platform_channel_pair; |
79 // Give one end to the shell so that it can create an instance. | 75 // Give one end to the shell so that it can create an instance. |
80 mojo::embedder::ScopedPlatformHandle platform_channel = | 76 mojo::edk::ScopedPlatformHandle platform_channel = |
81 platform_channel_pair.PassServerHandle(); | 77 platform_channel_pair.PassServerHandle(); |
82 | 78 |
83 mojo::ScopedMessagePipeHandle handle(mojo::embedder::CreateChannel( | 79 mojo::ScopedMessagePipeHandle handle(mojo::edk::CreateMessagePipe( |
84 std::move(platform_channel), | 80 std::move(platform_channel))); |
85 base::Bind(&TargetApplicationDelegate::DidCreateChannel, | |
86 weak_factory_.GetWeakPtr()), | |
87 base::ThreadTaskRunnerHandle::Get())); | |
88 | 81 |
89 // The platform handle used for the new EDK's broker. | 82 // The platform handle used for the new EDK's broker. |
90 mojo::edk::PlatformChannelPair broker_channel_pair; | 83 mojo::edk::PlatformChannelPair broker_channel_pair; |
91 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 84 std::string client_handle_as_string = |
92 std::string client_handle_as_string = | 85 broker_channel_pair.PrepareToPassClientHandleToChildProcessAsString( |
93 broker_channel_pair.PrepareToPassClientHandleToChildProcessAsString( | 86 &handle_passing_info); |
94 &handle_passing_info); | 87 MojoResult rv = MojoWriteMessage( |
95 MojoResult rv = MojoWriteMessage( | 88 handle.get().value(), client_handle_as_string.c_str(), |
96 handle.get().value(), client_handle_as_string.c_str(), | 89 static_cast<uint32_t>(client_handle_as_string.size()), nullptr, 0, |
97 static_cast<uint32_t>(client_handle_as_string.size()), nullptr, 0, | 90 MOJO_WRITE_MESSAGE_FLAG_NONE); |
98 MOJO_WRITE_MESSAGE_FLAG_NONE); | 91 DCHECK_EQ(rv, MOJO_RESULT_OK); |
99 DCHECK_EQ(rv, MOJO_RESULT_OK); | |
100 } | |
101 | 92 |
102 mojo::CapabilityFilterPtr filter(mojo::CapabilityFilter::New()); | 93 mojo::CapabilityFilterPtr filter(mojo::CapabilityFilter::New()); |
103 mojo::Array<mojo::String> test_interfaces; | 94 mojo::Array<mojo::String> test_interfaces; |
104 test_interfaces.push_back( | 95 test_interfaces.push_back( |
105 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); | 96 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); |
106 filter->filter.insert("mojo:mojo_shell_apptests", | 97 filter->filter.insert("mojo:mojo_shell_apptests", |
107 std::move(test_interfaces)); | 98 std::move(test_interfaces)); |
108 mojo::shell::mojom::PIDReceiverPtr receiver; | 99 mojo::shell::mojom::PIDReceiverPtr receiver; |
109 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 100 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
110 GetProxy(&receiver); | 101 GetProxy(&receiver); |
111 application_manager->CreateInstanceForHandle( | 102 application_manager->CreateInstanceForHandle( |
112 mojo::ScopedHandle(mojo::Handle(handle.release().value())), | 103 mojo::ScopedHandle(mojo::Handle(handle.release().value())), |
113 "exe:application_manager_apptest_target", std::move(filter), | 104 "exe:application_manager_apptest_target", std::move(filter), |
114 std::move(request)); | 105 std::move(request)); |
115 // Put the other end on the command line used to launch the target. | 106 // Put the other end on the command line used to launch the target. |
116 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | 107 platform_channel_pair.PrepareToPassClientHandleToChildProcess( |
117 &child_command_line, &handle_passing_info); | 108 &child_command_line, &handle_passing_info); |
118 | 109 |
119 base::LaunchOptions options; | 110 base::LaunchOptions options; |
120 #if defined(OS_WIN) | 111 #if defined(OS_WIN) |
121 options.handles_to_inherit = &handle_passing_info; | 112 options.handles_to_inherit = &handle_passing_info; |
122 #elif defined(OS_POSIX) | 113 #elif defined(OS_POSIX) |
123 options.fds_to_remap = &handle_passing_info; | 114 options.fds_to_remap = &handle_passing_info; |
124 #endif | 115 #endif |
125 target_ = base::LaunchProcess(child_command_line, options); | 116 target_ = base::LaunchProcess(child_command_line, options); |
126 DCHECK(target_.IsValid()); | 117 DCHECK(target_.IsValid()); |
127 receiver->SetPID(target_.Pid()); | 118 receiver->SetPID(target_.Pid()); |
128 | 119 |
129 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 120 MojoHandle platform_handle_wrapper; |
130 MojoHandle platform_handle_wrapper; | 121 rv = mojo::edk::CreatePlatformHandleWrapper( |
131 MojoResult rv = mojo::edk::CreatePlatformHandleWrapper( | 122 broker_channel_pair.PassServerHandle(), &platform_handle_wrapper); |
132 broker_channel_pair.PassServerHandle(), &platform_handle_wrapper); | 123 DCHECK_EQ(rv, MOJO_RESULT_OK); |
133 DCHECK_EQ(rv, MOJO_RESULT_OK); | 124 application_manager->RegisterProcessWithBroker( |
134 application_manager->RegisterProcessWithBroker( | 125 target_.Pid(), |
135 target_.Pid(), | 126 mojo::ScopedHandle(mojo::Handle(platform_handle_wrapper))); |
136 mojo::ScopedHandle(mojo::Handle(platform_handle_wrapper))); | |
137 } | |
138 } | 127 } |
139 bool ConfigureIncomingConnection( | 128 bool ConfigureIncomingConnection( |
140 mojo::ApplicationConnection* connection) override { | 129 mojo::ApplicationConnection* connection) override { |
141 connection->AddService<Driver>(this); | 130 connection->AddService<Driver>(this); |
142 return true; | 131 return true; |
143 } | 132 } |
144 | 133 |
145 // mojo::InterfaceFactory<Driver>: | 134 // mojo::InterfaceFactory<Driver>: |
146 void Create(mojo::ApplicationConnection* connection, | 135 void Create(mojo::ApplicationConnection* connection, |
147 mojo::InterfaceRequest<Driver> request) override { | 136 mojo::InterfaceRequest<Driver> request) override { |
148 bindings_.AddBinding(this, std::move(request)); | 137 bindings_.AddBinding(this, std::move(request)); |
149 } | 138 } |
150 | 139 |
151 // Driver: | 140 // Driver: |
152 void QuitDriver() override { | 141 void QuitDriver() override { |
153 target_.Terminate(0, false); | 142 target_.Terminate(0, false); |
154 app_->Quit(); | 143 app_->Quit(); |
155 } | 144 } |
156 | 145 |
157 void DidCreateChannel(mojo::embedder::ChannelInfo* channel_info) {} | |
158 | |
159 mojo::ApplicationImpl* app_; | 146 mojo::ApplicationImpl* app_; |
160 base::Process target_; | 147 base::Process target_; |
161 mojo::WeakBindingSet<Driver> bindings_; | 148 mojo::WeakBindingSet<Driver> bindings_; |
162 base::WeakPtrFactory<TargetApplicationDelegate> weak_factory_; | 149 base::WeakPtrFactory<TargetApplicationDelegate> weak_factory_; |
163 | 150 |
164 DISALLOW_COPY_AND_ASSIGN(TargetApplicationDelegate); | 151 DISALLOW_COPY_AND_ASSIGN(TargetApplicationDelegate); |
165 }; | 152 }; |
166 | 153 |
167 } // namespace | 154 } // namespace |
168 | 155 |
169 int main(int argc, char** argv) { | 156 int main(int argc, char** argv) { |
170 base::AtExitManager at_exit; | 157 base::AtExitManager at_exit; |
171 base::CommandLine::Init(argc, argv); | 158 base::CommandLine::Init(argc, argv); |
172 | 159 |
173 mojo::runner::InitializeLogging(); | 160 mojo::runner::InitializeLogging(); |
174 | 161 |
175 TargetApplicationDelegate delegate; | 162 TargetApplicationDelegate delegate; |
176 return mojo::runner::TestNativeMain(&delegate); | 163 return mojo::runner::TestNativeMain(&delegate); |
177 } | 164 } |
OLD | NEW |