summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/SimpleTest/ChromePowers.js
blob: 97de578157c0254e69a6de2dff244aaaff27d2be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 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/. */

function ChromePowers(window) {
  this.window = Components.utils.getWeakReference(window);

  // In the case of browser-chrome tests, we are running as a [ChromeWindow]
  // and we have no window.QueryInterface available, content.window is what we need
  if (typeof(window) == "ChromeWindow" && typeof(content.window) == "Window") {
    this.DOMWindowUtils = bindDOMWindowUtils(content.window);
    this.window = Components.utils.getWeakReference(content.window);
  } else {
    this.DOMWindowUtils = bindDOMWindowUtils(window);
  }

  this.spObserver = new SpecialPowersObserverAPI();
  this.spObserver._sendReply = this._sendReply.bind(this);
  this.listeners = new Map();
}

ChromePowers.prototype = new SpecialPowersAPI();

ChromePowers.prototype.toString = function() { return "[ChromePowers]"; };
ChromePowers.prototype.sanityCheck = function() { return "foo"; };

// This gets filled in in the constructor.
ChromePowers.prototype.DOMWindowUtils = undefined;

ChromePowers.prototype._sendReply = function(aOrigMsg, aType, aMsg) {
  var msg = {'name':aType, 'json': aMsg, 'data': aMsg};
  if (!this.listeners.has(aType)) {
    throw new Error(`No listener for ${aType}`);
  }
  this.listeners.get(aType)(msg);
};

ChromePowers.prototype._sendSyncMessage = function(aType, aMsg) {
  var msg = {'name':aType, 'json': aMsg, 'data': aMsg};
  return [this._receiveMessage(msg)];
};

ChromePowers.prototype._sendAsyncMessage = function(aType, aMsg) {
  var msg = {'name':aType, 'json': aMsg, 'data': aMsg};
  this._receiveMessage(msg);
};

ChromePowers.prototype._addMessageListener = function(aType, aCallback) {
  if (this.listeners.has(aType)) {
    throw new Error(`unable to handle multiple listeners for ${aType}`);
  }
  this.listeners.set(aType, aCallback);
};
ChromePowers.prototype._removeMessageListener = function(aType, aCallback) {
  this.listeners.delete(aType);
};

ChromePowers.prototype.registerProcessCrashObservers = function() {
  this._sendSyncMessage("SPProcessCrashService", { op: "register-observer" });
};

ChromePowers.prototype.unregisterProcessCrashObservers = function() {
  this._sendSyncMessage("SPProcessCrashService", { op: "unregister-observer" });
};

ChromePowers.prototype._receiveMessage = function(aMessage) {
  switch (aMessage.name) {
    case "SpecialPowers.Quit":
      let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
      appStartup.quit(Ci.nsIAppStartup.eForceQuit);
      break;
    case "SPProcessCrashService":
      if (aMessage.json.op == "register-observer" || aMessage.json.op == "unregister-observer") {
        // Hack out register/unregister specifically for browser-chrome leaks
        break;
      } else if (aMessage.type == "crash-observed") {
        for (let e of msg.dumpIDs) {
          this._encounteredCrashDumpFiles.push(e.id + "." + e.extension);
        }
      }
    default:
      // All calls go here, because we need to handle SPProcessCrashService calls as well
      return this.spObserver._receiveMessageAPI(aMessage);
  }
  return undefined;		// Avoid warning.
};

ChromePowers.prototype.quit = function() {
  // We come in here as SpecialPowers.quit, but SpecialPowers is really ChromePowers.
  // For some reason this.<func> resolves to TestRunner, so using SpecialPowers
  // allows us to use the ChromePowers object which we defined below.
  SpecialPowers._sendSyncMessage("SpecialPowers.Quit", {});
};

ChromePowers.prototype.focus = function(aWindow) {
  // We come in here as SpecialPowers.focus, but SpecialPowers is really ChromePowers.
  // For some reason this.<func> resolves to TestRunner, so using SpecialPowers
  // allows us to use the ChromePowers object which we defined below.
  if (aWindow)
    aWindow.focus();
};

ChromePowers.prototype.executeAfterFlushingMessageQueue = function(aCallback) {
  aCallback();
};

// Expose everything but internal APIs (starting with underscores) to
// web content.  We cannot use Object.keys to view SpecialPowers.prototype since
// we are using the functions from SpecialPowersAPI.prototype
ChromePowers.prototype.__exposedProps__ = {};
for (var i in ChromePowers.prototype) {
  if (i.charAt(0) != "_")
    ChromePowers.prototype.__exposedProps__[i] = "r";
}

if ((window.parent !== null) &&
    (window.parent !== undefined) &&
    (window.parent.wrappedJSObject.SpecialPowers) &&
    !(window.wrappedJSObject.SpecialPowers)) {
  window.wrappedJSObject.SpecialPowers = window.parent.SpecialPowers;
} else {
  window.wrappedJSObject.SpecialPowers = new ChromePowers(window);
}