Revert of Revert of Fix closure compile after https://codereview.chromium.org/1212653...
[chromium-blink-merge.git] / ppapi / proxy / proxy_channel.cc
blob2784e4b978bce53cbe57bebd930fd000bcebc705
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
3 // found in the LICENSE file.
5 #include "ppapi/proxy/proxy_channel.h"
7 #include "base/logging.h"
8 #include "ipc/ipc_platform_file.h"
9 #include "ipc/ipc_test_sink.h"
11 #if defined(OS_NACL)
12 #include <unistd.h>
13 #endif
15 namespace ppapi {
16 namespace proxy {
18 ProxyChannel::ProxyChannel()
19 : delegate_(NULL),
20 peer_pid_(base::kNullProcessId),
21 test_sink_(NULL) {
24 ProxyChannel::~ProxyChannel() {
25 DVLOG(1) << "ProxyChannel::~ProxyChannel()";
28 bool ProxyChannel::InitWithChannel(Delegate* delegate,
29 base::ProcessId peer_pid,
30 const IPC::ChannelHandle& channel_handle,
31 bool is_client) {
32 delegate_ = delegate;
33 peer_pid_ = peer_pid;
34 IPC::Channel::Mode mode = is_client
35 ? IPC::Channel::MODE_CLIENT
36 : IPC::Channel::MODE_SERVER;
37 channel_ = IPC::SyncChannel::Create(channel_handle, mode, this,
38 delegate->GetIPCTaskRunner(), true,
39 delegate->GetShutdownEvent());
40 return true;
43 void ProxyChannel::InitWithTestSink(IPC::TestSink* test_sink) {
44 DCHECK(!test_sink_);
45 test_sink_ = test_sink;
46 #if !defined(OS_NACL)
47 peer_pid_ = base::GetCurrentProcId();
48 #endif
51 void ProxyChannel::OnChannelError() {
52 channel_.reset();
55 #if defined(OS_POSIX) && !defined(OS_NACL)
56 base::ScopedFD ProxyChannel::TakeRendererFD() {
57 DCHECK(channel());
58 return channel()->TakeClientFileDescriptor();
60 #endif
62 IPC::PlatformFileForTransit ProxyChannel::ShareHandleWithRemote(
63 base::PlatformFile handle,
64 bool should_close_source) {
65 // Channel could be closed if the plugin crashes.
66 if (!channel_.get()) {
67 if (should_close_source) {
68 base::File file_closer(handle);
70 return IPC::InvalidPlatformFileForTransit();
72 DCHECK(peer_pid_ != base::kNullProcessId);
73 return delegate_->ShareHandleWithRemote(handle, peer_pid_,
74 should_close_source);
77 base::SharedMemoryHandle ProxyChannel::ShareSharedMemoryHandleWithRemote(
78 const base::SharedMemoryHandle& handle) {
79 if (!channel_.get())
80 return base::SharedMemory::NULLHandle();
82 DCHECK(peer_pid_ != base::kNullProcessId);
83 return delegate_->ShareSharedMemoryHandleWithRemote(handle, peer_pid_);
86 bool ProxyChannel::Send(IPC::Message* msg) {
87 if (test_sink_)
88 return test_sink_->Send(msg);
89 if (channel_.get())
90 return channel_->Send(msg);
92 // Remote side crashed, drop this message.
93 delete msg;
94 return false;
97 } // namespace proxy
98 } // namespace ppapi