Home | History | Annotate | Download | only in devfs
      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_EVENT_EMITTER_H_
      6 #define LIBRARIES_NACL_IO_DEVFS_JSPIPE_EVENT_EMITTER_H_
      7 
      8 #include <poll.h>
      9 #include <ppapi/c/pp_var.h>
     10 #include <stdint.h>
     11 #include <stdlib.h>
     12 
     13 #include <string>
     14 
     15 #include "nacl_io/fifo_char.h"
     16 #include "nacl_io/pipe/pipe_event_emitter.h"
     17 
     18 #include "sdk_util/auto_lock.h"
     19 #include "sdk_util/macros.h"
     20 
     21 namespace nacl_io {
     22 
     23 class PepperInterface;
     24 class MessagingInterface;
     25 class VarInterface;
     26 class VarArrayInterface;
     27 class VarArrayBufferInterface;
     28 class VarDictionaryInterface;
     29 
     30 class JSPipeEventEmitter;
     31 typedef sdk_util::ScopedRef<JSPipeEventEmitter> ScopedJSPipeEventEmitter;
     32 
     33 class JSPipeEventEmitter : public EventEmitter {
     34  public:
     35   JSPipeEventEmitter(PepperInterface* ppapi, size_t size);
     36   virtual void Destroy();
     37 
     38   Error Read_Locked(char* data, size_t len, int* out_bytes);
     39   Error Write_Locked(const char* data, size_t len, int* out_bytes);
     40 
     41   size_t GetOSpace() {
     42     return post_message_buffer_size_ - BytesOutstanding();
     43   };
     44   size_t GetISpace() {
     45     return input_fifo_.WriteAvailable();
     46   };
     47   Error SetName(const char* name);
     48   Error HandleJSMessage(PP_Var message);
     49 
     50  protected:
     51   size_t HandleJSWrite(const char* data, size_t len);
     52   void HandleJSAck(size_t byte_count);
     53   Error HandleJSWrite(PP_Var message);
     54   Error HandleJSAck(PP_Var message);
     55   void UpdateStatus_Locked();
     56   PP_Var VarFromCStr(const char* string);
     57   Error SendAckMessage(size_t byte_count);
     58   size_t BytesOutstanding() { return bytes_sent_ - bytes_acked_; }
     59   Error SendMessageToJS(PP_Var operation, PP_Var payload);
     60   Error SendWriteMessage(const void* buf, size_t count);
     61   int VarStrcmp(PP_Var a, PP_Var b);
     62 
     63  private:
     64   std::string name_;
     65   FIFOChar input_fifo_;
     66 
     67   // Number of bytes that to send via PostMessage before and ACK
     68   // is required.
     69   size_t post_message_buffer_size_;
     70   size_t bytes_sent_;
     71   size_t bytes_acked_;
     72   size_t bytes_read_;
     73 
     74   PepperInterface* ppapi_;
     75   MessagingInterface* messaging_iface_;
     76   VarInterface* var_iface_;
     77   VarArrayInterface* array_iface_;
     78   VarArrayBufferInterface* buffer_iface_;
     79   VarDictionaryInterface* dict_iface_;
     80 
     81   PP_Var pipe_name_var_;
     82   PP_Var pipe_key_;
     83   PP_Var operation_key_;
     84   PP_Var payload_key_;
     85   PP_Var write_var_;
     86   PP_Var ack_var_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(JSPipeEventEmitter);
     89 };
     90 
     91 }  // namespace nacl_io
     92 
     93 #endif  // LIBRARIES_NACL_IO_DEVFS_JSPIPE_EVENT_EMITTER_H_
     94