summaryrefslogtreecommitdiff
path: root/bootstrap.js
blob: 1734225e7345512f089f9748bf00bf3daa43de18 (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
"use strict";

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

function loadIntoWindow(window) {
	if (!window)
		return;

	var log = Log4Moz.repository.getLogger("topmenu.Bootstrap");

	try {
		window.topmenuProxy = TopMenuService.createWindowProxy(window);
	} catch (ex) {
		log.warn("Could not create window proxy: " + ex);
	}
}

function unloadFromWindow(window) {
	if (!window)
		return;
	if (!window.topmenuProxy)
		return;

	var log = Log4Moz.repository.getLogger("topmenu.Bootstrap");

	try {
		window.topmenuProxy.unload();
		window.topmenuProxy.dispose();
	} catch (ex) {
		log.warn("Could not unload window proxy: " + ex);
	}

	window.topmenuProxy = null;
}

var windowListener = {
	onOpenWindow: function(window) {
		// Wait for the window to finish loading
		var domWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
		domWindow.addEventListener("load", function onLoad() {
			domWindow.removeEventListener("load", onLoad, false);
			domWindow.addEventListener("unload", function onUnload() {
				domWindow.removeEventListener("unload", onUnload, false);
				unloadFromWindow(domWindow);
			}, false);
			loadIntoWindow(domWindow);
		}, false);
	},

	onCloseWindow: function(window) {},
	onWindowTitleChange: function(window, title) {}
};

function startup(data, reason) {
	// Setup logging
	Cu.import("chrome://topmenu/content/log4moz.js");
	var formatter = new Log4Moz.BasicFormatter();
	var root = Log4Moz.repository.rootLogger;
	root.level = Log4Moz.Level.Warn;

	var capp = new Log4Moz.ConsoleAppender(formatter);
	capp.level = Log4Moz.Level.Warn;
    root.addAppender(capp);

	var log = Log4Moz.repository.getLogger("topmenu.Bootstrap");

	// Load module
	Cu.import("chrome://topmenu/content/topmenuservice.js");

	// Load into any existing windows
	var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
	var windows = wm.getEnumerator(null);
	while (windows.hasMoreElements()) {
		var domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
		loadIntoWindow(domWindow);
	}

	// Load into any new windows
	wm.addListener(windowListener);
}

function shutdown(data, reason) {
	// When the application is shutting down we normally don't have to clean
	// up any UI changes made
	if (reason === APP_SHUTDOWN)
		return;

	var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);

	// Stop listening for new windows
	wm.removeListener(windowListener);

	// Unload from any existing windows
	var windows = wm.getEnumerator(null);
	while (windows.hasMoreElements()) {
		var domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
		unloadFromWindow(domWindow);
	}

	// Stop logging and unload modules
	Cu.unload("chrome://topmenu/content/topmenuservice.js");
	Cu.unload("chrome://topmenu/content/log4moz.js");
}

function install(data, reason) {

}

function uninstall(data, reason) {

}