|
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/. */ |
|
4 'use strict'; |
|
5 |
|
6 const { Loader } = require('sdk/test/loader'); |
|
7 |
|
8 const { loader } = LoaderWithHookedConsole(module); |
|
9 |
|
10 const pb = loader.require('sdk/private-browsing'); |
|
11 const pbUtils = loader.require('sdk/private-browsing/utils'); |
|
12 const xulApp = require("sdk/system/xul-app"); |
|
13 const { open: openWindow, getMostRecentBrowserWindow } = require('sdk/window/utils'); |
|
14 const { openTab, getTabContentWindow, getActiveTab, setTabURL, closeTab } = require('sdk/tabs/utils'); |
|
15 const promise = require("sdk/core/promise"); |
|
16 const windowHelpers = require('sdk/window/helpers'); |
|
17 const events = require("sdk/system/events"); |
|
18 |
|
19 function LoaderWithHookedConsole(module) { |
|
20 let globals = {}; |
|
21 let errors = []; |
|
22 |
|
23 globals.console = Object.create(console, { |
|
24 error: { |
|
25 value: function(e) { |
|
26 errors.push(e); |
|
27 if (!/DEPRECATED:/.test(e)) { |
|
28 console.error(e); |
|
29 } |
|
30 } |
|
31 } |
|
32 }); |
|
33 |
|
34 let loader = Loader(module, globals); |
|
35 |
|
36 return { |
|
37 loader: loader, |
|
38 errors: errors |
|
39 } |
|
40 } |
|
41 |
|
42 function deactivate(callback) { |
|
43 if (pbUtils.isGlobalPBSupported) { |
|
44 if (callback) |
|
45 pb.once('stop', callback); |
|
46 pb.deactivate(); |
|
47 } |
|
48 } |
|
49 exports.deactivate = deactivate; |
|
50 |
|
51 exports.pb = pb; |
|
52 exports.pbUtils = pbUtils; |
|
53 exports.LoaderWithHookedConsole = LoaderWithHookedConsole; |
|
54 |
|
55 exports.openWebpage = function openWebpage(url, enablePrivate) { |
|
56 if (xulApp.is("Fennec")) { |
|
57 let chromeWindow = getMostRecentBrowserWindow(); |
|
58 let rawTab = openTab(chromeWindow, url, { |
|
59 isPrivate: enablePrivate |
|
60 }); |
|
61 |
|
62 return { |
|
63 ready: promise.resolve(getTabContentWindow(rawTab)), |
|
64 close: function () { |
|
65 closeTab(rawTab); |
|
66 // Returns a resolved promise as there is no need to wait |
|
67 return promise.resolve(); |
|
68 } |
|
69 }; |
|
70 } |
|
71 else { |
|
72 let win = openWindow(null, { |
|
73 features: { |
|
74 private: enablePrivate |
|
75 } |
|
76 }); |
|
77 let deferred = promise.defer(); |
|
78 |
|
79 // Wait for delayed startup code to be executed, in order to ensure |
|
80 // that the window is really ready |
|
81 events.on("browser-delayed-startup-finished", function onReady({subject}) { |
|
82 if (subject == win) { |
|
83 events.off("browser-delayed-startup-finished", onReady); |
|
84 deferred.resolve(win); |
|
85 |
|
86 let rawTab = getActiveTab(win); |
|
87 setTabURL(rawTab, url); |
|
88 deferred.resolve(getTabContentWindow(rawTab)); |
|
89 } |
|
90 }, true); |
|
91 |
|
92 return { |
|
93 ready: deferred.promise, |
|
94 close: function () { |
|
95 return windowHelpers.close(win); |
|
96 } |
|
97 }; |
|
98 } |
|
99 return null; |
|
100 } |