js/xpconnect/public/nsAutoJSValHolder.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=8 sts=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 __NSAUTOJSVALHOLDER_H__
michael@0 8 #define __NSAUTOJSVALHOLDER_H__
michael@0 9
michael@0 10 #include "nsDebug.h"
michael@0 11 #include "jsapi.h"
michael@0 12
michael@0 13 /**
michael@0 14 * Simple class that looks and acts like a JS::Value except that it unroots
michael@0 15 * itself automatically if Root() is ever called. Designed to be rooted on the
michael@0 16 * context or runtime (but not both!).
michael@0 17 */
michael@0 18 class nsAutoJSValHolder
michael@0 19 {
michael@0 20 public:
michael@0 21 nsAutoJSValHolder()
michael@0 22 : mVal(JSVAL_NULL), mRt(nullptr)
michael@0 23 {
michael@0 24 // nothing to do
michael@0 25 }
michael@0 26
michael@0 27 /**
michael@0 28 * Always release on destruction.
michael@0 29 */
michael@0 30 virtual ~nsAutoJSValHolder() {
michael@0 31 Release();
michael@0 32 }
michael@0 33
michael@0 34 nsAutoJSValHolder(const nsAutoJSValHolder& aOther)
michael@0 35 : mVal(JSVAL_NULL), mRt(nullptr)
michael@0 36 {
michael@0 37 *this = aOther;
michael@0 38 }
michael@0 39
michael@0 40 nsAutoJSValHolder& operator=(const nsAutoJSValHolder& aOther) {
michael@0 41 if (this != &aOther) {
michael@0 42 if (aOther.IsHeld()) {
michael@0 43 // XXX No error handling here...
michael@0 44 this->Hold(aOther.mRt);
michael@0 45 }
michael@0 46 else {
michael@0 47 this->Release();
michael@0 48 }
michael@0 49 *this = static_cast<JS::Value>(aOther);
michael@0 50 }
michael@0 51 return *this;
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Hold by rooting on the context's runtime.
michael@0 56 */
michael@0 57 bool Hold(JSContext* aCx) {
michael@0 58 return Hold(JS_GetRuntime(aCx));
michael@0 59 }
michael@0 60
michael@0 61 /**
michael@0 62 * Hold by rooting on the runtime.
michael@0 63 * Note that mVal may be JSVAL_NULL, which is not a problem.
michael@0 64 */
michael@0 65 bool Hold(JSRuntime* aRt) {
michael@0 66 MOZ_ASSERT_IF(mRt, mRt == aRt);
michael@0 67
michael@0 68 if (!mRt && JS::AddNamedValueRootRT(aRt, &mVal, "nsAutoJSValHolder")) {
michael@0 69 mRt = aRt;
michael@0 70 }
michael@0 71
michael@0 72 return !!mRt;
michael@0 73 }
michael@0 74
michael@0 75 /**
michael@0 76 * Manually release, nullifying mVal, and mRt, but returning
michael@0 77 * the original JS::Value.
michael@0 78 */
michael@0 79 JS::Value Release() {
michael@0 80 JS::Value oldval = mVal;
michael@0 81
michael@0 82 if (mRt) {
michael@0 83 JS::RemoveValueRootRT(mRt, &mVal); // infallible
michael@0 84 mRt = nullptr;
michael@0 85 }
michael@0 86
michael@0 87 mVal = JSVAL_NULL;
michael@0 88
michael@0 89 return oldval;
michael@0 90 }
michael@0 91
michael@0 92 /**
michael@0 93 * Determine if Hold has been called.
michael@0 94 */
michael@0 95 bool IsHeld() const {
michael@0 96 return !!mRt;
michael@0 97 }
michael@0 98
michael@0 99 /**
michael@0 100 * Explicit JSObject* conversion.
michael@0 101 */
michael@0 102 JSObject* ToJSObject() const {
michael@0 103 return mVal.isObject()
michael@0 104 ? &mVal.toObject()
michael@0 105 : nullptr;
michael@0 106 }
michael@0 107
michael@0 108 /**
michael@0 109 * Pretend to be a JS::Value.
michael@0 110 */
michael@0 111 operator JS::Value() const { return mVal; }
michael@0 112 JS::Value get() const { return mVal; }
michael@0 113
michael@0 114 nsAutoJSValHolder &operator=(JSObject* aOther) {
michael@0 115 return *this = OBJECT_TO_JSVAL(aOther);
michael@0 116 }
michael@0 117
michael@0 118 nsAutoJSValHolder &operator=(JS::Value aOther) {
michael@0 119 #ifdef DEBUG
michael@0 120 if (JSVAL_IS_GCTHING(aOther) && !JSVAL_IS_NULL(aOther)) {
michael@0 121 MOZ_ASSERT(IsHeld(), "Not rooted!");
michael@0 122 }
michael@0 123 #endif
michael@0 124 mVal = aOther;
michael@0 125 return *this;
michael@0 126 }
michael@0 127
michael@0 128 private:
michael@0 129 JS::Heap<JS::Value> mVal;
michael@0 130 JSRuntime* mRt;
michael@0 131 };
michael@0 132
michael@0 133 #endif /* __NSAUTOJSVALHOLDER_H__ */

mercurial