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 #ifndef LIBRARIES_NACL_IO_DEVFS_JSPIPE_NODE_H_ 6 #define LIBRARIES_NACL_IO_DEVFS_JSPIPE_NODE_H_ 7 8 #include <ppapi/c/pp_var.h> 9 #include <string> 10 11 #include "nacl_io/devfs/jspipe_event_emitter.h" 12 #include "nacl_io/stream/stream_node.h" 13 14 namespace nacl_io { 15 16 class MessagingInterface; 17 class VarInterface; 18 class VarArrayInterface; 19 class VarArrayBufferInterface; 20 class VarDictionaryInterface; 21 22 /** 23 * JSPipeNode represents a two-way channel for communicating with JavaScript 24 * via calls to PostMessage. In order to use these some amount of logic on 25 * the JavaScript side is also required. The protocol to the communication 26 * looks the same in both directions and consists of two message types: 27 * 'write' and 'ack'. 28 * The messages are formated as JavaScript dictionary objects and take the 29 * following form: 30 * { 31 * pipe: <pipe_name>, 32 * operation: <operation_name>, 33 * payload: <operations_payload> 34 * } 35 * The payload for 'write' message is a ArrayBuffer containing binary data. 36 * The payload for 'ack' messages is the total number of bytes recieved at 37 * the other end. 38 * For example: { pipe: 'jspipe1', operation: 'ack', payload: 234 } 39 * 40 * Messages coming from JavaScript must be delivered using the 41 * NACL_IOC_HANDLEMESSAGE ioctl on the file handle. 42 */ 43 class JSPipeNode : public Node { 44 public: 45 explicit JSPipeNode(Filesystem* filesystem); 46 47 virtual void Destroy() { 48 LOG_TRACE("JSPipeNode: Destroy"); 49 } 50 51 virtual JSPipeEventEmitter* GetEventEmitter(); 52 53 virtual Error VIoctl(int request, va_list args); 54 55 virtual Error Read(const HandleAttr& attr, 56 void* buf, 57 size_t count, 58 int* out_bytes); 59 virtual Error Write(const HandleAttr& attr, 60 const void* buf, 61 size_t count, 62 int* out_bytes); 63 64 protected: 65 Error SendAck(); 66 67 ScopedJSPipeEventEmitter pipe_; 68 }; 69 70 } // namespace nacl_io 71 72 #endif // LIBRARIES_NACL_IO_DEVFS_JSPIPE_NODE_H_ 73