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 "MiniShmChild.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace plugins { michael@0: michael@0: MiniShmChild::MiniShmChild() michael@0: : mParentEvent(nullptr), michael@0: mParentGuard(nullptr), michael@0: mChildEvent(nullptr), michael@0: mChildGuard(nullptr), michael@0: mFileMapping(nullptr), michael@0: mRegWait(nullptr), michael@0: mView(nullptr), michael@0: mTimeout(INFINITE) michael@0: {} michael@0: michael@0: MiniShmChild::~MiniShmChild() michael@0: { michael@0: if (mRegWait) { michael@0: ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); michael@0: } michael@0: if (mParentGuard) { michael@0: // Try to avoid shutting down while the parent's event handler is running. michael@0: ::WaitForSingleObject(mParentGuard, mTimeout); michael@0: ::CloseHandle(mParentGuard); michael@0: } michael@0: if (mParentEvent) { michael@0: ::CloseHandle(mParentEvent); michael@0: } michael@0: if (mChildEvent) { michael@0: ::CloseHandle(mChildEvent); michael@0: } michael@0: if (mChildGuard) { michael@0: ::CloseHandle(mChildGuard); michael@0: } michael@0: if (mView) { michael@0: ::UnmapViewOfFile(mView); michael@0: } michael@0: if (mFileMapping) { michael@0: ::CloseHandle(mFileMapping); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: MiniShmChild::Init(MiniShmObserver* aObserver, const std::wstring& aCookie, michael@0: const DWORD aTimeout) michael@0: { michael@0: if (aCookie.empty() || !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: std::wistringstream iss(aCookie); michael@0: HANDLE mapHandle = nullptr; michael@0: iss >> mapHandle; michael@0: if (!iss) { michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: ScopedMappedFileView view(::MapViewOfFile(mapHandle, 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: MEMORY_BASIC_INFORMATION memInfo = {0}; michael@0: SIZE_T querySize = ::VirtualQuery(view, &memInfo, sizeof(memInfo)); michael@0: unsigned int mappingSize = 0; michael@0: if (querySize) { michael@0: if (memInfo.RegionSize <= std::numeric_limits::max()) { michael@0: mappingSize = static_cast(memInfo.RegionSize); michael@0: } michael@0: } michael@0: if (!querySize || !mappingSize) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: nsresult rv = SetView(view, mappingSize, true); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: const MiniShmInit* initStruct = nullptr; michael@0: rv = GetReadPtr(initStruct); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: if (!initStruct->mParentEvent || !initStruct->mParentGuard || michael@0: !initStruct->mChildEvent || !initStruct->mChildGuard) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: rv = SetGuard(initStruct->mParentGuard, aTimeout); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: if (!::RegisterWaitForSingleObject(&mRegWait, michael@0: initStruct->mChildEvent, 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: MiniShmInitComplete* initCompleteStruct = nullptr; michael@0: rv = GetWritePtrInternal(initCompleteStruct); michael@0: if (NS_FAILED(rv)) { michael@0: ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); michael@0: mRegWait = nullptr; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: initCompleteStruct->mSucceeded = true; michael@0: michael@0: // We must set the member variables before we signal the event michael@0: mFileMapping = mapHandle; michael@0: mView = view.Take(); michael@0: mParentEvent = initStruct->mParentEvent; michael@0: mParentGuard = initStruct->mParentGuard; michael@0: mChildEvent = initStruct->mChildEvent; michael@0: mChildGuard = initStruct->mChildGuard; michael@0: SetObserver(aObserver); michael@0: mTimeout = aTimeout; michael@0: michael@0: rv = Send(); michael@0: if (NS_FAILED(rv)) { michael@0: initCompleteStruct->mSucceeded = false; michael@0: mFileMapping = nullptr; michael@0: view.Set(mView); michael@0: mView = nullptr; michael@0: mParentEvent = nullptr; michael@0: mParentGuard = nullptr; michael@0: mChildEvent = nullptr; michael@0: mChildGuard = nullptr; michael@0: ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); michael@0: mRegWait = nullptr; michael@0: return rv; michael@0: } michael@0: michael@0: OnConnect(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: MiniShmChild::Send() michael@0: { michael@0: if (!mParentEvent) { michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: } michael@0: if (!::SetEvent(mParentEvent)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: MiniShmChild::OnEvent() michael@0: { michael@0: MiniShmBase::OnEvent(); michael@0: ::SetEvent(mChildGuard); michael@0: } michael@0: michael@0: } // namespace plugins michael@0: } // namespace mozilla michael@0: