blob: 2bf369ba002830b5b6c0178417b2877359b72252 [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/*
2 * @author: ucla-cs
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
4 * This is the ccnx protocol handler for NDN.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07005 * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
6 */
7
8const Cc = Components.classes;
9const Ci = Components.interfaces;
10const Cr = Components.results;
11
12const nsIProtocolHandler = Ci.nsIProtocolHandler;
13
14Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
15Components.utils.import("chrome://modules/content/ndn-js.jsm");
16Components.utils.import("chrome://modules/content/ContentChannel.jsm");
17
18function CcnxProtocol() {
19}
20
21CcnxProtocol.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) {
38 var interest = aURI.spec.split(":")[1];
Jeff Thompsonf0761452012-10-09 23:17:40 -070039 // 131.179.141.18 is lioncub.metwi.ucla.edu .
40 var ndn = new NDN('131.179.141.18');
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070041
42 var coListener = {
43 onReceivedContentObject : function(contentObject) {
44 // Set up defaults.
45 var content = "";
46 var contentType = "text/html";
47 var contentCharset = "utf-8";
Jeff Thompsonf0761452012-10-09 23:17:40 -070048
49 // TODO: Need to check the signature, confirm that the name matches, etc.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070050 if (contentObject.content != null) {
51 content = DataUtils.toString(contentObject.content);
Jeff Thompson36512512012-10-10 03:23:29 -070052
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070053 // TODO: Should look at the returned Name to get contentType. For now,
Jeff Thompson36512512012-10-10 03:23:29 -070054 // just look for a file extension in the original interest.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070055 var interestLowerCase = interest.toLowerCase();
56 if (interestLowerCase.indexOf(".gif") >= 0) {
57 contentType = "image/gif";
58 contentCharset = "ISO-8859-1";
59 }
60 else if (interestLowerCase.indexOf(".jpg") >= 0 ||
61 interestLowerCase.indexOf(".jpeg") >= 0) {
62 contentType = "image/jpeg";
63 contentCharset = "ISO-8859-1";
64 }
65 else if (interestLowerCase.indexOf(".png") >= 0) {
66 contentType = "image/png";
67 contentCharset = "ISO-8859-1";
68 }
69 else if (interestLowerCase.indexOf(".bmp") >= 0) {
70 contentType = "image/bmp";
71 contentCharset = "ISO-8859-1";
72 }
Jeff Thompson36512512012-10-10 03:23:29 -070073 else if (interestLowerCase.indexOf(".css") >= 0)
74 contentType = "text/css";
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070075 }
76
77 contentListener.onReceivedContent(content, contentType, contentCharset);
78 }
79 };
80
81 ndn.getAsync(interest, coListener);
82 };
83
84 return new ContentChannel(aURI, requestContent);
85 } catch (ex) {
86 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
87 }
88 },
89
90 classDescription: "ccnx Protocol Handler",
91 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
92 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
93 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
94}
95
96if (XPCOMUtils.generateNSGetFactory)
97 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
98else
99 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
100
101