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
3 // found in the LICENSE file.
4
5 #include "components/nacl/renderer/manifest_service_channel.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/callback_helpers.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "ipc/ipc_channel.h"
12 #include "ipc/ipc_sync_channel.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15
16 namespace nacl {
17
ManifestServiceChannel(const IPC::ChannelHandle & handle,const base::Callback<void (int32_t)> & connected_callback,scoped_ptr<Delegate> delegate,base::WaitableEvent * waitable_event)18 ManifestServiceChannel::ManifestServiceChannel(
19 const IPC::ChannelHandle& handle,
20 const base::Callback<void(int32_t)>& connected_callback,
21 scoped_ptr<Delegate> delegate,
22 base::WaitableEvent* waitable_event)
23 : connected_callback_(connected_callback),
24 delegate_(delegate.Pass()),
25 channel_(IPC::SyncChannel::Create(
26 handle,
27 IPC::Channel::MODE_CLIENT,
28 this,
29 content::RenderThread::Get()->GetIOMessageLoopProxy(),
30 true,
31 waitable_event)),
32 weak_ptr_factory_(this) {
33 }
34
~ManifestServiceChannel()35 ManifestServiceChannel::~ManifestServiceChannel() {
36 if (!connected_callback_.is_null())
37 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
38 }
39
Send(IPC::Message * message)40 void ManifestServiceChannel::Send(IPC::Message* message) {
41 channel_->Send(message);
42 }
43
OnMessageReceived(const IPC::Message & message)44 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) {
45 bool handled = true;
46 IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message)
47 IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete,
48 OnStartupInitializationComplete)
49 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_OpenResource,
50 OnOpenResource)
51 IPC_MESSAGE_UNHANDLED(handled = false)
52 IPC_END_MESSAGE_MAP()
53 return handled;
54 }
55
OnChannelConnected(int32 peer_pid)56 void ManifestServiceChannel::OnChannelConnected(int32 peer_pid) {
57 if (!connected_callback_.is_null())
58 base::ResetAndReturn(&connected_callback_).Run(PP_OK);
59 }
60
OnChannelError()61 void ManifestServiceChannel::OnChannelError() {
62 if (!connected_callback_.is_null())
63 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
64 }
65
OnStartupInitializationComplete()66 void ManifestServiceChannel::OnStartupInitializationComplete() {
67 delegate_->StartupInitializationComplete();
68 }
69
OnOpenResource(const std::string & key,IPC::Message * reply)70 void ManifestServiceChannel::OnOpenResource(
71 const std::string& key, IPC::Message* reply) {
72 // Currently this is used only for non-SFI mode, which is not supported on
73 // windows.
74 #if !defined(OS_WIN)
75 delegate_->OpenResource(
76 key,
77 base::Bind(&ManifestServiceChannel::DidOpenResource,
78 weak_ptr_factory_.GetWeakPtr(), reply));
79 #else
80 PpapiHostMsg_OpenResource::WriteReplyParams(
81 reply, ppapi::proxy::SerializedHandle());
82 Send(reply);
83 #endif
84 }
85
86 #if !defined(OS_WIN)
DidOpenResource(IPC::Message * reply,const base::PlatformFile & platform_file)87 void ManifestServiceChannel::DidOpenResource(
88 IPC::Message* reply, const base::PlatformFile& platform_file) {
89 // Here, PlatformFileForTransit is alias of base::FileDescriptor.
90 PpapiHostMsg_OpenResource::WriteReplyParams(
91 reply,
92 ppapi::proxy::SerializedHandle(
93 ppapi::proxy::SerializedHandle::FILE,
94 base::FileDescriptor(platform_file, true)));
95 Send(reply);
96 }
97 #endif
98
99 } // namespace nacl
100