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

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

Issue 679453002: ChannelMojo: Replace hand written messsages with mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Landing (again) Created 6 years, 1 month 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
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_readers.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "ipc/mojo/ipc_channel_mojo.h" 5 #include "ipc/mojo/ipc_channel_mojo.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "ipc/ipc_listener.h" 10 #include "ipc/ipc_listener.h"
11 #include "ipc/mojo/client_channel.mojom.h"
11 #include "ipc/mojo/ipc_channel_mojo_readers.h" 12 #include "ipc/mojo/ipc_channel_mojo_readers.h"
12 #include "ipc/mojo/ipc_mojo_bootstrap.h" 13 #include "ipc/mojo/ipc_mojo_bootstrap.h"
13 #include "mojo/edk/embedder/embedder.h" 14 #include "mojo/edk/embedder/embedder.h"
15 #include "mojo/public/cpp/bindings/error_handler.h"
14 16
15 #if defined(OS_POSIX) && !defined(OS_NACL) 17 #if defined(OS_POSIX) && !defined(OS_NACL)
16 #include "ipc/file_descriptor_set_posix.h" 18 #include "ipc/file_descriptor_set_posix.h"
17 #endif 19 #endif
18 20
19 namespace IPC { 21 namespace IPC {
20 22
21 namespace { 23 namespace {
22 24
23 class MojoChannelFactory : public ChannelFactory { 25 class MojoChannelFactory : public ChannelFactory {
(...skipping 10 matching lines...) Expand all
34 scoped_ptr<Channel> BuildChannel(Listener* listener) override { 36 scoped_ptr<Channel> BuildChannel(Listener* listener) override {
35 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener); 37 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener);
36 } 38 }
37 39
38 private: 40 private:
39 ChannelMojo::Delegate* delegate_; 41 ChannelMojo::Delegate* delegate_;
40 ChannelHandle channel_handle_; 42 ChannelHandle channel_handle_;
41 Channel::Mode mode_; 43 Channel::Mode mode_;
42 }; 44 };
43 45
46 //------------------------------------------------------------------------------
47
48 class ClientChannelMojo
49 : public ChannelMojo,
50 public NON_EXPORTED_BASE(mojo::InterfaceImpl<ClientChannel>) {
51 public:
52 ClientChannelMojo(ChannelMojo::Delegate* delegate,
53 const ChannelHandle& handle,
54 Listener* listener);
55 ~ClientChannelMojo() override;
56 // MojoBootstrap::Delegate implementation
57 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
58 // InterfaceImpl implementation
59 void OnConnectionError() override;
60 // ClientChannel implementation
61 void Init(
62 mojo::ScopedMessagePipeHandle pipe,
63 int32_t peer_pid,
64 const mojo::Callback<void(int32_t)>& callback) override;
65
66 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo);
67 };
68
69 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate,
70 const ChannelHandle& handle,
71 Listener* listener)
72 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener) {
73 }
74
75 ClientChannelMojo::~ClientChannelMojo() {
76 }
77
78 void ClientChannelMojo::OnPipeAvailable(
79 mojo::embedder::ScopedPlatformHandle handle) {
80 mojo::WeakBindToPipe(this, CreateMessagingPipe(handle.Pass()));
81 }
82
83 void ClientChannelMojo::OnConnectionError() {
84 listener()->OnChannelError();
85 }
86
87 void ClientChannelMojo::Init(
88 mojo::ScopedMessagePipeHandle pipe,
89 int32_t peer_pid,
90 const mojo::Callback<void(int32_t)>& callback) {
91 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid));
92 callback.Run(GetSelfPID());
93 }
94
95 //------------------------------------------------------------------------------
96
97 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler {
98 public:
99 ServerChannelMojo(ChannelMojo::Delegate* delegate,
100 const ChannelHandle& handle,
101 Listener* listener);
102 ~ServerChannelMojo() override;
103
104 // MojoBootstrap::Delegate implementation
105 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
106 // ErrorHandler implementation
107 void OnConnectionError() override;
108 // Channel override
109 void Close() override;
110
111 private:
112 // ClientChannelClient implementation
113 void ClientChannelWasInitialized(int32_t peer_pid);
114
115 mojo::InterfacePtr<ClientChannel> client_channel_;
116 mojo::ScopedMessagePipeHandle message_pipe_;
117
118 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo);
119 };
120
121 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate,
122 const ChannelHandle& handle,
123 Listener* listener)
124 : ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) {
125 }
126
127 ServerChannelMojo::~ServerChannelMojo() {
128 Close();
129 }
130
131 void ServerChannelMojo::OnPipeAvailable(
132 mojo::embedder::ScopedPlatformHandle handle) {
133 mojo::ScopedMessagePipeHandle peer;
134 MojoResult create_result =
135 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer);
136 if (create_result != MOJO_RESULT_OK) {
137 DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
138 listener()->OnChannelError();
139 return;
140 }
141
142 client_channel_.Bind(CreateMessagingPipe(handle.Pass()));
143 client_channel_.set_error_handler(this);
144 client_channel_->Init(
145 peer.Pass(),
146 static_cast<int32_t>(GetSelfPID()),
147 base::Bind(&ServerChannelMojo::ClientChannelWasInitialized,
148 base::Unretained(this)));
149 }
150
151 void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) {
152 InitMessageReader(message_pipe_.Pass(), peer_pid);
153 }
154
155 void ServerChannelMojo::OnConnectionError() {
156 listener()->OnChannelError();
157 }
158
159 void ServerChannelMojo::Close() {
160 client_channel_.reset();
161 message_pipe_.reset();
162 ChannelMojo::Close();
163 }
164
44 } // namespace 165 } // namespace
45 166
46 //------------------------------------------------------------------------------ 167 //------------------------------------------------------------------------------
47 168
48 void ChannelMojo::ChannelInfoDeleter::operator()( 169 void ChannelMojo::ChannelInfoDeleter::operator()(
49 mojo::embedder::ChannelInfo* ptr) const { 170 mojo::embedder::ChannelInfo* ptr) const {
50 mojo::embedder::DestroyChannelOnIOThread(ptr); 171 mojo::embedder::DestroyChannelOnIOThread(ptr);
51 } 172 }
52 173
53 //------------------------------------------------------------------------------ 174 //------------------------------------------------------------------------------
54 175
55 // static 176 // static
56 bool ChannelMojo::ShouldBeUsed() { 177 bool ChannelMojo::ShouldBeUsed() {
57 // TODO(morrita): Turn this on for a set of platforms. 178 // TODO(morrita): Turn this on for a set of platforms.
58 return false; 179 return false;
59 } 180 }
60 181
61 // static 182 // static
62 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, 183 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate,
63 const ChannelHandle& channel_handle, 184 const ChannelHandle& channel_handle,
64 Mode mode, 185 Mode mode,
65 Listener* listener) { 186 Listener* listener) {
66 return make_scoped_ptr( 187 switch (mode) {
67 new ChannelMojo(delegate, channel_handle, mode, listener)); 188 case Channel::MODE_CLIENT:
189 return make_scoped_ptr(
190 new ClientChannelMojo(delegate, channel_handle, listener));
191 case Channel::MODE_SERVER:
192 return make_scoped_ptr(
193 new ServerChannelMojo(delegate, channel_handle, listener));
194 default:
195 NOTREACHED();
196 return nullptr;
197 }
68 } 198 }
69 199
70 // static 200 // static
71 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( 201 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
72 ChannelMojo::Delegate* delegate, 202 ChannelMojo::Delegate* delegate,
73 const ChannelHandle& channel_handle) { 203 const ChannelHandle& channel_handle) {
74 return make_scoped_ptr( 204 return make_scoped_ptr(
75 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); 205 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER));
76 } 206 }
77 207
(...skipping 30 matching lines...) Expand all
108 238
109 ChannelMojo::~ChannelMojo() { 239 ChannelMojo::~ChannelMojo() {
110 Close(); 240 Close();
111 } 241 }
112 242
113 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { 243 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) {
114 delegate_ = delegate->ToWeakPtr(); 244 delegate_ = delegate->ToWeakPtr();
115 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); 245 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr());
116 } 246 }
117 247
118 void ChannelMojo::InitControlReader( 248 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe(
119 mojo::embedder::ScopedPlatformHandle handle) { 249 mojo::embedder::ScopedPlatformHandle handle) {
120 DCHECK(base::MessageLoopForIO::IsCurrent()); 250 DCHECK(!channel_info_.get());
121 mojo::embedder::ChannelInfo* channel_info; 251 mojo::embedder::ChannelInfo* channel_info;
122 mojo::ScopedMessagePipeHandle control_pipe = 252 mojo::ScopedMessagePipeHandle pipe =
123 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); 253 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info);
124 channel_info_.reset(channel_info); 254 channel_info_.reset(channel_info);
125 255 return pipe.Pass();
126 switch (mode_) {
127 case MODE_SERVER:
128 control_reader_.reset(
129 new internal::ServerControlReader(control_pipe.Pass(), this));
130 break;
131 case MODE_CLIENT:
132 control_reader_.reset(
133 new internal::ClientControlReader(control_pipe.Pass(), this));
134 break;
135 default:
136 NOTREACHED();
137 break;
138 }
139 } 256 }
140 257
141 bool ChannelMojo::Connect() { 258 bool ChannelMojo::Connect() {
142 DCHECK(!message_reader_); 259 DCHECK(!message_reader_);
143 DCHECK(!control_reader_);
144 return bootstrap_->Connect(); 260 return bootstrap_->Connect();
145 } 261 }
146 262
147 void ChannelMojo::Close() { 263 void ChannelMojo::Close() {
148 control_reader_.reset();
149 message_reader_.reset(); 264 message_reader_.reset();
150 channel_info_.reset(); 265 channel_info_.reset();
151 } 266 }
152 267
153 void ChannelMojo::OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) {
154 InitControlReader(handle.Pass());
155 control_reader_->Connect();
156 }
157
158 void ChannelMojo::OnBootstrapError() { 268 void ChannelMojo::OnBootstrapError() {
159 listener_->OnChannelError(); 269 listener_->OnChannelError();
160 } 270 }
161 271
162 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) { 272 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe,
273 int32_t peer_pid) {
163 message_reader_ = 274 message_reader_ =
164 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); 275 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this));
165 276
166 for (size_t i = 0; i < pending_messages_.size(); ++i) { 277 for (size_t i = 0; i < pending_messages_.size(); ++i) {
167 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); 278 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i]));
168 pending_messages_[i] = NULL; 279 pending_messages_[i] = NULL;
169 if (!sent) { 280 if (!sent) {
170 pending_messages_.clear(); 281 pending_messages_.clear();
171 listener_->OnChannelError(); 282 listener_->OnChannelError();
172 return; 283 return;
173 } 284 }
174 } 285 }
175 286
176 pending_messages_.clear(); 287 pending_messages_.clear();
177 288
178 listener_->OnChannelConnected(GetPeerPID()); 289 set_peer_pid(peer_pid);
290 listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID()));
179 } 291 }
180 292
181 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { 293 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) {
182 Close(); 294 Close();
183 } 295 }
184 296
185 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { 297 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) {
186 listener_->OnChannelError(); 298 listener_->OnChannelError();
187 } 299 }
188 300
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 393
282 fdset->CommitAll(); 394 fdset->CommitAll();
283 } 395 }
284 396
285 return MOJO_RESULT_OK; 397 return MOJO_RESULT_OK;
286 } 398 }
287 399
288 #endif // defined(OS_POSIX) && !defined(OS_NACL) 400 #endif // defined(OS_POSIX) && !defined(OS_NACL)
289 401
290 } // namespace IPC 402 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_readers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698