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