Bug 1897589 - Simplify worker shutdown checks. r=jstutte,dom-worker-reviewers
[gecko.git] / ipc / glue / FileDescriptor.h
blob15160624da7af034711de2ba12ad13bb10df5103
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/. */
7 #ifndef mozilla_ipc_FileDescriptor_h
8 #define mozilla_ipc_FileDescriptor_h
10 #include "base/basictypes.h"
11 #include "base/process.h"
12 #include "mozilla/UniquePtrExtensions.h"
14 namespace mozilla {
15 namespace ipc {
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.
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;
29 using UniquePlatformHandle = mozilla::UniqueFileHandle;
30 using PlatformHandleType = UniquePlatformHandle::ElementType;
32 // This should only ever be created by IPDL.
33 struct IPDLPrivate {};
35 // Represents an invalid handle.
36 FileDescriptor();
38 // Copy constructor will duplicate a new handle.
39 FileDescriptor(const FileDescriptor& aOther);
41 FileDescriptor(FileDescriptor&& aOther);
43 // This constructor will duplicate a new handle.
44 // The caller still have to close aHandle.
45 explicit FileDescriptor(PlatformHandleType aHandle);
47 explicit FileDescriptor(UniquePlatformHandle&& aHandle);
49 ~FileDescriptor();
51 FileDescriptor& operator=(const FileDescriptor& aOther);
53 FileDescriptor& operator=(FileDescriptor&& aOther);
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;
59 // Returns a duplicated handle, it is caller's responsibility to close the
60 // handle.
61 UniquePlatformHandle ClonePlatformHandle() const;
63 // Extracts the underlying handle and makes this object an invalid handle.
64 // (Compare UniquePtr::release.)
65 UniquePlatformHandle TakePlatformHandle();
67 // Only used in nsTArray.
68 bool operator==(const FileDescriptor& aOther) const;
70 private:
71 static UniqueFileHandle Clone(PlatformHandleType aHandle);
73 UniquePlatformHandle mHandle;
76 } // namespace ipc
77 } // namespace mozilla
79 #endif // mozilla_ipc_FileDescriptor_h