michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=8 et : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "chrome/common/child_process_info.h" michael@0: michael@0: #include "mozilla/ipc/Transport.h" michael@0: #include "mozilla/ipc/FileDescriptor.h" michael@0: michael@0: using namespace base; michael@0: using namespace std; michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: bool michael@0: CreateTransport(ProcessHandle /*unused*/, ProcessHandle /*unused*/, michael@0: TransportDescriptor* aOne, TransportDescriptor* aTwo) michael@0: { michael@0: // Gecko doesn't care about this random ID, and the argument to this michael@0: // function isn't really necessary, it can be just any random michael@0: // pointer value michael@0: wstring id = ChildProcessInfo::GenerateRandomChannelID(aOne); michael@0: // Use MODE_SERVER to force creation of the socketpair michael@0: Transport t(id, Transport::MODE_SERVER, nullptr); michael@0: int fd1 = t.GetFileDescriptor(); michael@0: int fd2, dontcare; michael@0: t.GetClientFileDescriptorMapping(&fd2, &dontcare); michael@0: if (fd1 < 0 || fd2 < 0) { michael@0: return false; michael@0: } michael@0: michael@0: // The Transport closes these fds when it goes out of scope, so we michael@0: // dup them here michael@0: fd1 = dup(fd1); michael@0: fd2 = dup(fd2); michael@0: if (fd1 < 0 || fd2 < 0) { michael@0: return false; michael@0: } michael@0: michael@0: aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/); michael@0: aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/); michael@0: return true; michael@0: } michael@0: michael@0: Transport* michael@0: OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode) michael@0: { michael@0: return new Transport(aTd.mFd.fd, aMode, nullptr); michael@0: } michael@0: michael@0: Transport* michael@0: OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode) michael@0: { michael@0: return new Transport(aFd.PlatformHandle(), aMode, nullptr); michael@0: } michael@0: michael@0: void michael@0: CloseDescriptor(const TransportDescriptor& aTd) michael@0: { michael@0: close(aTd.mFd.fd); michael@0: } michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla