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