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. 4 5 #include "nacl_io/devfs/jspipe_node.h" 6 7 #include <cstring> 8 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" 15 16 #define TRACE(format, ...) LOG_TRACE("jspipe: " format, ##__VA_ARGS__) 17 #define ERROR(format, ...) LOG_TRACE("jspipe: " format, ##__VA_ARGS__) 18 19 namespace { 20 const size_t kPostMessageBufferSize = 512 * 1024; 21 } 22 23 namespace nacl_io { 24 25 JSPipeNode::JSPipeNode(Filesystem* filesystem) 26 : Node(filesystem), 27 pipe_(new JSPipeEventEmitter(filesystem_->ppapi(), 28 kPostMessageBufferSize)) { 29 } 30 31 JSPipeEventEmitter* JSPipeNode::GetEventEmitter() { 32 return pipe_.get(); 33 } 34 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; 40 41 EventListenerLock wait(GetEventEmitter()); 42 Error err = wait.WaitOnEvent(POLLIN, ms); 43 if (err == ETIMEDOUT) 44 err = EWOULDBLOCK; 45 if (err) 46 return err; 47 48 return GetEventEmitter()->Read_Locked(static_cast<char*>(buf), count, 49 out_bytes); 50 } 51 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); 58 59 EventListenerLock wait(GetEventEmitter()); 60 Error err = wait.WaitOnEvent(POLLOUT, ms); 61 if (err == ETIMEDOUT) 62 err = EWOULDBLOCK; 63 if (err) 64 return err; 65 66 return GetEventEmitter()->Write_Locked(static_cast<const char*>(buf), 67 count, out_bytes); 68 } 69 70 Error JSPipeNode::VIoctl(int request, va_list args) { 71 AUTO_LOCK(node_lock_); 72 73 switch (request) { 74 case NACL_IOC_PIPE_SETNAME: { 75 const char* new_name = va_arg(args, char*); 76 return GetEventEmitter()->SetName(new_name); 77 } 78 case NACL_IOC_PIPE_GETISPACE: { 79 int* space = va_arg(args, int*); 80 *space = GetEventEmitter()->GetISpace(); 81 return 0; 82 } 83 case NACL_IOC_PIPE_GETOSPACE: { 84 int* space = va_arg(args, int*); 85 *space = GetEventEmitter()->GetOSpace(); 86 return 0; 87 } 88 case NACL_IOC_HANDLEMESSAGE: { 89 struct PP_Var* message = va_arg(args, struct PP_Var*); 90 return GetEventEmitter()->HandleJSMessage(*message); 91 } 92 default: 93 TRACE("unknown ioctl: %#x", request); 94 break; 95 } 96 97 return EINVAL; 98 } 99 100 } // namespace nacl_io 101