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_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 | 24 |
25 namespace content { | 25 namespace content { |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kMojoShellInstanceIdentity[] = "mojo_shell_instance_identity"; | 28 const char kMojoRenderProcessHostConnection[] = |
| 29 "mojo_render_process_host_connection"; |
29 | 30 |
30 class InstanceIdentity : public base::SupportsUserData::Data { | 31 class RenderProcessHostConnection : public base::SupportsUserData::Data { |
31 public: | 32 public: |
32 explicit InstanceIdentity(const mojo::Identity& identity) | 33 explicit RenderProcessHostConnection(scoped_ptr<mojo::Connection> connection) |
33 : identity_(identity) {} | 34 : connection_(std::move(connection)) {} |
34 ~InstanceIdentity() override {} | 35 ~RenderProcessHostConnection() override {} |
35 | 36 |
36 mojo::Identity get() const { return identity_; } | 37 mojo::Connection* get() const { return connection_.get(); } |
37 | 38 |
38 private: | 39 private: |
39 mojo::Identity identity_; | 40 scoped_ptr<mojo::Connection> connection_; |
40 | 41 |
41 DISALLOW_COPY_AND_ASSIGN(InstanceIdentity); | 42 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostConnection); |
42 }; | 43 }; |
43 | 44 |
44 void SetMojoIdentity(RenderProcessHost* render_process_host, | 45 void SetMojoConnection(RenderProcessHost* render_process_host, |
45 const mojo::Identity& identity) { | 46 scoped_ptr<mojo::Connection> connection) { |
46 render_process_host->SetUserData(kMojoShellInstanceIdentity, | 47 render_process_host->SetUserData( |
47 new InstanceIdentity(identity)); | 48 kMojoRenderProcessHostConnection, |
| 49 new RenderProcessHostConnection(std::move(connection))); |
48 } | 50 } |
49 | 51 |
50 class PIDSender : public RenderProcessHostObserver { | 52 class PIDSender : public RenderProcessHostObserver { |
51 public: | 53 public: |
52 PIDSender( | 54 PIDSender( |
53 RenderProcessHost* host, | 55 RenderProcessHost* host, |
54 mojo::shell::mojom::PIDReceiverPtr pid_receiver) | 56 mojo::shell::mojom::PIDReceiverPtr pid_receiver) |
55 : host_(host), | 57 : host_(host), |
56 pid_receiver_(std::move(pid_receiver)) { | 58 pid_receiver_(std::move(pid_receiver)) { |
57 pid_receiver_.set_connection_error_handler([this]() { delete this; }); | 59 pid_receiver_.set_connection_error_handler([this]() { delete this; }); |
(...skipping 16 matching lines...) Expand all Loading... |
74 DCHECK_EQ(host_, host); | 76 DCHECK_EQ(host_, host); |
75 host_ = nullptr; | 77 host_ = nullptr; |
76 } | 78 } |
77 | 79 |
78 RenderProcessHost* host_; | 80 RenderProcessHost* host_; |
79 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; | 81 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; |
80 | 82 |
81 DISALLOW_COPY_AND_ASSIGN(PIDSender); | 83 DISALLOW_COPY_AND_ASSIGN(PIDSender); |
82 }; | 84 }; |
83 | 85 |
84 void OnConnectionComplete(mojo::shell::mojom::ConnectResult result) {} | |
85 | |
86 } // namespace | 86 } // namespace |
87 | 87 |
88 std::string RegisterChildWithExternalShell( | 88 std::string MojoConnectToChild(int child_process_id, |
89 int child_process_id, | 89 int instance_id, |
90 int instance_id, | 90 RenderProcessHost* render_process_host) { |
91 RenderProcessHost* render_process_host) { | |
92 // Generate a token and create a pipe which is bound to it. This pipe is | 91 // Generate a token and create a pipe which is bound to it. This pipe is |
93 // passed to the shell if one is available. | 92 // passed to the shell if one is available. |
94 std::string pipe_token = mojo::edk::GenerateRandomToken(); | 93 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
95 mojo::ScopedMessagePipeHandle request_pipe = | 94 mojo::ScopedMessagePipeHandle request_pipe = |
96 mojo::edk::CreateParentMessagePipe(pipe_token); | 95 mojo::edk::CreateParentMessagePipe(pipe_token); |
97 | 96 |
98 // Some process types get created before the main message loop. In this case | 97 // 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 | 98 // the shell request pipe will simply be closed, and the child can detect |
100 // this. | 99 // this. |
101 if (!MojoShellConnection::Get()) | 100 if (!MojoShellConnection::Get()) |
102 return pipe_token; | 101 return pipe_token; |
103 | 102 |
104 mojo::shell::mojom::ShellPtr shell; | |
105 MojoShellConnection::Get()->GetConnector()->ConnectToInterface( | |
106 "mojo:shell", &shell); | |
107 | |
108 mojo::shell::mojom::PIDReceiverPtr pid_receiver; | |
109 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | |
110 GetProxy(&pid_receiver); | |
111 new PIDSender(render_process_host, std::move(pid_receiver)); | |
112 | |
113 mojo::shell::mojom::ShellClientFactoryPtr factory; | 103 mojo::shell::mojom::ShellClientFactoryPtr factory; |
114 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( | 104 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( |
115 std::move(request_pipe), 0u)); | 105 std::move(request_pipe), 0u)); |
| 106 mojo::shell::mojom::PIDReceiverPtr pid_receiver; |
| 107 mojo::shell::mojom::PIDReceiverRequest pid_receiver_request = |
| 108 GetProxy(&pid_receiver); |
| 109 // PIDSender manages its own lifetime. |
| 110 new PIDSender(render_process_host, std::move(pid_receiver)); |
| 111 |
116 | 112 |
117 mojo::Identity target("exe:chrome_renderer", | 113 mojo::Identity target("exe:chrome_renderer", |
118 mojo::shell::mojom::kInheritUserID, | 114 mojo::shell::mojom::kInheritUserID, |
119 base::StringPrintf("%d_%d", child_process_id, | 115 base::StringPrintf("%d_%d", child_process_id, |
120 instance_id)); | 116 instance_id)); |
121 shell->CreateInstance(std::move(factory), | 117 mojo::Connector::ConnectParams params(target); |
122 mojo::shell::mojom::Identity::From(target), | 118 params.set_client_process_connection(std::move(factory), |
123 std::move(request), base::Bind(&OnConnectionComplete)); | 119 std::move(pid_receiver_request)); |
| 120 scoped_ptr<mojo::Connection> connection = |
| 121 MojoShellConnection::Get()->GetConnector()->Connect(¶ms); |
124 | 122 |
125 // Store the Identity on the RPH so client code can access it later via | 123 // Store the connection on the RPH so client code can access it later via |
126 // GetMojoIdentity(). | 124 // GetMojoConnection(). |
127 SetMojoIdentity(render_process_host, target); | 125 SetMojoConnection(render_process_host, std::move(connection)); |
128 | 126 |
129 return pipe_token; | 127 return pipe_token; |
130 } | 128 } |
131 | 129 |
132 mojo::Identity GetMojoIdentity(RenderProcessHost* render_process_host) { | 130 mojo::Connection* GetMojoConnection(RenderProcessHost* render_process_host) { |
133 InstanceIdentity* instance_identity = static_cast<InstanceIdentity*>( | 131 RenderProcessHostConnection* connection = |
134 render_process_host->GetUserData(kMojoShellInstanceIdentity)); | 132 static_cast<RenderProcessHostConnection*>( |
135 return instance_identity ? instance_identity->get() : mojo::Identity(); | 133 render_process_host->GetUserData(kMojoRenderProcessHostConnection)); |
| 134 return connection ? connection->get() : nullptr; |
136 } | 135 } |
137 | 136 |
138 } // namespace content | 137 } // namespace content |
OLD | NEW |