Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: content/browser/mojo/mojo_shell_client_host.cc

Issue 1776513003: Allow client process information to be passed via Connector::Connect(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/mojo/mojo_shell_client_host.h"
6
7 #include <stdint.h>
8 #include <utility>
9
10 #include "base/macros.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "build/build_config.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_process_host_observer.h"
17 #include "content/public/common/mojo_shell_connection.h"
18 #include "ipc/ipc_sender.h"
19 #include "mojo/converters/network/network_type_converters.h"
20 #include "mojo/edk/embedder/embedder.h"
21 #include "mojo/public/cpp/system/message_pipe.h"
22 #include "mojo/shell/public/cpp/connector.h"
23 #include "mojo/shell/public/interfaces/shell.mojom.h"
24
25 namespace content {
26 namespace {
27
28 const char kMojoShellInstanceIdentity[] = "mojo_shell_instance_identity";
29
30 class InstanceIdentity : public base::SupportsUserData::Data {
31 public:
32 explicit InstanceIdentity(const mojo::Identity& identity)
33 : identity_(identity) {}
34 ~InstanceIdentity() override {}
35
36 mojo::Identity get() const { return identity_; }
37
38 private:
39 mojo::Identity identity_;
40
41 DISALLOW_COPY_AND_ASSIGN(InstanceIdentity);
42 };
43
44 void SetMojoIdentity(RenderProcessHost* render_process_host,
45 const mojo::Identity& identity) {
46 render_process_host->SetUserData(kMojoShellInstanceIdentity,
47 new InstanceIdentity(identity));
48 }
49
50 class PIDSender : public RenderProcessHostObserver {
51 public:
52 PIDSender(
53 RenderProcessHost* host,
54 mojo::shell::mojom::PIDReceiverPtr pid_receiver)
55 : host_(host),
56 pid_receiver_(std::move(pid_receiver)) {
57 pid_receiver_.set_connection_error_handler([this]() { delete this; });
58 DCHECK(!host_->IsReady());
59 host_->AddObserver(this);
60 }
61 ~PIDSender() override {
62 if (host_)
63 host_->RemoveObserver(this);
64 }
65
66 private:
67 // Overridden from RenderProcessHostObserver:
68 void RenderProcessReady(RenderProcessHost* host) override {
69 pid_receiver_->SetPID(base::GetProcId(host->GetHandle()));
70 delete this;
71 }
72
73 void RenderProcessHostDestroyed(RenderProcessHost* host) override {
74 DCHECK_EQ(host_, host);
75 host_ = nullptr;
76 }
77
78 RenderProcessHost* host_;
79 mojo::shell::mojom::PIDReceiverPtr pid_receiver_;
80
81 DISALLOW_COPY_AND_ASSIGN(PIDSender);
82 };
83
84 void OnConnectionComplete(mojo::shell::mojom::ConnectResult result) {}
85
86 } // namespace
87
88 std::string RegisterChildWithExternalShell(
89 int child_process_id,
90 int instance_id,
91 RenderProcessHost* render_process_host) {
92 // Generate a token and create a pipe which is bound to it. This pipe is
93 // passed to the shell if one is available.
94 std::string pipe_token = mojo::edk::GenerateRandomToken();
95 mojo::ScopedMessagePipeHandle request_pipe =
96 mojo::edk::CreateParentMessagePipe(pipe_token);
97
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
100 // this.
101 if (!MojoShellConnection::Get())
102 return pipe_token;
103
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;
114 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>(
115 std::move(request_pipe), 0u));
116
117 mojo::Identity target("exe:chrome_renderer",
118 mojo::shell::mojom::kInheritUserID,
119 base::StringPrintf("%d_%d", child_process_id,
120 instance_id));
121 shell->CreateInstance(std::move(factory),
122 mojo::shell::mojom::Identity::From(target),
123 std::move(request), base::Bind(&OnConnectionComplete));
124
125 // Store the Identity on the RPH so client code can access it later via
126 // GetMojoIdentity().
127 SetMojoIdentity(render_process_host, target);
128
129 return pipe_token;
130 }
131
132 mojo::Identity GetMojoIdentity(RenderProcessHost* render_process_host) {
133 InstanceIdentity* instance_identity = static_cast<InstanceIdentity*>(
134 render_process_host->GetUserData(kMojoShellInstanceIdentity));
135 return instance_identity ? instance_identity->get() : mojo::Identity();
136 }
137
138 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/mojo/mojo_shell_client_host.h ('k') | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698