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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 110 |
121 } // namespace | 111 } // namespace |
122 | 112 |
123 void RegisterChildWithExternalShell(int child_process_id, | 113 void RegisterChildWithExternalShell(int child_process_id, |
124 RenderProcessHost* render_process_host) { | 114 RenderProcessHost* render_process_host) { |
125 // Some process types get created before the main message loop. | 115 // Some process types get created before the main message loop. |
126 if (!MojoShellConnection::Get()) | 116 if (!MojoShellConnection::Get()) |
127 return; | 117 return; |
128 | 118 |
129 // Create the channel to be shared with the target process. | 119 // Create the channel to be shared with the target process. |
130 mojo::embedder::HandlePassingInformation handle_passing_info; | 120 mojo::edk::HandlePassingInformation handle_passing_info; |
131 mojo::embedder::PlatformChannelPair platform_channel_pair; | 121 mojo::edk::PlatformChannelPair platform_channel_pair; |
132 | 122 |
133 // Give one end to the shell so that it can create an instance. | 123 // Give one end to the shell so that it can create an instance. |
134 mojo::embedder::ScopedPlatformHandle platform_channel = | 124 mojo::edk::ScopedPlatformHandle platform_channel = |
135 platform_channel_pair.PassServerHandle(); | 125 platform_channel_pair.PassServerHandle(); |
136 mojo::ScopedMessagePipeHandle handle(mojo::embedder::CreateChannel( | 126 mojo::ScopedMessagePipeHandle handle(mojo::edk::CreateMessagePipe( |
137 std::move(platform_channel), base::Bind(&DidCreateChannel), | 127 std::move(platform_channel))); |
138 base::ThreadTaskRunnerHandle::Get())); | |
139 mojo::shell::mojom::ApplicationManagerPtr application_manager; | 128 mojo::shell::mojom::ApplicationManagerPtr application_manager; |
140 MojoShellConnection::Get()->GetApplication()->ConnectToService( | 129 MojoShellConnection::Get()->GetApplication()->ConnectToService( |
141 "mojo:shell", &application_manager); | 130 "mojo:shell", &application_manager); |
142 // The content of the URL/qualifier we pass is actually meaningless, it's only | 131 // The content of the URL/qualifier we pass is actually meaningless, it's only |
143 // important that they're unique per process. | 132 // important that they're unique per process. |
144 // TODO(beng): We need to specify a restrictive CapabilityFilter here that | 133 // TODO(beng): We need to specify a restrictive CapabilityFilter here that |
145 // matches the needs of the target process. Figure out where that | 134 // matches the needs of the target process. Figure out where that |
146 // specification is best determined (not here, this is a common | 135 // specification is best determined (not here, this is a common |
147 // chokepoint for all process types) and how to wire it through. | 136 // chokepoint for all process types) and how to wire it through. |
148 // http://crbug.com/555393 | 137 // http://crbug.com/555393 |
149 std::string url = | 138 std::string url = |
150 base::StringPrintf("exe:chrome_renderer%d", child_process_id); | 139 base::StringPrintf("exe:chrome_renderer%d", child_process_id); |
151 | 140 |
152 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 141 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
153 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 142 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
154 GetProxy(&pid_receiver); | 143 GetProxy(&pid_receiver); |
155 new PIDSender(render_process_host, std::move(pid_receiver)); | 144 new PIDSender(render_process_host, std::move(pid_receiver)); |
156 | 145 |
157 application_manager->CreateInstanceForHandle( | 146 application_manager->CreateInstanceForHandle( |
158 mojo::ScopedHandle(mojo::Handle(handle.release().value())), | 147 mojo::ScopedHandle(mojo::Handle(handle.release().value())), |
159 url, | 148 url, |
160 CreateCapabilityFilterForRenderer(), | 149 CreateCapabilityFilterForRenderer(), |
161 std::move(request)); | 150 std::move(request)); |
162 | 151 |
163 // Send the other end to the child via Chrome IPC. | 152 // Send the other end to the child via Chrome IPC. |
164 base::PlatformFile client_file = PlatformFileFromScopedPlatformHandle( | 153 base::PlatformFile client_file = |
165 platform_channel_pair.PassClientHandle()); | 154 platform_channel_pair.PassClientHandle().release().handle; |
166 SetMojoPlatformFile(render_process_host, client_file); | 155 SetMojoPlatformFile(render_process_host, client_file); |
167 | 156 |
168 // Store the URL on the RPH so client code can access it later via | 157 // Store the URL on the RPH so client code can access it later via |
169 // GetMojoApplicationInstanceURL(). | 158 // GetMojoApplicationInstanceURL(). |
170 SetMojoApplicationInstanceURL(render_process_host, url); | 159 SetMojoApplicationInstanceURL(render_process_host, url); |
171 } | 160 } |
172 | 161 |
173 std::string GetMojoApplicationInstanceURL( | 162 std::string GetMojoApplicationInstanceURL( |
174 RenderProcessHost* render_process_host) { | 163 RenderProcessHost* render_process_host) { |
175 InstanceURL* instance_url = static_cast<InstanceURL*>( | 164 InstanceURL* instance_url = static_cast<InstanceURL*>( |
176 render_process_host->GetUserData(kMojoShellInstanceURL)); | 165 render_process_host->GetUserData(kMojoShellInstanceURL)); |
177 return instance_url ? instance_url->get() : std::string(); | 166 return instance_url ? instance_url->get() : std::string(); |
178 } | 167 } |
179 | 168 |
180 void SendExternalMojoShellHandleToChild( | 169 void SendExternalMojoShellHandleToChild( |
181 base::ProcessHandle process_handle, | 170 base::ProcessHandle process_handle, |
182 RenderProcessHost* render_process_host) { | 171 RenderProcessHost* render_process_host) { |
183 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( | 172 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( |
184 render_process_host->GetUserData(kMojoPlatformFile)); | 173 render_process_host->GetUserData(kMojoPlatformFile)); |
185 if (!client_file) | 174 if (!client_file) |
186 return; | 175 return; |
187 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( | 176 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( |
188 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); | 177 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); |
189 } | 178 } |
190 | 179 |
191 mojo::embedder::ScopedPlatformHandle RegisterProcessWithBroker( | 180 mojo::edk::ScopedPlatformHandle RegisterProcessWithBroker( |
192 base::ProcessId pid) { | 181 base::ProcessId pid) { |
193 mojo::embedder::PlatformChannelPair platform_channel_pair; | 182 mojo::edk::PlatformChannelPair platform_channel_pair; |
194 | 183 |
195 MojoHandle platform_handle_wrapper; | 184 MojoHandle platform_handle_wrapper; |
196 MojoResult rv = mojo::embedder::CreatePlatformHandleWrapper( | 185 MojoResult rv = mojo::edk::CreatePlatformHandleWrapper( |
197 platform_channel_pair.PassServerHandle(), &platform_handle_wrapper); | 186 platform_channel_pair.PassServerHandle(), &platform_handle_wrapper); |
198 CHECK_EQ(rv, MOJO_RESULT_OK); | 187 CHECK_EQ(rv, MOJO_RESULT_OK); |
199 | 188 |
200 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 189 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
201 CallRegisterProcessWithBroker(pid, platform_handle_wrapper); | 190 CallRegisterProcessWithBroker(pid, platform_handle_wrapper); |
202 } else { | 191 } else { |
203 BrowserThread::PostTask( | 192 BrowserThread::PostTask( |
204 BrowserThread::UI, | 193 BrowserThread::UI, |
205 FROM_HERE, | 194 FROM_HERE, |
206 base::Bind(CallRegisterProcessWithBroker, pid, | 195 base::Bind(CallRegisterProcessWithBroker, pid, |
207 platform_handle_wrapper)); | 196 platform_handle_wrapper)); |
208 } | 197 } |
209 | 198 |
210 return platform_channel_pair.PassClientHandle(); | 199 return platform_channel_pair.PassClientHandle(); |
211 } | 200 } |
212 | 201 |
213 } // namespace content | 202 } // namespace content |
OLD | NEW |