1 // Copyright 2014 The Chromium Authors
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 <utility>
8
9 #include "base/functional/bind.h"
10 #include "base/functional/callback.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/task/single_thread_task_runner.h"
13 #include "build/build_config.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "ipc/ipc_channel.h"
16 #include "ipc/ipc_platform_file.h"
17 #include "ipc/ipc_sync_channel.h"
18 #include "ppapi/c/pp_errors.h"
19 #include "ppapi/c/ppb_file_io.h"
20 #include "ppapi/proxy/ppapi_messages.h"
21
22 namespace nacl {
23
ManifestServiceChannel(const IPC::ChannelHandle & handle,base::OnceCallback<void (int32_t)> connected_callback,std::unique_ptr<Delegate> delegate,base::WaitableEvent * waitable_event)24 ManifestServiceChannel::ManifestServiceChannel(
25 const IPC::ChannelHandle& handle,
26 base::OnceCallback<void(int32_t)> connected_callback,
27 std::unique_ptr<Delegate> delegate,
28 base::WaitableEvent* waitable_event)
29 : connected_callback_(std::move(connected_callback)),
30 delegate_(std::move(delegate)),
31 channel_(IPC::SyncChannel::Create(
32 handle,
33 IPC::Channel::MODE_CLIENT,
34 this,
35 content::RenderThread::Get()->GetIOTaskRunner(),
36 base::SingleThreadTaskRunner::GetCurrentDefault(),
37 true,
38 waitable_event)),
39 peer_pid_(base::kNullProcessId) {}
40
~ManifestServiceChannel()41 ManifestServiceChannel::~ManifestServiceChannel() {
42 if (!connected_callback_.is_null())
43 std::move(connected_callback_).Run(PP_ERROR_FAILED);
44 }
45
Send(IPC::Message * message)46 void ManifestServiceChannel::Send(IPC::Message* message) {
47 channel_->Send(message);
48 }
49
OnMessageReceived(const IPC::Message & message)50 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) {
51 bool handled = true;
52 IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message)
53 IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete,
54 OnStartupInitializationComplete)
55 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_OpenResource,
56 OnOpenResource)
57 IPC_MESSAGE_UNHANDLED(handled = false)
58 IPC_END_MESSAGE_MAP()
59 return handled;
60 }
61
OnChannelConnected(int32_t peer_pid)62 void ManifestServiceChannel::OnChannelConnected(int32_t peer_pid) {
63 peer_pid_ = peer_pid;
64 if (!connected_callback_.is_null())
65 std::move(connected_callback_).Run(PP_OK);
66 }
67
OnChannelError()68 void ManifestServiceChannel::OnChannelError() {
69 if (!connected_callback_.is_null())
70 std::move(connected_callback_).Run(PP_ERROR_FAILED);
71 }
72
OnStartupInitializationComplete()73 void ManifestServiceChannel::OnStartupInitializationComplete() {
74 delegate_->StartupInitializationComplete();
75 }
76
OnOpenResource(const std::string & key,IPC::Message * reply)77 void ManifestServiceChannel::OnOpenResource(
78 const std::string& key, IPC::Message* reply) {
79 delegate_->OpenResource(
80 key, base::BindOnce(&ManifestServiceChannel::DidOpenResource,
81 weak_ptr_factory_.GetWeakPtr(), reply));
82 }
83
DidOpenResource(IPC::Message * reply,base::File file,uint64_t token_lo,uint64_t token_hi)84 void ManifestServiceChannel::DidOpenResource(IPC::Message* reply,
85 base::File file,
86 uint64_t token_lo,
87 uint64_t token_hi) {
88 ppapi::proxy::SerializedHandle handle;
89 if (file.IsValid()) {
90 IPC::PlatformFileForTransit file_for_transit =
91 IPC::TakePlatformFileForTransit(std::move(file));
92 handle.set_file_handle(file_for_transit, PP_FILEOPENFLAG_READ, 0);
93 }
94 PpapiHostMsg_OpenResource::WriteReplyParams(reply, token_lo, token_hi,
95 handle);
96 Send(reply);
97 }
98
99 } // namespace nacl
100