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 "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #endif | 23 #endif |
24 | 24 |
25 namespace IPC { | 25 namespace IPC { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 class MojoChannelFactory : public ChannelFactory { | 29 class MojoChannelFactory : public ChannelFactory { |
30 public: | 30 public: |
31 MojoChannelFactory(scoped_refptr<base::TaskRunner> io_runner, | 31 MojoChannelFactory(scoped_refptr<base::TaskRunner> io_runner, |
32 ChannelHandle channel_handle, | 32 ChannelHandle channel_handle, |
33 Channel::Mode mode, | 33 Channel::Mode mode) |
34 AttachmentBroker* broker) | 34 : io_runner_(io_runner), channel_handle_(channel_handle), mode_(mode) {} |
35 : io_runner_(io_runner), | |
36 channel_handle_(channel_handle), | |
37 mode_(mode), | |
38 broker_(broker) {} | |
39 | 35 |
40 std::string GetName() const override { | 36 std::string GetName() const override { |
41 return channel_handle_.name; | 37 return channel_handle_.name; |
42 } | 38 } |
43 | 39 |
44 scoped_ptr<Channel> BuildChannel(Listener* listener) override { | 40 scoped_ptr<Channel> BuildChannel(Listener* listener) override { |
45 return ChannelMojo::Create(io_runner_, channel_handle_, mode_, listener, | 41 return ChannelMojo::Create(io_runner_, channel_handle_, mode_, listener); |
46 broker_); | |
47 } | 42 } |
48 | 43 |
49 private: | 44 private: |
50 scoped_refptr<base::TaskRunner> io_runner_; | 45 scoped_refptr<base::TaskRunner> io_runner_; |
51 ChannelHandle channel_handle_; | 46 ChannelHandle channel_handle_; |
52 Channel::Mode mode_; | 47 Channel::Mode mode_; |
53 AttachmentBroker* broker_; | |
54 }; | 48 }; |
55 | 49 |
56 //------------------------------------------------------------------------------ | 50 //------------------------------------------------------------------------------ |
57 | 51 |
58 class ClientChannelMojo : public ChannelMojo, public ClientChannel { | 52 class ClientChannelMojo : public ChannelMojo, public ClientChannel { |
59 public: | 53 public: |
60 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 54 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
61 const ChannelHandle& handle, | 55 const ChannelHandle& handle, |
62 Listener* listener, | 56 Listener* listener); |
63 AttachmentBroker* broker); | |
64 ~ClientChannelMojo() override; | 57 ~ClientChannelMojo() override; |
65 // MojoBootstrap::Delegate implementation | 58 // MojoBootstrap::Delegate implementation |
66 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 59 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
67 | 60 |
68 // ClientChannel implementation | 61 // ClientChannel implementation |
69 void Init( | 62 void Init( |
70 mojo::ScopedMessagePipeHandle pipe, | 63 mojo::ScopedMessagePipeHandle pipe, |
71 int32_t peer_pid, | 64 int32_t peer_pid, |
72 const mojo::Callback<void(int32_t)>& callback) override; | 65 const mojo::Callback<void(int32_t)>& callback) override; |
73 | 66 |
74 private: | 67 private: |
75 void BindPipe(mojo::ScopedMessagePipeHandle handle); | 68 void BindPipe(mojo::ScopedMessagePipeHandle handle); |
76 void OnConnectionError(); | 69 void OnConnectionError(); |
77 | 70 |
78 mojo::Binding<ClientChannel> binding_; | 71 mojo::Binding<ClientChannel> binding_; |
79 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; | 72 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; |
80 | 73 |
81 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); | 74 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
82 }; | 75 }; |
83 | 76 |
84 ClientChannelMojo::ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 77 ClientChannelMojo::ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
85 const ChannelHandle& handle, | 78 const ChannelHandle& handle, |
86 Listener* listener, | 79 Listener* listener) |
87 AttachmentBroker* broker) | 80 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), |
88 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener, broker), | |
89 binding_(this), | 81 binding_(this), |
90 weak_factory_(this) { | 82 weak_factory_(this) {} |
91 } | |
92 | 83 |
93 ClientChannelMojo::~ClientChannelMojo() { | 84 ClientChannelMojo::~ClientChannelMojo() { |
94 } | 85 } |
95 | 86 |
96 void ClientChannelMojo::OnPipeAvailable( | 87 void ClientChannelMojo::OnPipeAvailable( |
97 mojo::embedder::ScopedPlatformHandle handle) { | 88 mojo::embedder::ScopedPlatformHandle handle) { |
98 CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe, | 89 CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe, |
99 weak_factory_.GetWeakPtr())); | 90 weak_factory_.GetWeakPtr())); |
100 } | 91 } |
101 | 92 |
(...skipping 12 matching lines...) Expand all Loading... |
114 void ClientChannelMojo::OnConnectionError() { | 105 void ClientChannelMojo::OnConnectionError() { |
115 listener()->OnChannelError(); | 106 listener()->OnChannelError(); |
116 } | 107 } |
117 | 108 |
118 //------------------------------------------------------------------------------ | 109 //------------------------------------------------------------------------------ |
119 | 110 |
120 class ServerChannelMojo : public ChannelMojo { | 111 class ServerChannelMojo : public ChannelMojo { |
121 public: | 112 public: |
122 ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 113 ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
123 const ChannelHandle& handle, | 114 const ChannelHandle& handle, |
124 Listener* listener, | 115 Listener* listener); |
125 AttachmentBroker* broker); | |
126 ~ServerChannelMojo() override; | 116 ~ServerChannelMojo() override; |
127 | 117 |
128 // MojoBootstrap::Delegate implementation | 118 // MojoBootstrap::Delegate implementation |
129 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 119 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
130 // Channel override | 120 // Channel override |
131 void Close() override; | 121 void Close() override; |
132 | 122 |
133 private: | 123 private: |
134 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, | 124 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, |
135 mojo::ScopedMessagePipeHandle handle); | 125 mojo::ScopedMessagePipeHandle handle); |
136 void OnConnectionError(); | 126 void OnConnectionError(); |
137 | 127 |
138 // ClientChannelClient implementation | 128 // ClientChannelClient implementation |
139 void ClientChannelWasInitialized(int32_t peer_pid); | 129 void ClientChannelWasInitialized(int32_t peer_pid); |
140 | 130 |
141 mojo::InterfacePtr<ClientChannel> client_channel_; | 131 mojo::InterfacePtr<ClientChannel> client_channel_; |
142 mojo::ScopedMessagePipeHandle message_pipe_; | 132 mojo::ScopedMessagePipeHandle message_pipe_; |
143 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; | 133 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; |
144 | 134 |
145 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); | 135 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
146 }; | 136 }; |
147 | 137 |
148 ServerChannelMojo::ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 138 ServerChannelMojo::ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
149 const ChannelHandle& handle, | 139 const ChannelHandle& handle, |
150 Listener* listener, | 140 Listener* listener) |
151 AttachmentBroker* broker) | 141 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), |
152 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener, broker), | 142 weak_factory_(this) {} |
153 weak_factory_(this) { | |
154 } | |
155 | 143 |
156 ServerChannelMojo::~ServerChannelMojo() { | 144 ServerChannelMojo::~ServerChannelMojo() { |
157 Close(); | 145 Close(); |
158 } | 146 } |
159 | 147 |
160 void ServerChannelMojo::OnPipeAvailable( | 148 void ServerChannelMojo::OnPipeAvailable( |
161 mojo::embedder::ScopedPlatformHandle handle) { | 149 mojo::embedder::ScopedPlatformHandle handle) { |
162 mojo::ScopedMessagePipeHandle peer; | 150 mojo::ScopedMessagePipeHandle peer; |
163 MojoResult create_result = | 151 MojoResult create_result = |
164 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); | 152 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable | 226 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable |
239 // this at some point. http://crbug.com/500019 | 227 // this at some point. http://crbug.com/500019 |
240 return false; | 228 return false; |
241 } | 229 } |
242 | 230 |
243 // static | 231 // static |
244 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 232 scoped_ptr<ChannelMojo> ChannelMojo::Create( |
245 scoped_refptr<base::TaskRunner> io_runner, | 233 scoped_refptr<base::TaskRunner> io_runner, |
246 const ChannelHandle& channel_handle, | 234 const ChannelHandle& channel_handle, |
247 Mode mode, | 235 Mode mode, |
248 Listener* listener, | 236 Listener* listener) { |
249 AttachmentBroker* broker) { | |
250 switch (mode) { | 237 switch (mode) { |
251 case Channel::MODE_CLIENT: | 238 case Channel::MODE_CLIENT: |
252 return make_scoped_ptr( | 239 return make_scoped_ptr( |
253 new ClientChannelMojo(io_runner, channel_handle, listener, broker)); | 240 new ClientChannelMojo(io_runner, channel_handle, listener)); |
254 case Channel::MODE_SERVER: | 241 case Channel::MODE_SERVER: |
255 return make_scoped_ptr( | 242 return make_scoped_ptr( |
256 new ServerChannelMojo(io_runner, channel_handle, listener, broker)); | 243 new ServerChannelMojo(io_runner, channel_handle, listener)); |
257 default: | 244 default: |
258 NOTREACHED(); | 245 NOTREACHED(); |
259 return nullptr; | 246 return nullptr; |
260 } | 247 } |
261 } | 248 } |
262 | 249 |
263 // static | 250 // static |
264 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 251 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
265 scoped_refptr<base::TaskRunner> io_runner, | 252 scoped_refptr<base::TaskRunner> io_runner, |
266 const ChannelHandle& channel_handle, | 253 const ChannelHandle& channel_handle) { |
267 AttachmentBroker* broker) { | 254 return make_scoped_ptr( |
268 return make_scoped_ptr(new MojoChannelFactory(io_runner, channel_handle, | 255 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_SERVER)); |
269 Channel::MODE_SERVER, broker)); | |
270 } | 256 } |
271 | 257 |
272 // static | 258 // static |
273 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 259 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
274 scoped_refptr<base::TaskRunner> io_runner, | 260 scoped_refptr<base::TaskRunner> io_runner, |
275 const ChannelHandle& channel_handle, | 261 const ChannelHandle& channel_handle) { |
276 AttachmentBroker* broker) { | 262 return make_scoped_ptr( |
277 return make_scoped_ptr(new MojoChannelFactory(io_runner, channel_handle, | 263 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); |
278 Channel::MODE_CLIENT, broker)); | |
279 } | 264 } |
280 | 265 |
281 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 266 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
282 const ChannelHandle& handle, | 267 const ChannelHandle& handle, |
283 Mode mode, | 268 Mode mode, |
284 Listener* listener, | 269 Listener* listener) |
285 AttachmentBroker* broker) | |
286 : listener_(listener), | 270 : listener_(listener), |
287 peer_pid_(base::kNullProcessId), | 271 peer_pid_(base::kNullProcessId), |
288 io_runner_(io_runner), | 272 io_runner_(io_runner), |
289 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), | 273 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), |
290 waiting_connect_(true), | 274 waiting_connect_(true), |
291 weak_factory_(this) { | 275 weak_factory_(this) { |
292 // Create MojoBootstrap after all members are set as it touches | 276 // Create MojoBootstrap after all members are set as it touches |
293 // ChannelMojo from a different thread. | 277 // ChannelMojo from a different thread. |
294 bootstrap_ = MojoBootstrap::Create(handle, mode, this, broker); | 278 bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
295 if (io_runner == base::MessageLoop::current()->task_runner()) { | 279 if (io_runner == base::MessageLoop::current()->task_runner()) { |
296 InitOnIOThread(); | 280 InitOnIOThread(); |
297 } else { | 281 } else { |
298 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, | 282 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, |
299 base::Unretained(this))); | 283 base::Unretained(this))); |
300 } | 284 } |
301 } | 285 } |
302 | 286 |
303 ChannelMojo::~ChannelMojo() { | 287 ChannelMojo::~ChannelMojo() { |
304 Close(); | 288 Close(); |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 if (!ok) { | 536 if (!ok) { |
553 LOG(ERROR) << "Failed to add new Mojo handle."; | 537 LOG(ERROR) << "Failed to add new Mojo handle."; |
554 return MOJO_RESULT_UNKNOWN; | 538 return MOJO_RESULT_UNKNOWN; |
555 } | 539 } |
556 } | 540 } |
557 | 541 |
558 return MOJO_RESULT_OK; | 542 return MOJO_RESULT_OK; |
559 } | 543 } |
560 | 544 |
561 } // namespace IPC | 545 } // namespace IPC |
OLD | NEW |