FileDescriptorUtils.h (1611B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_FileDescriptorUtils_h 8 #define mozilla_ipc_FileDescriptorUtils_h 9 10 #include "mozilla/ipc/FileDescriptor.h" 11 #include "nsThreadUtils.h" 12 #include <stdio.h> 13 14 namespace mozilla { 15 namespace ipc { 16 17 // When Dispatch() is called (from main thread) this class arranges to close the 18 // provided FileDescriptor on one of the socket transport service threads (to 19 // avoid main thread I/O). 20 class CloseFileRunnable final : public Runnable { 21 typedef mozilla::ipc::FileDescriptor FileDescriptor; 22 23 FileDescriptor mFileDescriptor; 24 25 public: 26 explicit CloseFileRunnable(const FileDescriptor& aFileDescriptor); 27 28 NS_DECL_ISUPPORTS_INHERITED 29 NS_DECL_NSIRUNNABLE 30 31 void Dispatch(); 32 33 private: 34 ~CloseFileRunnable(); 35 36 void CloseFile(); 37 }; 38 39 // On failure, FileDescriptorToFILE returns nullptr; on success, 40 // returns duplicated FILE*. 41 // This is meant for use with FileDescriptors received over IPC. 42 FILE* FileDescriptorToFILE(const FileDescriptor& aDesc, const char* aOpenMode); 43 44 // FILEToFileDescriptor does not consume the given FILE*; it must be 45 // fclose()d as normal, and this does not invalidate the returned 46 // FileDescriptor. 47 FileDescriptor FILEToFileDescriptor(FILE* aStream); 48 49 } // namespace ipc 50 } // namespace mozilla 51 52 #endif // mozilla_ipc_FileDescriptorUtils_h