Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "MiniShmChild.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <limits> |
michael@0 | 10 | #include <sstream> |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | namespace plugins { |
michael@0 | 14 | |
michael@0 | 15 | MiniShmChild::MiniShmChild() |
michael@0 | 16 | : mParentEvent(nullptr), |
michael@0 | 17 | mParentGuard(nullptr), |
michael@0 | 18 | mChildEvent(nullptr), |
michael@0 | 19 | mChildGuard(nullptr), |
michael@0 | 20 | mFileMapping(nullptr), |
michael@0 | 21 | mRegWait(nullptr), |
michael@0 | 22 | mView(nullptr), |
michael@0 | 23 | mTimeout(INFINITE) |
michael@0 | 24 | {} |
michael@0 | 25 | |
michael@0 | 26 | MiniShmChild::~MiniShmChild() |
michael@0 | 27 | { |
michael@0 | 28 | if (mRegWait) { |
michael@0 | 29 | ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); |
michael@0 | 30 | } |
michael@0 | 31 | if (mParentGuard) { |
michael@0 | 32 | // Try to avoid shutting down while the parent's event handler is running. |
michael@0 | 33 | ::WaitForSingleObject(mParentGuard, mTimeout); |
michael@0 | 34 | ::CloseHandle(mParentGuard); |
michael@0 | 35 | } |
michael@0 | 36 | if (mParentEvent) { |
michael@0 | 37 | ::CloseHandle(mParentEvent); |
michael@0 | 38 | } |
michael@0 | 39 | if (mChildEvent) { |
michael@0 | 40 | ::CloseHandle(mChildEvent); |
michael@0 | 41 | } |
michael@0 | 42 | if (mChildGuard) { |
michael@0 | 43 | ::CloseHandle(mChildGuard); |
michael@0 | 44 | } |
michael@0 | 45 | if (mView) { |
michael@0 | 46 | ::UnmapViewOfFile(mView); |
michael@0 | 47 | } |
michael@0 | 48 | if (mFileMapping) { |
michael@0 | 49 | ::CloseHandle(mFileMapping); |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | nsresult |
michael@0 | 54 | MiniShmChild::Init(MiniShmObserver* aObserver, const std::wstring& aCookie, |
michael@0 | 55 | const DWORD aTimeout) |
michael@0 | 56 | { |
michael@0 | 57 | if (aCookie.empty() || !aTimeout) { |
michael@0 | 58 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 59 | } |
michael@0 | 60 | if (mFileMapping) { |
michael@0 | 61 | return NS_ERROR_ALREADY_INITIALIZED; |
michael@0 | 62 | } |
michael@0 | 63 | std::wistringstream iss(aCookie); |
michael@0 | 64 | HANDLE mapHandle = nullptr; |
michael@0 | 65 | iss >> mapHandle; |
michael@0 | 66 | if (!iss) { |
michael@0 | 67 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 68 | } |
michael@0 | 69 | ScopedMappedFileView view(::MapViewOfFile(mapHandle, |
michael@0 | 70 | FILE_MAP_WRITE, |
michael@0 | 71 | 0, 0, 0)); |
michael@0 | 72 | if (!view.IsValid()) { |
michael@0 | 73 | return NS_ERROR_FAILURE; |
michael@0 | 74 | } |
michael@0 | 75 | MEMORY_BASIC_INFORMATION memInfo = {0}; |
michael@0 | 76 | SIZE_T querySize = ::VirtualQuery(view, &memInfo, sizeof(memInfo)); |
michael@0 | 77 | unsigned int mappingSize = 0; |
michael@0 | 78 | if (querySize) { |
michael@0 | 79 | if (memInfo.RegionSize <= std::numeric_limits<unsigned int>::max()) { |
michael@0 | 80 | mappingSize = static_cast<unsigned int>(memInfo.RegionSize); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | if (!querySize || !mappingSize) { |
michael@0 | 84 | return NS_ERROR_FAILURE; |
michael@0 | 85 | } |
michael@0 | 86 | nsresult rv = SetView(view, mappingSize, true); |
michael@0 | 87 | if (NS_FAILED(rv)) { |
michael@0 | 88 | return rv; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | const MiniShmInit* initStruct = nullptr; |
michael@0 | 92 | rv = GetReadPtr(initStruct); |
michael@0 | 93 | if (NS_FAILED(rv)) { |
michael@0 | 94 | return rv; |
michael@0 | 95 | } |
michael@0 | 96 | if (!initStruct->mParentEvent || !initStruct->mParentGuard || |
michael@0 | 97 | !initStruct->mChildEvent || !initStruct->mChildGuard) { |
michael@0 | 98 | return NS_ERROR_FAILURE; |
michael@0 | 99 | } |
michael@0 | 100 | rv = SetGuard(initStruct->mParentGuard, aTimeout); |
michael@0 | 101 | if (NS_FAILED(rv)) { |
michael@0 | 102 | return rv; |
michael@0 | 103 | } |
michael@0 | 104 | if (!::RegisterWaitForSingleObject(&mRegWait, |
michael@0 | 105 | initStruct->mChildEvent, |
michael@0 | 106 | &SOnEvent, |
michael@0 | 107 | this, |
michael@0 | 108 | INFINITE, |
michael@0 | 109 | WT_EXECUTEDEFAULT)) { |
michael@0 | 110 | return NS_ERROR_FAILURE; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | MiniShmInitComplete* initCompleteStruct = nullptr; |
michael@0 | 114 | rv = GetWritePtrInternal(initCompleteStruct); |
michael@0 | 115 | if (NS_FAILED(rv)) { |
michael@0 | 116 | ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); |
michael@0 | 117 | mRegWait = nullptr; |
michael@0 | 118 | return NS_ERROR_FAILURE; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | initCompleteStruct->mSucceeded = true; |
michael@0 | 122 | |
michael@0 | 123 | // We must set the member variables before we signal the event |
michael@0 | 124 | mFileMapping = mapHandle; |
michael@0 | 125 | mView = view.Take(); |
michael@0 | 126 | mParentEvent = initStruct->mParentEvent; |
michael@0 | 127 | mParentGuard = initStruct->mParentGuard; |
michael@0 | 128 | mChildEvent = initStruct->mChildEvent; |
michael@0 | 129 | mChildGuard = initStruct->mChildGuard; |
michael@0 | 130 | SetObserver(aObserver); |
michael@0 | 131 | mTimeout = aTimeout; |
michael@0 | 132 | |
michael@0 | 133 | rv = Send(); |
michael@0 | 134 | if (NS_FAILED(rv)) { |
michael@0 | 135 | initCompleteStruct->mSucceeded = false; |
michael@0 | 136 | mFileMapping = nullptr; |
michael@0 | 137 | view.Set(mView); |
michael@0 | 138 | mView = nullptr; |
michael@0 | 139 | mParentEvent = nullptr; |
michael@0 | 140 | mParentGuard = nullptr; |
michael@0 | 141 | mChildEvent = nullptr; |
michael@0 | 142 | mChildGuard = nullptr; |
michael@0 | 143 | ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); |
michael@0 | 144 | mRegWait = nullptr; |
michael@0 | 145 | return rv; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | OnConnect(); |
michael@0 | 149 | return NS_OK; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | nsresult |
michael@0 | 153 | MiniShmChild::Send() |
michael@0 | 154 | { |
michael@0 | 155 | if (!mParentEvent) { |
michael@0 | 156 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 157 | } |
michael@0 | 158 | if (!::SetEvent(mParentEvent)) { |
michael@0 | 159 | return NS_ERROR_FAILURE; |
michael@0 | 160 | } |
michael@0 | 161 | return NS_OK; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | void |
michael@0 | 165 | MiniShmChild::OnEvent() |
michael@0 | 166 | { |
michael@0 | 167 | MiniShmBase::OnEvent(); |
michael@0 | 168 | ::SetEvent(mChildGuard); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | } // namespace plugins |
michael@0 | 172 | } // namespace mozilla |
michael@0 | 173 |