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.
5 #include "components/nacl/renderer/manifest_service_channel.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"
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_(new IPC::SyncChannel(
26 handle, IPC::Channel::MODE_CLIENT, this,
27 content::RenderThread::Get()->GetIOMessageLoopProxy(),
28 true, waitable_event)),
29 weak_ptr_factory_(this) {
32 ManifestServiceChannel::~ManifestServiceChannel() {
33 if (!connected_callback_.is_null())
34 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
37 void ManifestServiceChannel::Send(IPC::Message* message) {
38 channel_->Send(message);
41 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) {
43 IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message)
44 IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete,
45 OnStartupInitializationComplete)
46 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_OpenResource,
48 IPC_MESSAGE_UNHANDLED(handled = false)
53 void ManifestServiceChannel::OnChannelConnected(int32 peer_pid) {
54 if (!connected_callback_.is_null())
55 base::ResetAndReturn(&connected_callback_).Run(PP_OK);
58 void ManifestServiceChannel::OnChannelError() {
59 if (!connected_callback_.is_null())
60 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
63 void ManifestServiceChannel::OnStartupInitializationComplete() {
64 delegate_->StartupInitializationComplete();
67 void ManifestServiceChannel::OnOpenResource(
68 const std::string& key, IPC::Message* reply) {
69 // Currently this is used only for non-SFI mode, which is not supported on
72 delegate_->OpenResource(
74 base::Bind(&ManifestServiceChannel::DidOpenResource,
75 weak_ptr_factory_.GetWeakPtr(), reply));
77 PpapiHostMsg_OpenResource::WriteReplyParams(
78 reply, ppapi::proxy::SerializedHandle());
84 void ManifestServiceChannel::DidOpenResource(
85 IPC::Message* reply, const base::PlatformFile& platform_file) {
86 // Here, PlatformFileForTransit is alias of base::FileDescriptor.
87 PpapiHostMsg_OpenResource::WriteReplyParams(
89 ppapi::proxy::SerializedHandle(
90 ppapi::proxy::SerializedHandle::FILE,
91 base::FileDescriptor(platform_file, true)));