helper.js 2.7 KB

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