2012-08-15 21:02:32 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "FileDescriptor.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#define INVALID_HANDLE INVALID_HANDLE_VALUE
|
2013-01-10 04:54:27 -08:00
|
|
|
#else
|
2012-08-15 21:02:32 -07:00
|
|
|
#include <unistd.h>
|
2013-01-10 04:00:17 -08:00
|
|
|
#define INVALID_HANDLE -1
|
2013-01-10 04:54:27 -08:00
|
|
|
#endif
|
2013-01-10 04:00:17 -08:00
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
|
|
|
|
FileDescriptor::FileDescriptor()
|
2013-01-10 04:54:27 -08:00
|
|
|
: mHandle(INVALID_HANDLE)
|
2012-08-15 21:02:32 -07:00
|
|
|
{ }
|
|
|
|
|
2013-01-10 04:00:17 -08:00
|
|
|
FileDescriptor::PickleType
|
|
|
|
FileDescriptor::ShareTo(const FileDescriptor::IPDLPrivate&,
|
|
|
|
FileDescriptor::ProcessHandle aOtherProcess) const
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
2013-01-10 04:54:27 -08:00
|
|
|
if (mHandle == INVALID_HANDLE) {
|
|
|
|
return INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlatformHandleType newHandle;
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), mHandle, aOtherProcess, &newHandle,
|
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_WARNING("Failed to duplicate file handle!");
|
2013-01-10 04:54:27 -08:00
|
|
|
return INVALID_HANDLE;
|
2012-08-15 21:02:32 -07:00
|
|
|
}
|
2013-01-10 04:54:27 -08:00
|
|
|
|
|
|
|
return newHandle;
|
2012-08-15 21:02:32 -07:00
|
|
|
#else // XP_WIN
|
2013-01-10 04:54:27 -08:00
|
|
|
if (mHandle == INVALID_HANDLE) {
|
|
|
|
return base::FileDescriptor();
|
2012-08-15 21:02:32 -07:00
|
|
|
}
|
2013-01-10 04:54:27 -08:00
|
|
|
|
|
|
|
PlatformHandleType newHandle = dup(mHandle);
|
|
|
|
if (newHandle < 0) {
|
|
|
|
NS_WARNING("Failed to duplicate file descriptor!");
|
|
|
|
return base::FileDescriptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This file descriptor must be closed once the caller is done using it, so
|
|
|
|
// pass true here for the 'auto_close' argument.
|
|
|
|
return base::FileDescriptor(newHandle, true);
|
2012-08-15 21:02:32 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_NOT_REACHED("Must not get here!");
|
|
|
|
return PickleType();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FileDescriptor::IsValid() const
|
|
|
|
{
|
|
|
|
return mHandle != INVALID_HANDLE;
|
|
|
|
}
|