Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / nacl_irt / manifest_service.cc
blob41e8601f3b5af96c5bee72ba73d5e5ded2cd6ecb
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/single_thread_task_runner.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"
16 #if !defined(OS_NACL_SFI)
17 #include <pthread.h>
18 #include <map>
19 #include <string>
20 #endif
22 namespace ppapi {
24 // IPC channel is asynchronously set up. So, the NaCl process may try to
25 // send a OpenResource message to the host before the connection is
26 // established. In such a case, it is necessary to wait for the set up
27 // completion.
28 class ManifestMessageFilter : public IPC::SyncMessageFilter {
29 public:
30 ManifestMessageFilter(base::WaitableEvent* shutdown_event)
31 : SyncMessageFilter(shutdown_event,
32 false /* is_channel_send_thread_safe */),
33 connected_event_(
34 true /* manual_reset */, false /* initially_signaled */) {
37 bool Send(IPC::Message* message) override {
38 // Wait until set up is actually done.
39 connected_event_.Wait();
40 return SyncMessageFilter::Send(message);
43 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
44 // Send().
45 void OnFilterAdded(IPC::Sender* sender) override {
46 SyncMessageFilter::OnFilterAdded(sender);
47 connected_event_.Signal();
50 // If an error is found, unblocks the Send(), too, to return an error.
51 void OnChannelError() override {
52 SyncMessageFilter::OnChannelError();
53 connected_event_.Signal();
56 // Similar to OnChannelError, unblocks the Send() on the channel closing.
57 void OnChannelClosing() override {
58 SyncMessageFilter::OnChannelClosing();
59 connected_event_.Signal();
62 private:
63 base::WaitableEvent connected_event_;
65 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter);
68 ManifestService::ManifestService(
69 const IPC::ChannelHandle& handle,
70 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
71 base::WaitableEvent* shutdown_event) {
72 filter_ = new ManifestMessageFilter(shutdown_event);
73 channel_ = IPC::ChannelProxy::Create(handle, IPC::Channel::MODE_SERVER,
74 NULL, // Listener
75 io_task_runner.get());
76 channel_->AddFilter(filter_.get());
79 ManifestService::~ManifestService() {
82 void ManifestService::StartupInitializationComplete() {
83 filter_->Send(new PpapiHostMsg_StartupInitializationComplete);
86 bool ManifestService::OpenResource(const char* file, int* fd) {
87 // We currently restrict to only allow one concurrent open_resource() call
88 // per plugin. This could be fixed by doing a token lookup with
89 // NaClProcessMsg_ResolveFileTokenAsyncReply instead of using a
90 // global inside components/nacl/loader/nacl_listener.cc
91 base::AutoLock lock(open_resource_lock_);
93 // OpenResource will return INVALID SerializedHandle, if it is not supported.
94 // Specifically, PNaCl doesn't support open resource.
95 ppapi::proxy::SerializedHandle ipc_fd;
97 // File tokens are ignored here, but needed when the message is processed
98 // inside NaClIPCAdapter.
99 uint64_t file_token_lo = 0;
100 uint64_t file_token_hi = 0;
101 if (!filter_->Send(new PpapiHostMsg_OpenResource(
102 file,
103 &ipc_fd,
104 &file_token_lo,
105 &file_token_hi))) {
106 LOG(ERROR) << "ManifestService::OpenResource failed:" << file;
107 *fd = -1;
108 return false;
111 // File tokens are used internally by NaClIPCAdapter and should have
112 // been cleared from the message when it is received here.
113 // These tokens should never be set for Non-SFI mode.
114 CHECK(file_token_lo == 0);
115 CHECK(file_token_hi == 0);
117 // Copy the file if we received a valid file descriptor. Otherwise, if we got
118 // a reply, the file doesn't exist, so provide an fd of -1.
119 // See IrtOpenResource() for how this function's result is interpreted.
120 if (ipc_fd.is_file())
121 *fd = ipc_fd.descriptor().fd;
122 else
123 *fd = -1;
124 return true;
127 #if !defined(OS_NACL_SFI)
128 namespace {
130 pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
131 std::map<std::string, int>* g_prefetched_fds;
133 } // namespace
135 void RegisterPreopenedDescriptorsNonSfi(
136 std::map<std::string, int>* key_fd_map) {
137 pthread_mutex_lock(&g_mu);
138 DCHECK(!g_prefetched_fds);
139 g_prefetched_fds = key_fd_map;
140 pthread_mutex_unlock(&g_mu);
142 #endif
144 int IrtOpenResource(const char* file, int* fd) {
145 // Remove leading '/' character.
146 if (file[0] == '/')
147 ++file;
149 #if !defined(OS_NACL_SFI)
150 // Fast path for prefetched FDs.
151 pthread_mutex_lock(&g_mu);
152 if (g_prefetched_fds) {
153 std::map<std::string, int>::iterator it = g_prefetched_fds->find(file);
154 if (it != g_prefetched_fds->end()) {
155 *fd = it->second;
156 g_prefetched_fds->erase(it);
157 pthread_mutex_unlock(&g_mu);
158 return 0;
161 pthread_mutex_unlock(&g_mu);
162 #endif
164 ManifestService* manifest_service = GetManifestService();
165 if (manifest_service == NULL ||
166 !manifest_service->OpenResource(file, fd)) {
167 return NACL_ABI_EIO;
169 return (*fd == -1) ? NACL_ABI_ENOENT : 0;
172 } // namespace ppapi