Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 6 | var EXPORTED_SYMBOLS = ["NdnProtocolInfo"]; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 7 | |
| 8 | const Cc = Components.classes; |
| 9 | const Ci = Components.interfaces; |
| 10 | const Cr = Components.results; |
| 11 | |
| 12 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 13 | Components.utils.import("chrome://modules/content/ndn-js.jsm"); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 14 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 15 | var NdnProtocolInfo = function NdnProtocolInfo(){ |
| 16 | }; |
| 17 | |
Jeff Thompson | d51cb58 | 2013-03-17 19:00:39 -0700 | [diff] [blame] | 18 | NdnProtocolInfo.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); }, |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 19 | verify: false }); |
| 20 | |
| 21 | // These are set once a connection is established. |
| 22 | NdnProtocolInfo.connectedNdnHubHost = null; |
| 23 | NdnProtocolInfo.connectedNdnHubPort = null; |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 24 | NdnProtocolInfo.ndnHubChangedListenerList = []; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * When the NDN hub host or port is changed, the system calls listener(host, port). |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 28 | * If the current host and port are not null, call listener with the values to initialize. |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 29 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 30 | NdnProtocolInfo.addNdnHubChangedListener = function(listener) { |
| 31 | NdnProtocolInfo.ndnHubChangedListenerList.push(listener); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 32 | |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 33 | if (NdnProtocolInfo.connectedNdnHubHost != null && NdnProtocolInfo.connectedNdnHubPort != null) { |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 34 | try { |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 35 | listener(NdnProtocolInfo.connectedNdnHubHost, NdnProtocolInfo.connectedNdnHubPort); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 36 | } |
| 37 | catch (ex) { |
| 38 | // Ignore error from the listener. |
| 39 | } |
| 40 | } |
Jeff Thompson | d51cb58 | 2013-03-17 19:00:39 -0700 | [diff] [blame] | 41 | }; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * If host and port are different than ndnHubHost or ndnHubPort, set them and call each |
| 45 | * listener in ndnHubChangedListenerList. |
| 46 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 47 | NdnProtocolInfo.setConnectedNdnHub = function(host, port) { |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 48 | if (host == NdnProtocolInfo.connectedNdnHubHost && port == NdnProtocolInfo.connectedNdnHubPort) |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 49 | // No change. |
| 50 | return; |
| 51 | |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 52 | NdnProtocolInfo.connectedNdnHubHost = host; |
| 53 | NdnProtocolInfo.connectedNdnHubPort = port; |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 54 | for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) { |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 55 | try { |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 56 | NdnProtocolInfo.ndnHubChangedListenerList[i](host, port); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 57 | } |
| 58 | catch (ex) { |
| 59 | // Ignore error from the listener. |
| 60 | } |
| 61 | } |
Jeff Thompson | d51cb58 | 2013-03-17 19:00:39 -0700 | [diff] [blame] | 62 | }; |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 63 | |
| 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 70 | NdnProtocolInfo.splitUri = function(spec) { |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 71 | 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 Thompson | d51cb58 | 2013-03-17 19:00:39 -0700 | [diff] [blame] | 90 | }; |
| 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 | */ |
| 98 | NdnProtocolInfo.getVersion = function(selector, currentWindow, alertFunction) { |
Jeff Thompson | d51cb58 | 2013-03-17 19:00:39 -0700 | [diff] [blame] | 99 | try { |
| 100 | if (currentWindow._content.document.location.protocol != "ndn:") { |
| 101 | alertFunction("The address must start with ndn:"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Parse the same as in ndnProtocolService newChannel. |
| 106 | var uriParts = NdnProtocolInfo.splitUri(currentWindow._content.document.location.href); |
| 107 | var name = new Name(uriParts.name); |
| 108 | var indexOfVersion = getIndexOfVersion(name); |
| 109 | if (indexOfVersion < 0) { |
| 110 | alertFunction("The ndn address does not have a version"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | var escapedVersion = Name.toEscapedString(name.components[indexOfVersion]); |
| 115 | |
| 116 | var childSelector; |
| 117 | if (selector == "earliest") |
| 118 | childSelector = "ndn.ChildSelector=0"; |
| 119 | else if (selector == "latest") |
| 120 | childSelector = "ndn.ChildSelector=1"; |
| 121 | else if (selector == "previous") |
| 122 | childSelector = "ndn.ChildSelector=1&ndn.Exclude=" + escapedVersion + ",*"; |
| 123 | else if (selector == "next") |
| 124 | childSelector = "ndn.ChildSelector=0&ndn.Exclude=*," + escapedVersion; |
| 125 | else |
| 126 | // Don't expect this to happen. |
| 127 | return; |
| 128 | |
| 129 | var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion)); |
| 130 | var searchWithChildSelector = (uriParts.search == "" ? "?" : uriParts.search + "&") + childSelector; |
| 131 | |
| 132 | var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash; |
| 133 | currentWindow._content.document.location = uri; |
| 134 | } catch (ex) { |
| 135 | dump("ndnToolbarGetVersion exception: " + ex + "\n" + ex.stack); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * Return the index of the last component that starts with 0xfd, or -1 if not found. |
| 141 | */ |
| 142 | function getIndexOfVersion(name) { |
| 143 | for (var i = name.components.length - 1; i >= 0; --i) { |
| 144 | if (name.components[i].length >= 1 && name.components[i][0] == 0xfd) |
| 145 | return i; |
| 146 | } |
| 147 | |
| 148 | return -1; |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 149 | } |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 150 | |
Jeff Thompson | 9c24c47 | 2013-03-17 22:11:07 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Prompt the user and set the hub. If the current address is ndn: then try to reload. |
| 153 | * If changing the hub, then return a message to display such as "Hub: trying host:port", |
| 154 | * otherwise null for no message. |
| 155 | * currentWindow is the window with the address. |
| 156 | */ |
| 157 | NdnProtocolInfo.setHub = function(currentWindow) { |
| 158 | var host = currentWindow.prompt("Enter hub host:", NdnProtocolInfo.ndn.host); |
| 159 | if (!host) |
| 160 | return null; |
| 161 | |
| 162 | host = host.trim(); |
| 163 | if (host == "") |
| 164 | return null; |
| 165 | if (host == NdnProtocolInfo.ndn.host) |
| 166 | // No change. |
| 167 | return null; |
| 168 | |
| 169 | var port = 9695; |
| 170 | NdnProtocolInfo.ndn.createRoute(host, port); |
| 171 | if (currentWindow._content.document.location.protocol == "ndn:") |
| 172 | // Reload with the new hub. |
| 173 | currentWindow._content.document.location = currentWindow._content.document.location.href; |
| 174 | |
| 175 | return "Hub: trying " + host + ":" + port; |
| 176 | } |
| 177 | |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 178 | /// The following is only used by Firefox for Android to set up the menus. |
| 179 | /// It really doesn't belong in this file, but it needs to be in a module that we know is |
| 180 | /// run on startup. If it can be put somewhere else without circular references, then it should be. |
| 181 | |
| 182 | function loadIntoWindow(window) { |
| 183 | if (!window) |
| 184 | return; |
| 185 | |
| 186 | // Add to Firefox for Android menu. |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 187 | window.NativeWindow.menu.add("NDN Get Version...", null, function() { |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 188 | ndnGetVersionClick(window); |
| 189 | }); |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 190 | window.NativeWindow.menu.add("NDN Hub...", null, function() { |
| 191 | ndnHubClick(window); |
| 192 | }); |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | function ndnGetVersionClick(window) { |
| 196 | var alertFunction = function(message) { window.NativeWindow.toast.show(message, "short"); }; |
| 197 | |
| 198 | var buttons = [ |
| 199 | { |
| 200 | label: "Earliest", |
| 201 | callback: function () { |
| 202 | NdnProtocolInfo.getVersion("earliest", window, alertFunction); |
| 203 | } |
| 204 | }, |
| 205 | { |
| 206 | label: "Prev.", |
| 207 | callback: function () { |
| 208 | NdnProtocolInfo.getVersion("previous", window, alertFunction); |
| 209 | } |
| 210 | }, |
| 211 | { |
| 212 | label: "Next", |
| 213 | callback: function () { |
| 214 | NdnProtocolInfo.getVersion("next", window, alertFunction); |
| 215 | } |
| 216 | }, |
| 217 | { |
| 218 | label: "Latest", |
| 219 | callback: function () { |
| 220 | NdnProtocolInfo.getVersion("latest", window, alertFunction); |
| 221 | } |
| 222 | } |
| 223 | ]; |
| 224 | |
| 225 | window.NativeWindow.doorhanger.show("NDN Get Version", "ndn-get-version", buttons, |
| 226 | window.BrowserApp.selectedTab.id, { persistence: 1 }); |
| 227 | } |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 228 | |
| 229 | function ndnHubClick(window) { |
| 230 | var alertFunction = function(message) { window.NativeWindow.toast.show(message, "short"); }; |
| 231 | |
| 232 | var buttons = [ |
| 233 | { |
Jeff Thompson | 9c24c47 | 2013-03-17 22:11:07 -0700 | [diff] [blame] | 234 | label: "Set...", |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 235 | callback: function () { |
Jeff Thompson | 9c24c47 | 2013-03-17 22:11:07 -0700 | [diff] [blame] | 236 | var message = NdnProtocolInfo.setHub(window); |
| 237 | if (message != null) |
| 238 | androidHubMessage = message; |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | ]; |
| 242 | |
| 243 | window.NativeWindow.doorhanger.show(androidHubMessage, "ndn-hub", buttons, |
| 244 | window.BrowserApp.selectedTab.id, { persistence: 1 }); |
| 245 | } |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 246 | |
| 247 | var windowListener = { |
| 248 | onOpenWindow: function(aWindow) { |
| 249 | // Wait for the window to finish loading |
| 250 | let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); |
| 251 | domWindow.addEventListener("load", function() { |
| 252 | domWindow.removeEventListener("load", arguments.callee, false); |
| 253 | loadIntoWindow(domWindow); |
| 254 | }, false); |
| 255 | }, |
| 256 | |
| 257 | onCloseWindow: function(aWindow) {}, |
| 258 | onWindowTitleChange: function(aWindow, aTitle) {} |
| 259 | }; |
| 260 | |
| 261 | function androidStartup(aData, aReason) { |
| 262 | let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); |
| 263 | |
| 264 | // Load into any existing windows |
| 265 | let windows = wm.getEnumerator("navigator:browser"); |
| 266 | while (windows.hasMoreElements()) { |
| 267 | let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); |
| 268 | loadIntoWindow(domWindow); |
| 269 | } |
| 270 | |
| 271 | // Load into any new windows |
| 272 | wm.addListener(windowListener); |
| 273 | } |
| 274 | |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 275 | var androidHubMessage = "Hub: not connected"; |
| 276 | |
| 277 | function androidOnNdnHubChanged(host, port) { |
| 278 | androidHubMessage = "Hub: " + host + ":" + port; |
| 279 | } |
| 280 | |
Jeff Thompson | ca5130a | 2013-03-17 20:35:51 -0700 | [diff] [blame] | 281 | // Do this here instead of using bootstrap.js since we don't want to set bootstrap true in install.rdf. |
Jeff Thompson | cd376e7 | 2013-03-17 21:34:36 -0700 | [diff] [blame] | 282 | try { |
| 283 | // startup() will only succeed on Android. |
| 284 | androidStartup(); |
| 285 | NdnProtocolInfo.addNdnHubChangedListener(androidOnNdnHubChanged); |
| 286 | } catch (ex) {} |