ipc/glue/FileDescriptor.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef mozilla_ipc_FileDescriptor_h
michael@0 6 #define mozilla_ipc_FileDescriptor_h
michael@0 7
michael@0 8 #include "base/basictypes.h"
michael@0 9 #include "base/process.h"
michael@0 10 #include "mozilla/DebugOnly.h"
michael@0 11 #include "nscore.h"
michael@0 12
michael@0 13 #ifdef XP_WIN
michael@0 14 // Need the HANDLE typedef.
michael@0 15 #include <winnt.h>
michael@0 16 #else
michael@0 17 #include "base/file_descriptor_posix.h"
michael@0 18 #endif
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace ipc {
michael@0 22
michael@0 23 // This class is used by IPDL to share file descriptors across processes. When
michael@0 24 // sending a FileDescriptor IPDL will first duplicate a platform-specific file
michael@0 25 // handle type ('PlatformHandleType') into a handle that is valid in the other
michael@0 26 // process. Then IPDL will convert the duplicated handle into a type suitable
michael@0 27 // for pickling ('PickleType') and then send that through the IPC pipe. In the
michael@0 28 // receiving process the pickled data is converted into a platform-specific file
michael@0 29 // handle and then returned to the receiver.
michael@0 30 //
michael@0 31 // To use this class add 'FileDescriptor' as an argument in the IPDL protocol
michael@0 32 // and then pass a file descriptor from C++ to the Call/Send method. The
michael@0 33 // Answer/Recv method will receive a FileDescriptor& on which PlatformHandle()
michael@0 34 // can be called to return the platform file handle.
michael@0 35 class FileDescriptor
michael@0 36 {
michael@0 37 public:
michael@0 38 typedef base::ProcessHandle ProcessHandle;
michael@0 39
michael@0 40 #ifdef XP_WIN
michael@0 41 typedef HANDLE PlatformHandleType;
michael@0 42 typedef HANDLE PickleType;
michael@0 43 #else
michael@0 44 typedef int PlatformHandleType;
michael@0 45 typedef base::FileDescriptor PickleType;
michael@0 46 #endif
michael@0 47
michael@0 48 // This should only ever be created by IPDL.
michael@0 49 struct IPDLPrivate
michael@0 50 {};
michael@0 51
michael@0 52 FileDescriptor();
michael@0 53
michael@0 54 FileDescriptor(const FileDescriptor& aOther)
michael@0 55 : mHandleCreatedByOtherProcess(false),
michael@0 56 mHandleCreatedByOtherProcessWasUsed(false)
michael@0 57 {
michael@0 58 // Don't use operator= here because that will call
michael@0 59 // CloseCurrentProcessHandle() on this (uninitialized) object.
michael@0 60 Assign(aOther);
michael@0 61 }
michael@0 62
michael@0 63 FileDescriptor(PlatformHandleType aHandle);
michael@0 64
michael@0 65 FileDescriptor(const IPDLPrivate&, const PickleType& aPickle)
michael@0 66 #ifdef XP_WIN
michael@0 67 : mHandle(aPickle)
michael@0 68 #else
michael@0 69 : mHandle(aPickle.fd)
michael@0 70 #endif
michael@0 71 , mHandleCreatedByOtherProcess(true)
michael@0 72 , mHandleCreatedByOtherProcessWasUsed(false)
michael@0 73 { }
michael@0 74
michael@0 75 ~FileDescriptor()
michael@0 76 {
michael@0 77 CloseCurrentProcessHandle();
michael@0 78 }
michael@0 79
michael@0 80 FileDescriptor&
michael@0 81 operator=(const FileDescriptor& aOther)
michael@0 82 {
michael@0 83 CloseCurrentProcessHandle();
michael@0 84 Assign(aOther);
michael@0 85 return *this;
michael@0 86 }
michael@0 87
michael@0 88 // Performs platform-specific actions to duplicate mHandle in the other
michael@0 89 // process (e.g. dup() on POSIX, DuplicateHandle() on Windows). Returns a
michael@0 90 // pickled value that can be passed to the other process via IPC.
michael@0 91 PickleType
michael@0 92 ShareTo(const IPDLPrivate&, ProcessHandle aOtherProcess) const;
michael@0 93
michael@0 94 // Tests mHandle against a well-known invalid platform-specific file handle
michael@0 95 // (e.g. -1 on POSIX, INVALID_HANDLE_VALUE on Windows).
michael@0 96 bool
michael@0 97 IsValid() const
michael@0 98 {
michael@0 99 return IsValid(mHandle);
michael@0 100 }
michael@0 101
michael@0 102 PlatformHandleType
michael@0 103 PlatformHandle() const
michael@0 104 {
michael@0 105 if (mHandleCreatedByOtherProcess) {
michael@0 106 mHandleCreatedByOtherProcessWasUsed = true;
michael@0 107 }
michael@0 108 return mHandle;
michael@0 109 }
michael@0 110
michael@0 111 bool
michael@0 112 operator==(const FileDescriptor& aOther) const
michael@0 113 {
michael@0 114 return mHandle == aOther.mHandle;
michael@0 115 }
michael@0 116
michael@0 117 private:
michael@0 118 void
michael@0 119 Assign(const FileDescriptor& aOther)
michael@0 120 {
michael@0 121 if (aOther.mHandleCreatedByOtherProcess) {
michael@0 122 mHandleCreatedByOtherProcess = true;
michael@0 123 mHandleCreatedByOtherProcessWasUsed =
michael@0 124 aOther.mHandleCreatedByOtherProcessWasUsed;
michael@0 125 mHandle = aOther.PlatformHandle();
michael@0 126 } else {
michael@0 127 DuplicateInCurrentProcess(aOther.PlatformHandle());
michael@0 128 mHandleCreatedByOtherProcess = false;
michael@0 129 mHandleCreatedByOtherProcessWasUsed = false;
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 static bool
michael@0 134 IsValid(PlatformHandleType aHandle);
michael@0 135
michael@0 136 void
michael@0 137 DuplicateInCurrentProcess(PlatformHandleType aHandle);
michael@0 138
michael@0 139 void
michael@0 140 CloseCurrentProcessHandle();
michael@0 141
michael@0 142 PlatformHandleType mHandle;
michael@0 143
michael@0 144 // If this is true then this instance is created by IPDL to ferry a handle to
michael@0 145 // its eventual consumer and we never close the handle. If this is false then
michael@0 146 // we are a RAII wrapper around the handle and we close the handle on
michael@0 147 // destruction.
michael@0 148 bool mHandleCreatedByOtherProcess;
michael@0 149
michael@0 150 // This is to ensure that we don't leak the handle (which is only possible
michael@0 151 // when we're in the receiving process).
michael@0 152 mutable DebugOnly<bool> mHandleCreatedByOtherProcessWasUsed;
michael@0 153 };
michael@0 154
michael@0 155 } // namespace ipc
michael@0 156 } // namespace mozilla
michael@0 157
michael@0 158 #endif // mozilla_ipc_FileDescriptor_h

mercurial