-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbootstrap.js
169 lines (134 loc) · 4.41 KB
/
bootstrap.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* See license.txt for terms of usage */
// ********************************************************************************************* //
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cm = Components.manager;
Cm.QueryInterface(Ci.nsIComponentRegistrar);
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
// ********************************************************************************************* //
// Bootstrap API
var FBTrace;
function startup(data, reason)
{
var resource = Services.io.getProtocolHandler("resource").
QueryInterface(Ci.nsIResProtocolHandler);
resource.setSubstitution("httpmonitor", data.resourceURI);
FBTrace = Cu.import("resource://httpmonitor/modules/fbtrace.js").FBTrace;
// Load server. It'll be launched only if "extensions.httpmonitor.serverMode"
// preference is set to true.
loadServer();
// Load Monitor into all existing browser windows.
var enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements())
loadBrowserOverlay(enumerator.getNext());
// Listen for new windows, the overlay must be loaded into them too.
Services.ww.registerNotification(windowWatcher);
}
function shutdown(data, reason)
{
if (reason == APP_SHUTDOWN)
return;
var resource = Services.io.getProtocolHandler("resource").
QueryInterface(Ci.nsIResProtocolHandler);
resource.setSubstitution("httpmonitor", null);
// Unload Monitor UI from all existing browser windows.
var enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements())
unloadBrowserOverlay(enumerator.getNext());
// Remove "new window" listener.
Services.ww.unregisterNotification(windowWatcher);
// Close all HTTP Monitor windows
var win;
while (win = Services.wm.getMostRecentWindow("HTTPMonitor"))
win.close();
// Shutdown the server (in case we are in server mode).
unloadServer();
// xxxHonza: remove all default preferences
// xxxHonza: remove all loaded *.jsm modules (those from modules directory)
}
function install(data, reason)
{
}
function uninstall(data, reason)
{
}
// ********************************************************************************************* //
// Browser Overlay
function loadBrowserOverlay(win)
{
try
{
Services.scriptloader.loadSubScript(
"chrome://httpmonitor/content/app/browserOverlay.js",
win);
}
catch (e)
{
Cu.reportError(e);
}
try
{
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
if (prefs.getBoolPref("extensions.httpmonitor.alwaysOpen"))
win.HttpMonitorOverlay.open();
}
catch (e)
{
// Ignore the exception if the pref doesn't exist.
}
}
function unloadBrowserOverlay(win)
{
win.HttpMonitorOverlay.shutdown();
}
// ********************************************************************************************* //
// Server
var serverScope = {};
function loadServer()
{
var serverMode = false;
try
{
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
serverMode = prefs.getBoolPref("extensions.httpmonitor.serverMode");
}
catch (e)
{
// The pref doesn't have to exist.
}
try
{
if (serverMode)
{
Services.scriptloader.loadSubScript(
"chrome://httpmonitor/content/server/main.js",
serverScope);
}
}
catch (e)
{
Cu.reportError(e);
}
}
function unloadServer()
{
if (serverScope.HttpServer)
serverScope.HttpServer.shutdown();
}
// ********************************************************************************************* //
// Window Listener
var windowWatcher = function windowWatcher(win, topic)
{
if (topic != "domwindowopened")
return;
win.addEventListener("load", function onLoad()
{
win.removeEventListener("load", onLoad, false);
if (win.document.documentElement.getAttribute("windowtype") == "navigator:browser")
loadBrowserOverlay(win);
}, false);
}
// ********************************************************************************************* //