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

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

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

Powered by Google App Engine
This is Rietveld 408576698