dom/plugins/ipc/hangui/MiniShmBase.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 #ifndef mozilla_plugins_MiniShmBase_h
michael@0 8 #define mozilla_plugins_MiniShmBase_h
michael@0 9
michael@0 10 #include "base/basictypes.h"
michael@0 11
michael@0 12 #include "nsDebug.h"
michael@0 13
michael@0 14 #include <windows.h>
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace plugins {
michael@0 18
michael@0 19 /**
michael@0 20 * This class is used to provide RAII semantics for mapped views.
michael@0 21 * @see ScopedHandle
michael@0 22 */
michael@0 23 class ScopedMappedFileView
michael@0 24 {
michael@0 25 public:
michael@0 26 explicit
michael@0 27 ScopedMappedFileView(LPVOID aView)
michael@0 28 : mView(aView)
michael@0 29 {
michael@0 30 }
michael@0 31
michael@0 32 ~ScopedMappedFileView()
michael@0 33 {
michael@0 34 Close();
michael@0 35 }
michael@0 36
michael@0 37 void
michael@0 38 Close()
michael@0 39 {
michael@0 40 if (mView) {
michael@0 41 ::UnmapViewOfFile(mView);
michael@0 42 mView = nullptr;
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 Set(LPVOID aView)
michael@0 48 {
michael@0 49 Close();
michael@0 50 mView = aView;
michael@0 51 }
michael@0 52
michael@0 53 LPVOID
michael@0 54 Get() const
michael@0 55 {
michael@0 56 return mView;
michael@0 57 }
michael@0 58
michael@0 59 LPVOID
michael@0 60 Take()
michael@0 61 {
michael@0 62 LPVOID result = mView;
michael@0 63 mView = nullptr;
michael@0 64 return result;
michael@0 65 }
michael@0 66
michael@0 67 operator LPVOID()
michael@0 68 {
michael@0 69 return mView;
michael@0 70 }
michael@0 71
michael@0 72 bool
michael@0 73 IsValid() const
michael@0 74 {
michael@0 75 return (mView);
michael@0 76 }
michael@0 77
michael@0 78 private:
michael@0 79 DISALLOW_COPY_AND_ASSIGN(ScopedMappedFileView);
michael@0 80
michael@0 81 LPVOID mView;
michael@0 82 };
michael@0 83
michael@0 84 class MiniShmBase;
michael@0 85
michael@0 86 class MiniShmObserver
michael@0 87 {
michael@0 88 public:
michael@0 89 /**
michael@0 90 * This function is called whenever there is a new shared memory request.
michael@0 91 * @param aMiniShmObj MiniShmBase object that may be used to read and
michael@0 92 * write from shared memory.
michael@0 93 */
michael@0 94 virtual void OnMiniShmEvent(MiniShmBase *aMiniShmObj) = 0;
michael@0 95 /**
michael@0 96 * This function is called once when a MiniShmParent and a MiniShmChild
michael@0 97 * object have successfully negotiated a connection.
michael@0 98 *
michael@0 99 * @param aMiniShmObj MiniShmBase object that may be used to read and
michael@0 100 * write from shared memory.
michael@0 101 */
michael@0 102 virtual void OnMiniShmConnect(MiniShmBase *aMiniShmObj) { }
michael@0 103 };
michael@0 104
michael@0 105 /**
michael@0 106 * Base class for MiniShm connections. This class defines the common
michael@0 107 * interfaces and code between parent and child.
michael@0 108 */
michael@0 109 class MiniShmBase
michael@0 110 {
michael@0 111 public:
michael@0 112 /**
michael@0 113 * Obtains a writable pointer into shared memory of type T.
michael@0 114 * typename T must be plain-old-data and contain an unsigned integral
michael@0 115 * member T::identifier that uniquely identifies T with respect to
michael@0 116 * other types used by the protocol being implemented.
michael@0 117 *
michael@0 118 * @param aPtr Pointer to receive the shared memory address.
michael@0 119 * This value is set if and only if the function
michael@0 120 * succeeded.
michael@0 121 * @return NS_OK if and only if aPtr was successfully obtained.
michael@0 122 * NS_ERROR_ILLEGAL_VALUE if type T is not valid for MiniShm.
michael@0 123 * NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
michael@0 124 * NS_ERROR_NOT_AVAILABLE if the memory is not safe to write.
michael@0 125 */
michael@0 126 template<typename T> nsresult
michael@0 127 GetWritePtr(T*& aPtr)
michael@0 128 {
michael@0 129 if (!mWriteHeader || !mGuard) {
michael@0 130 return NS_ERROR_NOT_INITIALIZED;
michael@0 131 }
michael@0 132 if (sizeof(T) > mPayloadMaxLen ||
michael@0 133 T::identifier <= RESERVED_CODE_LAST) {
michael@0 134 return NS_ERROR_ILLEGAL_VALUE;
michael@0 135 }
michael@0 136 if (::WaitForSingleObject(mGuard, mTimeout) != WAIT_OBJECT_0) {
michael@0 137 return NS_ERROR_NOT_AVAILABLE;
michael@0 138 }
michael@0 139 mWriteHeader->mId = T::identifier;
michael@0 140 mWriteHeader->mPayloadLen = sizeof(T);
michael@0 141 aPtr = reinterpret_cast<T*>(mWriteHeader + 1);
michael@0 142 return NS_OK;
michael@0 143 }
michael@0 144
michael@0 145 /**
michael@0 146 * Obtains a readable pointer into shared memory of type T.
michael@0 147 * typename T must be plain-old-data and contain an unsigned integral
michael@0 148 * member T::identifier that uniquely identifies T with respect to
michael@0 149 * other types used by the protocol being implemented.
michael@0 150 *
michael@0 151 * @param aPtr Pointer to receive the shared memory address.
michael@0 152 * This value is set if and only if the function
michael@0 153 * succeeded.
michael@0 154 * @return NS_OK if and only if aPtr was successfully obtained.
michael@0 155 * NS_ERROR_ILLEGAL_VALUE if type T is not valid for MiniShm or if
michael@0 156 * type T does not match the type of the data
michael@0 157 * stored in shared memory.
michael@0 158 * NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
michael@0 159 */
michael@0 160 template<typename T> nsresult
michael@0 161 GetReadPtr(const T*& aPtr)
michael@0 162 {
michael@0 163 if (!mReadHeader) {
michael@0 164 return NS_ERROR_NOT_INITIALIZED;
michael@0 165 }
michael@0 166 if (mReadHeader->mId != T::identifier ||
michael@0 167 sizeof(T) != mReadHeader->mPayloadLen) {
michael@0 168 return NS_ERROR_ILLEGAL_VALUE;
michael@0 169 }
michael@0 170 aPtr = reinterpret_cast<const T*>(mReadHeader + 1);
michael@0 171 return NS_OK;
michael@0 172 }
michael@0 173
michael@0 174 /**
michael@0 175 * Fires the peer's event causing its request handler to execute.
michael@0 176 *
michael@0 177 * @return Should return NS_OK if the send was successful.
michael@0 178 */
michael@0 179 virtual nsresult
michael@0 180 Send() = 0;
michael@0 181
michael@0 182 protected:
michael@0 183 /**
michael@0 184 * MiniShm reserves some identifier codes for its own use. Any
michael@0 185 * identifiers used by MiniShm protocol implementations must be
michael@0 186 * greater than RESERVED_CODE_LAST.
michael@0 187 */
michael@0 188 enum ReservedCodes
michael@0 189 {
michael@0 190 RESERVED_CODE_INIT = 0,
michael@0 191 RESERVED_CODE_INIT_COMPLETE = 1,
michael@0 192 RESERVED_CODE_LAST = RESERVED_CODE_INIT_COMPLETE
michael@0 193 };
michael@0 194
michael@0 195 struct MiniShmHeader
michael@0 196 {
michael@0 197 unsigned int mId;
michael@0 198 unsigned int mPayloadLen;
michael@0 199 };
michael@0 200
michael@0 201 struct MiniShmInit
michael@0 202 {
michael@0 203 enum identifier_t
michael@0 204 {
michael@0 205 identifier = RESERVED_CODE_INIT
michael@0 206 };
michael@0 207 HANDLE mParentEvent;
michael@0 208 HANDLE mParentGuard;
michael@0 209 HANDLE mChildEvent;
michael@0 210 HANDLE mChildGuard;
michael@0 211 };
michael@0 212
michael@0 213 struct MiniShmInitComplete
michael@0 214 {
michael@0 215 enum identifier_t
michael@0 216 {
michael@0 217 identifier = RESERVED_CODE_INIT_COMPLETE
michael@0 218 };
michael@0 219 bool mSucceeded;
michael@0 220 };
michael@0 221
michael@0 222 MiniShmBase()
michael@0 223 : mObserver(nullptr),
michael@0 224 mWriteHeader(nullptr),
michael@0 225 mReadHeader(nullptr),
michael@0 226 mPayloadMaxLen(0),
michael@0 227 mGuard(nullptr),
michael@0 228 mTimeout(INFINITE)
michael@0 229 {
michael@0 230 }
michael@0 231 virtual ~MiniShmBase()
michael@0 232 { }
michael@0 233
michael@0 234 virtual void
michael@0 235 OnEvent()
michael@0 236 {
michael@0 237 if (mObserver) {
michael@0 238 mObserver->OnMiniShmEvent(this);
michael@0 239 }
michael@0 240 }
michael@0 241
michael@0 242 virtual void
michael@0 243 OnConnect()
michael@0 244 {
michael@0 245 if (mObserver) {
michael@0 246 mObserver->OnMiniShmConnect(this);
michael@0 247 }
michael@0 248 }
michael@0 249
michael@0 250 nsresult
michael@0 251 SetView(LPVOID aView, const unsigned int aSize, bool aIsChild)
michael@0 252 {
michael@0 253 if (!aView || aSize <= 2 * sizeof(MiniShmHeader)) {
michael@0 254 return NS_ERROR_ILLEGAL_VALUE;
michael@0 255 }
michael@0 256 // Divide the region into halves for parent and child
michael@0 257 if (aIsChild) {
michael@0 258 mReadHeader = static_cast<MiniShmHeader*>(aView);
michael@0 259 mWriteHeader = reinterpret_cast<MiniShmHeader*>(static_cast<char*>(aView)
michael@0 260 + aSize / 2U);
michael@0 261 } else {
michael@0 262 mWriteHeader = static_cast<MiniShmHeader*>(aView);
michael@0 263 mReadHeader = reinterpret_cast<MiniShmHeader*>(static_cast<char*>(aView)
michael@0 264 + aSize / 2U);
michael@0 265 }
michael@0 266 mPayloadMaxLen = aSize / 2U - sizeof(MiniShmHeader);
michael@0 267 return NS_OK;
michael@0 268 }
michael@0 269
michael@0 270 nsresult
michael@0 271 SetGuard(HANDLE aGuard, DWORD aTimeout)
michael@0 272 {
michael@0 273 if (!aGuard || !aTimeout) {
michael@0 274 return NS_ERROR_ILLEGAL_VALUE;
michael@0 275 }
michael@0 276 mGuard = aGuard;
michael@0 277 mTimeout = aTimeout;
michael@0 278 return NS_OK;
michael@0 279 }
michael@0 280
michael@0 281 inline void
michael@0 282 SetObserver(MiniShmObserver *aObserver) { mObserver = aObserver; }
michael@0 283
michael@0 284 /**
michael@0 285 * Obtains a writable pointer into shared memory of type T. This version
michael@0 286 * differs from GetWritePtr in that it allows typename T to be one of
michael@0 287 * the private data structures declared in MiniShmBase.
michael@0 288 *
michael@0 289 * @param aPtr Pointer to receive the shared memory address.
michael@0 290 * This value is set if and only if the function
michael@0 291 * succeeded.
michael@0 292 * @return NS_OK if and only if aPtr was successfully obtained.
michael@0 293 * NS_ERROR_ILLEGAL_VALUE if type T not an internal MiniShm struct.
michael@0 294 * NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
michael@0 295 */
michael@0 296 template<typename T> nsresult
michael@0 297 GetWritePtrInternal(T*& aPtr)
michael@0 298 {
michael@0 299 if (!mWriteHeader) {
michael@0 300 return NS_ERROR_NOT_INITIALIZED;
michael@0 301 }
michael@0 302 if (sizeof(T) > mPayloadMaxLen ||
michael@0 303 T::identifier > RESERVED_CODE_LAST) {
michael@0 304 return NS_ERROR_ILLEGAL_VALUE;
michael@0 305 }
michael@0 306 mWriteHeader->mId = T::identifier;
michael@0 307 mWriteHeader->mPayloadLen = sizeof(T);
michael@0 308 aPtr = reinterpret_cast<T*>(mWriteHeader + 1);
michael@0 309 return NS_OK;
michael@0 310 }
michael@0 311
michael@0 312 static VOID CALLBACK
michael@0 313 SOnEvent(PVOID aContext, BOOLEAN aIsTimer)
michael@0 314 {
michael@0 315 MiniShmBase* object = static_cast<MiniShmBase*>(aContext);
michael@0 316 object->OnEvent();
michael@0 317 }
michael@0 318
michael@0 319 private:
michael@0 320 MiniShmObserver* mObserver;
michael@0 321 MiniShmHeader* mWriteHeader;
michael@0 322 MiniShmHeader* mReadHeader;
michael@0 323 unsigned int mPayloadMaxLen;
michael@0 324 HANDLE mGuard;
michael@0 325 DWORD mTimeout;
michael@0 326
michael@0 327 DISALLOW_COPY_AND_ASSIGN(MiniShmBase);
michael@0 328 };
michael@0 329
michael@0 330 } // namespace plugins
michael@0 331 } // namespace mozilla
michael@0 332
michael@0 333 #endif // mozilla_plugins_MiniShmBase_h
michael@0 334

mercurial