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" |
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 #include "mojo/shell/public/interfaces/shell_client.mojom.h" |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 namespace { | 27 namespace { |
27 | 28 |
28 const char kMojoRenderProcessHostConnection[] = | 29 const char kMojoRenderProcessHostConnection[] = |
29 "mojo_render_process_host_connection"; | 30 "mojo_render_process_host_connection"; |
30 | 31 |
31 class RenderProcessHostConnection : public base::SupportsUserData::Data { | 32 class RenderProcessHostConnection : public base::SupportsUserData::Data { |
32 public: | 33 public: |
33 explicit RenderProcessHostConnection(scoped_ptr<mojo::Connection> connection) | 34 explicit RenderProcessHostConnection(scoped_ptr<mojo::Connection> connection) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 }; | 85 }; |
85 | 86 |
86 } // namespace | 87 } // namespace |
87 | 88 |
88 std::string MojoConnectToChild(int child_process_id, | 89 std::string MojoConnectToChild(int child_process_id, |
89 int instance_id, | 90 int instance_id, |
90 RenderProcessHost* render_process_host) { | 91 RenderProcessHost* render_process_host) { |
91 // Generate a token and create a pipe which is bound to it. This pipe is | 92 // Generate a token and create a pipe which is bound to it. This pipe is |
92 // passed to the shell if one is available. | 93 // passed to the shell if one is available. |
93 std::string pipe_token = mojo::edk::GenerateRandomToken(); | 94 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
94 mojo::ScopedMessagePipeHandle request_pipe = | 95 mojo::ScopedMessagePipeHandle shell_client_pipe = |
95 mojo::edk::CreateParentMessagePipe(pipe_token); | 96 mojo::edk::CreateParentMessagePipe(pipe_token); |
96 | 97 |
97 // 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 |
98 // 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 |
99 // this. | 100 // this. |
100 if (!MojoShellConnection::Get()) | 101 if (!MojoShellConnection::Get()) |
101 return pipe_token; | 102 return pipe_token; |
102 | 103 |
103 mojo::shell::mojom::ShellClientFactoryPtr factory; | 104 mojo::shell::mojom::ShellClientPtr client; |
104 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( | 105 client.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClient>( |
105 std::move(request_pipe), 0u)); | 106 std::move(shell_client_pipe), 0u)); |
106 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | 107 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
107 mojo::shell::mojom::PIDReceiverRequest pid_receiver_request = | 108 mojo::shell::mojom::PIDReceiverRequest pid_receiver_request = |
108 GetProxy(&pid_receiver); | 109 GetProxy(&pid_receiver); |
109 // PIDSender manages its own lifetime. | 110 // PIDSender manages its own lifetime. |
110 new PIDSender(render_process_host, std::move(pid_receiver)); | 111 new PIDSender(render_process_host, std::move(pid_receiver)); |
111 | 112 |
112 | 113 |
113 mojo::Identity target("exe:chrome_renderer", | 114 mojo::Identity target("exe:chrome_renderer", |
114 mojo::shell::mojom::kInheritUserID, | 115 mojo::shell::mojom::kInheritUserID, |
115 base::StringPrintf("%d_%d", child_process_id, | 116 base::StringPrintf("%d_%d", child_process_id, |
116 instance_id)); | 117 instance_id)); |
117 mojo::Connector::ConnectParams params(target); | 118 mojo::Connector::ConnectParams params(target); |
118 params.set_client_process_connection(std::move(factory), | 119 params.set_client_process_connection(std::move(client), |
119 std::move(pid_receiver_request)); | 120 std::move(pid_receiver_request)); |
120 scoped_ptr<mojo::Connection> connection = | 121 scoped_ptr<mojo::Connection> connection = |
121 MojoShellConnection::Get()->GetConnector()->Connect(¶ms); | 122 MojoShellConnection::Get()->GetConnector()->Connect(¶ms); |
122 | 123 |
123 // Store the connection on the RPH so client code can access it later via | 124 // Store the connection on the RPH so client code can access it later via |
124 // GetMojoConnection(). | 125 // GetMojoConnection(). |
125 SetMojoConnection(render_process_host, std::move(connection)); | 126 SetMojoConnection(render_process_host, std::move(connection)); |
126 | 127 |
127 return pipe_token; | 128 return pipe_token; |
128 } | 129 } |
129 | 130 |
130 mojo::Connection* GetMojoConnection(RenderProcessHost* render_process_host) { | 131 mojo::Connection* GetMojoConnection(RenderProcessHost* render_process_host) { |
131 RenderProcessHostConnection* connection = | 132 RenderProcessHostConnection* connection = |
132 static_cast<RenderProcessHostConnection*>( | 133 static_cast<RenderProcessHostConnection*>( |
133 render_process_host->GetUserData(kMojoRenderProcessHostConnection)); | 134 render_process_host->GetUserData(kMojoRenderProcessHostConnection)); |
134 return connection ? connection->get() : nullptr; | 135 return connection ? connection->get() : nullptr; |
135 } | 136 } |
136 | 137 |
137 } // namespace content | 138 } // namespace content |
OLD | NEW |