Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / devfs / jspipe_node.cc
blob1cb62685f4bfe77c859db8a550b551918af378e6
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 "nacl_io/devfs/jspipe_node.h"
7 #include <cstring>
9 #include "nacl_io/devfs/dev_fs.h"
10 #include "nacl_io/error.h"
11 #include "nacl_io/ioctl.h"
12 #include "nacl_io/kernel_handle.h"
13 #include "nacl_io/log.h"
14 #include "nacl_io/pepper_interface.h"
16 #define TRACE(format, ...) LOG_TRACE("jspipe: " format, ##__VA_ARGS__)
17 #define ERROR(format, ...) LOG_TRACE("jspipe: " format, ##__VA_ARGS__)
19 namespace {
20 const size_t kPostMessageBufferSize = 512 * 1024;
23 namespace nacl_io {
25 JSPipeNode::JSPipeNode(Filesystem* filesystem)
26 : Node(filesystem),
27 pipe_(new JSPipeEventEmitter(filesystem_->ppapi(),
28 kPostMessageBufferSize)) {
31 JSPipeEventEmitter* JSPipeNode::GetEventEmitter() {
32 return pipe_.get();
35 Error JSPipeNode::Read(const HandleAttr& attr,
36 void* buf,
37 size_t count,
38 int* out_bytes) {
39 int ms = attr.IsBlocking() ? -1 : 0;
41 EventListenerLock wait(GetEventEmitter());
42 Error err = wait.WaitOnEvent(POLLIN, ms);
43 if (err == ETIMEDOUT)
44 err = EWOULDBLOCK;
45 if (err)
46 return err;
48 return GetEventEmitter()->Read_Locked(static_cast<char*>(buf), count,
49 out_bytes);
52 Error JSPipeNode::Write(const HandleAttr& attr,
53 const void* buf,
54 size_t count,
55 int* out_bytes) {
56 int ms = attr.IsBlocking() ? -1 : 0;
57 TRACE("write timeout=%d", ms);
59 EventListenerLock wait(GetEventEmitter());
60 Error err = wait.WaitOnEvent(POLLOUT, ms);
61 if (err == ETIMEDOUT)
62 err = EWOULDBLOCK;
63 if (err)
64 return err;
66 return GetEventEmitter()->Write_Locked(static_cast<const char*>(buf),
67 count, out_bytes);
70 Error JSPipeNode::VIoctl(int request, va_list args) {
71 AUTO_LOCK(node_lock_);
73 switch (request) {
74 case NACL_IOC_PIPE_SETNAME: {
75 const char* new_name = va_arg(args, char*);
76 return GetEventEmitter()->SetName(new_name);
78 case NACL_IOC_PIPE_GETISPACE: {
79 int* space = va_arg(args, int*);
80 *space = GetEventEmitter()->GetISpace();
81 return 0;
83 case NACL_IOC_PIPE_GETOSPACE: {
84 int* space = va_arg(args, int*);
85 *space = GetEventEmitter()->GetOSpace();
86 return 0;
88 case NACL_IOC_HANDLEMESSAGE: {
89 struct PP_Var* message = va_arg(args, struct PP_Var*);
90 return GetEventEmitter()->HandleJSMessage(*message);
92 default:
93 TRACE("unknown ioctl: %#x", request);
94 break;
97 return EINVAL;
100 } // namespace nacl_io