Roll src/third_party/skia 1107e90:06f3647
[chromium-blink-merge.git] / ppapi / nacl_irt / manifest_service.cc
bloba97c5d42d02c10e88160e26045c63bc0b0556436
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"
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 connected_event_(
33 true /* manual_reset */, false /* initially_signaled */) {
36 bool Send(IPC::Message* message) override {
37 // Wait until set up is actually done.
38 connected_event_.Wait();
39 return SyncMessageFilter::Send(message);
42 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
43 // Send().
44 void OnFilterAdded(IPC::Sender* sender) override {
45 SyncMessageFilter::OnFilterAdded(sender);
46 connected_event_.Signal();
49 // If an error is found, unblocks the Send(), too, to return an error.
50 void OnChannelError() override {
51 SyncMessageFilter::OnChannelError();
52 connected_event_.Signal();
55 // Similar to OnChannelError, unblocks the Send() on the channel closing.
56 void OnChannelClosing() override {
57 SyncMessageFilter::OnChannelClosing();
58 connected_event_.Signal();
61 private:
62 base::WaitableEvent connected_event_;
64 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter);
67 ManifestService::ManifestService(
68 const IPC::ChannelHandle& handle,
69 scoped_refptr<base::MessageLoopProxy> io_message_loop,
70 base::WaitableEvent* shutdown_event) {
71 filter_ = new ManifestMessageFilter(shutdown_event);
72 channel_ = IPC::ChannelProxy::Create(handle,
73 IPC::Channel::MODE_SERVER,
74 NULL, // Listener
75 io_message_loop.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