blob: f959e2510746e715e4c9b4f582a3bbe83bbdcceb [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);
51 // TODO: Should look at the returned Name to get contentType. For now,
52 // just look for an image file extension in the original interest.
53 var interestLowerCase = interest.toLowerCase();
54 if (interestLowerCase.indexOf(".gif") >= 0) {
55 contentType = "image/gif";
56 contentCharset = "ISO-8859-1";
57 }
58 else if (interestLowerCase.indexOf(".jpg") >= 0 ||
59 interestLowerCase.indexOf(".jpeg") >= 0) {
60 contentType = "image/jpeg";
61 contentCharset = "ISO-8859-1";
62 }
63 else if (interestLowerCase.indexOf(".png") >= 0) {
64 contentType = "image/png";
65 contentCharset = "ISO-8859-1";
66 }
67 else if (interestLowerCase.indexOf(".bmp") >= 0) {
68 contentType = "image/bmp";
69 contentCharset = "ISO-8859-1";
70 }
71 }
72
73 contentListener.onReceivedContent(content, contentType, contentCharset);
74 }
75 };
76
77 ndn.getAsync(interest, coListener);
78 };
79
80 return new ContentChannel(aURI, requestContent);
81 } catch (ex) {
82 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
83 }
84 },
85
86 classDescription: "ccnx Protocol Handler",
87 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
88 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
89 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
90}
91
92if (XPCOMUtils.generateNSGetFactory)
93 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
94else
95 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
96
97