blob: c7d4d1eccf891dc1a04bfee8ab11c06e87a9e1ca [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) {
Jeff Thompson34419762012-10-15 22:24:12 -070038 var name = 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
Jeff Thompson34419762012-10-15 22:24:12 -070042 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 Thompsonf0761452012-10-09 23:17:40 -070058
Jeff Thompson34419762012-10-15 22:24:12 -070059 if (contentObject.content != null) {
60 content = DataUtils.toString(contentObject.content);
Jeff Thompson36512512012-10-10 03:23:29 -070061
Jeff Thompson34419762012-10-15 22:24:12 -070062 // 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 Thompson08ab3cd2012-10-08 02:56:20 -070068 }
Jeff Thompson34419762012-10-15 22:24:12 -070069 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 Thompson08ab3cd2012-10-08 02:56:20 -070084 }
Jeff Thompson34419762012-10-15 22:24:12 -070085
86 contentListener.onReceivedContent(content, contentType, contentCharset);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070087 };
88
Jeff Thompson34419762012-10-15 22:24:12 -070089 ndn.expressInterest(new Name(name), new ContentClosure());
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070090 };
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
104if (XPCOMUtils.generateNSGetFactory)
105 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
106else
107 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
108
109