addon-sdk/source/lib/sdk/util/contract.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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "unstable"
     9 };
    11 const { validateOptions: valid } = require("../deprecated/api-utils");
    13 // Function takes property validation rules and returns function that given
    14 // an `options` object will return validated / normalized options back. If
    15 // option(s) are invalid validator will throw exception described by rules.
    16 // Returned will also have contain `rules` property with a given validation
    17 // rules and `properties` function that can be used to generate validated
    18 // property getter and setters can be mixed into prototype. For more details
    19 // see `properties` function below.
    20 function contract(rules) {
    21   function validator(options) {
    22     return valid(options || {}, rules);
    23   }
    24   validator.rules = rules
    25   validator.properties = function(modelFor) {
    26     return properties(modelFor, rules);
    27   }
    28   return validator;
    29 }
    30 exports.contract = contract
    32 // Function takes `modelFor` instance state model accessor functions and
    33 // a property validation rules and generates object with getters and setters
    34 // that can be mixed into prototype. Property accessors update model for the
    35 // given instance. If you wish to react to property updates you can always
    36 // override setters to put specific logic.
    37 function properties(modelFor, rules) {
    38   let descriptor = Object.keys(rules).reduce(function(descriptor, name) {
    39     descriptor[name] = {
    40       get: function() { return modelFor(this)[name] },
    41       set: function(value) {
    42         let change = {};
    43         change[name] = value;
    44         modelFor(this)[name] = valid(change, rules)[name];
    45       }
    46     }
    47     return descriptor
    48   }, {});
    49   return Object.create(Object.prototype, descriptor);
    50 }
    51 exports.properties = properties

mercurial