Updating require paths and adding a simple test for getCollusionTab

This commit is contained in:
Erik Vold 2013-06-18 17:24:55 -07:00
Родитель dd2f0d7c20
Коммит ff4c89ee5a
8 изменённых файлов: 87 добавлений и 54 удалений

Просмотреть файл

@ -2,17 +2,16 @@
//
// Convert an HTTP request (channel) to a loggable, visualizable connection object, if possible
var {
Cc, Ci, Cr
} = require('chrome');
const { Cc, Ci, Cr } = require('chrome');
const Request = require('sdk/request').Request;
var timers = require('sdk/timers');
const timers = require('sdk/timers');
const { on, once, off, emit } = require('sdk/event/core');
const ss = require('sdk/simple-storage');
var eTLDSvc = Cc["@mozilla.org/network/effective-tld-service;1"].
getService(Ci.nsIEffectiveTLDService);
const { getTabForChannel } = require('./tab/utils');
const {on, once, off, emit} = require('sdk/event/core');
var captureAndStoreConnections = true;
@ -22,7 +21,6 @@ exports.addConnection = addConnection;
// FIXME: Move persistence into a component
/* BEGIN FLAG PERSISTENCE */
var restored = false;
var ss = require('simple-storage');
var storage = ss.storage;
function restore(){
// only called when add-on is initialized, not when ui page is refreshed

Просмотреть файл

@ -1,12 +1,11 @@
"use strict";
const obSvc = require('sdk/deprecated/observer-service');
const {Connection, addConnection } = require('./connection');
const { PageMod } = require("sdk/page-mod");
const { Connection, addConnection } = require('./connection');
const tabEvents = require('./tab/events');
const ui = require('./ui');
// const addontab = require("sdk/addon-page");
const {mainPage, contentScript, attachToCollusionPage } = require('ui');
obSvc.add("http-on-examine-response", function(subject) {
var connection = Connection.fromSubject(subject);
@ -35,7 +34,7 @@ function matchesCurrentTab(connection){
}
require("page-mod").PageMod({
PageMod({
include: ui.mainPage,
contentScriptWhen: 'start',
contentScriptFile: ui.contentScript,

Просмотреть файл

@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
'use strict';
const windowUtils = require("sdk/deprecated/window-utils");
const { Class } = require("sdk/core/heritage");
@ -9,23 +9,25 @@ const { validateOptions } = require("sdk/deprecated/api-utils");
const { on, emit, once, off } = require("sdk/event/core");
const { isBrowser } = require("sdk/window/utils");
const { EventTarget } = require('sdk/event/target');
const { unload } = require("shared/unload+");
const { unload } = require('./unload+');
const menuitemNS = require("namespace").ns();
const menuitemNS = require("sdk/core/namespace").ns();
const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
function MenuitemOptions(options) {
return validateOptions(options, {
id: { is: ['string'] },
menuid: { is: ['undefined', 'string'] },
insertbefore: { is: ['undefined', 'string', 'object'] },
insertbefore: { is: ['undefined', 'string', 'object', 'number'] },
label: { is: ["string"] },
include: { is: ['string', 'undefined'] },
disabled: { is: ["undefined", "boolean"], map: function(v) !!v},
accesskey: { is: ["undefined", "string"] },
key: { is: ["undefined", "string"] },
checked: { is: ['undefined', 'boolean'] },
className: { is: ["undefined", "string"] },
onCommand: { is: ['undefined', 'function'] }
onCommand: { is: ['undefined', 'function'] },
useChrome: { map: function(v) !!v }
});
}
@ -81,7 +83,13 @@ function addMenuitems(self, options) {
// setup window tracker
windowUtils.WindowTracker({
onTrack: function (window) {
if (!isBrowser(window) || menuitemNS(self).destroyed) return;
if (menuitemNS(self).destroyed) return;
if (options.include) {
if (options.include != window.location) return;
}
else if (!isBrowser(window)) {
return;
}
// add the new menuitem to a menu
var menuitem = updateMenuitemAttributes(
@ -89,11 +97,11 @@ function addMenuitems(self, options) {
var menuitems_i = menuitems.push(menuitem) - 1;
// add the menutiem to the ui
updateMenuitemParent(menuitem, options, function(id) window.document.getElementById(id));
let added = updateMenuitemParent(menuitem, options, function(id) window.document.getElementById(id));
menuitem.addEventListener("command", function() {
if (!self.disabled)
emit(self, 'command');
emit(self, 'command', options.useChrome ? window : null);
}, true);
// add unloader

Просмотреть файл

@ -1,40 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
'use strict';
const { Class } = require("sdk/core/heritage");
const unloadNS = require("sdk/core/namespace").ns();
const { when: unload } = require("sdk/system/unload");
var Unloader = exports.Unloader = Class({
initialize: function Unloader() {
let unloaders = unloadNS(this).unloaders = [];
unloadNS(this).unloaders = [];
unloadNS(this).unloadersUnload = unloadersUnload.bind(null, unloadNS(this).unloaders);
let unloadersUnlaod = unloadNS(this).unloadersUnlaod = function() {
unloaders.slice().forEach(function(u) u());
unloaders.length = 0;
}
require("unload").when(unloadersUnlaod);
// run the unloaders on unload
unload(unloadNS(this).unloadersUnload);
},
unload: function unload(callback, container) {
// Calling with no arguments runs all the unloader callbacks
if (callback == null) {
unloadNS(this).unloadersUnlaod();
unloadNS(this).unloadersUnload();
return null;
}
var remover = removeUnloader.bind(null, unloader, unloadNS(this).unloaders);
let windowRemover = windowUnloader.bind(null, unloader, unloadNS(this).unloaders);
// The callback is bound to the lifetime of the container if we have one
if (container != null) {
// Remove the unloader when the container unloads
container.addEventListener("unload", remover, false);
container.addEventListener("unload", windowRemover, false);
// Wrap the callback to additionally remove the unload listener
let origCallback = callback;
callback = function() {
container.removeEventListener("unload", remover, false);
container.removeEventListener("unload", windowRemover, false);
origCallback();
}
}
@ -51,14 +49,28 @@ var Unloader = exports.Unloader = Class({
unloadNS(this).unloaders.push(unloader);
// Provide a way to remove the unloader
return remover;
return removeUnloader.bind(null, unloader, unloadNS(this).unloaders);
}
});
function removeUnloader(unloader, unloaders) {
function sliceUnloader(unloader, unloaders) {
let index = unloaders.indexOf(unloader);
if (index != -1)
unloaders.splice(index, 1);
if (index < 0)
return [];
return unloaders.splice(index, 1);
}
// wraps sliceUnloader and doesn't return anything
function removeUnloader(unloader, unloaders) {
sliceUnloader.apply(null, arguments);
}
function windowUnloader(unloader, unloaders) {
sliceUnloader.apply(null, arguments).forEach(function(u) u());
}
function unloadersUnload(unloaders) {
// run all the pending unloaders
unloaders.slice().forEach(function(u) u());
// clear the unload array
unloaders.length = 0;
}
/**

Просмотреть файл

@ -6,11 +6,10 @@ exports.getTabForChannel = getTabForChannel;
exports.on = onTab;
exports.getTabInfo = getTabInfo;
let tabs = require('tabs');
let { Cc, Ci, Cr } = require('chrome');
let winutils = require('sdk/window/utils');
const { Cc, Ci, Cr } = require('chrome');
const { isPrivate } = require("sdk/private-browsing");
const tabs = require('sdk/tabs');
const winutils = require('sdk/window/utils');
const { getTabForContentWindow, getBrowserForTab } = require('sdk/tabs/utils');
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
@ -57,7 +56,7 @@ function getTabForChannel(aHttpChannel) {
var tab = getTabForContentWindow(win);
// http://developer.mozilla.org/en/docs/XUL:tab
var browser = getBrowserForTab(tab);
tab.isPrivate = require("private-browsing").isPrivate(browser);
tab.isPrivate = isPrivate(browser);
return tab;
}

Просмотреть файл

@ -1,8 +1,11 @@
'use strict';
const tabs = require('tabs');
const { data } = require("self");
let { Connection } = require('./connection');
const tabs = require('sdk/tabs');
const { data } = require("sdk/self");
const { isPrivate } = require("sdk/private-browsing");
const { Widget } = require("sdk/widget");
const { Connection } = require('./connection');
const mainPage = data.url("index.html");
const contentScript = data.url('content-script.js');
@ -110,8 +113,7 @@ function attachToCollusionPage(worker) {
Connection.on('tempConnections', onTempConnections);
worker.port.on("privateWindowCheck", function(){
var isPrivate = require("private-browsing").isPrivate( getCollusionTab() );
worker.port.emit("isPrivateWindow", isPrivate);
worker.port.emit("isPrivateWindow", isPrivate( getCollusionTab() ));
});
worker.port.on('uiready', function(){
@ -136,14 +138,14 @@ function attachToCollusionPage(worker) {
}
function getCollusionTab(){
for(var i = 0; i < tabs.length; i++){
var tab = tabs[i];
function getCollusionTab() {
for each (let tab in tabs) {
if (tab.url === mainPage){
return tab;
}
}
}
exports.getCollusionTab = getCollusionTab;
// Set up the menu item to open the main UI page:
var menuitem = require("shared/menuitems").Menuitem({
@ -181,7 +183,7 @@ function openOrSwitchToOrClose(url){
}
// Set up the status bar button to open the main UI page:
var widget = require("widget").Widget({
var widget = Widget({
id: "collusion_Widget",
label: "Collusion",
tooltip: "Show Collusion",

Просмотреть файл

@ -1,7 +1,5 @@
'use strict';
var main = require('main');
//var main = require('main');
exports.test_run = function(test) {
test.pass('Unit test running');
};
require('test').run(exports);

17
test/test-ui.js Normal file
Просмотреть файл

@ -0,0 +1,17 @@
'use strict';
const tabs = require('sdk/tabs');
const { getCollusionTab, mainPage } = require('ui');
exports.testGetCollusionTab = function(assert, done) {
tabs.open({
url: mainPage,
onReady: function(tab) {
assert.equal(getCollusionTab(), tab, 'getCollusionTab found the correct tab');
tab.close(done);
}
})
}
require('test').run(exports);