michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ 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 "MiniShmParent.h" michael@0: michael@0: #include "base/scoped_handle.h" michael@0: michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace plugins { michael@0: michael@0: // static michael@0: const unsigned int MiniShmParent::kDefaultMiniShmSectionSize = 0x1000; michael@0: michael@0: MiniShmParent::MiniShmParent() michael@0: : mSectionSize(0), michael@0: mParentEvent(nullptr), michael@0: mParentGuard(nullptr), michael@0: mChildEvent(nullptr), michael@0: mChildGuard(nullptr), michael@0: mRegWait(nullptr), michael@0: mFileMapping(nullptr), michael@0: mView(nullptr), michael@0: mIsConnected(false), michael@0: mTimeout(INFINITE) michael@0: { michael@0: } michael@0: michael@0: MiniShmParent::~MiniShmParent() michael@0: { michael@0: CleanUp(); michael@0: } michael@0: michael@0: void michael@0: MiniShmParent::CleanUp() michael@0: { michael@0: if (mRegWait) { michael@0: ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); michael@0: mRegWait = nullptr; michael@0: } michael@0: if (mParentEvent) { michael@0: ::CloseHandle(mParentEvent); michael@0: mParentEvent = nullptr; michael@0: } michael@0: if (mParentGuard) { michael@0: ::CloseHandle(mParentGuard); michael@0: mParentGuard = nullptr; michael@0: } michael@0: if (mChildEvent) { michael@0: ::CloseHandle(mChildEvent); michael@0: mChildEvent = nullptr; michael@0: } michael@0: if (mChildGuard) { michael@0: ::CloseHandle(mChildGuard); michael@0: mChildGuard = nullptr; michael@0: } michael@0: if (mView) { michael@0: ::UnmapViewOfFile(mView); michael@0: mView = nullptr; michael@0: } michael@0: if (mFileMapping) { michael@0: ::CloseHandle(mFileMapping); michael@0: mFileMapping = nullptr; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: MiniShmParent::Init(MiniShmObserver* aObserver, const DWORD aTimeout, michael@0: const unsigned int aSectionSize) michael@0: { michael@0: if (!aObserver || !aSectionSize || (aSectionSize % 0x1000) || !aTimeout) { michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: if (mFileMapping) { michael@0: return NS_ERROR_ALREADY_INITIALIZED; michael@0: } michael@0: SECURITY_ATTRIBUTES securityAttributes = {sizeof(securityAttributes), michael@0: nullptr, michael@0: TRUE}; michael@0: ScopedHandle parentEvent(::CreateEvent(&securityAttributes, michael@0: FALSE, michael@0: FALSE, michael@0: nullptr)); michael@0: if (!parentEvent.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: ScopedHandle parentGuard(::CreateEvent(&securityAttributes, michael@0: FALSE, michael@0: TRUE, michael@0: nullptr)); michael@0: if (!parentGuard.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: ScopedHandle childEvent(::CreateEvent(&securityAttributes, michael@0: FALSE, michael@0: FALSE, michael@0: nullptr)); michael@0: if (!childEvent.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: ScopedHandle childGuard(::CreateEvent(&securityAttributes, michael@0: FALSE, michael@0: TRUE, michael@0: nullptr)); michael@0: if (!childGuard.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: ScopedHandle mapping(::CreateFileMapping(INVALID_HANDLE_VALUE, michael@0: &securityAttributes, michael@0: PAGE_READWRITE, michael@0: 0, michael@0: aSectionSize, michael@0: nullptr)); michael@0: if (!mapping.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: ScopedMappedFileView view(::MapViewOfFile(mapping, michael@0: FILE_MAP_WRITE, michael@0: 0, 0, 0)); michael@0: if (!view.IsValid()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: nsresult rv = SetView(view, aSectionSize, false); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = SetGuard(childGuard, aTimeout); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: MiniShmInit* initStruct = nullptr; michael@0: rv = GetWritePtrInternal(initStruct); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: initStruct->mParentEvent = parentEvent; michael@0: initStruct->mParentGuard = parentGuard; michael@0: initStruct->mChildEvent = childEvent; michael@0: initStruct->mChildGuard = childGuard; michael@0: michael@0: if (!::RegisterWaitForSingleObject(&mRegWait, michael@0: parentEvent, michael@0: &SOnEvent, michael@0: this, michael@0: INFINITE, michael@0: WT_EXECUTEDEFAULT)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mParentEvent = parentEvent.Take(); michael@0: mParentGuard = parentGuard.Take(); michael@0: mChildEvent = childEvent.Take(); michael@0: mChildGuard = childGuard.Take(); michael@0: mFileMapping = mapping.Take(); michael@0: mView = view.Take(); michael@0: mSectionSize = aSectionSize; michael@0: SetObserver(aObserver); michael@0: mTimeout = aTimeout; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: MiniShmParent::GetCookie(std::wstring& cookie) michael@0: { michael@0: if (!mFileMapping) { michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: } michael@0: std::wostringstream oss; michael@0: oss << mFileMapping; michael@0: if (!oss) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: cookie = oss.str(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: MiniShmParent::Send() michael@0: { michael@0: if (!mChildEvent) { michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: } michael@0: if (!::SetEvent(mChildEvent)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: MiniShmParent::IsConnected() const michael@0: { michael@0: return mIsConnected; michael@0: } michael@0: michael@0: void michael@0: MiniShmParent::OnEvent() michael@0: { michael@0: if (mIsConnected) { michael@0: MiniShmBase::OnEvent(); michael@0: } else { michael@0: FinalizeConnection(); michael@0: } michael@0: ::SetEvent(mParentGuard); michael@0: } michael@0: michael@0: void michael@0: MiniShmParent::FinalizeConnection() michael@0: { michael@0: const MiniShmInitComplete* initCompleteStruct = nullptr; michael@0: nsresult rv = GetReadPtr(initCompleteStruct); michael@0: mIsConnected = NS_SUCCEEDED(rv) && initCompleteStruct->mSucceeded; michael@0: if (mIsConnected) { michael@0: OnConnect(); michael@0: } michael@0: } michael@0: michael@0: } // namespace plugins michael@0: } // namespace mozilla michael@0: