michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { Loader } = require('sdk/test/loader'); michael@0: michael@0: const { loader } = LoaderWithHookedConsole(module); michael@0: michael@0: const pb = loader.require('sdk/private-browsing'); michael@0: const pbUtils = loader.require('sdk/private-browsing/utils'); michael@0: const xulApp = require("sdk/system/xul-app"); michael@0: const { open: openWindow, getMostRecentBrowserWindow } = require('sdk/window/utils'); michael@0: const { openTab, getTabContentWindow, getActiveTab, setTabURL, closeTab } = require('sdk/tabs/utils'); michael@0: const promise = require("sdk/core/promise"); michael@0: const windowHelpers = require('sdk/window/helpers'); michael@0: const events = require("sdk/system/events"); michael@0: michael@0: function LoaderWithHookedConsole(module) { michael@0: let globals = {}; michael@0: let errors = []; michael@0: michael@0: globals.console = Object.create(console, { michael@0: error: { michael@0: value: function(e) { michael@0: errors.push(e); michael@0: if (!/DEPRECATED:/.test(e)) { michael@0: console.error(e); michael@0: } michael@0: } michael@0: } michael@0: }); michael@0: michael@0: let loader = Loader(module, globals); michael@0: michael@0: return { michael@0: loader: loader, michael@0: errors: errors michael@0: } michael@0: } michael@0: michael@0: function deactivate(callback) { michael@0: if (pbUtils.isGlobalPBSupported) { michael@0: if (callback) michael@0: pb.once('stop', callback); michael@0: pb.deactivate(); michael@0: } michael@0: } michael@0: exports.deactivate = deactivate; michael@0: michael@0: exports.pb = pb; michael@0: exports.pbUtils = pbUtils; michael@0: exports.LoaderWithHookedConsole = LoaderWithHookedConsole; michael@0: michael@0: exports.openWebpage = function openWebpage(url, enablePrivate) { michael@0: if (xulApp.is("Fennec")) { michael@0: let chromeWindow = getMostRecentBrowserWindow(); michael@0: let rawTab = openTab(chromeWindow, url, { michael@0: isPrivate: enablePrivate michael@0: }); michael@0: michael@0: return { michael@0: ready: promise.resolve(getTabContentWindow(rawTab)), michael@0: close: function () { michael@0: closeTab(rawTab); michael@0: // Returns a resolved promise as there is no need to wait michael@0: return promise.resolve(); michael@0: } michael@0: }; michael@0: } michael@0: else { michael@0: let win = openWindow(null, { michael@0: features: { michael@0: private: enablePrivate michael@0: } michael@0: }); michael@0: let deferred = promise.defer(); michael@0: michael@0: // Wait for delayed startup code to be executed, in order to ensure michael@0: // that the window is really ready michael@0: events.on("browser-delayed-startup-finished", function onReady({subject}) { michael@0: if (subject == win) { michael@0: events.off("browser-delayed-startup-finished", onReady); michael@0: deferred.resolve(win); michael@0: michael@0: let rawTab = getActiveTab(win); michael@0: setTabURL(rawTab, url); michael@0: deferred.resolve(getTabContentWindow(rawTab)); michael@0: } michael@0: }, true); michael@0: michael@0: return { michael@0: ready: deferred.promise, michael@0: close: function () { michael@0: return windowHelpers.close(win); michael@0: } michael@0: }; michael@0: } michael@0: return null; michael@0: }