js/src/vm/WeakMapPtr.cpp

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: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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 "js/WeakMapPtr.h"
michael@0 8
michael@0 9 #include "jsweakmap.h"
michael@0 10
michael@0 11 //
michael@0 12 // Machinery for the externally-linkable JS::WeakMapPtr, which wraps js::WeakMap
michael@0 13 // for a few public data types.
michael@0 14 //
michael@0 15
michael@0 16 using namespace js;
michael@0 17
michael@0 18 namespace {
michael@0 19
michael@0 20 template<typename T>
michael@0 21 struct DataType
michael@0 22 {
michael@0 23 };
michael@0 24
michael@0 25 template<>
michael@0 26 struct DataType<JSObject*>
michael@0 27 {
michael@0 28 typedef EncapsulatedPtrObject Encapsulated;
michael@0 29 static JSObject *NullValue() { return nullptr; }
michael@0 30 };
michael@0 31
michael@0 32 template<>
michael@0 33 struct DataType<JS::Value>
michael@0 34 {
michael@0 35 typedef EncapsulatedValue Encapsulated;
michael@0 36 static JS::Value NullValue() { return JS::UndefinedValue(); }
michael@0 37 };
michael@0 38
michael@0 39 template <typename K, typename V>
michael@0 40 struct Utils
michael@0 41 {
michael@0 42 typedef typename DataType<K>::Encapsulated KeyType;
michael@0 43 typedef typename DataType<V>::Encapsulated ValueType;
michael@0 44 typedef WeakMap<KeyType, ValueType> Type;
michael@0 45 typedef Type* PtrType;
michael@0 46 static PtrType cast(void *ptr) { return static_cast<PtrType>(ptr); }
michael@0 47 };
michael@0 48
michael@0 49 } /* namespace */
michael@0 50
michael@0 51 template <typename K, typename V>
michael@0 52 void
michael@0 53 JS::WeakMapPtr<K, V>::destroy()
michael@0 54 {
michael@0 55 MOZ_ASSERT(initialized());
michael@0 56 auto map = Utils<K, V>::cast(ptr);
michael@0 57 // If this destruction happens mid-GC, we might be in the compartment's list
michael@0 58 // of known live weakmaps. If we are, remove ourselves before deleting.
michael@0 59 if (map->isInList())
michael@0 60 WeakMapBase::removeWeakMapFromList(map);
michael@0 61 map->check();
michael@0 62 js_delete(map);
michael@0 63 ptr = nullptr;
michael@0 64 }
michael@0 65
michael@0 66 template <typename K, typename V>
michael@0 67 bool
michael@0 68 JS::WeakMapPtr<K, V>::init(JSContext *cx)
michael@0 69 {
michael@0 70 MOZ_ASSERT(!initialized());
michael@0 71 typename Utils<K, V>::PtrType map = cx->runtime()->new_<typename Utils<K,V>::Type>(cx);
michael@0 72 if (!map || !map->init())
michael@0 73 return false;
michael@0 74 ptr = map;
michael@0 75 return true;
michael@0 76 }
michael@0 77
michael@0 78 template <typename K, typename V>
michael@0 79 void
michael@0 80 JS::WeakMapPtr<K, V>::trace(JSTracer *trc)
michael@0 81 {
michael@0 82 MOZ_ASSERT(initialized());
michael@0 83 return Utils<K, V>::cast(ptr)->trace(trc);
michael@0 84 }
michael@0 85
michael@0 86 template <typename K, typename V>
michael@0 87 V
michael@0 88 JS::WeakMapPtr<K, V>::lookup(const K &key)
michael@0 89 {
michael@0 90 MOZ_ASSERT(initialized());
michael@0 91 typename Utils<K, V>::Type::Ptr result = Utils<K, V>::cast(ptr)->lookup(key);
michael@0 92 if (!result)
michael@0 93 return DataType<V>::NullValue();
michael@0 94 return result->value();
michael@0 95 }
michael@0 96
michael@0 97 template <typename K, typename V>
michael@0 98 /* static */ void
michael@0 99 JS::WeakMapPtr<K, V>::keyMarkCallback(JSTracer *trc, K key, void *data)
michael@0 100 {
michael@0 101 auto map = static_cast< JS::WeakMapPtr<K, V>* >(data);
michael@0 102 K prior = key;
michael@0 103 JS_CallObjectTracer(trc, &key, "WeakMapPtr key");
michael@0 104 return Utils<K, V>::cast(map->ptr)->rekeyIfMoved(prior, key);
michael@0 105 }
michael@0 106
michael@0 107 template <typename K, typename V>
michael@0 108 bool
michael@0 109 JS::WeakMapPtr<K, V>::put(JSContext *cx, const K &key, const V &value)
michael@0 110 {
michael@0 111 MOZ_ASSERT(initialized());
michael@0 112 if (!Utils<K, V>::cast(ptr)->put(key, value))
michael@0 113 return false;
michael@0 114 JS_StoreObjectPostBarrierCallback(cx, keyMarkCallback, key, this);
michael@0 115 // Values do not need to be barriered because only put() is supported,
michael@0 116 // which is always an initializing write.
michael@0 117 return true;
michael@0 118 }
michael@0 119
michael@0 120 //
michael@0 121 // Supported specializations of JS::WeakMap:
michael@0 122 //
michael@0 123
michael@0 124 template class JS::WeakMapPtr<JSObject*, JSObject*>;
michael@0 125
michael@0 126 #ifdef DEBUG
michael@0 127 // Nobody's using this at the moment, but we want to make sure it compiles.
michael@0 128 template class JS::WeakMapPtr<JSObject*, JS::Value>;
michael@0 129 #endif

mercurial