summaryrefslogtreecommitdiff
path: root/tools/quitter/contentscript.js
blob: e25f5dbc84349856da94e242c0831a7bca87e907 (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
/* 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/. */

var Ci = Components.interfaces;
var Cc = Components.classes;
var Cu = Components.utils;

function Quitter() {
}

Quitter.prototype = {
  toString: function() { return "[Quitter]"; },
  quit: function() { sendSyncMessage('Quitter.Quit', {}); }
};

// This is a frame script, so it may be running in a content process.
// In any event, it is targeted at a specific "tab", so we listen for
// the DOMWindowCreated event to be notified about content windows
// being created in this context.

function QuitterManager() {
  addEventListener("DOMWindowCreated", this, false);
}

QuitterManager.prototype = {
  handleEvent: function handleEvent(aEvent) {
    var quitter = new Quitter(window);
    var window = aEvent.target.defaultView;
    window.wrappedJSObject.Quitter = Cu.cloneInto({
      toString: quitter.toString.bind(quitter),
      quit: quitter.quit.bind(quitter)
    }, window, {cloneFunctions: true});
  }
};

var quittermanager = new QuitterManager();