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