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

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: 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()
viettrungluu 2014/09/15 22:37:28 nit: Needs a period at the end.
Hajime Morrita 2014/09/15 23:51:33 Done.
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 // MojoBootstrap for client processes. You should create the instance
41 // using MojoBootstrap::Create()
viettrungluu 2014/09/15 22:37:27 "
Hajime Morrita 2014/09/15 23:51:33 Done.
42 class IPC_MOJO_EXPORT MojoClientBootstrap : public MojoBootstrap {
43 public:
44 MojoClientBootstrap();
45
46 virtual void OnClientLaunched(base::ProcessHandle process) OVERRIDE;
47
48 private:
49 // Listener implementations
50 virtual bool OnMessageReceived(const Message& message) OVERRIDE;
51 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
52
53 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap);
54 };
55
56 } // namespace
57
58 // MojoBootstrap
59
60 // static
61 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle,
62 Channel::Mode mode,
63 Delegate* delegate) {
64 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER);
65 scoped_ptr<MojoBootstrap> self =
66 mode == Channel::MODE_CLIENT
67 ? scoped_ptr<MojoBootstrap>(new MojoClientBootstrap())
68 : scoped_ptr<MojoBootstrap>(new MojoServerBootstrap());
69 scoped_ptr<Channel> bootstrap_channel =
70 Channel::Create(handle, mode, self.get());
71 self->Init(bootstrap_channel.Pass(), delegate);
72 return self.Pass();
73 }
74
75 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) {
76 }
77
78 MojoBootstrap::~MojoBootstrap() {
79 }
80
81 void MojoBootstrap::Init(scoped_ptr<Channel> channel, Delegate* delegate) {
82 channel_ = channel.Pass();
83 delegate_ = delegate;
84 }
85
86 bool MojoBootstrap::Connect() {
87 return channel_->Connect();
88 }
89
90 void MojoBootstrap::OnBadMessageReceived(const Message& message) {
91 delegate_->OnBootstrapError();
92 }
93
94 void MojoBootstrap::OnChannelError() {
95 if (state_ == STATE_READY)
96 return;
97 DLOG(WARNING) << "Detected error on Mojo bootstrap channel.";
98 delegate()->OnBootstrapError();
99 }
100
101 bool MojoBootstrap::Send(Message* message) {
102 return channel_->Send(message);
103 }
104
105 #if defined(OS_POSIX) && !defined(OS_NACL)
106 int MojoBootstrap::GetClientFileDescriptor() const {
107 return channel_->GetClientFileDescriptor();
108 }
109
110 int MojoBootstrap::TakeClientFileDescriptor() {
111 return channel_->TakeClientFileDescriptor();
112 }
113 #endif // defined(OS_POSIX) && !defined(OS_NACL)
114
115 // MojoServerBootstrap
116
117 MojoServerBootstrap::MojoServerBootstrap()
viettrungluu 2014/09/15 22:37:28 Apparently, it's legal to define stuff that's decl
Hajime Morrita 2014/09/15 23:51:33 Ouch. This is indeed weird. Surprised that the com
118 : client_process_(base::kNullProcessHandle), connected_(false) {
119 }
120
121 void MojoServerBootstrap::SendClientPipe() {
122 DCHECK_EQ(state(), STATE_INITIALIZED);
123 DCHECK_NE(client_process_, base::kNullProcessHandle);
124 DCHECK(connected_);
125
126 mojo::embedder::PlatformChannelPair channel_pair;
127 server_pipe_ = channel_pair.PassServerHandle();
128 PlatformFileForTransit client_pipe = GetFileHandleForProcess(
129 channel_pair.PassClientHandle().release().ToPlaformFile(),
130 client_process_,
131 true);
132 CHECK(client_pipe != IPC::InvalidPlatformFileForTransit());
133 scoped_ptr<Message> message(new Message());
134 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe);
135 Send(message.release());
136
137 set_state(STATE_WAITING_ACK);
138 }
139
140 void MojoServerBootstrap::SendClientPipeIfReady() {
141 // Is the client launched?
142 if (client_process_ == base::kNullProcessHandle)
143 return;
144 // Has the bootstrap channel been made?
145 if (!connected_)
146 return;
147
148 SendClientPipe();
149 }
150
151 void MojoServerBootstrap::OnClientLaunched(base::ProcessHandle process) {
152 DCHECK_EQ(state(), STATE_INITIALIZED);
153 DCHECK_NE(process, base::kNullProcessHandle);
154 client_process_ = process;
155 SendClientPipeIfReady();
156 }
157
158 void MojoServerBootstrap::OnChannelConnected(int32 peer_pid) {
159 DCHECK(state() == STATE_INITIALIZED);
160 connected_ = true;
161 SendClientPipeIfReady();
162 }
163
164 bool MojoServerBootstrap::OnMessageReceived(const Message&) {
165 DCHECK(state() == STATE_WAITING_ACK);
166 set_state(STATE_READY);
167
168 delegate()->OnPipeAvailable(
169 mojo::embedder::ScopedPlatformHandle(server_pipe_.release()));
170
171 return true;
172 }
173
174 // MojoClientBootstrap
175
176 MojoClientBootstrap::MojoClientBootstrap() {
177 }
178
179 bool MojoClientBootstrap::OnMessageReceived(const Message& message) {
180 PlatformFileForTransit pipe;
181 PickleIterator iter(message);
182 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) {
183 DLOG(WARNING) << "Failed to read a file handle from bootstrap channel.";
184 message.set_dispatch_error();
185 return false;
186 }
187
188 // Sends ACK back.
189 Send(new Message());
190 set_state(STATE_READY);
191 delegate()->OnPipeAvailable(
192 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle(
193 PlatformFileForTransitToPlatformFile(pipe))));
194
195 return true;
196 }
197
198 void MojoClientBootstrap::OnClientLaunched(base::ProcessHandle process) {
199 // This notification should happen only on server processes.
200 NOTREACHED();
201 }
202
203 void MojoClientBootstrap::OnChannelConnected(int32 peer_pid) {
204 }
205
206 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698