blob: b466067f701673219bdbae213799973f00b36ffb [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 Thompsond4617ff2012-10-25 20:40:53 -070037 var trimmedSpec = aURI.spec.trim();
38
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070039 // Save the mostRecentWindow from the moment of newChannel.
40 var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
41 var mostRecentWindow = wm.getMostRecentWindow("navigator:browser");
42
Jeff Thompson6576bbb2012-10-28 22:20:02 -070043 var contentChannel;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070044 var requestContent = function(contentListener) {
Jeff Thompson25b06412012-10-21 20:07:57 -070045 // Set nameString to the URI without the protocol.
Jeff Thompsond4617ff2012-10-25 20:40:53 -070046 var nameString = trimmedSpec;
Jeff Thompson25b06412012-10-21 20:07:57 -070047 var colonIndex = nameString.indexOf(':');
48 if (colonIndex >= 0)
Jeff Thompsond4617ff2012-10-25 20:40:53 -070049 nameString = nameString.substr
50 (colonIndex + 1, nameString.length - colonIndex - 1).trim();
Jeff Thompson25b06412012-10-21 20:07:57 -070051
52 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070053 // TODO: Strip off an ending implicit digest before checking the last component?
54 var uriEndsWithSequence = endsWithSequence(name);
55
Jeff Thompsonf0761452012-10-09 23:17:40 -070056 // 131.179.141.18 is lioncub.metwi.ucla.edu .
57 var ndn = new NDN('131.179.141.18');
Jeff Thompson10de4592012-10-21 23:54:18 -070058
Jeff Thompson34419762012-10-15 22:24:12 -070059 var ContentClosure = function ContentClosure() {
60 // Inherit from Closure.
61 Closure.call(this);
62 }
63 ContentClosure.prototype.upcall = function(kind, upcallInfo) {
64 if (!(kind == Closure.UPCALL_CONTENT ||
65 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
66 // The upcall is not for us.
Jeff Thompson741108b2012-10-15 23:07:09 -070067 return Closure.RESULT_ERR;
Jeff Thompson34419762012-10-15 22:24:12 -070068
69 var contentObject = upcallInfo.contentObject;
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070070 if (contentObject.content == null) {
71 dump("CcnxProtocol.newChannel: contentObject.content is null\n");
72 return Closure.RESULT_ERR;
73 }
Jeff Thompson34419762012-10-15 22:24:12 -070074
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070075 var content = DataUtils.toString(contentObject.content);
Jeff Thompson25b06412012-10-21 20:07:57 -070076 var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070077 contentListener.onReceivedContent(content,
Jeff Thompson25b06412012-10-21 20:07:57 -070078 contentTypeEtc.contentType, contentTypeEtc.contentCharset);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070079
Jeff Thompson6576bbb2012-10-28 22:20:02 -070080 // Load flags bit 19 "LOAD_INITIAL_DOCUMENT_URI" means the channel is
81 // for the main window with the URL bar.
82 if (contentChannel.loadFlags & (1<<19)) {
83 // Update the URL bar.
Jeff Thompson10de4592012-10-21 23:54:18 -070084 // Show the name without the ending sequence (unless the original URI had it).
Jeff Thompson6576bbb2012-10-28 22:20:02 -070085 var urlBar = mostRecentWindow.gURLBar;
Jeff Thompson10de4592012-10-21 23:54:18 -070086 if (!uriEndsWithSequence && endsWithSequence(contentObject.name)) {
87 var nameWithoutSequence = new Name
88 (contentObject.name.components.slice
89 (0, contentObject.name.components.length - 1));
90 urlBar.value = "ccnx:" + nameWithoutSequence.to_uri();
91 }
92 else
93 urlBar.value = "ccnx:" + contentObject.name.to_uri();
94 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070095
Jeff Thompson741108b2012-10-15 23:07:09 -070096 return Closure.RESULT_OK;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070097 };
98
Jeff Thompson25b06412012-10-21 20:07:57 -070099 ndn.expressInterest(name, new ContentClosure());
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700100 };
101
Jeff Thompson6576bbb2012-10-28 22:20:02 -0700102 contentChannel = new ContentChannel(aURI, requestContent);
103 return contentChannel;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700104 } catch (ex) {
105 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
106 }
107 },
108
109 classDescription: "ccnx Protocol Handler",
110 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
111 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
112 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
113}
114
115if (XPCOMUtils.generateNSGetFactory)
116 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
117else
118 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
119
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700120/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700121 * Scan the name from the last component to the first (skipping special CCNx components)
122 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700123 */
124function getContentTypeAndCharset(name) {
Jeff Thompson25b06412012-10-21 20:07:57 -0700125 for (var i = name.components.length - 1; i >= 0; --i) {
126 var component = name.components[i];
127 if (component.length <= 0)
128 continue;
129
130 // Skip special components which just may have ".gif", etc.
131 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
132 (component[0] >= 0xF5 && component[0] <= 0xFF))
133 continue;
134
135 var str = DataUtils.toString(component).toLowerCase();
136 if (str.indexOf(".gif") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700137 return { contentType: "image/gif", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700138 else if (str.indexOf(".jpg") >= 0 ||
139 str.indexOf(".jpeg") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700140 return { contentType: "image/jpeg", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700141 else if (str.indexOf(".png") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700142 return { contentType: "image/png", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700143 else if (str.indexOf(".bmp") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700144 return { contentType: "image/bmp", charset: "ISO-8859-1" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700145 else if (str.indexOf(".css") >= 0)
Jeff Thompson10de4592012-10-21 23:54:18 -0700146 return { contentType: "text/css", charset: "utf-8" }
Jeff Thompson25b06412012-10-21 20:07:57 -0700147 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700148
Jeff Thompson25b06412012-10-21 20:07:57 -0700149 // default
150 return { contentType: "text/html", charset: "utf-8" };
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700151}
Jeff Thompson10de4592012-10-21 23:54:18 -0700152
153/*
154 * Return true if the last component in the name is a sequence..
155 */
156function endsWithSequence(name) {
157 return name.components != null && name.components.length >= 1 &&
158 name.components[name.components.length - 1].length >= 1 &&
159 name.components[name.components.length - 1][0] == 0;
160}