Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / nacl / renderer / manifest_service_channel.cc
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
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) {
30 }
31
32 ManifestServiceChannel::~ManifestServiceChannel() {
33   if (!connected_callback_.is_null())
34     base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
35 }
36
37 void ManifestServiceChannel::Send(IPC::Message* message) {
38   channel_->Send(message);
39 }
40
41 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) {
42   bool handled = true;
43   IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message)
44       IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete,
45                           OnStartupInitializationComplete)
46       IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_OpenResource,
47                                       OnOpenResource)
48       IPC_MESSAGE_UNHANDLED(handled = false)
49   IPC_END_MESSAGE_MAP()
50   return handled;
51 }
52
53 void ManifestServiceChannel::OnChannelConnected(int32 peer_pid) {
54   if (!connected_callback_.is_null())
55     base::ResetAndReturn(&connected_callback_).Run(PP_OK);
56 }
57
58 void ManifestServiceChannel::OnChannelError() {
59   if (!connected_callback_.is_null())
60     base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED);
61 }
62
63 void ManifestServiceChannel::OnStartupInitializationComplete() {
64   delegate_->StartupInitializationComplete();
65 }
66
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
70   // windows.
71 #if !defined(OS_WIN)
72   delegate_->OpenResource(
73       key,
74       base::Bind(&ManifestServiceChannel::DidOpenResource,
75                  weak_ptr_factory_.GetWeakPtr(), reply));
76 #else
77   PpapiHostMsg_OpenResource::WriteReplyParams(
78       reply, ppapi::proxy::SerializedHandle());
79   Send(reply);
80 #endif
81 }
82
83 #if !defined(OS_WIN)
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(
88       reply,
89       ppapi::proxy::SerializedHandle(
90           ppapi::proxy::SerializedHandle::FILE,
91           base::FileDescriptor(platform_file, true)));
92   Send(reply);
93 }
94 #endif
95
96 }  // namespace nacl