OLD | NEW |
---|---|
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 Loading... | |
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 | |
56 // MojoBootstrap::Delegate implementation | |
57 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | |
58 // InterfaceImpl implementation | |
59 void OnConnectionError() override; | |
60 // ClientChannel implementation | |
61 void InitClientChannel( | |
62 mojo::ScopedMessagePipeHandle pipe, | |
63 int32_t peer_pid, | |
64 const mojo::Callback<void(int32_t)>& callback) override; | |
65 }; | |
viettrungluu
2014/10/24 20:17:00
nit: DISALLOW_COPY_AND_ASSIGN
Hajime Morrita
2014/10/24 21:05:37
Done.
| |
66 | |
67 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, | |
68 const ChannelHandle& handle, | |
69 Listener* listener) | |
70 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener) { | |
71 } | |
72 | |
73 void ClientChannelMojo::OnPipeAvailable( | |
74 mojo::embedder::ScopedPlatformHandle handle) { | |
75 mojo::WeakBindToPipe(this, CreateMessagingPipe(handle.Pass())); | |
76 } | |
77 | |
78 void ClientChannelMojo::OnConnectionError() { | |
79 listener()->OnChannelError(); | |
80 } | |
81 | |
82 void ClientChannelMojo::InitClientChannel( | |
83 mojo::ScopedMessagePipeHandle pipe, | |
84 int32_t peer_pid, | |
85 const mojo::Callback<void(int32_t)>& callback) { | |
86 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); | |
87 callback.Run(GetSelfPID()); | |
viettrungluu
2014/10/24 20:17:00
static_cast<int32>, like you have below?
This is
Hajime Morrita
2014/10/24 21:05:37
Turned it to uint32.
viettrungluu
2014/10/24 22:48:05
That just gets you into trouble when you do |stati
| |
88 } | |
89 | |
90 //------------------------------------------------------------------------------ | |
91 | |
92 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { | |
93 public: | |
94 ServerChannelMojo(ChannelMojo::Delegate* delegate, | |
95 const ChannelHandle& handle, | |
96 Listener* listener); | |
97 virtual ~ServerChannelMojo(); | |
viettrungluu
2014/10/24 20:17:00
Should be marked "override" instead of "virtual".
Hajime Morrita
2014/10/24 21:05:37
Done.
| |
98 | |
99 // MojoBootstrap::Delegate implementation | |
100 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | |
101 // ErrorHandler implementation | |
102 void OnConnectionError() override; | |
103 // Channel override | |
104 void Close() override; | |
105 | |
106 private: | |
107 // ClientChannelClient implementation | |
108 void ClientChannelWasInitialized(int32_t peer_pid); | |
109 | |
110 mojo::InterfacePtr<ClientChannel> client_channel_; | |
111 mojo::ScopedMessagePipeHandle message_pipe_; | |
112 }; | |
viettrungluu
2014/10/24 20:17:00
"
Hajime Morrita
2014/10/24 21:05:37
Done.
| |
113 | |
114 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, | |
115 const ChannelHandle& handle, | |
116 Listener* listener) | |
117 : ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) { | |
118 } | |
119 | |
120 ServerChannelMojo::~ServerChannelMojo() { | |
121 Close(); | |
122 } | |
123 | |
124 void ServerChannelMojo::OnPipeAvailable( | |
125 mojo::embedder::ScopedPlatformHandle handle) { | |
126 mojo::ScopedMessagePipeHandle peer; | |
127 MojoResult create_result = | |
128 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); | |
129 if (create_result != MOJO_RESULT_OK) { | |
130 DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; | |
131 listener()->OnChannelError(); | |
132 return; | |
133 } | |
134 | |
135 client_channel_.Bind(CreateMessagingPipe(handle.Pass())); | |
136 client_channel_.set_error_handler(this); | |
137 client_channel_->InitClientChannel( | |
138 peer.Pass(), | |
139 static_cast<int32_t>(GetSelfPID()), | |
140 base::Bind(&ServerChannelMojo::ClientChannelWasInitialized, | |
141 base::Unretained(this))); | |
142 } | |
143 | |
144 void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) { | |
145 InitMessageReader(message_pipe_.Pass(), peer_pid); | |
146 } | |
147 | |
148 void ServerChannelMojo::OnConnectionError() { | |
149 listener()->OnChannelError(); | |
150 } | |
151 | |
152 void ServerChannelMojo::Close() { | |
153 client_channel_.reset(); | |
154 message_pipe_.reset(); | |
155 ChannelMojo::Close(); | |
156 } | |
157 | |
44 } // namespace | 158 } // namespace |
45 | 159 |
46 //------------------------------------------------------------------------------ | 160 //------------------------------------------------------------------------------ |
47 | 161 |
48 void ChannelMojo::ChannelInfoDeleter::operator()( | 162 void ChannelMojo::ChannelInfoDeleter::operator()( |
49 mojo::embedder::ChannelInfo* ptr) const { | 163 mojo::embedder::ChannelInfo* ptr) const { |
50 mojo::embedder::DestroyChannelOnIOThread(ptr); | 164 mojo::embedder::DestroyChannelOnIOThread(ptr); |
51 } | 165 } |
52 | 166 |
53 //------------------------------------------------------------------------------ | 167 //------------------------------------------------------------------------------ |
54 | 168 |
55 // static | 169 // static |
56 bool ChannelMojo::ShouldBeUsed() { | 170 bool ChannelMojo::ShouldBeUsed() { |
57 // TODO(morrita): Turn this on for a set of platforms. | 171 // TODO(morrita): Turn this on for a set of platforms. |
58 return false; | 172 return false; |
59 } | 173 } |
60 | 174 |
61 // static | 175 // static |
62 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, | 176 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, |
63 const ChannelHandle& channel_handle, | 177 const ChannelHandle& channel_handle, |
64 Mode mode, | 178 Mode mode, |
65 Listener* listener) { | 179 Listener* listener) { |
66 return make_scoped_ptr( | 180 switch (mode) { |
67 new ChannelMojo(delegate, channel_handle, mode, listener)); | 181 case Channel::MODE_CLIENT: |
182 return make_scoped_ptr( | |
183 new ClientChannelMojo(delegate, channel_handle, listener)); | |
184 case Channel::MODE_SERVER: | |
185 return make_scoped_ptr( | |
186 new ServerChannelMojo(delegate, channel_handle, listener)); | |
187 default: | |
188 NOTREACHED(); | |
189 return nullptr; | |
190 } | |
68 } | 191 } |
69 | 192 |
70 // static | 193 // static |
71 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 194 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
72 ChannelMojo::Delegate* delegate, | 195 ChannelMojo::Delegate* delegate, |
73 const ChannelHandle& channel_handle) { | 196 const ChannelHandle& channel_handle) { |
74 return make_scoped_ptr( | 197 return make_scoped_ptr( |
75 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); | 198 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); |
76 } | 199 } |
77 | 200 |
(...skipping 30 matching lines...) Expand all Loading... | |
108 | 231 |
109 ChannelMojo::~ChannelMojo() { | 232 ChannelMojo::~ChannelMojo() { |
110 Close(); | 233 Close(); |
111 } | 234 } |
112 | 235 |
113 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { | 236 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { |
114 delegate_ = delegate->ToWeakPtr(); | 237 delegate_ = delegate->ToWeakPtr(); |
115 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); | 238 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
116 } | 239 } |
117 | 240 |
118 void ChannelMojo::InitControlReader( | 241 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( |
119 mojo::embedder::ScopedPlatformHandle handle) { | 242 mojo::embedder::ScopedPlatformHandle handle) { |
120 DCHECK(base::MessageLoopForIO::IsCurrent()); | 243 DCHECK(!channel_info_.get()); |
121 mojo::embedder::ChannelInfo* channel_info; | 244 mojo::embedder::ChannelInfo* channel_info; |
122 mojo::ScopedMessagePipeHandle control_pipe = | 245 mojo::ScopedMessagePipeHandle pipe = |
123 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 246 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
124 channel_info_.reset(channel_info); | 247 channel_info_.reset(channel_info); |
125 | 248 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 } | 249 } |
140 | 250 |
141 bool ChannelMojo::Connect() { | 251 bool ChannelMojo::Connect() { |
142 DCHECK(!message_reader_); | 252 DCHECK(!message_reader_); |
143 DCHECK(!control_reader_); | |
144 return bootstrap_->Connect(); | 253 return bootstrap_->Connect(); |
145 } | 254 } |
146 | 255 |
147 void ChannelMojo::Close() { | 256 void ChannelMojo::Close() { |
148 control_reader_.reset(); | |
149 message_reader_.reset(); | 257 message_reader_.reset(); |
150 channel_info_.reset(); | 258 channel_info_.reset(); |
151 } | 259 } |
152 | 260 |
153 void ChannelMojo::OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) { | |
154 InitControlReader(handle.Pass()); | |
155 control_reader_->Connect(); | |
156 } | |
157 | |
158 void ChannelMojo::OnBootstrapError() { | 261 void ChannelMojo::OnBootstrapError() { |
159 listener_->OnChannelError(); | 262 listener_->OnChannelError(); |
160 } | 263 } |
161 | 264 |
162 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) { | 265 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, |
266 int32_t peer_pid) { | |
163 message_reader_ = | 267 message_reader_ = |
164 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); | 268 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); |
165 | 269 |
166 for (size_t i = 0; i < pending_messages_.size(); ++i) { | 270 for (size_t i = 0; i < pending_messages_.size(); ++i) { |
167 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); | 271 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); |
168 pending_messages_[i] = NULL; | 272 pending_messages_[i] = NULL; |
169 if (!sent) { | 273 if (!sent) { |
170 pending_messages_.clear(); | 274 pending_messages_.clear(); |
171 listener_->OnChannelError(); | 275 listener_->OnChannelError(); |
172 return; | 276 return; |
173 } | 277 } |
174 } | 278 } |
175 | 279 |
176 pending_messages_.clear(); | 280 pending_messages_.clear(); |
177 | 281 |
282 set_peer_pid(peer_pid); | |
178 listener_->OnChannelConnected(GetPeerPID()); | 283 listener_->OnChannelConnected(GetPeerPID()); |
179 } | 284 } |
180 | 285 |
181 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { | 286 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { |
182 Close(); | 287 Close(); |
183 } | 288 } |
184 | 289 |
185 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { | 290 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { |
186 listener_->OnChannelError(); | 291 listener_->OnChannelError(); |
187 } | 292 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 | 386 |
282 fdset->CommitAll(); | 387 fdset->CommitAll(); |
283 } | 388 } |
284 | 389 |
285 return MOJO_RESULT_OK; | 390 return MOJO_RESULT_OK; |
286 } | 391 } |
287 | 392 |
288 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 393 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
289 | 394 |
290 } // namespace IPC | 395 } // namespace IPC |
OLD | NEW |