tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FileDescriptor.h (2546B)


      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_FileDescriptor_h
      8 #define mozilla_ipc_FileDescriptor_h
      9 
     10 #include "base/basictypes.h"
     11 #include "base/process.h"
     12 #include "mozilla/UniquePtrExtensions.h"
     13 
     14 namespace mozilla {
     15 namespace ipc {
     16 
     17 // This class is used by IPDL to share file descriptors across processes. When
     18 // sending a FileDescriptor, IPDL will transfer a duplicate of the handle into
     19 // the remote process.
     20 //
     21 // To use this class add 'FileDescriptor' as an argument in the IPDL protocol
     22 // and then pass a file descriptor from C++ to the Send method. The Recv method
     23 // will receive a FileDescriptor& on which PlatformHandle() can be called to
     24 // return the platform file handle.
     25 class FileDescriptor {
     26 public:
     27  typedef base::ProcessId ProcessId;
     28 
     29  using UniquePlatformHandle = mozilla::UniqueFileHandle;
     30  using PlatformHandleType = UniquePlatformHandle::element_type;
     31 
     32  // This should only ever be created by IPDL.
     33  struct IPDLPrivate {};
     34 
     35  // Represents an invalid handle.
     36  FileDescriptor();
     37 
     38  // Copy constructor will duplicate a new handle.
     39  FileDescriptor(const FileDescriptor& aOther);
     40 
     41  FileDescriptor(FileDescriptor&& aOther);
     42 
     43  // This constructor will duplicate a new handle.
     44  // The caller still have to close aHandle.
     45  explicit FileDescriptor(PlatformHandleType aHandle);
     46 
     47  explicit FileDescriptor(UniquePlatformHandle&& aHandle);
     48 
     49  ~FileDescriptor();
     50 
     51  FileDescriptor& operator=(const FileDescriptor& aOther);
     52 
     53  FileDescriptor& operator=(FileDescriptor&& aOther);
     54 
     55  // Tests mHandle against a well-known invalid platform-specific file handle
     56  // (e.g. -1 on POSIX, INVALID_HANDLE_VALUE on Windows).
     57  bool IsValid() const;
     58 
     59  // Returns a duplicated handle, it is caller's responsibility to close the
     60  // handle.
     61  UniquePlatformHandle ClonePlatformHandle() const;
     62 
     63  // Extracts the underlying handle and makes this object an invalid handle.
     64  // (Compare UniquePtr::release.)
     65  UniquePlatformHandle TakePlatformHandle();
     66 
     67  // Only used in nsTArray.
     68  bool operator==(const FileDescriptor& aOther) const;
     69 
     70 private:
     71  static UniqueFileHandle Clone(PlatformHandleType aHandle);
     72 
     73  UniquePlatformHandle mHandle;
     74 };
     75 
     76 }  // namespace ipc
     77 }  // namespace mozilla
     78 
     79 #endif  // mozilla_ipc_FileDescriptor_h