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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "ipc/ipc_platform_file_attachment_posix.h" | 21 #include "ipc/ipc_platform_file_attachment_posix.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace IPC { | 24 namespace IPC { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 class MojoChannelFactory : public ChannelFactory { | 28 class MojoChannelFactory : public ChannelFactory { |
29 public: | 29 public: |
30 MojoChannelFactory(ChannelMojo::Delegate* delegate, | 30 MojoChannelFactory(ChannelMojo::Delegate* delegate, |
31 scoped_refptr<base::TaskRunner> io_runner, | |
32 ChannelHandle channel_handle, | 31 ChannelHandle channel_handle, |
33 Channel::Mode mode) | 32 Channel::Mode mode) |
34 : delegate_(delegate), | 33 : delegate_(delegate), channel_handle_(channel_handle), mode_(mode) {} |
35 io_runner_(io_runner), | |
36 channel_handle_(channel_handle), | |
37 mode_(mode) {} | |
38 | 34 |
39 std::string GetName() const override { | 35 std::string GetName() const override { |
40 return channel_handle_.name; | 36 return channel_handle_.name; |
41 } | 37 } |
42 | 38 |
43 scoped_ptr<Channel> BuildChannel(Listener* listener) override { | 39 scoped_ptr<Channel> BuildChannel(Listener* listener) override { |
44 return ChannelMojo::Create(delegate_, io_runner_, channel_handle_, mode_, | 40 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener); |
45 listener); | |
46 } | 41 } |
47 | 42 |
48 private: | 43 private: |
49 ChannelMojo::Delegate* delegate_; | 44 ChannelMojo::Delegate* delegate_; |
50 scoped_refptr<base::TaskRunner> io_runner_; | |
51 ChannelHandle channel_handle_; | 45 ChannelHandle channel_handle_; |
52 Channel::Mode mode_; | 46 Channel::Mode mode_; |
53 }; | 47 }; |
54 | 48 |
55 //------------------------------------------------------------------------------ | 49 //------------------------------------------------------------------------------ |
56 | 50 |
57 class ClientChannelMojo : public ChannelMojo, | 51 class ClientChannelMojo : public ChannelMojo, |
58 public ClientChannel, | 52 public ClientChannel, |
59 public mojo::ErrorHandler { | 53 public mojo::ErrorHandler { |
60 public: | 54 public: |
61 ClientChannelMojo(ChannelMojo::Delegate* delegate, | 55 ClientChannelMojo(ChannelMojo::Delegate* delegate, |
62 scoped_refptr<base::TaskRunner> io_runner, | |
63 const ChannelHandle& handle, | 56 const ChannelHandle& handle, |
64 Listener* listener); | 57 Listener* listener); |
65 ~ClientChannelMojo() override; | 58 ~ClientChannelMojo() override; |
66 // MojoBootstrap::Delegate implementation | 59 // MojoBootstrap::Delegate implementation |
67 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 60 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
68 // mojo::ErrorHandler implementation | 61 // mojo::ErrorHandler implementation |
69 void OnConnectionError() override; | 62 void OnConnectionError() override; |
70 // ClientChannel implementation | 63 // ClientChannel implementation |
71 void Init( | 64 void Init( |
72 mojo::ScopedMessagePipeHandle pipe, | 65 mojo::ScopedMessagePipeHandle pipe, |
73 int32_t peer_pid, | 66 int32_t peer_pid, |
74 const mojo::Callback<void(int32_t)>& callback) override; | 67 const mojo::Callback<void(int32_t)>& callback) override; |
75 | 68 |
76 private: | 69 private: |
77 mojo::Binding<ClientChannel> binding_; | 70 mojo::Binding<ClientChannel> binding_; |
78 | 71 |
79 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); | 72 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
80 }; | 73 }; |
81 | 74 |
82 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, | 75 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, |
83 scoped_refptr<base::TaskRunner> io_runner, | |
84 const ChannelHandle& handle, | 76 const ChannelHandle& handle, |
85 Listener* listener) | 77 Listener* listener) |
86 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_CLIENT, listener), | 78 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener), |
87 binding_(this) { | 79 binding_(this) { |
88 } | 80 } |
89 | 81 |
90 ClientChannelMojo::~ClientChannelMojo() { | 82 ClientChannelMojo::~ClientChannelMojo() { |
91 } | 83 } |
92 | 84 |
93 void ClientChannelMojo::OnPipeAvailable( | 85 void ClientChannelMojo::OnPipeAvailable( |
94 mojo::embedder::ScopedPlatformHandle handle) { | 86 mojo::embedder::ScopedPlatformHandle handle) { |
95 binding_.Bind(CreateMessagingPipe(handle.Pass())); | 87 binding_.Bind(CreateMessagingPipe(handle.Pass())); |
96 } | 88 } |
97 | 89 |
98 void ClientChannelMojo::OnConnectionError() { | 90 void ClientChannelMojo::OnConnectionError() { |
99 listener()->OnChannelError(); | 91 listener()->OnChannelError(); |
100 } | 92 } |
101 | 93 |
102 void ClientChannelMojo::Init( | 94 void ClientChannelMojo::Init( |
103 mojo::ScopedMessagePipeHandle pipe, | 95 mojo::ScopedMessagePipeHandle pipe, |
104 int32_t peer_pid, | 96 int32_t peer_pid, |
105 const mojo::Callback<void(int32_t)>& callback) { | 97 const mojo::Callback<void(int32_t)>& callback) { |
106 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); | 98 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); |
107 callback.Run(GetSelfPID()); | 99 callback.Run(GetSelfPID()); |
108 } | 100 } |
109 | 101 |
110 //------------------------------------------------------------------------------ | 102 //------------------------------------------------------------------------------ |
111 | 103 |
112 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { | 104 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { |
113 public: | 105 public: |
114 ServerChannelMojo(ChannelMojo::Delegate* delegate, | 106 ServerChannelMojo(ChannelMojo::Delegate* delegate, |
115 scoped_refptr<base::TaskRunner> io_runner, | |
116 const ChannelHandle& handle, | 107 const ChannelHandle& handle, |
117 Listener* listener); | 108 Listener* listener); |
118 ~ServerChannelMojo() override; | 109 ~ServerChannelMojo() override; |
119 | 110 |
120 // MojoBootstrap::Delegate implementation | 111 // MojoBootstrap::Delegate implementation |
121 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 112 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
122 // mojo::ErrorHandler implementation | 113 // mojo::ErrorHandler implementation |
123 void OnConnectionError() override; | 114 void OnConnectionError() override; |
124 // Channel override | 115 // Channel override |
125 void Close() override; | 116 void Close() override; |
126 | 117 |
127 private: | 118 private: |
128 // ClientChannelClient implementation | 119 // ClientChannelClient implementation |
129 void ClientChannelWasInitialized(int32_t peer_pid); | 120 void ClientChannelWasInitialized(int32_t peer_pid); |
130 | 121 |
131 mojo::InterfacePtr<ClientChannel> client_channel_; | 122 mojo::InterfacePtr<ClientChannel> client_channel_; |
132 mojo::ScopedMessagePipeHandle message_pipe_; | 123 mojo::ScopedMessagePipeHandle message_pipe_; |
133 | 124 |
134 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); | 125 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
135 }; | 126 }; |
136 | 127 |
137 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, | 128 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, |
138 scoped_refptr<base::TaskRunner> io_runner, | |
139 const ChannelHandle& handle, | 129 const ChannelHandle& handle, |
140 Listener* listener) | 130 Listener* listener) |
141 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_SERVER, listener) { | 131 : ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) { |
142 } | 132 } |
143 | 133 |
144 ServerChannelMojo::~ServerChannelMojo() { | 134 ServerChannelMojo::~ServerChannelMojo() { |
145 Close(); | 135 Close(); |
146 } | 136 } |
147 | 137 |
148 void ServerChannelMojo::OnPipeAvailable( | 138 void ServerChannelMojo::OnPipeAvailable( |
149 mojo::embedder::ScopedPlatformHandle handle) { | 139 mojo::embedder::ScopedPlatformHandle handle) { |
150 mojo::ScopedMessagePipeHandle peer; | 140 mojo::ScopedMessagePipeHandle peer; |
151 MojoResult create_result = | 141 MojoResult create_result = |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 189 |
200 //------------------------------------------------------------------------------ | 190 //------------------------------------------------------------------------------ |
201 | 191 |
202 // static | 192 // static |
203 bool ChannelMojo::ShouldBeUsed() { | 193 bool ChannelMojo::ShouldBeUsed() { |
204 // TODO(morrita): Remove this if it sticks. | 194 // TODO(morrita): Remove this if it sticks. |
205 return true; | 195 return true; |
206 } | 196 } |
207 | 197 |
208 // static | 198 // static |
209 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 199 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, |
210 ChannelMojo::Delegate* delegate, | 200 const ChannelHandle& channel_handle, |
211 scoped_refptr<base::TaskRunner> io_runner, | 201 Mode mode, |
212 const ChannelHandle& channel_handle, | 202 Listener* listener) { |
213 Mode mode, | |
214 Listener* listener) { | |
215 switch (mode) { | 203 switch (mode) { |
216 case Channel::MODE_CLIENT: | 204 case Channel::MODE_CLIENT: |
217 return make_scoped_ptr( | 205 return make_scoped_ptr( |
218 new ClientChannelMojo(delegate, io_runner, channel_handle, listener)); | 206 new ClientChannelMojo(delegate, channel_handle, listener)); |
219 case Channel::MODE_SERVER: | 207 case Channel::MODE_SERVER: |
220 return make_scoped_ptr( | 208 return make_scoped_ptr( |
221 new ServerChannelMojo(delegate, io_runner, channel_handle, listener)); | 209 new ServerChannelMojo(delegate, channel_handle, listener)); |
222 default: | 210 default: |
223 NOTREACHED(); | 211 NOTREACHED(); |
224 return nullptr; | 212 return nullptr; |
225 } | 213 } |
226 } | 214 } |
227 | 215 |
228 // static | 216 // static |
229 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 217 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
230 ChannelMojo::Delegate* delegate, | 218 ChannelMojo::Delegate* delegate, |
231 scoped_refptr<base::TaskRunner> io_runner, | |
232 const ChannelHandle& channel_handle) { | 219 const ChannelHandle& channel_handle) { |
233 return make_scoped_ptr(new MojoChannelFactory( | 220 return make_scoped_ptr( |
234 delegate, io_runner, channel_handle, Channel::MODE_SERVER)); | 221 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); |
235 } | 222 } |
236 | 223 |
237 // static | 224 // static |
238 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 225 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
239 ChannelMojo::Delegate* delegate, | 226 ChannelMojo::Delegate* delegate, |
240 scoped_refptr<base::TaskRunner> io_runner, | |
241 const ChannelHandle& channel_handle) { | 227 const ChannelHandle& channel_handle) { |
242 return make_scoped_ptr(new MojoChannelFactory( | 228 return make_scoped_ptr( |
243 delegate, io_runner, channel_handle, Channel::MODE_CLIENT)); | 229 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_CLIENT)); |
244 } | 230 } |
245 | 231 |
246 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, | 232 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
247 scoped_refptr<base::TaskRunner> io_runner, | |
248 const ChannelHandle& handle, | 233 const ChannelHandle& handle, |
249 Mode mode, | 234 Mode mode, |
250 Listener* listener) | 235 Listener* listener) |
251 : mode_(mode), | 236 : mode_(mode), |
252 listener_(listener), | 237 listener_(listener), |
253 peer_pid_(base::kNullProcessId), | 238 peer_pid_(base::kNullProcessId), |
254 weak_factory_(this) { | 239 weak_factory_(this) { |
255 // Create MojoBootstrap after all members are set as it touches | 240 // Create MojoBootstrap after all members are set as it touches |
256 // ChannelMojo from a different thread. | 241 // ChannelMojo from a different thread. |
257 bootstrap_ = MojoBootstrap::Create(handle, mode, this); | 242 bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
258 if (io_runner == base::MessageLoop::current()->message_loop_proxy()) { | 243 if (delegate) { |
259 InitOnIOThread(delegate); | 244 if (delegate->GetIOTaskRunner() == |
260 } else { | 245 base::MessageLoop::current()->message_loop_proxy()) { |
261 io_runner->PostTask(FROM_HERE, | 246 InitDelegate(delegate); |
262 base::Bind(&ChannelMojo::InitOnIOThread, | 247 } else { |
263 base::Unretained(this), delegate)); | 248 delegate->GetIOTaskRunner()->PostTask( |
| 249 FROM_HERE, |
| 250 base::Bind( |
| 251 &ChannelMojo::InitDelegate, base::Unretained(this), delegate)); |
| 252 } |
264 } | 253 } |
265 } | 254 } |
266 | 255 |
267 ChannelMojo::~ChannelMojo() { | 256 ChannelMojo::~ChannelMojo() { |
268 Close(); | 257 Close(); |
269 } | 258 } |
270 | 259 |
271 void ChannelMojo::InitOnIOThread(ChannelMojo::Delegate* delegate) { | 260 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { |
272 ipc_support_.reset( | 261 ipc_support_.reset( |
273 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); | 262 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); |
274 if (!delegate) | |
275 return; | |
276 delegate_ = delegate->ToWeakPtr(); | 263 delegate_ = delegate->ToWeakPtr(); |
277 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); | 264 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
278 } | 265 } |
279 | 266 |
280 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( | 267 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( |
281 mojo::embedder::ScopedPlatformHandle handle) { | 268 mojo::embedder::ScopedPlatformHandle handle) { |
282 DCHECK(!channel_info_.get()); | 269 DCHECK(!channel_info_.get()); |
283 mojo::embedder::ChannelInfo* channel_info; | 270 mojo::embedder::ChannelInfo* channel_info; |
284 mojo::ScopedMessagePipeHandle pipe = | 271 mojo::ScopedMessagePipeHandle pipe = |
285 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 272 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 if (!ok) { | 430 if (!ok) { |
444 LOG(ERROR) << "Failed to add new Mojo handle."; | 431 LOG(ERROR) << "Failed to add new Mojo handle."; |
445 return MOJO_RESULT_UNKNOWN; | 432 return MOJO_RESULT_UNKNOWN; |
446 } | 433 } |
447 } | 434 } |
448 | 435 |
449 return MOJO_RESULT_OK; | 436 return MOJO_RESULT_OK; |
450 } | 437 } |
451 | 438 |
452 } // namespace IPC | 439 } // namespace IPC |
OLD | NEW |