blob: 12da336834a88bc8683fd753610d1e9a0f76573f [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 {
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070037 // Save the mostRecentWindow from the moment of newChannel.
38 var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
39 var mostRecentWindow = wm.getMostRecentWindow("navigator:browser");
40
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070041 var requestContent = function(contentListener) {
Jeff Thompson25b06412012-10-21 20:07:57 -070042 // Set nameString to the URI without the protocol.
43 var nameString = aURI.spec;
44 var colonIndex = nameString.indexOf(':');
45 if (colonIndex >= 0)
46 nameString = nameString.substr(colonIndex + 1, nameString.length - colonIndex - 1);
47
48 var name = new Name(nameString);
Jeff Thompsonf0761452012-10-09 23:17:40 -070049 // 131.179.141.18 is lioncub.metwi.ucla.edu .
50 var ndn = new NDN('131.179.141.18');
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070051
Jeff Thompson34419762012-10-15 22:24:12 -070052 var ContentClosure = function ContentClosure() {
53 // Inherit from Closure.
54 Closure.call(this);
55 }
56 ContentClosure.prototype.upcall = function(kind, upcallInfo) {
57 if (!(kind == Closure.UPCALL_CONTENT ||
58 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
59 // The upcall is not for us.
Jeff Thompson741108b2012-10-15 23:07:09 -070060 return Closure.RESULT_ERR;
Jeff Thompson34419762012-10-15 22:24:12 -070061
62 var contentObject = upcallInfo.contentObject;
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070063 if (contentObject.content == null) {
64 dump("CcnxProtocol.newChannel: contentObject.content is null\n");
65 return Closure.RESULT_ERR;
66 }
Jeff Thompson34419762012-10-15 22:24:12 -070067
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070068 var content = DataUtils.toString(contentObject.content);
Jeff Thompson25b06412012-10-21 20:07:57 -070069 var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070070 contentListener.onReceivedContent(content,
Jeff Thompson25b06412012-10-21 20:07:57 -070071 contentTypeEtc.contentType, contentTypeEtc.contentCharset);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070072
73 // Assume that onReceivedContent sends all the content immediately and that
Jeff Thompson25b06412012-10-21 20:07:57 -070074 // the gURLBar is updated if the content is for the main window.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070075 var urlBar = mostRecentWindow.gURLBar;
76
Jeff Thompson741108b2012-10-15 23:07:09 -070077 return Closure.RESULT_OK;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070078 };
79
Jeff Thompson25b06412012-10-21 20:07:57 -070080 ndn.expressInterest(name, new ContentClosure());
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070081 };
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
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700100/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700101 * Scan the name from the last component to the first (skipping special CCNx components)
102 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700103 */
104function getContentTypeAndCharset(name) {
Jeff Thompson25b06412012-10-21 20:07:57 -0700105 for (var i = name.components.length - 1; i >= 0; --i) {
106 var component = name.components[i];
107 if (component.length <= 0)
108 continue;
109
110 // Skip special components which just may have ".gif", etc.
111 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
112 (component[0] >= 0xF5 && component[0] <= 0xFF))
113 continue;
114
115 var str = DataUtils.toString(component).toLowerCase();
116 if (str.indexOf(".gif") >= 0)
117 return { contentType: "image/gif", charset: "ISO-8859-1" }
118 else if (str.indexOf(".jpg") >= 0 ||
119 str.indexOf(".jpeg") >= 0)
120 return { contentType: "image/jpeg", charset: "ISO-8859-1" }
121 else if (str.indexOf(".png") >= 0)
122 return { contentType: "image/png", charset: "ISO-8859-1" }
123 else if (str.indexOf(".bmp") >= 0)
124 return { contentType: "image/bmp", charset: "ISO-8859-1" }
125 else if (str.indexOf(".css") >= 0)
126 return { contentType: "text/css", charset: "utf-8" }
127 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700128
Jeff Thompson25b06412012-10-21 20:07:57 -0700129 // default
130 return { contentType: "text/html", charset: "utf-8" };
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700131}