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 "content/browser/mojo/mojo_shell_client_host.h" | 5 #include "content/browser/mojo/mojo_shell_client_host.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "content/common/mojo/mojo_messages.h" | 14 #include "content/common/mojo/mojo_messages.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
17 #include "content/public/browser/render_process_host_observer.h" | 17 #include "content/public/browser/render_process_host_observer.h" |
18 #include "content/public/common/mojo_shell_connection.h" | 18 #include "content/public/common/mojo_shell_connection.h" |
19 #include "ipc/ipc_sender.h" | 19 #include "ipc/ipc_sender.h" |
20 #include "mojo/converters/network/network_type_converters.h" | 20 #include "mojo/converters/network/network_type_converters.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/system/message_pipe.h" |
21 #include "mojo/shell/public/cpp/application_impl.h" | 25 #include "mojo/shell/public/cpp/application_impl.h" |
22 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 26 #include "mojo/shell/public/interfaces/application_manager.mojom.h" |
23 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
24 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | |
25 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 namespace { | 29 namespace { |
29 | 30 |
30 const char kMojoShellInstanceURL[] = "mojo_shell_instance_url"; | 31 const char kMojoShellInstanceURL[] = "mojo_shell_instance_url"; |
31 const char kMojoPlatformFile[] = "mojo_platform_file"; | 32 const char kMojoPlatformFile[] = "mojo_platform_file"; |
32 | 33 |
33 void DidCreateChannel(mojo::embedder::ChannelInfo* info) {} | |
34 | |
35 base::PlatformFile PlatformFileFromScopedPlatformHandle( | |
36 mojo::embedder::ScopedPlatformHandle handle) { | |
37 #if defined(OS_POSIX) | |
38 return handle.release().fd; | |
39 #elif defined(OS_WIN) | |
40 return handle.release().handle; | |
41 #endif | |
42 } | |
43 | |
44 class InstanceURL : public base::SupportsUserData::Data { | 34 class InstanceURL : public base::SupportsUserData::Data { |
45 public: | 35 public: |
46 InstanceURL(const std::string& instance_url) : instance_url_(instance_url) {} | 36 InstanceURL(const std::string& instance_url) : instance_url_(instance_url) {} |
47 ~InstanceURL() override {} | 37 ~InstanceURL() override {} |
48 | 38 |
49 std::string get() const { return instance_url_; } | 39 std::string get() const { return instance_url_; } |
50 | 40 |
51 private: | 41 private: |
52 std::string instance_url_; | 42 std::string instance_url_; |
53 | 43 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 116 |
127 } // namespace | 117 } // namespace |
128 | 118 |
129 void RegisterChildWithExternalShell(int child_process_id, | 119 void RegisterChildWithExternalShell(int child_process_id, |
130 RenderProcessHost* render_process_host) { | 120 RenderProcessHost* render_process_host) { |
131 // Some process types get created before the main message loop. | 121 // Some process types get created before the main message loop. |
132 if (!MojoShellConnection::Get()) | 122 if (!MojoShellConnection::Get()) |
133 return; | 123 return; |
134 | 124 |
135 // Create the channel to be shared with the target process. | 125 // Create the channel to be shared with the target process. |
136 mojo::embedder::HandlePassingInformation handle_passing_info; | 126 mojo::edk::HandlePassingInformation handle_passing_info; |
137 mojo::embedder::PlatformChannelPair platform_channel_pair; | 127 mojo::edk::PlatformChannelPair platform_channel_pair; |
138 | 128 |
139 // Give one end to the shell so that it can create an instance. | 129 // Give one end to the shell so that it can create an instance. |
140 mojo::embedder::ScopedPlatformHandle platform_channel = | 130 mojo::edk::ScopedPlatformHandle platform_channel = |
141 platform_channel_pair.PassServerHandle(); | 131 platform_channel_pair.PassServerHandle(); |
142 mojo::ScopedMessagePipeHandle handle(mojo::embedder::CreateChannel( | 132 mojo::ScopedMessagePipeHandle handle(mojo::edk::CreateMessagePipe( |
143 std::move(platform_channel), base::Bind(&DidCreateChannel), | 133 std::move(platform_channel))); |
144 base::ThreadTaskRunnerHandle::Get())); | |
145 mojo::shell::mojom::ApplicationManagerPtr application_manager; | 134 mojo::shell::mojom::ApplicationManagerPtr application_manager; |
146 MojoShellConnection::Get()->GetApplication()->ConnectToService( | 135 MojoShellConnection::Get()->GetApplication()->ConnectToService( |
147 "mojo:shell", &application_manager); | 136 "mojo:shell", &application_manager); |
148 // The content of the URL/qualifier we pass is actually meaningless, it's only | 137 // The content of the URL/qualifier we pass is actually meaningless, it's only |
149 // important that they're unique per process. | 138 // important that they're unique per process. |
150 // TODO(beng): We need to specify a restrictive CapabilityFilter here that | 139 // TODO(beng): We need to specify a restrictive CapabilityFilter here that |
151 // matches the needs of the target process. Figure out where that | 140 // matches the needs of the target process. Figure out where that |
152 // specification is best determined (not here, this is a common | 141 // specification is best determined (not here, this is a common |
153 // chokepoint for all process types) and how to wire it through. | 142 // chokepoint for all process types) and how to wire it through. |
154 // http://crbug.com/555393 | 143 // http://crbug.com/555393 |
155 std::string url = | 144 std::string url = |
156 base::StringPrintf("exe:chrome_renderer%d", child_process_id); | 145 base::StringPrintf("exe:chrome_renderer%d", child_process_id); |
157 | 146 |
158 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 147 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
159 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 148 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
160 GetProxy(&pid_receiver); | 149 GetProxy(&pid_receiver); |
161 new PIDSender(render_process_host, std::move(pid_receiver)); | 150 new PIDSender(render_process_host, std::move(pid_receiver)); |
162 | 151 |
163 application_manager->CreateInstanceForHandle( | 152 application_manager->CreateInstanceForHandle( |
164 mojo::ScopedHandle(mojo::Handle(handle.release().value())), | 153 mojo::ScopedHandle(mojo::Handle(handle.release().value())), |
165 url, | 154 url, |
166 CreateCapabilityFilterForRenderer(), | 155 CreateCapabilityFilterForRenderer(), |
167 std::move(request)); | 156 std::move(request)); |
168 | 157 |
169 // Send the other end to the child via Chrome IPC. | 158 // Send the other end to the child via Chrome IPC. |
170 base::PlatformFile client_file = PlatformFileFromScopedPlatformHandle( | 159 base::PlatformFile client_file = |
171 platform_channel_pair.PassClientHandle()); | 160 platform_channel_pair.PassClientHandle().release().handle; |
172 SetMojoPlatformFile(render_process_host, client_file); | 161 SetMojoPlatformFile(render_process_host, client_file); |
173 | 162 |
174 // Store the URL on the RPH so client code can access it later via | 163 // Store the URL on the RPH so client code can access it later via |
175 // GetMojoApplicationInstanceURL(). | 164 // GetMojoApplicationInstanceURL(). |
176 SetMojoApplicationInstanceURL(render_process_host, url); | 165 SetMojoApplicationInstanceURL(render_process_host, url); |
177 } | 166 } |
178 | 167 |
179 std::string GetMojoApplicationInstanceURL( | 168 std::string GetMojoApplicationInstanceURL( |
180 RenderProcessHost* render_process_host) { | 169 RenderProcessHost* render_process_host) { |
181 InstanceURL* instance_url = static_cast<InstanceURL*>( | 170 InstanceURL* instance_url = static_cast<InstanceURL*>( |
182 render_process_host->GetUserData(kMojoShellInstanceURL)); | 171 render_process_host->GetUserData(kMojoShellInstanceURL)); |
183 return instance_url ? instance_url->get() : std::string(); | 172 return instance_url ? instance_url->get() : std::string(); |
184 } | 173 } |
185 | 174 |
186 void SendExternalMojoShellHandleToChild( | 175 void SendExternalMojoShellHandleToChild( |
187 base::ProcessHandle process_handle, | 176 base::ProcessHandle process_handle, |
188 RenderProcessHost* render_process_host) { | 177 RenderProcessHost* render_process_host) { |
189 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( | 178 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( |
190 render_process_host->GetUserData(kMojoPlatformFile)); | 179 render_process_host->GetUserData(kMojoPlatformFile)); |
191 if (!client_file) | 180 if (!client_file) |
192 return; | 181 return; |
193 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( | 182 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( |
194 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); | 183 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); |
195 } | 184 } |
196 | 185 |
197 mojo::embedder::ScopedPlatformHandle RegisterProcessWithBroker( | 186 mojo::edk::ScopedPlatformHandle RegisterProcessWithBroker( |
198 base::ProcessId pid) { | 187 base::ProcessId pid) { |
199 mojo::embedder::PlatformChannelPair platform_channel_pair; | 188 mojo::edk::PlatformChannelPair platform_channel_pair; |
200 | 189 |
201 MojoHandle platform_handle_wrapper; | 190 MojoHandle platform_handle_wrapper; |
202 MojoResult rv = mojo::embedder::CreatePlatformHandleWrapper( | 191 MojoResult rv = mojo::edk::CreatePlatformHandleWrapper( |
203 platform_channel_pair.PassServerHandle(), &platform_handle_wrapper); | 192 platform_channel_pair.PassServerHandle(), &platform_handle_wrapper); |
204 CHECK_EQ(rv, MOJO_RESULT_OK); | 193 CHECK_EQ(rv, MOJO_RESULT_OK); |
205 | 194 |
206 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 195 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
207 CallRegisterProcessWithBroker(pid, platform_handle_wrapper); | 196 CallRegisterProcessWithBroker(pid, platform_handle_wrapper); |
208 } else { | 197 } else { |
209 BrowserThread::PostTask( | 198 BrowserThread::PostTask( |
210 BrowserThread::UI, | 199 BrowserThread::UI, |
211 FROM_HERE, | 200 FROM_HERE, |
212 base::Bind(CallRegisterProcessWithBroker, pid, | 201 base::Bind(CallRegisterProcessWithBroker, pid, |
213 platform_handle_wrapper)); | 202 platform_handle_wrapper)); |
214 } | 203 } |
215 | 204 |
216 return platform_channel_pair.PassClientHandle(); | 205 return platform_channel_pair.PassClientHandle(); |
217 } | 206 } |
218 | 207 |
219 } // namespace content | 208 } // namespace content |
OLD | NEW |