addon-sdk/source/lib/sdk/test/loader.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 const { resolveURI, Require,
michael@0 8 unload, override, descriptor } = require('../../toolkit/loader');
michael@0 9 const { ensure } = require('../system/unload');
michael@0 10 const addonWindow = require('../addon/window');
michael@0 11 const { PlainTextConsole } = require('sdk/console/plain-text');
michael@0 12
michael@0 13 let defaultGlobals = override(require('../system/globals'), {
michael@0 14 console: console
michael@0 15 });
michael@0 16
michael@0 17 function CustomLoader(module, globals, packaging, overrides={}) {
michael@0 18 let options = packaging || require("@loader/options");
michael@0 19 options = override(options, {
michael@0 20 id: overrides.id || options.id,
michael@0 21 globals: override(defaultGlobals, globals || {}),
michael@0 22 modules: override(options.modules || {}, {
michael@0 23 'sdk/addon/window': addonWindow
michael@0 24 })
michael@0 25 });
michael@0 26
michael@0 27 let loaderModule = options.isNative ? '../../toolkit/loader' : '../loader/cuddlefish';
michael@0 28 let { Loader } = require(loaderModule);
michael@0 29 let loader = Loader(options);
michael@0 30 let wrapper = Object.create(loader, descriptor({
michael@0 31 require: Require(loader, module),
michael@0 32 sandbox: function(id) {
michael@0 33 let requirement = loader.resolve(id, module.id);
michael@0 34 let uri = resolveURI(requirement, loader.mapping);
michael@0 35 return loader.sandboxes[uri];
michael@0 36 },
michael@0 37 unload: function(reason) {
michael@0 38 unload(loader, reason);
michael@0 39 }
michael@0 40 }));
michael@0 41 ensure(wrapper);
michael@0 42 return wrapper;
michael@0 43 };
michael@0 44 exports.Loader = CustomLoader;
michael@0 45
michael@0 46 function HookedPlainTextConsole(hook, print, innerID) {
michael@0 47 this.log = hook.bind(null, "log", innerID);
michael@0 48 this.info = hook.bind(null, "info", innerID);
michael@0 49 this.warn = hook.bind(null, "warn", innerID);
michael@0 50 this.error = hook.bind(null, "error", innerID);
michael@0 51 this.debug = hook.bind(null, "debug", innerID);
michael@0 52 this.exception = hook.bind(null, "exception", innerID);
michael@0 53 this.time = hook.bind(null, "time", innerID);
michael@0 54 this.timeEnd = hook.bind(null, "timeEnd", innerID);
michael@0 55
michael@0 56 this.__exposedProps__ = {
michael@0 57 log: "rw", info: "rw", warn: "rw", error: "rw", debug: "rw",
michael@0 58 exception: "rw", time: "rw", timeEnd: "rw"
michael@0 59 };
michael@0 60 }
michael@0 61
michael@0 62 // Creates a custom loader instance whose console module is hooked in order
michael@0 63 // to avoid printing messages to the console, and instead, expose them in the
michael@0 64 // returned `messages` array attribute
michael@0 65 exports.LoaderWithHookedConsole = function (module, callback) {
michael@0 66 let messages = [];
michael@0 67 function hook(type, innerID, msg) {
michael@0 68 messages.push({ type: type, msg: msg, innerID: innerID });
michael@0 69 if (callback)
michael@0 70 callback(type, msg, innerID);
michael@0 71 }
michael@0 72
michael@0 73 return {
michael@0 74 loader: CustomLoader(module, {
michael@0 75 console: new HookedPlainTextConsole(hook, null, null)
michael@0 76 }, override(require("@loader/options"), {
michael@0 77 modules: {
michael@0 78 'sdk/console/plain-text': {
michael@0 79 PlainTextConsole: HookedPlainTextConsole.bind(null, hook)
michael@0 80 }
michael@0 81 }
michael@0 82 })),
michael@0 83 messages: messages
michael@0 84 };
michael@0 85 }
michael@0 86
michael@0 87 // Same than LoaderWithHookedConsole with lower level, instead we get what is
michael@0 88 // actually printed to the command line console
michael@0 89 exports.LoaderWithHookedConsole2 = function (module, callback) {
michael@0 90 let messages = [];
michael@0 91 return {
michael@0 92 loader: CustomLoader(module, {
michael@0 93 console: new PlainTextConsole(function (msg) {
michael@0 94 messages.push(msg);
michael@0 95 if (callback)
michael@0 96 callback(msg);
michael@0 97 })
michael@0 98 }),
michael@0 99 messages: messages
michael@0 100 };
michael@0 101 }
michael@0 102
michael@0 103 // Creates a custom loader with a filtered console. The callback is passed every
michael@0 104 // console message type and message and if it returns false the message will
michael@0 105 // not be logged normally
michael@0 106 exports.LoaderWithFilteredConsole = function (module, callback) {
michael@0 107 function hook(type, innerID, msg) {
michael@0 108 if (callback && callback(type, msg, innerID) == false)
michael@0 109 return;
michael@0 110 console[type](msg);
michael@0 111 }
michael@0 112
michael@0 113 return CustomLoader(module, {
michael@0 114 console: new HookedPlainTextConsole(hook, null, null)
michael@0 115 }, override(require("@loader/options"), {
michael@0 116 modules: {
michael@0 117 'sdk/console/plain-text': {
michael@0 118 PlainTextConsole: HookedPlainTextConsole.bind(null, hook)
michael@0 119 }
michael@0 120 }
michael@0 121 }));
michael@0 122 }

mercurial