|
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 |
|
5 function ChromePowers(window) { |
|
6 this.window = Components.utils.getWeakReference(window); |
|
7 |
|
8 // In the case of browser-chrome tests, we are running as a [ChromeWindow] |
|
9 // and we have no window.QueryInterface available, content.window is what we need |
|
10 if (typeof(window) == "ChromeWindow" && typeof(content.window) == "Window") { |
|
11 this.DOMWindowUtils = bindDOMWindowUtils(content.window); |
|
12 this.window = Components.utils.getWeakReference(content.window); |
|
13 } else { |
|
14 this.DOMWindowUtils = bindDOMWindowUtils(window); |
|
15 } |
|
16 |
|
17 this.spObserver = new SpecialPowersObserverAPI(); |
|
18 } |
|
19 |
|
20 ChromePowers.prototype = new SpecialPowersAPI(); |
|
21 |
|
22 ChromePowers.prototype.toString = function() { return "[ChromePowers]"; }; |
|
23 ChromePowers.prototype.sanityCheck = function() { return "foo"; }; |
|
24 |
|
25 // This gets filled in in the constructor. |
|
26 ChromePowers.prototype.DOMWindowUtils = undefined; |
|
27 |
|
28 ChromePowers.prototype._sendSyncMessage = function(type, msg) { |
|
29 var aMessage = {'name':type, 'json': msg}; |
|
30 return [this._receiveMessage(aMessage)]; |
|
31 }; |
|
32 |
|
33 ChromePowers.prototype._sendAsyncMessage = function(type, msg) { |
|
34 var aMessage = {'name':type, 'json': msg}; |
|
35 this._receiveMessage(aMessage); |
|
36 }; |
|
37 |
|
38 ChromePowers.prototype.registerProcessCrashObservers = function() { |
|
39 this._sendSyncMessage("SPProcessCrashService", { op: "register-observer" }); |
|
40 }; |
|
41 |
|
42 ChromePowers.prototype.unregisterProcessCrashObservers = function() { |
|
43 this._sendSyncMessage("SPProcessCrashService", { op: "unregister-observer" }); |
|
44 }; |
|
45 |
|
46 ChromePowers.prototype._receiveMessage = function(aMessage) { |
|
47 switch (aMessage.name) { |
|
48 case "SpecialPowers.Quit": |
|
49 let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup); |
|
50 appStartup.quit(Ci.nsIAppStartup.eForceQuit); |
|
51 break; |
|
52 case "SPProcessCrashService": |
|
53 if (aMessage.json.op == "register-observer" || aMessage.json.op == "unregister-observer") { |
|
54 // Hack out register/unregister specifically for browser-chrome leaks |
|
55 break; |
|
56 } else if (aMessage.type == "crash-observed") { |
|
57 for (let e of msg.dumpIDs) { |
|
58 this._encounteredCrashDumpFiles.push(e.id + "." + e.extension); |
|
59 } |
|
60 } |
|
61 default: |
|
62 // All calls go here, because we need to handle SPProcessCrashService calls as well |
|
63 return this.spObserver._receiveMessageAPI(aMessage); |
|
64 } |
|
65 return undefined; // Avoid warning. |
|
66 }; |
|
67 |
|
68 ChromePowers.prototype.quit = function() { |
|
69 // We come in here as SpecialPowers.quit, but SpecialPowers is really ChromePowers. |
|
70 // For some reason this.<func> resolves to TestRunner, so using SpecialPowers |
|
71 // allows us to use the ChromePowers object which we defined below. |
|
72 SpecialPowers._sendSyncMessage("SpecialPowers.Quit", {}); |
|
73 }; |
|
74 |
|
75 ChromePowers.prototype.focus = function(aWindow) { |
|
76 // We come in here as SpecialPowers.focus, but SpecialPowers is really ChromePowers. |
|
77 // For some reason this.<func> resolves to TestRunner, so using SpecialPowers |
|
78 // allows us to use the ChromePowers object which we defined below. |
|
79 if (aWindow) |
|
80 aWindow.focus(); |
|
81 }; |
|
82 |
|
83 ChromePowers.prototype.executeAfterFlushingMessageQueue = function(aCallback) { |
|
84 aCallback(); |
|
85 }; |
|
86 |
|
87 // Expose everything but internal APIs (starting with underscores) to |
|
88 // web content. We cannot use Object.keys to view SpecialPowers.prototype since |
|
89 // we are using the functions from SpecialPowersAPI.prototype |
|
90 ChromePowers.prototype.__exposedProps__ = {}; |
|
91 for (var i in ChromePowers.prototype) { |
|
92 if (i.charAt(0) != "_") |
|
93 ChromePowers.prototype.__exposedProps__[i] = "r"; |
|
94 } |
|
95 |
|
96 if ((window.parent !== null) && |
|
97 (window.parent !== undefined) && |
|
98 (window.parent.wrappedJSObject.SpecialPowers) && |
|
99 !(window.wrappedJSObject.SpecialPowers)) { |
|
100 window.wrappedJSObject.SpecialPowers = window.parent.SpecialPowers; |
|
101 } else { |
|
102 window.wrappedJSObject.SpecialPowers = new ChromePowers(window); |
|
103 } |
|
104 |