-rwxr-xr-x | misc/xrdsContentHandler.js | 401 |
1 files changed, 401 insertions, 0 deletions
diff --git a/misc/xrdsContentHandler.js b/misc/xrdsContentHandler.js new file mode 100755 index 0000000..4f30310 --- a/dev/null +++ b/misc/xrdsContentHandler.js | |||
@@ -0,0 +1,401 @@ | |||
1 | |||
2 | const nsISupports = Components.interfaces.nsISupports; | ||
3 | |||
4 | const nsIBrowserDOMWindow = Components.interfaces.nsIBrowserDOMWindow; | ||
5 | const nsIChannel = Components.interfaces.nsIChannel; | ||
6 | const nsIContentHandler = Components.interfaces.nsIContentHandler; | ||
7 | const nsIDOMChromeWindow = Components.interfaces.nsIDOMChromeWindow; | ||
8 | const nsIDOMWindow = Components.interfaces.nsIDOMWindow; | ||
9 | const nsIFactory = Components.interfaces.nsIFactory; | ||
10 | const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler; | ||
11 | const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor; | ||
12 | const nsIPrefBranch = Components.interfaces.nsIPrefBranch; | ||
13 | const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString; | ||
14 | const nsISupportsString = Components.interfaces.nsISupportsString; | ||
15 | const nsIWebNavigation = Components.interfaces.nsIWebNavigation; | ||
16 | const nsIWindowMediator = Components.interfaces.nsIWindowMediator; | ||
17 | const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher; | ||
18 | const nsICategoryManager = Components.interfaces.nsICategoryManager; | ||
19 | const nsIWebNavigationInfo = Components.interfaces.nsIWebNavigationInfo; | ||
20 | const nsIStreamListener = Components.interfaces.nsIStreamListener; | ||
21 | const nsIRequestObserver = Components.interfaces.nsIRequestObserver; | ||
22 | const nsIURILoader = Components.interfaces.nsIURILoader; | ||
23 | const nsIURIContentListener = Components.interfaces.nsIURIContentListener; | ||
24 | |||
25 | const NS_BINDING_ABORTED = 0x80020006; | ||
26 | const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001; | ||
27 | const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT; | ||
28 | |||
29 | function shouldLoadURI(aURI) { | ||
30 | if (aURI.schemeIs("xri")) | ||
31 | return true; | ||
32 | dump("*** Preventing external load of chrome: URI into browser window\n"); | ||
33 | dump(" Use -chrome <uri> instead\n"); | ||
34 | return false; | ||
35 | } | ||
36 | |||
37 | |||
38 | |||
39 | |||
40 | function openWindow(parent, url, target, features, args) { | ||
41 | var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] | ||
42 | .getService(nsIWindowWatcher); | ||
43 | |||
44 | var argstring; | ||
45 | if (args) { | ||
46 | argstring = Components.classes["@mozilla.org/supports-string;1"] | ||
47 | .createInstance(nsISupportsString); | ||
48 | argstring.data = args; | ||
49 | } | ||
50 | return wwatch.openWindow(parent, url, target, features, argstring); | ||
51 | } | ||
52 | |||
53 | |||
54 | |||
55 | |||
56 | function getMostRecentWindow(aType) { | ||
57 | var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] | ||
58 | .getService(nsIWindowMediator); | ||
59 | return wm.getMostRecentWindow(aType); | ||
60 | } | ||
61 | |||
62 | |||
63 | |||
64 | |||
65 | // this returns the most recent non-popup browser window | ||
66 | function getMostRecentBrowserWindow() { | ||
67 | var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] | ||
68 | .getService(Components.interfaces.nsIWindowMediator); | ||
69 | |||
70 | var windowList = wm.getZOrderDOMWindowEnumerator("navigator:browser", true); | ||
71 | if (!windowList.hasMoreElements()) | ||
72 | return null; | ||
73 | |||
74 | var win = windowList.getNext(); | ||
75 | while (!win.toolbar.visible) { | ||
76 | if (!windowList.hasMoreElements()) | ||
77 | return null; | ||
78 | |||
79 | win = windowList.getNext(); | ||
80 | } | ||
81 | |||
82 | return win; | ||
83 | } | ||
84 | |||
85 | |||
86 | |||
87 | |||
88 | |||
89 | |||
90 | |||
91 | |||
92 | |||
93 | |||
94 | |||
95 | |||
96 | |||
97 | |||
98 | |||
99 | |||
100 | |||
101 | |||
102 | var xrdsContentHandler = { | ||
103 | |||
104 | /** browser window */ | ||
105 | contentWindow: null, | ||
106 | |||
107 | /** XRDS buffer */ | ||
108 | buf: null, | ||
109 | |||
110 | scriptableInStream: null, | ||
111 | |||
112 | init: function(contentWin) { | ||
113 | this.contentWindow = contentWindow; | ||
114 | }, | ||
115 | |||
116 | close: function() { | ||
117 | this.contentWindow = null; | ||
118 | }, | ||
119 | |||
120 | /* nsISupports */ | ||
121 | QueryInterface : function(iid) { | ||
122 | dump("xrdsCH ... "); | ||
123 | if (iid.equals(nsISupports)) { | ||
124 | dump("QI(nsISupports)\n"); | ||
125 | } | ||
126 | else if (iid.equals(nsIContentHandler)) { | ||
127 | dump("QI(nsIContentHandler)\n"); | ||
128 | } | ||
129 | else if (iid.equals(nsIStreamListener)) { | ||
130 | dump("QI(nsIStreamListener)\n"); | ||
131 | } | ||
132 | else if (iid.equals(nsIRequestObserver)) { | ||
133 | dump("QI(nsIRequestObserver)\n"); | ||
134 | } | ||
135 | else if (iid.equals(Components.interfaces.nsIURIContentListener)) { | ||
136 | dump("QI(nsIURIContentListener)\n"); | ||
137 | } | ||
138 | else if (iid.equals(nsIFactory)) { | ||
139 | dump("QI(nsIFactory)\n"); | ||
140 | } | ||
141 | else { | ||
142 | dump("QI(" + iid + ") - IDONTKNOW!!!!!\n"); | ||
143 | } | ||
144 | |||
145 | if (!iid.equals(nsISupports) && | ||
146 | !iid.equals(nsIContentHandler) && | ||
147 | !iid.equals(nsIURIContentListener) && | ||
148 | !iid.equals(nsIStreamListener) && | ||
149 | !iid.equals(nsIRequestObserver) && | ||
150 | !iid.equals(nsIFactory)) | ||
151 | throw Components.results.NS_ERROR_NO_INTERFACE; | ||
152 | |||
153 | dump("QI returning this..\n"); | ||
154 | return this; | ||
155 | }, | ||
156 | |||
157 | |||
158 | /* nsIURIContentListener */ | ||
159 | loadCookie: null, | ||
160 | |||
161 | parentContentListener: null, | ||
162 | |||
163 | onStartURIOpen: function(uri) | ||
164 | { | ||
165 | dump("xrdsCH onStartURIOpen '" + uri + "'"); | ||
166 | // ignore and don't abort | ||
167 | return false; | ||
168 | }, | ||
169 | |||
170 | doContent: function(contentType, isContentPreferred, request, contentHandler) | ||
171 | { | ||
172 | dump("doContent called\n"); | ||
173 | // forward the doContent to our content area webshell | ||
174 | var docShell = this.contentWindow.docShell; | ||
175 | var contentListener; | ||
176 | try { | ||
177 | contentListener = | ||
178 | docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor) | ||
179 | .getInterface(Components.interfaces.nsIURIContentListener); | ||
180 | } catch (ex) { | ||
181 | dump(ex); | ||
182 | } | ||
183 | |||
184 | dump("no content listener from docShell!\n"); | ||
185 | if (!contentListener) return false; | ||
186 | |||
187 | var rv = contentListener.doContent(contentType, isContentPreferred, request, contentHandler); | ||
188 | |||
189 | if (rv) { | ||
190 | dump("docShell wants to handle the load..\n"); | ||
191 | } | ||
192 | else { | ||
193 | dump("docShell does NOT want to handle the load..\n"); | ||
194 | |||
195 | contentHandler = contentHandler.value; | ||
196 | } | ||
197 | |||
198 | return rv; | ||
199 | |||
200 | }, | ||
201 | |||
202 | isPreferred: function(contentType, desiredContentType) | ||
203 | { | ||
204 | dump("isPreferred '" + contentType + "'\n"); | ||
205 | switch(contentType) { | ||
206 | case "application/xrds+xml": | ||
207 | case "application/xrd+xml": | ||
208 | dump("yes!!!\n"); | ||
209 | return true; | ||
210 | } | ||
211 | dump("erm.. nope!\n"); | ||
212 | return false; | ||
213 | }, | ||
214 | |||
215 | canHandleContent: function(contentType, isContentPreferred, desiredContentType) | ||
216 | { | ||
217 | dump("canHandleContent '" + contentType + "'\n"); | ||
218 | return this.isPreferred(contentType, desiredContentType); | ||
219 | }, | ||
220 | |||
221 | |||
222 | |||
223 | /* nsIRequestObserver */ | ||
224 | onStartRequest: function(request, ctx) | ||
225 | { | ||
226 | dump("xrdsContentHandler - onStartRequest\n"); | ||
227 | }, | ||
228 | |||
229 | onStopRequest: function(request, ctx, status) | ||
230 | { | ||
231 | dump("xrdsContentHandler - onStopRequest\n"); | ||
232 | this.scriptableInStream.close(); | ||
233 | }, | ||
234 | |||
235 | |||
236 | /* nsIStreamListener */ | ||
237 | onDataAvailable: function(request, domWindow, inputStream, offset, count) | ||
238 | { | ||
239 | dump("onDataAvailable, offset=" + offset + ", count=" + count + "\n"); | ||
240 | if (offset == 0) { | ||
241 | this.scriptableInStream.init(inputStream); | ||
242 | } | ||
243 | |||
244 | buf += this.scriptableInStream.read(count); | ||
245 | |||
246 | if (!request.isPending()) { | ||
247 | dump("request finished, buf = " + buf + "\n"); | ||
248 | var html = domWindow.document.createElement("html"); | ||
249 | html.createTextNode(buf); | ||
250 | this.scriptableInStream = null; | ||
251 | } | ||
252 | else { | ||
253 | dump("request pending...\n"); | ||
254 | } | ||
255 | }, | ||
256 | |||
257 | /* nsIContentHandler */ | ||
258 | |||
259 | handleContent : function(contentType, context, request) | ||
260 | { | ||
261 | dump("handleContent " + contentType + "\n"); | ||
262 | var parentWin; | ||
263 | try { | ||
264 | parentWin = context.getInterface(nsIDOMWindow); | ||
265 | } | ||
266 | catch (e) { | ||
267 | alert("no parent!!!"); // XXX | ||
268 | return; | ||
269 | } | ||
270 | |||
271 | dump("getting channel\n"); | ||
272 | var channel = request.QueryInterface(nsIChannel); | ||
273 | if (!channel) { | ||
274 | dump("no channel!!!\n"); | ||
275 | return; | ||
276 | } | ||
277 | |||
278 | if (this.scriptableInStream) { | ||
279 | dump("Hey! You can't possibly be reusing this handler?!\n"); | ||
280 | return; | ||
281 | } | ||
282 | |||
283 | dump("making scriptableInStream\n"); | ||
284 | this.scriptableInStream = Components.classes["@mozilla.org/scriptableinputstream;1"] | ||
285 | .createInstance(Components.interfaces.nsIScriptableInputStream); | ||
286 | |||
287 | buf = ''; | ||
288 | |||
289 | /* | ||
290 | try { | ||
291 | dump("before asyncOpen\n"); | ||
292 | channel.asyncOpen(this, parentWin); | ||
293 | return; | ||
294 | } | ||
295 | catch (e) { | ||
296 | dump("shit cannot asyncOpen... " + e + "\n"); | ||
297 | } | ||
298 | |||
299 | dump("try blocking open\n"); | ||
300 | var instream = channel.open(); | ||
301 | this.scriptableinputstream.init(instream); | ||
302 | while (true) { | ||
303 | var n = this.scriptableinputstream.available(); | ||
304 | if (n > 0) { | ||
305 | dump(this.scriptableinputstream.read(n) + "\n"); | ||
306 | } | ||
307 | } | ||
308 | |||
309 | dump("channel opened\n"); | ||
310 | */ | ||
311 | |||
312 | dump("handleContent returning \n"); | ||
313 | }, | ||
314 | |||
315 | /* nsIFactory */ | ||
316 | createInstance: function(outer, iid) | ||
317 | { | ||
318 | dump("xrdsCH createInstance called..\n"); | ||
319 | if (outer != null) | ||
320 | throw Components.results.NS_ERROR_NO_AGGREGATION; | ||
321 | |||
322 | dump("xrdsCH ok\n"); | ||
323 | return this.QueryInterface(iid); | ||
324 | }, | ||
325 | |||
326 | lockFactory : function bch_lock(lock) { | ||
327 | /* no-op */ | ||
328 | } | ||
329 | }; | ||
330 | |||
331 | const CLASS_ID = Components.ID("{e857f11b-fd59-4285-8f4a-c27cf9051a36}"); | ||
332 | const CLASS_NAME = "XRDS Content Handler"; | ||
333 | const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type="; | ||
334 | |||
335 | |||
336 | |||
337 | var Module = { | ||
338 | /* nsISupports */ | ||
339 | QueryInterface: function(iid) | ||
340 | { | ||
341 | dump("QI called\n"); | ||
342 | if (iid.equals(Components.interfaces.nsIModule) || | ||
343 | iid.equals(Components.interfaces.nsISupports)) | ||
344 | return this; | ||
345 | |||
346 | dump("QI throwing!!\n"); | ||
347 | throw Components.results.NS_ERROR_NO_INTERFACE; | ||
348 | }, | ||
349 | |||
350 | |||
351 | /* nsIModule */ | ||
352 | getClassObject: function(compMgr, cid, iid) | ||
353 | { | ||
354 | dump("getClassObject called\n"); | ||
355 | if (cid.equals(CLASS_ID)) | ||
356 | return xrdsContentHandler.QueryInterface(iid); | ||
357 | |||
358 | dump("gCO throwing!!\n"); | ||
359 | throw Components.results.NS_ERROR_NO_INTERFACE; | ||
360 | }, | ||
361 | |||
362 | |||
363 | registerSelf: function(compMgr, fileSpec, location, type) | ||
364 | { | ||
365 | var compReg = | ||
366 | compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); | ||
367 | |||
368 | compReg.registerFactoryLocation( CLASS_ID, | ||
369 | CLASS_NAME, | ||
370 | CONTRACTID_PREFIX + "application/xrds+xml", | ||
371 | fileSpec, | ||
372 | location, | ||
373 | type ); | ||
374 | |||
375 | compReg.registerFactoryLocation( CLASS_ID, | ||
376 | CLASS_NAME, | ||
377 | CONTRACTID_PREFIX + "text/uri-list", | ||
378 | fileSpec, | ||
379 | location, | ||
380 | type ); | ||
381 | |||
382 | dump("done registerSelf..\n"); | ||
383 | }, | ||
384 | |||
385 | |||
386 | unregisterSelf: function(compMgr, location, type) | ||
387 | { | ||
388 | var compReg = compMgr.QueryInterface(nsIComponentRegistrar); | ||
389 | compReg.unregisterFactoryLocation(CLASS_ID, location); | ||
390 | }, | ||
391 | |||
392 | |||
393 | canUnload: function(compMgr) { | ||
394 | return true; | ||
395 | } | ||
396 | }; | ||
397 | |||
398 | // NSGetModule: Return the nsIModule object. | ||
399 | function NSGetModule(compMgr, fileSpec) { | ||
400 | return Module; | ||
401 | } | ||