blob: 4b24d96004f0a17876ee52537c5c4f63f56cc731 [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(); },
19 host: "A.hub.ndn.ucla.edu", port: 9695,
Jeff Thompson4eb992a2013-03-09 21:05:53 -080020 verify: false });
21
22// These are set once a connection is established.
23NdnProtocolInfo.connectedNdnHubHost = null;
24NdnProtocolInfo.connectedNdnHubPort = null;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080025NdnProtocolInfo.ndnHubChangedListenerList = [];
Jeff Thompson6ad5c362012-12-27 17:57:02 -080026
27/*
28 * When the NDN hub host or port is changed, the system calls listener(host, port).
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080029 * If the current host and port are not null, call listener with the values to initialize.
Jeff Thompson6ad5c362012-12-27 17:57:02 -080030 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080031NdnProtocolInfo.addNdnHubChangedListener = function(listener) {
32 NdnProtocolInfo.ndnHubChangedListenerList.push(listener);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080033
Jeff Thompson4eb992a2013-03-09 21:05:53 -080034 if (NdnProtocolInfo.connectedNdnHubHost != null && NdnProtocolInfo.connectedNdnHubPort != null) {
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080035 try {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080036 listener(NdnProtocolInfo.connectedNdnHubHost, NdnProtocolInfo.connectedNdnHubPort);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080037 }
38 catch (ex) {
39 // Ignore error from the listener.
40 }
41 }
Jeff Thompsond51cb582013-03-17 19:00:39 -070042};
Jeff Thompson6ad5c362012-12-27 17:57:02 -080043
44/*
45 * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
46 * listener in ndnHubChangedListenerList.
47 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080048NdnProtocolInfo.setConnectedNdnHub = function(host, port) {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080049 if (host == NdnProtocolInfo.connectedNdnHubHost && port == NdnProtocolInfo.connectedNdnHubPort)
Jeff Thompson6ad5c362012-12-27 17:57:02 -080050 // No change.
51 return;
52
Jeff Thompson4eb992a2013-03-09 21:05:53 -080053 NdnProtocolInfo.connectedNdnHubHost = host;
54 NdnProtocolInfo.connectedNdnHubPort = port;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080055 for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) {
Jeff Thompson6ad5c362012-12-27 17:57:02 -080056 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080057 NdnProtocolInfo.ndnHubChangedListenerList[i](host, port);
Jeff Thompson6ad5c362012-12-27 17:57:02 -080058 }
59 catch (ex) {
60 // Ignore error from the listener.
61 }
62 }
Jeff Thompsond51cb582013-03-17 19:00:39 -070063};
Jeff Thompson8107ec82013-01-12 21:53:27 -080064
65/*
66 * Split the URI spec and return an object with protocol (including ':'), name,
67 * search (including '?') and hash value (including '#').
68 * All result strings are trimmed. This does not unescape the name.
69 * The name may include a host and port.
70 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080071NdnProtocolInfo.splitUri = function(spec) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080072 spec = spec.trim();
73 var result = {};
74 var preHash = spec.split('#', 1)[0];
75 result.hash = spec.substr(preHash.length).trim();
76 var preSearch = preHash.split('?', 1)[0];
77 result.search = preHash.substr(preSearch.length).trim();
78
79 preSearch = preSearch.trim();
80 var colonIndex = preSearch.indexOf(':');
81 if (colonIndex >= 0) {
82 result.protocol = preSearch.substr(0, colonIndex + 1).trim();
83 result.name = preSearch.substr(colonIndex + 1).trim();
84 }
85 else {
86 result.protocol = "";
87 result.name = preSearch;
88 }
89
90 return result;
Jeff Thompsond51cb582013-03-17 19:00:39 -070091};
92
93/*
94 * Do the work of the NDN Get Version buttons.
95 * selector is "earliest", "latest", "previous" or "next".
96 * currentWindow is the window with the address.
97 * alertFunction(message) shows an alert.
98 */
99NdnProtocolInfo.getVersion = function(selector, currentWindow, alertFunction) {
100 alertFunction("ndnGetVersion called");
101 try {
102 if (currentWindow._content.document.location.protocol != "ndn:") {
103 alertFunction("The address must start with ndn:");
104 return;
105 }
106
107 // Parse the same as in ndnProtocolService newChannel.
108 var uriParts = NdnProtocolInfo.splitUri(currentWindow._content.document.location.href);
109 var name = new Name(uriParts.name);
110 var indexOfVersion = getIndexOfVersion(name);
111 if (indexOfVersion < 0) {
112 alertFunction("The ndn address does not have a version");
113 return;
114 }
115
116 var escapedVersion = Name.toEscapedString(name.components[indexOfVersion]);
117
118 var childSelector;
119 if (selector == "earliest")
120 childSelector = "ndn.ChildSelector=0";
121 else if (selector == "latest")
122 childSelector = "ndn.ChildSelector=1";
123 else if (selector == "previous")
124 childSelector = "ndn.ChildSelector=1&ndn.Exclude=" + escapedVersion + ",*";
125 else if (selector == "next")
126 childSelector = "ndn.ChildSelector=0&ndn.Exclude=*," + escapedVersion;
127 else
128 // Don't expect this to happen.
129 return;
130
131 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
132 var searchWithChildSelector = (uriParts.search == "" ? "?" : uriParts.search + "&") + childSelector;
133
134 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
135 currentWindow._content.document.location = uri;
136 } catch (ex) {
137 dump("ndnToolbarGetVersion exception: " + ex + "\n" + ex.stack);
138 }
139};
140
141/*
142 * Return the index of the last component that starts with 0xfd, or -1 if not found.
143 */
144function getIndexOfVersion(name) {
145 for (var i = name.components.length - 1; i >= 0; --i) {
146 if (name.components[i].length >= 1 && name.components[i][0] == 0xfd)
147 return i;
148 }
149
150 return -1;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -0800151}