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 "MiniShmParent.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "base/scoped_handle.h" |
michael@0 | 10 | |
michael@0 | 11 | #include <sstream> |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace plugins { |
michael@0 | 15 | |
michael@0 | 16 | // static |
michael@0 | 17 | const unsigned int MiniShmParent::kDefaultMiniShmSectionSize = 0x1000; |
michael@0 | 18 | |
michael@0 | 19 | MiniShmParent::MiniShmParent() |
michael@0 | 20 | : mSectionSize(0), |
michael@0 | 21 | mParentEvent(nullptr), |
michael@0 | 22 | mParentGuard(nullptr), |
michael@0 | 23 | mChildEvent(nullptr), |
michael@0 | 24 | mChildGuard(nullptr), |
michael@0 | 25 | mRegWait(nullptr), |
michael@0 | 26 | mFileMapping(nullptr), |
michael@0 | 27 | mView(nullptr), |
michael@0 | 28 | mIsConnected(false), |
michael@0 | 29 | mTimeout(INFINITE) |
michael@0 | 30 | { |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | MiniShmParent::~MiniShmParent() |
michael@0 | 34 | { |
michael@0 | 35 | CleanUp(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | void |
michael@0 | 39 | MiniShmParent::CleanUp() |
michael@0 | 40 | { |
michael@0 | 41 | if (mRegWait) { |
michael@0 | 42 | ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE); |
michael@0 | 43 | mRegWait = nullptr; |
michael@0 | 44 | } |
michael@0 | 45 | if (mParentEvent) { |
michael@0 | 46 | ::CloseHandle(mParentEvent); |
michael@0 | 47 | mParentEvent = nullptr; |
michael@0 | 48 | } |
michael@0 | 49 | if (mParentGuard) { |
michael@0 | 50 | ::CloseHandle(mParentGuard); |
michael@0 | 51 | mParentGuard = nullptr; |
michael@0 | 52 | } |
michael@0 | 53 | if (mChildEvent) { |
michael@0 | 54 | ::CloseHandle(mChildEvent); |
michael@0 | 55 | mChildEvent = nullptr; |
michael@0 | 56 | } |
michael@0 | 57 | if (mChildGuard) { |
michael@0 | 58 | ::CloseHandle(mChildGuard); |
michael@0 | 59 | mChildGuard = nullptr; |
michael@0 | 60 | } |
michael@0 | 61 | if (mView) { |
michael@0 | 62 | ::UnmapViewOfFile(mView); |
michael@0 | 63 | mView = nullptr; |
michael@0 | 64 | } |
michael@0 | 65 | if (mFileMapping) { |
michael@0 | 66 | ::CloseHandle(mFileMapping); |
michael@0 | 67 | mFileMapping = nullptr; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | nsresult |
michael@0 | 72 | MiniShmParent::Init(MiniShmObserver* aObserver, const DWORD aTimeout, |
michael@0 | 73 | const unsigned int aSectionSize) |
michael@0 | 74 | { |
michael@0 | 75 | if (!aObserver || !aSectionSize || (aSectionSize % 0x1000) || !aTimeout) { |
michael@0 | 76 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 77 | } |
michael@0 | 78 | if (mFileMapping) { |
michael@0 | 79 | return NS_ERROR_ALREADY_INITIALIZED; |
michael@0 | 80 | } |
michael@0 | 81 | SECURITY_ATTRIBUTES securityAttributes = {sizeof(securityAttributes), |
michael@0 | 82 | nullptr, |
michael@0 | 83 | TRUE}; |
michael@0 | 84 | ScopedHandle parentEvent(::CreateEvent(&securityAttributes, |
michael@0 | 85 | FALSE, |
michael@0 | 86 | FALSE, |
michael@0 | 87 | nullptr)); |
michael@0 | 88 | if (!parentEvent.IsValid()) { |
michael@0 | 89 | return NS_ERROR_FAILURE; |
michael@0 | 90 | } |
michael@0 | 91 | ScopedHandle parentGuard(::CreateEvent(&securityAttributes, |
michael@0 | 92 | FALSE, |
michael@0 | 93 | TRUE, |
michael@0 | 94 | nullptr)); |
michael@0 | 95 | if (!parentGuard.IsValid()) { |
michael@0 | 96 | return NS_ERROR_FAILURE; |
michael@0 | 97 | } |
michael@0 | 98 | ScopedHandle childEvent(::CreateEvent(&securityAttributes, |
michael@0 | 99 | FALSE, |
michael@0 | 100 | FALSE, |
michael@0 | 101 | nullptr)); |
michael@0 | 102 | if (!childEvent.IsValid()) { |
michael@0 | 103 | return NS_ERROR_FAILURE; |
michael@0 | 104 | } |
michael@0 | 105 | ScopedHandle childGuard(::CreateEvent(&securityAttributes, |
michael@0 | 106 | FALSE, |
michael@0 | 107 | TRUE, |
michael@0 | 108 | nullptr)); |
michael@0 | 109 | if (!childGuard.IsValid()) { |
michael@0 | 110 | return NS_ERROR_FAILURE; |
michael@0 | 111 | } |
michael@0 | 112 | ScopedHandle mapping(::CreateFileMapping(INVALID_HANDLE_VALUE, |
michael@0 | 113 | &securityAttributes, |
michael@0 | 114 | PAGE_READWRITE, |
michael@0 | 115 | 0, |
michael@0 | 116 | aSectionSize, |
michael@0 | 117 | nullptr)); |
michael@0 | 118 | if (!mapping.IsValid()) { |
michael@0 | 119 | return NS_ERROR_FAILURE; |
michael@0 | 120 | } |
michael@0 | 121 | ScopedMappedFileView view(::MapViewOfFile(mapping, |
michael@0 | 122 | FILE_MAP_WRITE, |
michael@0 | 123 | 0, 0, 0)); |
michael@0 | 124 | if (!view.IsValid()) { |
michael@0 | 125 | return NS_ERROR_FAILURE; |
michael@0 | 126 | } |
michael@0 | 127 | nsresult rv = SetView(view, aSectionSize, false); |
michael@0 | 128 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 129 | rv = SetGuard(childGuard, aTimeout); |
michael@0 | 130 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 131 | |
michael@0 | 132 | MiniShmInit* initStruct = nullptr; |
michael@0 | 133 | rv = GetWritePtrInternal(initStruct); |
michael@0 | 134 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 135 | initStruct->mParentEvent = parentEvent; |
michael@0 | 136 | initStruct->mParentGuard = parentGuard; |
michael@0 | 137 | initStruct->mChildEvent = childEvent; |
michael@0 | 138 | initStruct->mChildGuard = childGuard; |
michael@0 | 139 | |
michael@0 | 140 | if (!::RegisterWaitForSingleObject(&mRegWait, |
michael@0 | 141 | parentEvent, |
michael@0 | 142 | &SOnEvent, |
michael@0 | 143 | this, |
michael@0 | 144 | INFINITE, |
michael@0 | 145 | WT_EXECUTEDEFAULT)) { |
michael@0 | 146 | return NS_ERROR_FAILURE; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | mParentEvent = parentEvent.Take(); |
michael@0 | 150 | mParentGuard = parentGuard.Take(); |
michael@0 | 151 | mChildEvent = childEvent.Take(); |
michael@0 | 152 | mChildGuard = childGuard.Take(); |
michael@0 | 153 | mFileMapping = mapping.Take(); |
michael@0 | 154 | mView = view.Take(); |
michael@0 | 155 | mSectionSize = aSectionSize; |
michael@0 | 156 | SetObserver(aObserver); |
michael@0 | 157 | mTimeout = aTimeout; |
michael@0 | 158 | return NS_OK; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | nsresult |
michael@0 | 162 | MiniShmParent::GetCookie(std::wstring& cookie) |
michael@0 | 163 | { |
michael@0 | 164 | if (!mFileMapping) { |
michael@0 | 165 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 166 | } |
michael@0 | 167 | std::wostringstream oss; |
michael@0 | 168 | oss << mFileMapping; |
michael@0 | 169 | if (!oss) { |
michael@0 | 170 | return NS_ERROR_FAILURE; |
michael@0 | 171 | } |
michael@0 | 172 | cookie = oss.str(); |
michael@0 | 173 | return NS_OK; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | nsresult |
michael@0 | 177 | MiniShmParent::Send() |
michael@0 | 178 | { |
michael@0 | 179 | if (!mChildEvent) { |
michael@0 | 180 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 181 | } |
michael@0 | 182 | if (!::SetEvent(mChildEvent)) { |
michael@0 | 183 | return NS_ERROR_FAILURE; |
michael@0 | 184 | } |
michael@0 | 185 | return NS_OK; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | bool |
michael@0 | 189 | MiniShmParent::IsConnected() const |
michael@0 | 190 | { |
michael@0 | 191 | return mIsConnected; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | void |
michael@0 | 195 | MiniShmParent::OnEvent() |
michael@0 | 196 | { |
michael@0 | 197 | if (mIsConnected) { |
michael@0 | 198 | MiniShmBase::OnEvent(); |
michael@0 | 199 | } else { |
michael@0 | 200 | FinalizeConnection(); |
michael@0 | 201 | } |
michael@0 | 202 | ::SetEvent(mParentGuard); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | void |
michael@0 | 206 | MiniShmParent::FinalizeConnection() |
michael@0 | 207 | { |
michael@0 | 208 | const MiniShmInitComplete* initCompleteStruct = nullptr; |
michael@0 | 209 | nsresult rv = GetReadPtr(initCompleteStruct); |
michael@0 | 210 | mIsConnected = NS_SUCCEEDED(rv) && initCompleteStruct->mSucceeded; |
michael@0 | 211 | if (mIsConnected) { |
michael@0 | 212 | OnConnect(); |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | } // namespace plugins |
michael@0 | 217 | } // namespace mozilla |
michael@0 | 218 |