addon-sdk/source/lib/sdk/lang/weak-set.js

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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 module.metadata = {
michael@0 6 "stability": "experimental"
michael@0 7 };
michael@0 8
michael@0 9 "use strict";
michael@0 10
michael@0 11 const { Cu } = require("chrome");
michael@0 12
michael@0 13 function makeGetterFor(Type) {
michael@0 14 let cache = new WeakMap();
michael@0 15
michael@0 16 return function getFor(target) {
michael@0 17 if (!cache.has(target))
michael@0 18 cache.set(target, new Type());
michael@0 19
michael@0 20 return cache.get(target);
michael@0 21 }
michael@0 22 }
michael@0 23
michael@0 24 let getLookupFor = makeGetterFor(WeakMap);
michael@0 25 let getRefsFor = makeGetterFor(Set);
michael@0 26
michael@0 27 function add(target, value) {
michael@0 28 if (has(target, value))
michael@0 29 return;
michael@0 30
michael@0 31 getLookupFor(target).set(value, true);
michael@0 32 getRefsFor(target).add(Cu.getWeakReference(value));
michael@0 33 }
michael@0 34 exports.add = add;
michael@0 35
michael@0 36 function remove(target, value) {
michael@0 37 getLookupFor(target).delete(value);
michael@0 38 }
michael@0 39 exports.remove = remove;
michael@0 40
michael@0 41 function has(target, value) {
michael@0 42 return getLookupFor(target).has(value);
michael@0 43 }
michael@0 44 exports.has = has;
michael@0 45
michael@0 46 function clear(target) {
michael@0 47 getLookupFor(target).clear();
michael@0 48 getRefsFor(target).clear();
michael@0 49 }
michael@0 50 exports.clear = clear;
michael@0 51
michael@0 52 function iterator(target) {
michael@0 53 let refs = getRefsFor(target);
michael@0 54
michael@0 55 for (let ref of refs) {
michael@0 56 let value = ref.get();
michael@0 57
michael@0 58 // If `value` is already gc'ed, it would be `null`.
michael@0 59 // The `has` function is using a WeakMap as lookup table, so passing `null`
michael@0 60 // would raise an exception because WeakMap accepts as value only non-null
michael@0 61 // object.
michael@0 62 // Plus, if `value` is already gc'ed, we do not have to take it in account
michael@0 63 // during the iteration, and remove it from the references.
michael@0 64 if (value !== null && has(target, value))
michael@0 65 yield value;
michael@0 66 else
michael@0 67 refs.delete(ref);
michael@0 68 }
michael@0 69 }
michael@0 70 exports.iterator = iterator;

mercurial