Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 1 | /*
|
| 2 | * @author: ucla-cs
|
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information.
|
| 4 | * This is the ccnx protocol handler for NDN.
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 5 | * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
|
| 6 | */
|
| 7 |
|
| 8 | const Cc = Components.classes;
|
| 9 | const Ci = Components.interfaces;
|
| 10 | const Cr = Components.results;
|
| 11 |
|
| 12 | const nsIProtocolHandler = Ci.nsIProtocolHandler;
|
| 13 |
|
| 14 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
| 15 | Components.utils.import("chrome://modules/content/ndn-js.jsm");
|
| 16 | Components.utils.import("chrome://modules/content/ContentChannel.jsm");
|
| 17 |
|
| 18 | function CcnxProtocol() {
|
| 19 | }
|
| 20 |
|
| 21 | CcnxProtocol.prototype = {
|
| 22 | scheme: "ccnx",
|
| 23 | protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
|
| 24 | nsIProtocolHandler.URI_NOAUTH |
|
| 25 | nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
|
| 26 |
|
| 27 | newURI: function(aSpec, aOriginCharset, aBaseURI)
|
| 28 | {
|
| 29 | var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
|
| 30 | uri.spec = aSpec;
|
| 31 | return uri;
|
| 32 | },
|
| 33 |
|
| 34 | newChannel: function(aURI)
|
| 35 | {
|
| 36 | try {
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 37 | // Save the mostRecentWindow from the moment of newChannel.
|
| 38 | var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
|
| 39 | var mostRecentWindow = wm.getMostRecentWindow("navigator:browser");
|
| 40 |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 41 | var requestContent = function(contentListener) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 42 | // Set nameString to the URI without the protocol.
|
| 43 | var nameString = aURI.spec;
|
| 44 | var colonIndex = nameString.indexOf(':');
|
| 45 | if (colonIndex >= 0)
|
| 46 | nameString = nameString.substr(colonIndex + 1, nameString.length - colonIndex - 1);
|
| 47 |
|
| 48 | var name = new Name(nameString);
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 49 | // 131.179.141.18 is lioncub.metwi.ucla.edu .
|
| 50 | var ndn = new NDN('131.179.141.18');
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 51 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 52 | var ContentClosure = function ContentClosure() {
|
| 53 | // Inherit from Closure.
|
| 54 | Closure.call(this);
|
| 55 | }
|
| 56 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
| 57 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 58 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 59 | // The upcall is not for us.
|
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 60 | return Closure.RESULT_ERR;
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 61 |
|
| 62 | var contentObject = upcallInfo.contentObject;
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 63 | if (contentObject.content == null) {
|
| 64 | dump("CcnxProtocol.newChannel: contentObject.content is null\n");
|
| 65 | return Closure.RESULT_ERR;
|
| 66 | }
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 67 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 68 | var content = DataUtils.toString(contentObject.content);
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 69 | var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 70 | contentListener.onReceivedContent(content,
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 71 | contentTypeEtc.contentType, contentTypeEtc.contentCharset);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 72 |
|
| 73 | // Assume that onReceivedContent sends all the content immediately and that
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 74 | // the gURLBar is updated if the content is for the main window.
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 75 | var urlBar = mostRecentWindow.gURLBar;
|
| 76 |
|
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 77 | return Closure.RESULT_OK;
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 78 | };
|
| 79 |
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 80 | ndn.expressInterest(name, new ContentClosure());
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 81 | };
|
| 82 |
|
| 83 | return new ContentChannel(aURI, requestContent);
|
| 84 | } catch (ex) {
|
| 85 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 86 | }
|
| 87 | },
|
| 88 |
|
| 89 | classDescription: "ccnx Protocol Handler",
|
| 90 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 91 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 92 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
| 93 | }
|
| 94 |
|
| 95 | if (XPCOMUtils.generateNSGetFactory)
|
| 96 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
| 97 | else
|
| 98 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 99 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 100 | /*
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 101 | * Scan the name from the last component to the first (skipping special CCNx components)
|
| 102 | * for a recognized file name extension, and return an object with properties contentType and charset.
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 103 | */
|
| 104 | function getContentTypeAndCharset(name) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 105 | for (var i = name.components.length - 1; i >= 0; --i) {
|
| 106 | var component = name.components[i];
|
| 107 | if (component.length <= 0)
|
| 108 | continue;
|
| 109 |
|
| 110 | // Skip special components which just may have ".gif", etc.
|
| 111 | if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
|
| 112 | (component[0] >= 0xF5 && component[0] <= 0xFF))
|
| 113 | continue;
|
| 114 |
|
| 115 | var str = DataUtils.toString(component).toLowerCase();
|
| 116 | if (str.indexOf(".gif") >= 0)
|
| 117 | return { contentType: "image/gif", charset: "ISO-8859-1" }
|
| 118 | else if (str.indexOf(".jpg") >= 0 ||
|
| 119 | str.indexOf(".jpeg") >= 0)
|
| 120 | return { contentType: "image/jpeg", charset: "ISO-8859-1" }
|
| 121 | else if (str.indexOf(".png") >= 0)
|
| 122 | return { contentType: "image/png", charset: "ISO-8859-1" }
|
| 123 | else if (str.indexOf(".bmp") >= 0)
|
| 124 | return { contentType: "image/bmp", charset: "ISO-8859-1" }
|
| 125 | else if (str.indexOf(".css") >= 0)
|
| 126 | return { contentType: "text/css", charset: "utf-8" }
|
| 127 | }
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 128 |
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 129 | // default
|
| 130 | return { contentType: "text/html", charset: "utf-8" };
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 131 | }
|