| 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_child_connection.h" | 5 #include "content/browser/mojo/mojo_child_connection.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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const char kMojoRenderProcessHostConnection[] = | 29 const char kMojoRenderProcessHostConnection[] = |
| 30 "mojo_render_process_host_connection"; | 30 "mojo_render_process_host_connection"; |
| 31 | 31 |
| 32 class RenderProcessHostConnection : public base::SupportsUserData::Data { | 32 class RenderProcessHostConnection : public base::SupportsUserData::Data { |
| 33 public: | 33 public: |
| 34 explicit RenderProcessHostConnection( | 34 explicit RenderProcessHostConnection( |
| 35 std::unique_ptr<mojo::Connection> connection) | 35 std::unique_ptr<shell::Connection> connection) |
| 36 : connection_(std::move(connection)) {} | 36 : connection_(std::move(connection)) {} |
| 37 ~RenderProcessHostConnection() override {} | 37 ~RenderProcessHostConnection() override {} |
| 38 | 38 |
| 39 mojo::Connection* get() const { return connection_.get(); } | 39 shell::Connection* get() const { return connection_.get(); } |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 std::unique_ptr<mojo::Connection> connection_; | 42 std::unique_ptr<shell::Connection> connection_; |
| 43 | 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostConnection); | 44 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostConnection); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 void SetMojoConnection(RenderProcessHost* render_process_host, | 47 void SetMojoConnection(RenderProcessHost* render_process_host, |
| 48 std::unique_ptr<mojo::Connection> connection) { | 48 std::unique_ptr<shell::Connection> connection) { |
| 49 render_process_host->SetUserData( | 49 render_process_host->SetUserData( |
| 50 kMojoRenderProcessHostConnection, | 50 kMojoRenderProcessHostConnection, |
| 51 new RenderProcessHostConnection(std::move(connection))); | 51 new RenderProcessHostConnection(std::move(connection))); |
| 52 } | 52 } |
| 53 | 53 |
| 54 class PIDSender : public RenderProcessHostObserver { | 54 class PIDSender : public RenderProcessHostObserver { |
| 55 public: | 55 public: |
| 56 PIDSender( | 56 PIDSender(RenderProcessHost* host, shell::mojom::PIDReceiverPtr pid_receiver) |
| 57 RenderProcessHost* host, | 57 : host_(host), pid_receiver_(std::move(pid_receiver)) { |
| 58 mojo::shell::mojom::PIDReceiverPtr pid_receiver) | |
| 59 : host_(host), | |
| 60 pid_receiver_(std::move(pid_receiver)) { | |
| 61 pid_receiver_.set_connection_error_handler([this]() { delete this; }); | 58 pid_receiver_.set_connection_error_handler([this]() { delete this; }); |
| 62 DCHECK(!host_->IsReady()); | 59 DCHECK(!host_->IsReady()); |
| 63 host_->AddObserver(this); | 60 host_->AddObserver(this); |
| 64 } | 61 } |
| 65 ~PIDSender() override { | 62 ~PIDSender() override { |
| 66 if (host_) | 63 if (host_) |
| 67 host_->RemoveObserver(this); | 64 host_->RemoveObserver(this); |
| 68 } | 65 } |
| 69 | 66 |
| 70 private: | 67 private: |
| 71 // Overridden from RenderProcessHostObserver: | 68 // Overridden from RenderProcessHostObserver: |
| 72 void RenderProcessReady(RenderProcessHost* host) override { | 69 void RenderProcessReady(RenderProcessHost* host) override { |
| 73 pid_receiver_->SetPID(base::GetProcId(host->GetHandle())); | 70 pid_receiver_->SetPID(base::GetProcId(host->GetHandle())); |
| 74 delete this; | 71 delete this; |
| 75 } | 72 } |
| 76 | 73 |
| 77 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | 74 void RenderProcessHostDestroyed(RenderProcessHost* host) override { |
| 78 DCHECK_EQ(host_, host); | 75 DCHECK_EQ(host_, host); |
| 79 host_ = nullptr; | 76 host_ = nullptr; |
| 80 } | 77 } |
| 81 | 78 |
| 82 RenderProcessHost* host_; | 79 RenderProcessHost* host_; |
| 83 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; | 80 shell::mojom::PIDReceiverPtr pid_receiver_; |
| 84 | 81 |
| 85 DISALLOW_COPY_AND_ASSIGN(PIDSender); | 82 DISALLOW_COPY_AND_ASSIGN(PIDSender); |
| 86 }; | 83 }; |
| 87 | 84 |
| 88 } // namespace | 85 } // namespace |
| 89 | 86 |
| 90 std::string MojoConnectToChild(int child_process_id, | 87 std::string MojoConnectToChild(int child_process_id, |
| 91 int instance_id, | 88 int instance_id, |
| 92 RenderProcessHost* render_process_host) { | 89 RenderProcessHost* render_process_host) { |
| 93 // Generate a token and create a pipe which is bound to it. This pipe is | 90 // Generate a token and create a pipe which is bound to it. This pipe is |
| 94 // passed to the shell if one is available. | 91 // passed to the shell if one is available. |
| 95 std::string pipe_token = mojo::edk::GenerateRandomToken(); | 92 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
| 96 mojo::ScopedMessagePipeHandle shell_client_pipe = | 93 mojo::ScopedMessagePipeHandle shell_client_pipe = |
| 97 mojo::edk::CreateParentMessagePipe(pipe_token); | 94 mojo::edk::CreateParentMessagePipe(pipe_token); |
| 98 | 95 |
| 99 // Some process types get created before the main message loop. In this case | 96 // Some process types get created before the main message loop. In this case |
| 100 // the shell request pipe will simply be closed, and the child can detect | 97 // the shell request pipe will simply be closed, and the child can detect |
| 101 // this. | 98 // this. |
| 102 if (!MojoShellConnection::Get()) | 99 if (!MojoShellConnection::Get()) |
| 103 return pipe_token; | 100 return pipe_token; |
| 104 | 101 |
| 105 mojo::shell::mojom::ShellClientPtr client; | 102 shell::mojom::ShellClientPtr client; |
| 106 client.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClient>( | 103 client.Bind(mojo::InterfacePtrInfo<shell::mojom::ShellClient>( |
| 107 std::move(shell_client_pipe), 0u)); | 104 std::move(shell_client_pipe), 0u)); |
| 108 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 105 shell::mojom::PIDReceiverPtr pid_receiver; |
| 109 mojo::shell::mojom::PIDReceiverRequest pid_receiver_request = | 106 shell::mojom::PIDReceiverRequest pid_receiver_request = |
| 110 GetProxy(&pid_receiver); | 107 GetProxy(&pid_receiver); |
| 111 // PIDSender manages its own lifetime. | 108 // PIDSender manages its own lifetime. |
| 112 new PIDSender(render_process_host, std::move(pid_receiver)); | 109 new PIDSender(render_process_host, std::move(pid_receiver)); |
| 113 | 110 |
| 114 mojo::Identity target(kRendererMojoApplicationName, | 111 shell::Identity target( |
| 115 mojo::shell::mojom::kInheritUserID, | 112 kRendererMojoApplicationName, shell::mojom::kInheritUserID, |
| 116 base::StringPrintf("%d_%d", child_process_id, | 113 base::StringPrintf("%d_%d", child_process_id, instance_id)); |
| 117 instance_id)); | 114 shell::Connector::ConnectParams params(target); |
| 118 mojo::Connector::ConnectParams params(target); | |
| 119 params.set_client_process_connection(std::move(client), | 115 params.set_client_process_connection(std::move(client), |
| 120 std::move(pid_receiver_request)); | 116 std::move(pid_receiver_request)); |
| 121 std::unique_ptr<mojo::Connection> connection = | 117 std::unique_ptr<shell::Connection> connection = |
| 122 MojoShellConnection::Get()->GetConnector()->Connect(¶ms); | 118 MojoShellConnection::Get()->GetConnector()->Connect(¶ms); |
| 123 | 119 |
| 124 // Store the connection on the RPH so client code can access it later via | 120 // Store the connection on the RPH so client code can access it later via |
| 125 // GetMojoConnection(). | 121 // GetMojoConnection(). |
| 126 SetMojoConnection(render_process_host, std::move(connection)); | 122 SetMojoConnection(render_process_host, std::move(connection)); |
| 127 | 123 |
| 128 return pipe_token; | 124 return pipe_token; |
| 129 } | 125 } |
| 130 | 126 |
| 131 mojo::Connection* GetMojoConnection(RenderProcessHost* render_process_host) { | 127 shell::Connection* GetMojoConnection(RenderProcessHost* render_process_host) { |
| 132 RenderProcessHostConnection* connection = | 128 RenderProcessHostConnection* connection = |
| 133 static_cast<RenderProcessHostConnection*>( | 129 static_cast<RenderProcessHostConnection*>( |
| 134 render_process_host->GetUserData(kMojoRenderProcessHostConnection)); | 130 render_process_host->GetUserData(kMojoRenderProcessHostConnection)); |
| 135 return connection ? connection->get() : nullptr; | 131 return connection ? connection->get() : nullptr; |
| 136 } | 132 } |
| 137 | 133 |
| 138 } // namespace content | 134 } // namespace content |
| OLD | NEW |