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 | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 37 | var trimmedSpec = aURI.spec.trim();
|
| 38 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 39 | // Save the mostRecentWindow from the moment of newChannel.
|
| 40 | var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
|
| 41 | var mostRecentWindow = wm.getMostRecentWindow("navigator:browser");
|
| 42 |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 43 | var requestContent = function(contentListener) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 44 | // Set nameString to the URI without the protocol.
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 45 | var nameString = trimmedSpec;
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 46 | var colonIndex = nameString.indexOf(':');
|
| 47 | if (colonIndex >= 0)
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 48 | nameString = nameString.substr
|
| 49 | (colonIndex + 1, nameString.length - colonIndex - 1).trim();
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 50 |
|
| 51 | var name = new Name(nameString);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 52 | // TODO: Strip off an ending implicit digest before checking the last component?
|
| 53 | var uriEndsWithSequence = endsWithSequence(name);
|
| 54 |
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 55 | // 131.179.141.18 is lioncub.metwi.ucla.edu .
|
| 56 | var ndn = new NDN('131.179.141.18');
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 57 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 58 | var ContentClosure = function ContentClosure() {
|
| 59 | // Inherit from Closure.
|
| 60 | Closure.call(this);
|
| 61 | }
|
| 62 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
| 63 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 64 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 65 | // The upcall is not for us.
|
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 66 | return Closure.RESULT_ERR;
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 67 |
|
| 68 | var contentObject = upcallInfo.contentObject;
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 69 | if (contentObject.content == null) {
|
| 70 | dump("CcnxProtocol.newChannel: contentObject.content is null\n");
|
| 71 | return Closure.RESULT_ERR;
|
| 72 | }
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 73 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 74 | var content = DataUtils.toString(contentObject.content);
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 75 | var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 76 | contentListener.onReceivedContent(content,
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 77 | contentTypeEtc.contentType, contentTypeEtc.contentCharset);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 78 |
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 79 | // Assume that onReceivedContent sends all the content immediately and updatse
|
| 80 | // the gURLBar if the content is for the main window.
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 81 | var urlBar = mostRecentWindow.gURLBar;
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 82 | if (urlBar && urlBar.value.trim() == trimmedSpec) {
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 83 | // Assume the URI is for the window. Update the URL bar.
|
| 84 | // Show the name without the ending sequence (unless the original URI had it).
|
| 85 | if (!uriEndsWithSequence && endsWithSequence(contentObject.name)) {
|
| 86 | var nameWithoutSequence = new Name
|
| 87 | (contentObject.name.components.slice
|
| 88 | (0, contentObject.name.components.length - 1));
|
| 89 | urlBar.value = "ccnx:" + nameWithoutSequence.to_uri();
|
| 90 | }
|
| 91 | else
|
| 92 | urlBar.value = "ccnx:" + contentObject.name.to_uri();
|
| 93 | }
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 94 |
|
Jeff Thompson | 741108b | 2012-10-15 23:07:09 -0700 | [diff] [blame] | 95 | return Closure.RESULT_OK;
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 96 | };
|
| 97 |
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 98 | ndn.expressInterest(name, new ContentClosure());
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 99 | };
|
| 100 |
|
| 101 | return new ContentChannel(aURI, requestContent);
|
| 102 | } catch (ex) {
|
| 103 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 104 | }
|
| 105 | },
|
| 106 |
|
| 107 | classDescription: "ccnx Protocol Handler",
|
| 108 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 109 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 110 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
| 111 | }
|
| 112 |
|
| 113 | if (XPCOMUtils.generateNSGetFactory)
|
| 114 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
| 115 | else
|
| 116 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 117 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 118 | /*
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 119 | * Scan the name from the last component to the first (skipping special CCNx components)
|
| 120 | * 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] | 121 | */
|
| 122 | function getContentTypeAndCharset(name) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 123 | for (var i = name.components.length - 1; i >= 0; --i) {
|
| 124 | var component = name.components[i];
|
| 125 | if (component.length <= 0)
|
| 126 | continue;
|
| 127 |
|
| 128 | // Skip special components which just may have ".gif", etc.
|
| 129 | if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
|
| 130 | (component[0] >= 0xF5 && component[0] <= 0xFF))
|
| 131 | continue;
|
| 132 |
|
| 133 | var str = DataUtils.toString(component).toLowerCase();
|
| 134 | if (str.indexOf(".gif") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 135 | return { contentType: "image/gif", charset: "ISO-8859-1" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 136 | else if (str.indexOf(".jpg") >= 0 ||
|
| 137 | str.indexOf(".jpeg") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 138 | return { contentType: "image/jpeg", charset: "ISO-8859-1" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 139 | else if (str.indexOf(".png") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 140 | return { contentType: "image/png", charset: "ISO-8859-1" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 141 | else if (str.indexOf(".bmp") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 142 | return { contentType: "image/bmp", charset: "ISO-8859-1" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 143 | else if (str.indexOf(".css") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 144 | return { contentType: "text/css", charset: "utf-8" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 145 | }
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 146 |
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 147 | // default
|
| 148 | return { contentType: "text/html", charset: "utf-8" };
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 149 | }
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 150 |
|
| 151 | /*
|
| 152 | * Return true if the last component in the name is a sequence..
|
| 153 | */
|
| 154 | function endsWithSequence(name) {
|
| 155 | return name.components != null && name.components.length >= 1 &&
|
| 156 | name.components[name.components.length - 1].length >= 1 &&
|
| 157 | name.components[name.components.length - 1][0] == 0;
|
| 158 | } |