blob: 91b590cf1d9aa88467adad168d4d58b106728d82 [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 Thompson10de4592012-10-21 23:54:18 -070049 // TODO: Strip off an ending implicit digest before checking the last component?
50 var uriEndsWithSequence = endsWithSequence(name);
51
Jeff Thompsonf0761452012-10-09 23:17:40 -070052 // 131.179.141.18 is lioncub.metwi.ucla.edu .
53 var ndn = new NDN('131.179.141.18');
Jeff Thompson10de4592012-10-21 23:54:18 -070054
Jeff Thompson34419762012-10-15 22:24:12 -070055 var ContentClosure = function ContentClosure() {
56 // Inherit from Closure.
57 Closure.call(this);
58 }
59 ContentClosure.prototype.upcall = function(kind, upcallInfo) {
60 if (!(kind == Closure.UPCALL_CONTENT ||
61 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
62 // The upcall is not for us.
Jeff Thompson741108b2012-10-15 23:07:09 -070063 return Closure.RESULT_ERR;
Jeff Thompson34419762012-10-15 22:24:12 -070064
65 var contentObject = upcallInfo.contentObject;
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070066 if (contentObject.content == null) {
67 dump("CcnxProtocol.newChannel: contentObject.content is null\n");
68 return Closure.RESULT_ERR;
69 }
Jeff Thompson34419762012-10-15 22:24:12 -070070
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070071 var content = DataUtils.toString(contentObject.content);
Jeff Thompson25b06412012-10-21 20:07:57 -070072 var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070073 contentListener.onReceivedContent(content,
Jeff Thompson25b06412012-10-21 20:07:57 -070074 contentTypeEtc.contentType, contentTypeEtc.contentCharset);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070075
Jeff Thompson10de4592012-10-21 23:54:18 -070076 // Assume that onReceivedContent sends all the content immediately and updatse
77 // the gURLBar if the content is for the main window.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070078 var urlBar = mostRecentWindow.gURLBar;
Jeff Thompson10de4592012-10-21 23:54:18 -070079 if (urlBar && urlBar.value == aURI.spec) {
80 // Assume the URI is for the window. Update the URL bar.
81 // Show the name without the ending sequence (unless the original URI had it).
82 if (!uriEndsWithSequence && endsWithSequence(contentObject.name)) {
83 var nameWithoutSequence = new Name
84 (contentObject.name.components.slice
85 (0, contentObject.name.components.length - 1));
86 urlBar.value = "ccnx:" + nameWithoutSequence.to_uri();
87 }
88 else
89 urlBar.value = "ccnx:" + contentObject.name.to_uri();
90 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070091
Jeff Thompson741108b2012-10-15 23:07:09 -070092 return Closure.RESULT_OK;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070093 };
94
Jeff Thompson25b06412012-10-21 20:07:57 -070095 ndn.expressInterest(name, new ContentClosure());
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070096 };
97
98 return new ContentChannel(aURI, requestContent);
99 } catch (ex) {
100 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
101 }
102 },
103
104 classDescription: "ccnx Protocol Handler",
105 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
106 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
107 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
108}
109
110if (XPCOMUtils.generateNSGetFactory)
111 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
112else
113 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
114
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700115/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700116 * Scan the name from the last component to the first (skipping special CCNx components)
117 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700118 */
119function getContentTypeAndCharset(name) {
Jeff Thompson25b06412012-10-21 20:07:57 -0700120 for (var i = name.components.length - 1; i >= 0; --i) {
121 var component = name.components[i];
122 if (component.length <= 0)
123 continue;
124
125 // Skip special components which just may have ".gif", etc.
126 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
127 (component[0] >= 0xF5 && component[0] <= 0xFF))
128 continue;
129
130 var str = DataUtils.toString(component).toLowerCase();
131 if (str.indexOf(".gif") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700132 return { contentType: "image/gif", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700133 else if (str.indexOf(".jpg") >= 0 ||
134 str.indexOf(".jpeg") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700135 return { contentType: "image/jpeg", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700136 else if (str.indexOf(".png") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700137 return { contentType: "image/png", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700138 else if (str.indexOf(".bmp") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700139 return { contentType: "image/bmp", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700140 else if (str.indexOf(".css") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700141 return { contentType: "text/css", charset: "utf-8" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700142 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700143
Jeff Thompson25b06412012-10-21 20:07:57 -0700144 // default
145 return { contentType: "text/html", charset: "utf-8" };
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700146}
Jeff Thompson10de4592012-10-21 23:54:18 -0700147
148/*
149 * Return true if the last component in the name is a sequence..
150 */
151function endsWithSequence(name) {
152 return name.components != null && name.components.length >= 1 &&
153 name.components[name.components.length - 1].length >= 1 &&
154 name.components[name.components.length - 1][0] == 0;
155}