| 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/shell.h" | 22 #include "mojo/shell/public/cpp/shell.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 RenderProcessHost* render_process_host) { | 87 int child_process_id, |
| 115 // Some process types get created before the main message loop. | 88 RenderProcessHost* render_process_host) { |
| 89 // Generate a token and create a pipe which is bound to it. This pipe is |
| 90 // passed to the shell if one is available. |
| 91 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
| 92 mojo::ScopedMessagePipeHandle request_pipe = |
| 93 mojo::edk::CreateParentMessagePipe(pipe_token); |
| 94 |
| 95 // Some process types get created before the main message loop. In this case |
| 96 // the shell request pipe will simply be closed, and the child can detect |
| 97 // this. |
| 116 if (!MojoShellConnection::Get()) | 98 if (!MojoShellConnection::Get()) |
| 117 return; | 99 return pipe_token; |
| 118 | |
| 119 // Create the channel to be shared with the target process. | |
| 120 mojo::edk::HandlePassingInformation handle_passing_info; | |
| 121 mojo::edk::PlatformChannelPair platform_channel_pair; | |
| 122 | |
| 123 // Give one end to the shell so that it can create an instance. | |
| 124 mojo::edk::ScopedPlatformHandle parent_pipe = | |
| 125 platform_channel_pair.PassServerHandle(); | |
| 126 | |
| 127 // Send the other end to the child via Chrome IPC. | |
| 128 base::PlatformFile client_file = PlatformFileFromScopedPlatformHandle( | |
| 129 platform_channel_pair.PassClientHandle()); | |
| 130 SetMojoPlatformFile(render_process_host, client_file); | |
| 131 | |
| 132 mojo::ScopedMessagePipeHandle request_pipe = | |
| 133 mojo::edk::CreateMessagePipe(std::move(parent_pipe)); | |
| 134 | 100 |
| 135 mojo::shell::mojom::ApplicationManagerPtr application_manager; | 101 mojo::shell::mojom::ApplicationManagerPtr application_manager; |
| 136 MojoShellConnection::Get()->GetShell()->ConnectToInterface( | 102 MojoShellConnection::Get()->GetShell()->ConnectToInterface( |
| 137 "mojo:shell", &application_manager); | 103 "mojo:shell", &application_manager); |
| 138 | 104 |
| 139 // The content of the URL/qualifier we pass is actually meaningless, it's only | 105 // The content of the URL/qualifier we pass is actually meaningless, it's only |
| 140 // important that they're unique per process. | 106 // important that they're unique per process. |
| 141 // TODO(beng): We need to specify a restrictive CapabilityFilter here that | 107 // TODO(beng): We need to specify a restrictive CapabilityFilter here that |
| 142 // matches the needs of the target process. Figure out where that | 108 // matches the needs of the target process. Figure out where that |
| 143 // specification is best determined (not here, this is a common | 109 // specification is best determined (not here, this is a common |
| 144 // chokepoint for all process types) and how to wire it through. | 110 // chokepoint for all process types) and how to wire it through. |
| 145 // http://crbug.com/555393 | 111 // http://crbug.com/555393 |
| 146 std::string url = | 112 std::string url = |
| 147 base::StringPrintf("exe:chrome_renderer%d", child_process_id); | 113 base::StringPrintf("exe:chrome_renderer%d", child_process_id); |
| 148 | 114 |
| 149 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 115 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
| 150 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 116 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
| 151 GetProxy(&pid_receiver); | 117 GetProxy(&pid_receiver); |
| 152 new PIDSender(render_process_host, std::move(pid_receiver)); | 118 new PIDSender(render_process_host, std::move(pid_receiver)); |
| 153 | 119 |
| 154 application_manager->CreateInstanceForHandle( | 120 application_manager->CreateInstanceForHandle( |
| 155 mojo::ScopedHandle(mojo::Handle(request_pipe.release().value())), | 121 mojo::ScopedHandle(mojo::Handle(request_pipe.release().value())), |
| 156 url, | 122 url, |
| 157 CreateCapabilityFilterForRenderer(), | 123 CreateCapabilityFilterForRenderer(), |
| 158 std::move(request)); | 124 std::move(request)); |
| 159 | 125 |
| 160 // Store the URL on the RPH so client code can access it later via | 126 // Store the URL on the RPH so client code can access it later via |
| 161 // GetMojoApplicationInstanceURL(). | 127 // GetMojoApplicationInstanceURL(). |
| 162 SetMojoApplicationInstanceURL(render_process_host, url); | 128 SetMojoApplicationInstanceURL(render_process_host, url); |
| 129 |
| 130 return pipe_token; |
| 163 } | 131 } |
| 164 | 132 |
| 165 std::string GetMojoApplicationInstanceURL( | 133 std::string GetMojoApplicationInstanceURL( |
| 166 RenderProcessHost* render_process_host) { | 134 RenderProcessHost* render_process_host) { |
| 167 InstanceURL* instance_url = static_cast<InstanceURL*>( | 135 InstanceURL* instance_url = static_cast<InstanceURL*>( |
| 168 render_process_host->GetUserData(kMojoShellInstanceURL)); | 136 render_process_host->GetUserData(kMojoShellInstanceURL)); |
| 169 return instance_url ? instance_url->get() : std::string(); | 137 return instance_url ? instance_url->get() : std::string(); |
| 170 } | 138 } |
| 171 | 139 |
| 172 void SendExternalMojoShellHandleToChild( | |
| 173 base::ProcessHandle process_handle, | |
| 174 RenderProcessHost* render_process_host) { | |
| 175 InstanceShellHandle* client_file = static_cast<InstanceShellHandle*>( | |
| 176 render_process_host->GetUserData(kMojoPlatformFile)); | |
| 177 if (!client_file) | |
| 178 return; | |
| 179 render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle( | |
| 180 IPC::GetFileHandleForProcess(client_file->get(), process_handle, true))); | |
| 181 } | |
| 182 | |
| 183 } // namespace content | 140 } // namespace content |
| OLD | NEW |