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 "ppapi/nacl_irt/manifest_service.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "ipc/ipc_channel_handle.h"
9 #include "ipc/ipc_channel_proxy.h"
10 #include "ipc/ipc_sync_message_filter.h"
11 #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
12 #include "ppapi/nacl_irt/irt_manifest.h"
13 #include "ppapi/nacl_irt/plugin_startup.h"
14 #include "ppapi/proxy/ppapi_messages.h"
18 const char kFilePrefix
[] = "files/";
20 // IPC channel is asynchronously set up. So, the NaCl process may try to
21 // send a OpenResource message to the host before the connection is
22 // established. In such a case, it is necessary to wait for the set up
24 class ManifestMessageFilter
: public IPC::SyncMessageFilter
{
26 ManifestMessageFilter(base::WaitableEvent
* shutdown_event
)
27 : SyncMessageFilter(shutdown_event
),
29 true /* manual_reset */, false /* initially_signaled */) {
32 virtual bool Send(IPC::Message
* message
) override
{
33 // Wait until set up is actually done.
34 connected_event_
.Wait();
35 return SyncMessageFilter::Send(message
);
38 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
40 virtual void OnFilterAdded(IPC::Sender
* sender
) override
{
41 SyncMessageFilter::OnFilterAdded(sender
);
42 connected_event_
.Signal();
45 // If an error is found, unblocks the Send(), too, to return an error.
46 virtual void OnChannelError() override
{
47 SyncMessageFilter::OnChannelError();
48 connected_event_
.Signal();
51 // Similar to OnChannelError, unblocks the Send() on the channel closing.
52 virtual void OnChannelClosing() override
{
53 SyncMessageFilter::OnChannelClosing();
54 connected_event_
.Signal();
58 base::WaitableEvent connected_event_
;
60 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter
);
63 ManifestService::ManifestService(
64 const IPC::ChannelHandle
& handle
,
65 scoped_refptr
<base::MessageLoopProxy
> io_message_loop
,
66 base::WaitableEvent
* shutdown_event
) {
67 filter_
= new ManifestMessageFilter(shutdown_event
);
68 channel_
= IPC::ChannelProxy::Create(handle
,
69 IPC::Channel::MODE_SERVER
,
71 io_message_loop
.get());
72 channel_
->AddFilter(filter_
.get());
75 ManifestService::~ManifestService() {
78 void ManifestService::StartupInitializationComplete() {
79 filter_
->Send(new PpapiHostMsg_StartupInitializationComplete
);
82 bool ManifestService::OpenResource(const char* file
, int* fd
) {
83 // We currently restrict to only allow one concurrent open_resource() call
84 // per plugin. This could be fixed by doing a token lookup with
85 // NaClProcessMsg_ResolveFileTokenAsyncReply instead of using a
86 // global inside components/nacl/loader/nacl_listener.cc
87 base::AutoLock
lock(open_resource_lock_
);
89 // OpenResource will return INVALID SerializedHandle, if it is not supported.
90 // Specifically, PNaCl doesn't support open resource.
91 ppapi::proxy::SerializedHandle ipc_fd
;
93 // File tokens are ignored here, but needed when the message is processed
94 // inside NaClIPCAdapter.
95 uint64_t file_token_lo
= 0;
96 uint64_t file_token_hi
= 0;
97 if (!filter_
->Send(new PpapiHostMsg_OpenResource(
98 std::string(kFilePrefix
) + file
,
102 LOG(ERROR
) << "ManifestService::OpenResource failed:" << file
;
107 // File tokens are used internally by NaClIPCAdapter and should have
108 // been cleared from the message when it is received here.
109 // These tokens should never be set for Non-SFI mode.
110 CHECK(file_token_lo
== 0);
111 CHECK(file_token_hi
== 0);
113 // Copy the file if we received a valid file descriptor. Otherwise, if we got
114 // a reply, the file doesn't exist, so provide an fd of -1.
115 // See IrtOpenResource() for how this function's result is interpreted.
116 if (ipc_fd
.is_file())
117 *fd
= ipc_fd
.descriptor().fd
;
123 int IrtOpenResource(const char* file
, int* fd
) {
124 // Remove leading '/' character.
128 ManifestService
* manifest_service
= GetManifestService();
129 if (manifest_service
== NULL
||
130 !manifest_service
->OpenResource(file
, fd
)) {
133 return (*fd
== -1) ? NACL_ABI_ENOENT
: 0;