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

Side by Side Diff: ipc/mojo/ipc_mojo_bootstrap.cc

Issue 553283002: IPC::ChannelMojo: Introduce IPC::MojoBootstrap for Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added ChannelMojoHost Created 6 years, 3 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 2014 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 "ipc/mojo/ipc_mojo_bootstrap.h"
6
7 #include "base/logging.h"
8 #include "base/process/process_handle.h"
9 #include "ipc/ipc_message_utils.h"
10 #include "ipc/ipc_platform_file.h"
11 #include "mojo/embedder/platform_channel_pair.h"
12
13 namespace IPC {
14
15 namespace {
16
17 // MojoBootstrap for the server process. You should create the instance
18 // using MojoBootstrap::Create().
19 class IPC_MOJO_EXPORT MojoServerBootstrap : public MojoBootstrap {
20 public:
21 MojoServerBootstrap();
22
23 virtual void OnClientLaunched(base::ProcessHandle process) OVERRIDE;
24
25 private:
26 void SendClientPipe();
27 void SendClientPipeIfReady();
28
29 // Listener implementations
30 virtual bool OnMessageReceived(const Message& message) OVERRIDE;
31 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
32
33 mojo::embedder::ScopedPlatformHandle server_pipe_;
34 base::ProcessHandle client_process_;
35 bool connected_;
36
37 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap);
38 };
39
40 MojoServerBootstrap::MojoServerBootstrap()
41 : client_process_(base::kNullProcessHandle), connected_(false) {
42 }
43
44 void MojoServerBootstrap::SendClientPipe() {
45 DCHECK_EQ(state(), STATE_INITIALIZED);
46 DCHECK_NE(client_process_, base::kNullProcessHandle);
47 DCHECK(connected_);
48
49 mojo::embedder::PlatformChannelPair channel_pair;
50 server_pipe_ = channel_pair.PassServerHandle();
51 PlatformFileForTransit client_pipe = GetFileHandleForProcess(
52 channel_pair.PassClientHandle().release().ToPlaformFile(),
53 client_process_,
54 true);
55 CHECK(client_pipe != IPC::InvalidPlatformFileForTransit());
56 scoped_ptr<Message> message(new Message());
57 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe);
58 Send(message.release());
59
60 set_state(STATE_WAITING_ACK);
61 }
62
63 void MojoServerBootstrap::SendClientPipeIfReady() {
64 // Is the client launched?
65 if (client_process_ == base::kNullProcessHandle)
66 return;
67 // Has the bootstrap channel been made?
68 if (!connected_)
69 return;
70 SendClientPipe();
71 }
72
73 void MojoServerBootstrap::OnClientLaunched(base::ProcessHandle process) {
74 DCHECK_EQ(state(), STATE_INITIALIZED);
75 DCHECK_NE(process, base::kNullProcessHandle);
76 client_process_ = process;
77 SendClientPipeIfReady();
78 }
79
80 void MojoServerBootstrap::OnChannelConnected(int32 peer_pid) {
81 DCHECK(state() == STATE_INITIALIZED);
82 connected_ = true;
83 SendClientPipeIfReady();
84 }
85
86 bool MojoServerBootstrap::OnMessageReceived(const Message&) {
87 DCHECK(state() == STATE_WAITING_ACK);
88 set_state(STATE_READY);
89
90 delegate()->OnPipeAvailable(
91 mojo::embedder::ScopedPlatformHandle(server_pipe_.release()));
92
93 return true;
94 }
95
96 // MojoBootstrap for client processes. You should create the instance
97 // using MojoBootstrap::Create().
98 class IPC_MOJO_EXPORT MojoClientBootstrap : public MojoBootstrap {
99 public:
100 MojoClientBootstrap();
101
102 virtual void OnClientLaunched(base::ProcessHandle process) OVERRIDE;
103
104 private:
105 // Listener implementations
106 virtual bool OnMessageReceived(const Message& message) OVERRIDE;
107 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
108
109 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap);
110 };
111
112 MojoClientBootstrap::MojoClientBootstrap() {
113 }
114
115 bool MojoClientBootstrap::OnMessageReceived(const Message& message) {
116 PlatformFileForTransit pipe;
117 PickleIterator iter(message);
118 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) {
119 DLOG(WARNING) << "Failed to read a file handle from bootstrap channel.";
120 message.set_dispatch_error();
121 return false;
122 }
123
124 // Sends ACK back.
125 Send(new Message());
126 set_state(STATE_READY);
127 delegate()->OnPipeAvailable(
128 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle(
129 PlatformFileForTransitToPlatformFile(pipe))));
130
131 return true;
132 }
133
134 void MojoClientBootstrap::OnClientLaunched(base::ProcessHandle process) {
135 // This notification should happen only on server processes.
136 NOTREACHED();
137 }
138
139 void MojoClientBootstrap::OnChannelConnected(int32 peer_pid) {
140 }
141
142 } // namespace
143
144 // MojoBootstrap
145
146 // static
147 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle,
148 Channel::Mode mode,
149 Delegate* delegate) {
150 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER);
151 scoped_ptr<MojoBootstrap> self =
152 mode == Channel::MODE_CLIENT
153 ? scoped_ptr<MojoBootstrap>(new MojoClientBootstrap())
154 : scoped_ptr<MojoBootstrap>(new MojoServerBootstrap());
155 scoped_ptr<Channel> bootstrap_channel =
156 Channel::Create(handle, mode, self.get());
157 self->Init(bootstrap_channel.Pass(), delegate);
158 return self.Pass();
159 }
160
161 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) {
162 }
163
164 MojoBootstrap::~MojoBootstrap() {
165 }
166
167 void MojoBootstrap::Init(scoped_ptr<Channel> channel, Delegate* delegate) {
168 channel_ = channel.Pass();
169 delegate_ = delegate;
170 }
171
172 bool MojoBootstrap::Connect() {
173 return channel_->Connect();
174 }
175
176 void MojoBootstrap::OnBadMessageReceived(const Message& message) {
177 delegate_->OnBootstrapError();
178 }
179
180 void MojoBootstrap::OnChannelError() {
181 if (state_ == STATE_READY)
182 return;
183 DLOG(WARNING) << "Detected error on Mojo bootstrap channel.";
184 delegate()->OnBootstrapError();
185 }
186
187 bool MojoBootstrap::Send(Message* message) {
188 return channel_->Send(message);
189 }
190
191 #if defined(OS_POSIX) && !defined(OS_NACL)
192 int MojoBootstrap::GetClientFileDescriptor() const {
193 return channel_->GetClientFileDescriptor();
194 }
195
196 int MojoBootstrap::TakeClientFileDescriptor() {
197 return channel_->TakeClientFileDescriptor();
198 }
199 #endif // defined(OS_POSIX) && !defined(OS_NACL)
200
201 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698