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" | |
15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
17 #include "content/public/browser/render_process_host_observer.h" | 16 #include "content/public/browser/render_process_host_observer.h" |
18 #include "content/public/common/mojo_shell_connection.h" | 17 #include "content/public/common/mojo_shell_connection.h" |
19 #include "ipc/ipc_sender.h" | 18 #include "ipc/ipc_sender.h" |
20 #include "mojo/converters/network/network_type_converters.h" | 19 #include "mojo/converters/network/network_type_converters.h" |
21 #include "mojo/edk/embedder/embedder.h" | 20 #include "mojo/edk/embedder/embedder.h" |
22 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
23 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
24 #include "mojo/shell/public/cpp/connector.h" | 22 #include "mojo/shell/public/cpp/connector.h" |
25 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 23 #include "mojo/shell/public/interfaces/application_manager.mojom.h" |
26 | 24 |
27 namespace content { | 25 namespace content { |
28 namespace { | 26 namespace { |
29 | 27 |
30 const char kMojoShellInstanceURL[] = "mojo_shell_instance_url"; | 28 const char kMojoShellInstanceURL[] = "mojo_shell_instance_url"; |
31 const char kMojoPlatformFile[] = "mojo_platform_file"; | |
32 | |
33 base::PlatformFile PlatformFileFromScopedPlatformHandle( | |
34 mojo::edk::ScopedPlatformHandle handle) { | |
35 return handle.release().handle; | |
36 } | |
37 | 29 |
38 class InstanceURL : public base::SupportsUserData::Data { | 30 class InstanceURL : public base::SupportsUserData::Data { |
39 public: | 31 public: |
40 InstanceURL(const std::string& instance_url) : instance_url_(instance_url) {} | 32 explicit InstanceURL(const std::string& instance_url) |
| 33 : instance_url_(instance_url) {} |
41 ~InstanceURL() override {} | 34 ~InstanceURL() override {} |
42 | 35 |
43 std::string get() const { return instance_url_; } | 36 std::string get() const { return instance_url_; } |
44 | 37 |
45 private: | 38 private: |
46 std::string instance_url_; | 39 std::string instance_url_; |
47 | 40 |
48 DISALLOW_COPY_AND_ASSIGN(InstanceURL); | 41 DISALLOW_COPY_AND_ASSIGN(InstanceURL); |
49 }; | 42 }; |
50 | 43 |
51 class InstanceShellHandle : public base::SupportsUserData::Data { | |
52 public: | |
53 InstanceShellHandle(base::PlatformFile shell_handle) | |
54 : shell_handle_(shell_handle) {} | |
55 ~InstanceShellHandle() override {} | |
56 | |
57 base::PlatformFile get() const { return shell_handle_; } | |
58 | |
59 private: | |
60 base::PlatformFile shell_handle_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(InstanceShellHandle); | |
63 }; | |
64 | |
65 void SetMojoApplicationInstanceURL(RenderProcessHost* render_process_host, | 44 void SetMojoApplicationInstanceURL(RenderProcessHost* render_process_host, |
66 const std::string& instance_url) { | 45 const std::string& instance_url) { |
67 render_process_host->SetUserData(kMojoShellInstanceURL, | 46 render_process_host->SetUserData(kMojoShellInstanceURL, |
68 new InstanceURL(instance_url)); | 47 new InstanceURL(instance_url)); |
69 } | 48 } |
70 | 49 |
71 void SetMojoPlatformFile(RenderProcessHost* render_process_host, | |
72 base::PlatformFile platform_file) { | |
73 render_process_host->SetUserData(kMojoPlatformFile, | |
74 new InstanceShellHandle(platform_file)); | |
75 } | |
76 | |
77 class PIDSender : public RenderProcessHostObserver { | 50 class PIDSender : public RenderProcessHostObserver { |
78 public: | 51 public: |
79 PIDSender( | 52 PIDSender( |
80 RenderProcessHost* host, | 53 RenderProcessHost* host, |
81 mojo::shell::mojom::PIDReceiverPtr pid_receiver) | 54 mojo::shell::mojom::PIDReceiverPtr pid_receiver) |
82 : host_(host), | 55 : host_(host), |
83 pid_receiver_(std::move(pid_receiver)) { | 56 pid_receiver_(std::move(pid_receiver)) { |
84 pid_receiver_.set_connection_error_handler([this]() { delete this; }); | 57 pid_receiver_.set_connection_error_handler([this]() { delete this; }); |
85 DCHECK(!host_->IsReady()); | 58 DCHECK(!host_->IsReady()); |
86 host_->AddObserver(this); | 59 host_->AddObserver(this); |
(...skipping 16 matching lines...) Expand all Loading... |
103 } | 76 } |
104 | 77 |
105 RenderProcessHost* host_; | 78 RenderProcessHost* host_; |
106 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; | 79 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; |
107 | 80 |
108 DISALLOW_COPY_AND_ASSIGN(PIDSender); | 81 DISALLOW_COPY_AND_ASSIGN(PIDSender); |
109 }; | 82 }; |
110 | 83 |
111 } // namespace | 84 } // namespace |
112 | 85 |
113 void RegisterChildWithExternalShell(int child_process_id, | 86 std::string RegisterChildWithExternalShell( |
114 int instance_id, | 87 int child_process_id, |
115 RenderProcessHost* render_process_host) { | 88 int instance_id, |
116 // Some process types get created before the main message loop. | 89 RenderProcessHost* render_process_host) { |
| 90 // Generate a token and create a pipe which is bound to it. This pipe is |
| 91 // passed to the shell if one is available. |
| 92 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
| 93 mojo::ScopedMessagePipeHandle request_pipe = |
| 94 mojo::edk::CreateParentMessagePipe(pipe_token); |
| 95 |
| 96 // Some process types get created before the main message loop. In this case |
| 97 // the shell request pipe will simply be closed, and the child can detect |
| 98 // this. |
117 if (!MojoShellConnection::Get()) | 99 if (!MojoShellConnection::Get()) |
118 return; | 100 return pipe_token; |
119 | |
120 // Create the channel to be shared with the target process. | |
121 mojo::edk::HandlePassingInformation handle_passing_info; | |
122 mojo::edk::PlatformChannelPair platform_channel_pair; | |
123 | |
124 // Give one end to the shell so that it can create an instance. | |
125 mojo::edk::ScopedPlatformHandle parent_pipe = | |
126 platform_channel_pair.PassServerHandle(); | |
127 | |
128 // Send the other end to the child via Chrome IPC. | |
129 base::PlatformFile client_file = PlatformFileFromScopedPlatformHandle( | |
130 platform_channel_pair.PassClientHandle()); | |
131 SetMojoPlatformFile(render_process_host, client_file); | |
132 | |
133 mojo::ScopedMessagePipeHandle request_pipe = | |
134 mojo::edk::CreateMessagePipe(std::move(parent_pipe)); | |
135 | 101 |
136 mojo::shell::mojom::ApplicationManagerPtr application_manager; | 102 mojo::shell::mojom::ApplicationManagerPtr application_manager; |
137 MojoShellConnection::Get()->GetConnector()->ConnectToInterface( | 103 MojoShellConnection::Get()->GetConnector()->ConnectToInterface( |
138 "mojo:shell", &application_manager); | 104 "mojo:shell", &application_manager); |
139 | 105 |
140 // The content of the URL/qualifier we pass is actually meaningless, it's only | 106 // The content of the URL/qualifier we pass is actually meaningless, it's only |
141 // important that they're unique per process. | 107 // important that they're unique per process. |
142 // TODO(beng): We need to specify a restrictive CapabilityFilter here that | 108 // TODO(beng): We need to specify a restrictive CapabilityFilter here that |
143 // matches the needs of the target process. Figure out where that | 109 // matches the needs of the target process. Figure out where that |
144 // specification is best determined (not here, this is a common | 110 // specification is best determined (not here, this is a common |
145 // chokepoint for all process types) and how to wire it through. | 111 // chokepoint for all process types) and how to wire it through. |
146 // http://crbug.com/555393 | 112 // http://crbug.com/555393 |
147 std::string url = base::StringPrintf( | 113 std::string url = base::StringPrintf( |
148 "exe:chrome_renderer%d_%d", child_process_id, instance_id); | 114 "exe:chrome_renderer%d_%d", child_process_id, instance_id); |
149 | 115 |
150 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 116 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
151 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 117 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
152 GetProxy(&pid_receiver); | 118 GetProxy(&pid_receiver); |
153 new PIDSender(render_process_host, std::move(pid_receiver)); | 119 new PIDSender(render_process_host, std::move(pid_receiver)); |
154 | 120 |
155 application_manager->CreateInstanceForHandle( | 121 application_manager->CreateInstanceForHandle( |
156 mojo::ScopedHandle(mojo::Handle(request_pipe.release().value())), | 122 mojo::ScopedHandle(mojo::Handle(request_pipe.release().value())), |
157 url, | 123 url, |
158 CreateCapabilityFilterForRenderer(), | 124 CreateCapabilityFilterForRenderer(), |
159 std::move(request)); | 125 std::move(request)); |
160 | 126 |
161 // Store the URL on the RPH so client code can access it later via | 127 // Store the URL on the RPH so client code can access it later via |
162 // GetMojoApplicationInstanceURL(). | 128 // GetMojoApplicationInstanceURL(). |
163 SetMojoApplicationInstanceURL(render_process_host, url); | 129 SetMojoApplicationInstanceURL(render_process_host, url); |
| 130 |
| 131 return pipe_token; |
164 } | 132 } |
165 | 133 |
166 std::string GetMojoApplicationInstanceURL( | 134 std::string GetMojoApplicationInstanceURL( |
167 RenderProcessHost* render_process_host) { | 135 RenderProcessHost* render_process_host) { |
168 InstanceURL* instance_url = static_cast<InstanceURL*>( | 136 InstanceURL* instance_url = static_cast<InstanceURL*>( |
169 render_process_host->GetUserData(kMojoShellInstanceURL)); | 137 render_process_host->GetUserData(kMojoShellInstanceURL)); |
170 return instance_url ? instance_url->get() : std::string(); | 138 return instance_url ? instance_url->get() : std::string(); |
171 } | 139 } |
172 | 140 |
173 void SendExternalMojoShellHandleToChild( | |
174 base::ProcessHandle process_handle, | |
175 RenderProcessHost* render_process_host) { | |
176 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( | |
177 render_process_host->GetUserData(kMojoPlatformFile)); | |
178 if (!client_file) | |
179 return; | |
180 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( | |
181 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); | |
182 } | |
183 | |
184 } // namespace content | 141 } // namespace content |
OLD | NEW |