1 // Copyright 2013 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_STREAM_STREAM_H_ 6 #define LIBRARIES_NACL_IO_STREAM_STREAM_H_ 7 8 #include "nacl_io/filesystem.h" 9 10 #include "ppapi/c/pp_completion_callback.h" 11 #include "ppapi/c/pp_resource.h" 12 13 namespace nacl_io { 14 15 // StreamFs provides a "mount point" for stream objects which do not provide a 16 // path, such as FDs returned by pipe, socket, and sockpair. It also provides 17 // a background thread for dispatching completion callbacks. 18 19 class StreamFs; 20 class StreamNode; 21 22 class StreamFs : public Filesystem { 23 public: 24 class Work { 25 public: 26 explicit Work(StreamFs* filesystem) : filesystem_(filesystem) {} 27 virtual ~Work() {} 28 29 // Called by adding work the queue, val should be safe to ignore. 30 virtual bool Start(int32_t val) = 0; 31 32 // Called as a completion of work in Start. Value of val depend on 33 // the function invoked in Start. 34 virtual void Run(int32_t val) = 0; 35 StreamFs* filesystem() { return filesystem_; } 36 37 private: 38 StreamFs* filesystem_; 39 }; 40 41 protected: 42 StreamFs(); 43 virtual ~StreamFs(); 44 45 public: 46 // Enqueue a work object onto this StreamFs's thread 47 void EnqueueWork(Work* work); 48 49 // Returns a completion callback which will execute the StartWork member 50 // of a MountSocketWork object. 51 static PP_CompletionCallback GetStartCompletion(Work* work); 52 53 // Returns a completion callback which will execute the RunCallback member 54 // of a MountSocketWork object. 55 static PP_CompletionCallback GetRunCompletion(Work* work); 56 57 virtual Error OpenWithMode(const Path& path, int o_flags, mode_t mode, 58 ScopedNode* out_node); 59 virtual Error Unlink(const Path& path); 60 virtual Error Mkdir(const Path& path, int permissions); 61 virtual Error Rmdir(const Path& path); 62 virtual Error Remove(const Path& path); 63 virtual Error Rename(const Path& path, const Path& newpath); 64 65 static void* StreamThreadThunk(void*); 66 void StreamThread(); 67 68 private: 69 PP_Resource message_loop_; 70 pthread_cond_t message_cond_; 71 sdk_util::SimpleLock message_lock_; 72 73 friend class KernelProxy; 74 DISALLOW_COPY_AND_ASSIGN(StreamFs); 75 }; 76 77 } // namespace nacl_io 78 79 #endif // LIBRARIES_NACL_IO_STREAM_STREAM_H_ 80