blob: cd1ae1a445c7d74f7d067c69234a132e729747b7 [file] [log] [blame]
Jeff Thompson6ad5c362012-12-27 17:57:02 -08001/*
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -08006var EXPORTED_SYMBOLS = ["NdnProtocolInfo"];
Jeff Thompson6ad5c362012-12-27 17:57:02 -08007
8const Cc = Components.classes;
9const Ci = Components.interfaces;
10const Cr = Components.results;
11
12Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Jeff Thompson4eb992a2013-03-09 21:05:53 -080013Components.utils.import("chrome://modules/content/ndn-js.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -080014
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080015var NdnProtocolInfo = function NdnProtocolInfo(){
16};
17
Jeff Thompsond51cb582013-03-17 19:00:39 -070018NdnProtocolInfo.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); },
Jeff Thompson4eb992a2013-03-09 21:05:53 -080019 verify: false });
20
21// These are set once a connection is established.
22NdnProtocolInfo.connectedNdnHubHost = null;
23NdnProtocolInfo.connectedNdnHubPort = null;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080024NdnProtocolInfo.ndnHubChangedListenerList = [];
Jeff Thompson6ad5c362012-12-27 17:57:02 -080025
26/*
27 * When the NDN hub host or port is changed, the system calls listener(host, port).
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080028 * If the current host and port are not null, call listener with the values to initialize.
Jeff Thompson6ad5c362012-12-27 17:57:02 -080029 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080030NdnProtocolInfo.addNdnHubChangedListener = function(listener) {
31 NdnProtocolInfo.ndnHubChangedListenerList.push(listener);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080032
Jeff Thompson4eb992a2013-03-09 21:05:53 -080033 if (NdnProtocolInfo.connectedNdnHubHost != null && NdnProtocolInfo.connectedNdnHubPort != null) {
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080034 try {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080035 listener(NdnProtocolInfo.connectedNdnHubHost, NdnProtocolInfo.connectedNdnHubPort);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080036 }
37 catch (ex) {
38 // Ignore error from the listener.
39 }
40 }
Jeff Thompsond51cb582013-03-17 19:00:39 -070041};
Jeff Thompson6ad5c362012-12-27 17:57:02 -080042
43/*
44 * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
45 * listener in ndnHubChangedListenerList.
46 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080047NdnProtocolInfo.setConnectedNdnHub = function(host, port) {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080048 if (host == NdnProtocolInfo.connectedNdnHubHost && port == NdnProtocolInfo.connectedNdnHubPort)
Jeff Thompson6ad5c362012-12-27 17:57:02 -080049 // No change.
50 return;
51
Jeff Thompson4eb992a2013-03-09 21:05:53 -080052 NdnProtocolInfo.connectedNdnHubHost = host;
53 NdnProtocolInfo.connectedNdnHubPort = port;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080054 for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) {
Jeff Thompson6ad5c362012-12-27 17:57:02 -080055 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080056 NdnProtocolInfo.ndnHubChangedListenerList[i](host, port);
Jeff Thompson6ad5c362012-12-27 17:57:02 -080057 }
58 catch (ex) {
59 // Ignore error from the listener.
60 }
61 }
Jeff Thompsond51cb582013-03-17 19:00:39 -070062};
Jeff Thompson8107ec82013-01-12 21:53:27 -080063
64/*
65 * Split the URI spec and return an object with protocol (including ':'), name,
66 * search (including '?') and hash value (including '#').
67 * All result strings are trimmed. This does not unescape the name.
68 * The name may include a host and port.
69 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080070NdnProtocolInfo.splitUri = function(spec) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080071 spec = spec.trim();
72 var result = {};
73 var preHash = spec.split('#', 1)[0];
74 result.hash = spec.substr(preHash.length).trim();
75 var preSearch = preHash.split('?', 1)[0];
76 result.search = preHash.substr(preSearch.length).trim();
77
78 preSearch = preSearch.trim();
79 var colonIndex = preSearch.indexOf(':');
80 if (colonIndex >= 0) {
81 result.protocol = preSearch.substr(0, colonIndex + 1).trim();
82 result.name = preSearch.substr(colonIndex + 1).trim();
83 }
84 else {
85 result.protocol = "";
86 result.name = preSearch;
87 }
88
89 return result;
Jeff Thompsond51cb582013-03-17 19:00:39 -070090};
91
92/*
93 * Do the work of the NDN Get Version buttons.
94 * selector is "earliest", "latest", "previous" or "next".
95 * currentWindow is the window with the address.
96 * alertFunction(message) shows an alert.
97 */
98NdnProtocolInfo.getVersion = function(selector, currentWindow, alertFunction) {
99 alertFunction("ndnGetVersion called");
100 try {
101 if (currentWindow._content.document.location.protocol != "ndn:") {
102 alertFunction("The address must start with ndn:");
103 return;
104 }
105
106 // Parse the same as in ndnProtocolService newChannel.
107 var uriParts = NdnProtocolInfo.splitUri(currentWindow._content.document.location.href);
108 var name = new Name(uriParts.name);
109 var indexOfVersion = getIndexOfVersion(name);
110 if (indexOfVersion < 0) {
111 alertFunction("The ndn address does not have a version");
112 return;
113 }
114
115 var escapedVersion = Name.toEscapedString(name.components[indexOfVersion]);
116
117 var childSelector;
118 if (selector == "earliest")
119 childSelector = "ndn.ChildSelector=0";
120 else if (selector == "latest")
121 childSelector = "ndn.ChildSelector=1";
122 else if (selector == "previous")
123 childSelector = "ndn.ChildSelector=1&ndn.Exclude=" + escapedVersion + ",*";
124 else if (selector == "next")
125 childSelector = "ndn.ChildSelector=0&ndn.Exclude=*," + escapedVersion;
126 else
127 // Don't expect this to happen.
128 return;
129
130 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
131 var searchWithChildSelector = (uriParts.search == "" ? "?" : uriParts.search + "&") + childSelector;
132
133 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
134 currentWindow._content.document.location = uri;
135 } catch (ex) {
136 dump("ndnToolbarGetVersion exception: " + ex + "\n" + ex.stack);
137 }
138};
139
140/*
141 * Return the index of the last component that starts with 0xfd, or -1 if not found.
142 */
143function getIndexOfVersion(name) {
144 for (var i = name.components.length - 1; i >= 0; --i) {
145 if (name.components[i].length >= 1 && name.components[i][0] == 0xfd)
146 return i;
147 }
148
149 return -1;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -0800150}