Bug 1534780 - Add free interconversion between UniqueFileHandle and ipc::FileDescriptor and use it in a few places. r=froydnj

Now that UniqueFileHandle can be used more widely, and with
ipc::FileDescriptor being essentially a copyable UniqueFileHandle, it
makes sense to add a move constructor and a "forget"-like method to
convert between them when needed.

Depends on D26737

Differential Revision: https://phabricator.services.mozilla.com/D26738

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jed Davis 2019-06-28 19:46:58 +00:00
Родитель 370bd5030f
Коммит 0ba66c379f
4 изменённых файлов: 18 добавлений и 12 удалений

Просмотреть файл

@ -31,6 +31,9 @@ FileDescriptor::FileDescriptor(FileDescriptor&& aOther)
FileDescriptor::FileDescriptor(PlatformHandleType aHandle)
: mHandle(Clone(aHandle)) {}
FileDescriptor::FileDescriptor(UniquePlatformHandle&& aHandle)
: mHandle(std::move(aHandle)) {}
FileDescriptor::FileDescriptor(const IPDLPrivate&, const PickleType& aPickle) {
#ifdef XP_WIN
mHandle.reset(aPickle);
@ -89,6 +92,10 @@ FileDescriptor::UniquePlatformHandle FileDescriptor::ClonePlatformHandle()
return Clone(mHandle.get());
}
FileDescriptor::UniquePlatformHandle FileDescriptor::TakePlatformHandle() {
return UniquePlatformHandle(mHandle.release());
}
bool FileDescriptor::operator==(const FileDescriptor& aOther) const {
return mHandle == aOther.mHandle;
}

Просмотреть файл

@ -60,6 +60,8 @@ class FileDescriptor {
// The caller still have to close aHandle.
explicit FileDescriptor(PlatformHandleType aHandle);
explicit FileDescriptor(UniquePlatformHandle&& aHandle);
// This constructor WILL NOT duplicate the handle.
// FileDescriptor takes the ownership from IPC message.
FileDescriptor(const IPDLPrivate&, const PickleType& aPickle);
@ -83,6 +85,10 @@ class FileDescriptor {
// handle.
UniquePlatformHandle ClonePlatformHandle() const;
// Extracts the underlying handle and makes this object an invalid handle.
// (Compare UniquePtr::release.)
UniquePlatformHandle TakePlatformHandle();
// Only used in nsTArray.
bool operator==(const FileDescriptor& aOther) const;

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/Preferences.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "mozilla/UniquePtrExtensions.h"
namespace mozilla {
namespace ipc {
@ -133,16 +134,13 @@ bool SharedPreferenceDeserializer::DeserializeFromSharedMemory(
return false;
}
// The FileDescriptor constructor will clone this handle when constructed,
// so store it in a UniquePlatformHandle to make sure the original gets
// closed.
FileDescriptor::UniquePlatformHandle handle(
parseHandleArg(aPrefMapHandleStr));
if (!aPrefMapHandleStr || aPrefMapHandleStr[0] != '\0') {
return false;
}
mPrefMapHandle.emplace(handle.get());
mPrefMapHandle.emplace(std::move(handle));
#endif
mPrefsLen = Some(parseUIntPtrArg(aPrefsLenStr));
@ -160,17 +158,12 @@ bool SharedPreferenceDeserializer::DeserializeFromSharedMemory(
MOZ_RELEASE_ASSERT(gPrefsFd != -1);
mPrefsHandle = Some(base::FileDescriptor(gPrefsFd, /* auto_close */ true));
FileDescriptor::UniquePlatformHandle handle(gPrefMapFd);
mPrefMapHandle.emplace(handle.get());
mPrefMapHandle.emplace(UniqueFileHandle(gPrefMapFd));
#elif XP_UNIX
mPrefsHandle = Some(base::FileDescriptor(kPrefsFileDescriptor,
/* auto_close */ true));
// The FileDescriptor constructor will clone this handle when constructed,
// so store it in a UniquePlatformHandle to make sure the original gets
// closed.
FileDescriptor::UniquePlatformHandle handle(kPrefMapFileDescriptor);
mPrefMapHandle.emplace(handle.get());
mPrefMapHandle.emplace(UniqueFileHandle(kPrefMapFileDescriptor));
#endif
if (mPrefsHandle.isNothing() || mPrefsLen.isNothing() ||

Просмотреть файл

@ -87,7 +87,7 @@ class SandboxBrokerTest : public ::testing::Test {
mServer = SandboxBroker::Create(GetPolicy(), getpid(), fd);
ASSERT_NE(mServer, nullptr);
ASSERT_TRUE(fd.IsValid());
auto rawFD = fd.ClonePlatformHandle();
auto rawFD = fd.TakePlatformHandle();
mClient.reset(new SandboxBrokerClient(rawFD.release()));
}