From c4b3096e166a0a9a60fd27c8f2dc06e688f13172 Mon Sep 17 00:00:00 2001 From: williamt Date: Fri, 05 Jan 2007 10:06:01 +0000 Subject: Initial revision --- (limited to 'src/chrome/content/xrdsHandler.js') diff --git a/src/chrome/content/xrdsHandler.js b/src/chrome/content/xrdsHandler.js new file mode 100755 index 0000000..d4b0d35 --- a/dev/null +++ b/src/chrome/content/xrdsHandler.js @@ -0,0 +1,258 @@ + +const nsISupports = Components.interfaces.nsISupports; +const nsIChannel = Components.interfaces.nsIChannel; +const nsIContentHandler = Components.interfaces.nsIContentHandler; +const nsIDOMWindow = Components.interfaces.nsIDOMWindow; +const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor; +const nsIStreamListener = Components.interfaces.nsIStreamListener; +const nsIRequestObserver = Components.interfaces.nsIRequestObserver; +const nsIURILoader = Components.interfaces.nsIURILoader; +const nsIURIContentListener = Components.interfaces.nsIURIContentListener; + + + + +var g_xrdsHandler = null; + + + + + + +XrdsContentHandler.prototype = { + + /** browser window */ + contentWindow: null, + + /** XRDS buffer */ + buf: null, + + scriptableInStream: null, + + init: function(contentWin) { + dump("XrdsContentHandler.init()\n"); + this.contentWindow = contentWin; + + var uriLoader = Components.classes["@mozilla.org/uriloader;1"] + .getService(Components.interfaces.nsIURILoader); + uriLoader.registerContentListener(this); + dump("XrdsContentHandler.init() returning\n"); + }, + + close: function() { + this.contentWindow = null; + var uriLoader = Components.classes["@mozilla.org/uriloader;1"] + .getService(Components.interfaces.nsIURILoader); + uriLoader.unRegisterContentListener(g_xrdsHandler); + }, + + /* nsISupports */ + QueryInterface : function(iid) { + dump("xrdsCH ... "); + if (iid.equals(nsISupports)) { + dump("QI(nsISupports)\n"); + } + else if (iid.equals(nsIContentHandler)) { + dump("QI(nsIContentHandler)\n"); + } + else if (iid.equals(nsIStreamListener)) { + dump("QI(nsIStreamListener)\n"); + } + else if (iid.equals(nsIRequestObserver)) { + dump("QI(nsIRequestObserver)\n"); + } + else if (iid.equals(Components.interfaces.nsIURIContentListener)) { + dump("QI(nsIURIContentListener)\n"); + } + else if (iid.equals(nsIFactory)) { + dump("QI(nsIFactory)\n"); + } + else { + dump("QI(" + iid + ") - IDONTKNOW!!!!!\n"); + } + + if (!iid.equals(nsISupports) && +// !iid.equals(nsIContentHandler) && + !iid.equals(nsIURIContentListener) && + !iid.equals(nsIStreamListener) && +// !iid.equals(nsIRequestObserver) && + !iid.equals(nsIFactory)) + throw Components.results.NS_ERROR_NO_INTERFACE; + + dump("QI returning this..\n"); + return this; + }, + + + /* nsIURIContentListener */ + loadCookie: null, + + parentContentListener: null, + + onStartURIOpen: function(uri) + { + dump("xrdsCH onStartURIOpen '" + uri + "'"); + // ignore and don't abort + return false; + }, + + doContent: function(contentType, isContentPreferred, request, contentHandler) + { + dump("doContent called\n"); + // forward the doContent to our content area webshell + var docShell = this.contentWindow.docShell; + var contentListener; + try { + contentListener = + docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor) + .getInterface(Components.interfaces.nsIURIContentListener); + } catch (ex) { + dump(ex); + } + + dump("no content listener from docShell!\n"); + if (!contentListener) return false; + + var rv = contentListener.doContent(contentType, isContentPreferred, request, contentHandler); + + if (rv) { + dump("docShell wants to handle the load..\n"); + } + else { + dump("docShell does NOT want to handle the load..\n"); + + contentHandler = contentHandler.value; + } + + return rv; + + }, + + isPreferred: function(contentType, desiredContentType) + { + dump("isPreferred '" + contentType + "'\n"); + switch(contentType) { + case "application/xrds+xml": + case "application/xrd+xml": + dump("yes!!!\n"); + return true; + } + dump("erm.. nope!\n"); + return false; + }, + + canHandleContent: function(contentType, isContentPreferred, desiredContentType) + { + dump("canHandleContent '" + contentType + "'\n"); + return this.isPreferred(contentType, desiredContentType); + }, + + + + /* nsIRequestObserver */ + onStartRequest: function(request, ctx) + { + dump("xrdsContentHandler - onStartRequest\n"); + }, + + onStopRequest: function(request, ctx, status) + { + dump("xrdsContentHandler - onStopRequest\n"); + this.scriptableInStream.close(); + }, + + + /* nsIStreamListener */ + onDataAvailable: function(request, domWindow, inputStream, offset, count) + { + dump("onDataAvailable, offset=" + offset + ", count=" + count + "\n"); + if (offset == 0) { + this.scriptableInStream.init(inputStream); + } + + buf += this.scriptableInStream.read(count); + + if (!request.isPending()) { + dump("request finished, buf = " + buf + "\n"); + var html = domWindow.document.createElement("html"); + html.createTextNode(buf); + this.scriptableInStream = null; + } + else { + dump("request pending...\n"); + } + }, + + /* nsIContentHandler */ + + handleContent : function(contentType, context, request) + { + dump("handleContent " + contentType + "\n"); + var parentWin; + try { + parentWin = context.getInterface(nsIDOMWindow); + } + catch (e) { + alert("no parent!!!"); // XXX + return; + } + + dump("getting channel\n"); + var channel = request.QueryInterface(nsIChannel); + if (!channel) { + dump("no channel!!!\n"); + return; + } + + if (this.scriptableInStream) { + dump("Hey! You can't possibly be reusing this handler?!\n"); + return; + } + + dump("making scriptableInStream\n"); + this.scriptableInStream = Components.classes["@mozilla.org/scriptableinputstream;1"] + .createInstance(Components.interfaces.nsIScriptableInputStream); + + buf = ''; + + } + +}; + + +function XrdsContentHandler(contentWindow) +{ + this.init(contentWindow); +} + + + + + + + + + + + + + +function foxri_startup() { + // our XRDS content-handler also does nsIURIContentListener + var xrdsHandler = new XrdsContentHandler(getBrowser()); + g_xrdsHandler = xrdsHandler; + +} + + +function foxri_shutdown() { + if (!g_xrdsHandler) + return; + + g_xrdsHandler.close(); +} + + + + +foxri_startup(); -- cgit v0.9.0.2