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 {
|
| 37 | var requestContent = function(contentListener) {
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 38 | var name = aURI.spec.split(":")[1];
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 39 | // 131.179.141.18 is lioncub.metwi.ucla.edu .
|
| 40 | var ndn = new NDN('131.179.141.18');
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 41 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 42 | var ContentClosure = function ContentClosure() {
|
| 43 | // Inherit from Closure.
|
| 44 | Closure.call(this);
|
| 45 | }
|
| 46 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
| 47 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 48 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 49 | // The upcall is not for us.
|
| 50 | return;
|
| 51 |
|
| 52 | var contentObject = upcallInfo.contentObject;
|
| 53 |
|
| 54 | // Set up defaults.
|
| 55 | var content = "";
|
| 56 | var contentType = "text/html";
|
| 57 | var contentCharset = "utf-8";
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 58 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 59 | if (contentObject.content != null) {
|
| 60 | content = DataUtils.toString(contentObject.content);
|
Jeff Thompson | 3651251 | 2012-10-10 03:23:29 -0700 | [diff] [blame] | 61 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 62 | // TODO: Should look at the returned Name to get contentType. For now,
|
| 63 | // just look for a file extension in the original name.
|
| 64 | var nameLowerCase = name.toLowerCase();
|
| 65 | if (nameLowerCase.indexOf(".gif") >= 0) {
|
| 66 | contentType = "image/gif";
|
| 67 | contentCharset = "ISO-8859-1";
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 68 | }
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 69 | else if (nameLowerCase.indexOf(".jpg") >= 0 ||
|
| 70 | nameLowerCase.indexOf(".jpeg") >= 0) {
|
| 71 | contentType = "image/jpeg";
|
| 72 | contentCharset = "ISO-8859-1";
|
| 73 | }
|
| 74 | else if (nameLowerCase.indexOf(".png") >= 0) {
|
| 75 | contentType = "image/png";
|
| 76 | contentCharset = "ISO-8859-1";
|
| 77 | }
|
| 78 | else if (nameLowerCase.indexOf(".bmp") >= 0) {
|
| 79 | contentType = "image/bmp";
|
| 80 | contentCharset = "ISO-8859-1";
|
| 81 | }
|
| 82 | else if (nameLowerCase.indexOf(".css") >= 0)
|
| 83 | contentType = "text/css";
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 84 | }
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 85 |
|
| 86 | contentListener.onReceivedContent(content, contentType, contentCharset);
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 87 | };
|
| 88 |
|
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 89 | ndn.expressInterest(new Name(name), new ContentClosure());
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 90 | };
|
| 91 |
|
| 92 | return new ContentChannel(aURI, requestContent);
|
| 93 | } catch (ex) {
|
| 94 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 95 | }
|
| 96 | },
|
| 97 |
|
| 98 | classDescription: "ccnx Protocol Handler",
|
| 99 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 100 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 101 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
| 102 | }
|
| 103 |
|
| 104 | if (XPCOMUtils.generateNSGetFactory)
|
| 105 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
| 106 | else
|
| 107 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 108 |
|
| 109 |
|