addon-sdk/source/lib/sdk/util/collection.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 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 exports.Collection = Collection;
michael@0 12
michael@0 13 /**
michael@0 14 * Adds a collection property to the given object. Setting the property to a
michael@0 15 * scalar value empties the collection and adds the value. Setting it to an
michael@0 16 * array empties the collection and adds all the items in the array.
michael@0 17 *
michael@0 18 * @param obj
michael@0 19 * The property will be defined on this object.
michael@0 20 * @param propName
michael@0 21 * The name of the property.
michael@0 22 * @param array
michael@0 23 * If given, this will be used as the collection's backing array.
michael@0 24 */
michael@0 25 exports.addCollectionProperty = function addCollProperty(obj, propName, array) {
michael@0 26 array = array || [];
michael@0 27 let publicIface = new Collection(array);
michael@0 28
michael@0 29 Object.defineProperty(obj, propName, {
michael@0 30 configurable: true,
michael@0 31 enumerable: true,
michael@0 32
michael@0 33 set: function set(itemOrItems) {
michael@0 34 array.splice(0, array.length);
michael@0 35 publicIface.add(itemOrItems);
michael@0 36 },
michael@0 37
michael@0 38 get: function get() {
michael@0 39 return publicIface;
michael@0 40 }
michael@0 41 });
michael@0 42 };
michael@0 43
michael@0 44 /**
michael@0 45 * A collection is ordered, like an array, but its items are unique, like a set.
michael@0 46 *
michael@0 47 * @param array
michael@0 48 * The collection is backed by an array. If this is given, it will be
michael@0 49 * used as the backing array. This way the caller can fully control the
michael@0 50 * collection. Otherwise a new empty array will be used, and no one but
michael@0 51 * the collection will have access to it.
michael@0 52 */
michael@0 53 function Collection(array) {
michael@0 54 array = array || [];
michael@0 55
michael@0 56 /**
michael@0 57 * Provides iteration over the collection. Items are yielded in the order
michael@0 58 * they were added.
michael@0 59 */
michael@0 60 this.__iterator__ = function Collection___iterator__() {
michael@0 61 let items = array.slice();
michael@0 62 for (let i = 0; i < items.length; i++)
michael@0 63 yield items[i];
michael@0 64 };
michael@0 65
michael@0 66 /**
michael@0 67 * The number of items in the collection.
michael@0 68 */
michael@0 69 this.__defineGetter__("length", function Collection_get_length() {
michael@0 70 return array.length;
michael@0 71 });
michael@0 72
michael@0 73 /**
michael@0 74 * Adds a single item or an array of items to the collection. Any items
michael@0 75 * already contained in the collection are ignored.
michael@0 76 *
michael@0 77 * @param itemOrItems
michael@0 78 * An item or array of items.
michael@0 79 * @return The collection.
michael@0 80 */
michael@0 81 this.add = function Collection_add(itemOrItems) {
michael@0 82 let items = toArray(itemOrItems);
michael@0 83 for (let i = 0; i < items.length; i++) {
michael@0 84 let item = items[i];
michael@0 85 if (array.indexOf(item) < 0)
michael@0 86 array.push(item);
michael@0 87 }
michael@0 88 return this;
michael@0 89 };
michael@0 90
michael@0 91 /**
michael@0 92 * Removes a single item or an array of items from the collection. Any items
michael@0 93 * not contained in the collection are ignored.
michael@0 94 *
michael@0 95 * @param itemOrItems
michael@0 96 * An item or array of items.
michael@0 97 * @return The collection.
michael@0 98 */
michael@0 99 this.remove = function Collection_remove(itemOrItems) {
michael@0 100 let items = toArray(itemOrItems);
michael@0 101 for (let i = 0; i < items.length; i++) {
michael@0 102 let idx = array.indexOf(items[i]);
michael@0 103 if (idx >= 0)
michael@0 104 array.splice(idx, 1);
michael@0 105 }
michael@0 106 return this;
michael@0 107 };
michael@0 108 };
michael@0 109
michael@0 110 function toArray(itemOrItems) {
michael@0 111 let isArr = itemOrItems &&
michael@0 112 itemOrItems.constructor &&
michael@0 113 itemOrItems.constructor.name === "Array";
michael@0 114 return isArr ? itemOrItems : [itemOrItems];
michael@0 115 }

mercurial