/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* vim: set ts=8 sts=2 et sw=2 tw=80: *//* 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"base/message_loop.h"#include"mozilla/ipc/Transport.h"#include"mozilla/ipc/ProtocolUtils.h"usingbase::ProcessHandle;namespacemozilla{namespaceipc{nsresultCreateTransport(base::ProcessIdaProcIdOne,TransportDescriptor*aOne,TransportDescriptor*aTwo){std::wstringid=IPC::Channel::GenerateVerifiedChannelID(std::wstring());// Use MODE_SERVER to force creation of the pipeTransportt(id,Transport::MODE_SERVER,nullptr);HANDLEserverPipe=t.GetServerPipeHandle();if(!serverPipe){returnNS_ERROR_TRANSPORT_INIT;}// NB: we create the server pipe immediately, instead of just// grabbing an ID, on purpose. In the current setup, the client// needs to connect to an existing server pipe, so to prevent race// conditions, we create the server side here. When we send the pipe// to the server, we DuplicateHandle it to the server process to give it// access.HANDLEserverDup;DWORDaccess=0;DWORDoptions=DUPLICATE_SAME_ACCESS;if(!DuplicateHandle(serverPipe,base::GetCurrentProcId(),&serverDup,access,options)){returnNS_ERROR_DUPLICATE_HANDLE;}aOne->mPipeName=aTwo->mPipeName=id;aOne->mServerPipeHandle=serverDup;aOne->mDestinationProcessId=aProcIdOne;aTwo->mServerPipeHandle=INVALID_HANDLE_VALUE;aTwo->mDestinationProcessId=0;returnNS_OK;}HANDLETransferHandleToProcess(HANDLEsource,base::ProcessIdpid){// At this point we're sending the handle to another process.if(source==INVALID_HANDLE_VALUE){returnsource;}HANDLEhandleDup;DWORDaccess=0;DWORDoptions=DUPLICATE_SAME_ACCESS;boolok=DuplicateHandle(source,pid,&handleDup,access,options);if(!ok){returnnullptr;}// Now close our own copy of the handle (we're supposed to be transferring,// not copying).CloseHandle(source);returnhandleDup;}UniquePtr<Transport>OpenDescriptor(constTransportDescriptor&aTd,Transport::ModeaMode){if(aTd.mServerPipeHandle!=INVALID_HANDLE_VALUE){MOZ_RELEASE_ASSERT(aTd.mDestinationProcessId==base::GetCurrentProcId());}returnMakeUnique<Transport>(aTd.mPipeName,aTd.mServerPipeHandle,aMode,nullptr);}UniquePtr<Transport>OpenDescriptor(constFileDescriptor&aFd,Transport::ModeaMode){MOZ_ASSERT_UNREACHABLE("Not implemented!");returnnullptr;}TransportDescriptorDuplicateDescriptor(constTransportDescriptor&aTd){// We're duplicating this handle in our own process for bookkeeping purposes.if(aTd.mServerPipeHandle==INVALID_HANDLE_VALUE){returnaTd;}HANDLEserverDup;DWORDaccess=0;DWORDoptions=DUPLICATE_SAME_ACCESS;boolok=DuplicateHandle(aTd.mServerPipeHandle,base::GetCurrentProcId(),&serverDup,access,options);if(!ok){AnnotateSystemError();}MOZ_RELEASE_ASSERT(ok);TransportDescriptordesc=aTd;desc.mServerPipeHandle=serverDup;returndesc;}voidCloseDescriptor(constTransportDescriptor&aTd){// We're closing our own local copy of the pipe.CloseHandle(aTd.mServerPipeHandle);}}// namespace ipc}// namespace mozilla