blob: d3eccb8b6d16e081d248dd3bbcb87452cbe8bfcb [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/*
2 * @author: ucla-cs
3 * This is the ccnx protocol handler.
4 * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
5 */
6
7const Cc = Components.classes;
8const Ci = Components.interfaces;
9const Cr = Components.results;
10
11const nsIProtocolHandler = Ci.nsIProtocolHandler;
12
13Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
14Components.utils.import("chrome://modules/content/ndn-js.jsm");
15Components.utils.import("chrome://modules/content/ContentChannel.jsm");
16
17function CcnxProtocol() {
18}
19
20CcnxProtocol.prototype = {
21 scheme: "ccnx",
22 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
23 nsIProtocolHandler.URI_NOAUTH |
24 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
25
26 newURI: function(aSpec, aOriginCharset, aBaseURI)
27 {
28 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
29 uri.spec = aSpec;
30 return uri;
31 },
32
33 newChannel: function(aURI)
34 {
35 try {
36 var requestContent = function(contentListener) {
37 var interest = aURI.spec.split(":")[1];
Jeff Thompsonf0761452012-10-09 23:17:40 -070038 // 131.179.141.18 is lioncub.metwi.ucla.edu .
39 var ndn = new NDN('131.179.141.18');
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070040
41 var coListener = {
42 onReceivedContentObject : function(contentObject) {
43 // Set up defaults.
44 var content = "";
45 var contentType = "text/html";
46 var contentCharset = "utf-8";
Jeff Thompsonf0761452012-10-09 23:17:40 -070047
48 // TODO: Need to check the signature, confirm that the name matches, etc.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070049 if (contentObject.content != null) {
50 content = DataUtils.toString(contentObject.content);
Jeff Thompson36512512012-10-10 03:23:29 -070051
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070052 // TODO: Should look at the returned Name to get contentType. For now,
Jeff Thompson36512512012-10-10 03:23:29 -070053 // just look for a file extension in the original interest.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070054 var interestLowerCase = interest.toLowerCase();
55 if (interestLowerCase.indexOf(".gif") >= 0) {
56 contentType = "image/gif";
57 contentCharset = "ISO-8859-1";
58 }
59 else if (interestLowerCase.indexOf(".jpg") >= 0 ||
60 interestLowerCase.indexOf(".jpeg") >= 0) {
61 contentType = "image/jpeg";
62 contentCharset = "ISO-8859-1";
63 }
64 else if (interestLowerCase.indexOf(".png") >= 0) {
65 contentType = "image/png";
66 contentCharset = "ISO-8859-1";
67 }
68 else if (interestLowerCase.indexOf(".bmp") >= 0) {
69 contentType = "image/bmp";
70 contentCharset = "ISO-8859-1";
71 }
Jeff Thompson36512512012-10-10 03:23:29 -070072 else if (interestLowerCase.indexOf(".css") >= 0)
73 contentType = "text/css";
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070074 }
75
76 contentListener.onReceivedContent(content, contentType, contentCharset);
77 }
78 };
79
80 ndn.getAsync(interest, coListener);
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
95if (XPCOMUtils.generateNSGetFactory)
96 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
97else
98 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
99
100