Chromium Code Reviews| 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/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/render_process_host_observer.h" | 16 #include "content/public/browser/render_process_host_observer.h" |
| 17 #include "content/public/common/mojo_shell_connection.h" | 17 #include "content/public/common/mojo_shell_connection.h" |
| 18 #include "ipc/ipc_sender.h" | 18 #include "ipc/ipc_sender.h" |
| 19 #include "mojo/converters/network/network_type_converters.h" | 19 #include "mojo/converters/network/network_type_converters.h" |
| 20 #include "mojo/edk/embedder/embedder.h" | 20 #include "mojo/edk/embedder/embedder.h" |
| 21 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
| 22 #include "mojo/shell/public/cpp/connector.h" | 22 #include "mojo/shell/public/cpp/connector.h" |
| 23 #include "mojo/shell/public/interfaces/shell.mojom.h" | 23 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kMojoShellInstanceURL[] = "mojo_shell_instance_url"; | 28 const char kMojoShellInstanceIdentity[] = "mojo_shell_instance_identity"; |
| 29 | 29 |
| 30 class InstanceURL : public base::SupportsUserData::Data { | 30 class InstanceIdentity : public base::SupportsUserData::Data { |
| 31 public: | 31 public: |
| 32 explicit InstanceURL(const std::string& instance_url) | 32 explicit InstanceIdentity(const mojo::Identity& identity) |
| 33 : instance_url_(instance_url) {} | 33 : identity_(identity) {} |
| 34 ~InstanceURL() override {} | 34 ~InstanceIdentity() override {} |
| 35 | 35 |
| 36 std::string get() const { return instance_url_; } | 36 mojo::Identity get() const { return identity_; } |
|
sky
2016/03/08 17:22:59
const mojo::Identity& ?
| |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 std::string instance_url_; | 39 mojo::Identity identity_; |
| 40 | 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(InstanceURL); | 41 DISALLOW_COPY_AND_ASSIGN(InstanceIdentity); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 void SetMojoApplicationInstanceURL(RenderProcessHost* render_process_host, | 44 void SetMojoIdentity(RenderProcessHost* render_process_host, |
| 45 const std::string& instance_url) { | 45 const mojo::Identity& identity) { |
| 46 render_process_host->SetUserData(kMojoShellInstanceURL, | 46 render_process_host->SetUserData(kMojoShellInstanceIdentity, |
| 47 new InstanceURL(instance_url)); | 47 new InstanceIdentity(identity)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 class PIDSender : public RenderProcessHostObserver { | 50 class PIDSender : public RenderProcessHostObserver { |
| 51 public: | 51 public: |
| 52 PIDSender( | 52 PIDSender( |
| 53 RenderProcessHost* host, | 53 RenderProcessHost* host, |
| 54 mojo::shell::mojom::PIDReceiverPtr pid_receiver) | 54 mojo::shell::mojom::PIDReceiverPtr pid_receiver) |
| 55 : host_(host), | 55 : host_(host), |
| 56 pid_receiver_(std::move(pid_receiver)) { | 56 pid_receiver_(std::move(pid_receiver)) { |
| 57 pid_receiver_.set_connection_error_handler([this]() { delete this; }); | 57 pid_receiver_.set_connection_error_handler([this]() { delete this; }); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 // Some process types get created before the main message loop. In this case | 98 // Some process types get created before the main message loop. In this case |
| 99 // the shell request pipe will simply be closed, and the child can detect | 99 // the shell request pipe will simply be closed, and the child can detect |
| 100 // this. | 100 // this. |
| 101 if (!MojoShellConnection::Get()) | 101 if (!MojoShellConnection::Get()) |
| 102 return pipe_token; | 102 return pipe_token; |
| 103 | 103 |
| 104 mojo::shell::mojom::ShellPtr shell; | 104 mojo::shell::mojom::ShellPtr shell; |
| 105 MojoShellConnection::Get()->GetConnector()->ConnectToInterface( | 105 MojoShellConnection::Get()->GetConnector()->ConnectToInterface( |
| 106 "mojo:shell", &shell); | 106 "mojo:shell", &shell); |
| 107 | 107 |
| 108 // The content of the URL/qualifier we pass is actually meaningless, it's only | |
| 109 // important that they're unique per process. | |
| 110 // TODO(beng): We need to specify a restrictive CapabilityFilter here that | |
| 111 // matches the needs of the target process. Figure out where that | |
| 112 // specification is best determined (not here, this is a common | |
| 113 // chokepoint for all process types) and how to wire it through. | |
| 114 // http://crbug.com/555393 | |
| 115 std::string url = base::StringPrintf( | |
| 116 "exe:chrome_renderer%d_%d", child_process_id, instance_id); | |
| 117 | |
| 118 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 108 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
| 119 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | 109 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = |
| 120 GetProxy(&pid_receiver); | 110 GetProxy(&pid_receiver); |
| 121 new PIDSender(render_process_host, std::move(pid_receiver)); | 111 new PIDSender(render_process_host, std::move(pid_receiver)); |
| 122 | 112 |
| 123 mojo::shell::mojom::ShellClientFactoryPtr factory; | 113 mojo::shell::mojom::ShellClientFactoryPtr factory; |
| 124 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( | 114 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( |
| 125 std::move(request_pipe), 0u)); | 115 std::move(request_pipe), 0u)); |
| 126 | 116 |
| 127 mojo::shell::mojom::IdentityPtr target(mojo::shell::mojom::Identity::New()); | 117 mojo::Identity target("exe:chrome_renderer", |
| 128 target->name = url; | 118 mojo::shell::mojom::kInheritUserID, |
| 129 target->user_id = mojo::shell::mojom::kInheritUserID; | 119 base::StringPrintf("%d_%d", child_process_id, |
| 130 target->instance = ""; | 120 instance_id)); |
| 131 shell->CreateInstance(std::move(factory), std::move(target), | 121 shell->CreateInstance(std::move(factory), |
| 122 mojo::shell::mojom::Identity::From(target), | |
| 132 CreateCapabilityFilterForRenderer(), | 123 CreateCapabilityFilterForRenderer(), |
| 133 std::move(request), base::Bind(&OnConnectionComplete)); | 124 std::move(request), base::Bind(&OnConnectionComplete)); |
| 134 | 125 |
| 135 // Store the URL on the RPH so client code can access it later via | 126 // Store the Identity on the RPH so client code can access it later via |
| 136 // GetMojoApplicationInstanceURL(). | 127 // GetMojoIdentity(). |
| 137 SetMojoApplicationInstanceURL(render_process_host, url); | 128 SetMojoIdentity(render_process_host, target); |
| 138 | 129 |
| 139 return pipe_token; | 130 return pipe_token; |
| 140 } | 131 } |
| 141 | 132 |
| 142 std::string GetMojoApplicationInstanceURL( | 133 mojo::Identity GetMojoIdentity(RenderProcessHost* render_process_host) { |
| 143 RenderProcessHost* render_process_host) { | 134 InstanceIdentity* instance_identity = static_cast<InstanceIdentity*>( |
| 144 InstanceURL* instance_url = static_cast<InstanceURL*>( | 135 render_process_host->GetUserData(kMojoShellInstanceIdentity)); |
| 145 render_process_host->GetUserData(kMojoShellInstanceURL)); | 136 return instance_identity ? instance_identity->get() : mojo::Identity(); |
| 146 return instance_url ? instance_url->get() : std::string(); | |
| 147 } | 137 } |
| 148 | 138 |
| 149 } // namespace content | 139 } // namespace content |
| OLD | NEW |