OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ipc_channel_proxy.h" | 5 #include "ipc/ipc_channel_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "ipc/ipc_channel_factory.h" |
14 #include "ipc/ipc_listener.h" | 15 #include "ipc/ipc_listener.h" |
15 #include "ipc/ipc_logging.h" | 16 #include "ipc/ipc_logging.h" |
16 #include "ipc/ipc_message_macros.h" | 17 #include "ipc/ipc_message_macros.h" |
17 #include "ipc/message_filter.h" | 18 #include "ipc/message_filter.h" |
18 #include "ipc/message_filter_router.h" | 19 #include "ipc/message_filter_router.h" |
19 | 20 |
20 namespace IPC { | 21 namespace IPC { |
21 | 22 |
22 //------------------------------------------------------------------------------ | 23 //------------------------------------------------------------------------------ |
23 | 24 |
(...skipping 17 matching lines...) Expand all Loading... |
41 DCHECK(!listener || (ipc_task_runner_.get() != listener_task_runner_.get())); | 42 DCHECK(!listener || (ipc_task_runner_.get() != listener_task_runner_.get())); |
42 } | 43 } |
43 | 44 |
44 ChannelProxy::Context::~Context() { | 45 ChannelProxy::Context::~Context() { |
45 } | 46 } |
46 | 47 |
47 void ChannelProxy::Context::ClearIPCTaskRunner() { | 48 void ChannelProxy::Context::ClearIPCTaskRunner() { |
48 ipc_task_runner_ = NULL; | 49 ipc_task_runner_ = NULL; |
49 } | 50 } |
50 | 51 |
51 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, | 52 void ChannelProxy::Context::CreateChannel(scoped_ptr<ChannelFactory> factory) { |
52 const Channel::Mode& mode) { | |
53 DCHECK(!channel_); | 53 DCHECK(!channel_); |
54 channel_id_ = handle.name; | 54 channel_id_ = factory->GetName(); |
55 channel_ = Channel::Create(handle, mode, this); | 55 channel_ = factory->BuildChannel(this); |
56 } | 56 } |
57 | 57 |
58 bool ChannelProxy::Context::TryFilters(const Message& message) { | 58 bool ChannelProxy::Context::TryFilters(const Message& message) { |
59 DCHECK(message_filter_router_); | 59 DCHECK(message_filter_router_); |
60 #ifdef IPC_MESSAGE_LOG_ENABLED | 60 #ifdef IPC_MESSAGE_LOG_ENABLED |
61 Logging* logger = Logging::GetInstance(); | 61 Logging* logger = Logging::GetInstance(); |
62 if (logger->Enabled()) | 62 if (logger->Enabled()) |
63 logger->OnPreDispatchMessage(message); | 63 logger->OnPreDispatchMessage(message); |
64 #endif | 64 #endif |
65 | 65 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 scoped_ptr<ChannelProxy> ChannelProxy::Create( | 308 scoped_ptr<ChannelProxy> ChannelProxy::Create( |
309 const IPC::ChannelHandle& channel_handle, | 309 const IPC::ChannelHandle& channel_handle, |
310 Channel::Mode mode, | 310 Channel::Mode mode, |
311 Listener* listener, | 311 Listener* listener, |
312 base::SingleThreadTaskRunner* ipc_task_runner) { | 312 base::SingleThreadTaskRunner* ipc_task_runner) { |
313 scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner)); | 313 scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner)); |
314 channel->Init(channel_handle, mode, true); | 314 channel->Init(channel_handle, mode, true); |
315 return channel.Pass(); | 315 return channel.Pass(); |
316 } | 316 } |
317 | 317 |
| 318 // static |
| 319 scoped_ptr<ChannelProxy> ChannelProxy::Create( |
| 320 scoped_ptr<ChannelFactory> factory, |
| 321 Listener* listener, |
| 322 base::SingleThreadTaskRunner* ipc_task_runner) { |
| 323 scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner)); |
| 324 channel->Init(factory.Pass(), true); |
| 325 return channel.Pass(); |
| 326 } |
| 327 |
318 ChannelProxy::ChannelProxy(Context* context) | 328 ChannelProxy::ChannelProxy(Context* context) |
319 : context_(context), | 329 : context_(context), |
320 did_init_(false) { | 330 did_init_(false) { |
321 } | 331 } |
322 | 332 |
323 ChannelProxy::ChannelProxy(Listener* listener, | 333 ChannelProxy::ChannelProxy(Listener* listener, |
324 base::SingleThreadTaskRunner* ipc_task_runner) | 334 base::SingleThreadTaskRunner* ipc_task_runner) |
325 : context_(new Context(listener, ipc_task_runner)), did_init_(false) { | 335 : context_(new Context(listener, ipc_task_runner)), did_init_(false) { |
326 } | 336 } |
327 | 337 |
328 ChannelProxy::~ChannelProxy() { | 338 ChannelProxy::~ChannelProxy() { |
329 DCHECK(CalledOnValidThread()); | 339 DCHECK(CalledOnValidThread()); |
330 | 340 |
331 Close(); | 341 Close(); |
332 } | 342 } |
333 | 343 |
334 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 344 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
335 Channel::Mode mode, | 345 Channel::Mode mode, |
336 bool create_pipe_now) { | 346 bool create_pipe_now) { |
337 DCHECK(CalledOnValidThread()); | |
338 DCHECK(!did_init_); | |
339 #if defined(OS_POSIX) | 347 #if defined(OS_POSIX) |
340 // When we are creating a server on POSIX, we need its file descriptor | 348 // When we are creating a server on POSIX, we need its file descriptor |
341 // to be created immediately so that it can be accessed and passed | 349 // to be created immediately so that it can be accessed and passed |
342 // to other processes. Forcing it to be created immediately avoids | 350 // to other processes. Forcing it to be created immediately avoids |
343 // race conditions that may otherwise arise. | 351 // race conditions that may otherwise arise. |
344 if (mode & Channel::MODE_SERVER_FLAG) { | 352 if (mode & Channel::MODE_SERVER_FLAG) { |
345 create_pipe_now = true; | 353 create_pipe_now = true; |
346 } | 354 } |
347 #endif // defined(OS_POSIX) | 355 #endif // defined(OS_POSIX) |
| 356 Init(ChannelFactory::Create(channel_handle, mode), |
| 357 create_pipe_now); |
| 358 } |
| 359 |
| 360 void ChannelProxy::Init(scoped_ptr<ChannelFactory> factory, |
| 361 bool create_pipe_now) { |
| 362 DCHECK(CalledOnValidThread()); |
| 363 DCHECK(!did_init_); |
348 | 364 |
349 if (create_pipe_now) { | 365 if (create_pipe_now) { |
350 // Create the channel immediately. This effectively sets up the | 366 // Create the channel immediately. This effectively sets up the |
351 // low-level pipe so that the client can connect. Without creating | 367 // low-level pipe so that the client can connect. Without creating |
352 // the pipe immediately, it is possible for a listener to attempt | 368 // the pipe immediately, it is possible for a listener to attempt |
353 // to connect and get an error since the pipe doesn't exist yet. | 369 // to connect and get an error since the pipe doesn't exist yet. |
354 context_->CreateChannel(channel_handle, mode); | 370 context_->CreateChannel(factory.Pass()); |
355 } else { | 371 } else { |
356 context_->ipc_task_runner()->PostTask( | 372 context_->ipc_task_runner()->PostTask( |
357 FROM_HERE, base::Bind(&Context::CreateChannel, context_.get(), | 373 FROM_HERE, base::Bind(&Context::CreateChannel, |
358 channel_handle, mode)); | 374 context_.get(), Passed(factory.Pass()))); |
359 } | 375 } |
360 | 376 |
361 // complete initialization on the background thread | 377 // complete initialization on the background thread |
362 context_->ipc_task_runner()->PostTask( | 378 context_->ipc_task_runner()->PostTask( |
363 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); | 379 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
364 | 380 |
365 did_init_ = true; | 381 did_init_ = true; |
366 } | 382 } |
367 | 383 |
368 void ChannelProxy::Close() { | 384 void ChannelProxy::Close() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 Channel* channel = context_.get()->channel_.get(); | 450 Channel* channel = context_.get()->channel_.get(); |
435 // Channel must have been created first. | 451 // Channel must have been created first. |
436 DCHECK(channel) << context_.get()->channel_id_; | 452 DCHECK(channel) << context_.get()->channel_id_; |
437 return channel->TakeClientFileDescriptor(); | 453 return channel->TakeClientFileDescriptor(); |
438 } | 454 } |
439 #endif | 455 #endif |
440 | 456 |
441 //----------------------------------------------------------------------------- | 457 //----------------------------------------------------------------------------- |
442 | 458 |
443 } // namespace IPC | 459 } // namespace IPC |
OLD | NEW |