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) { |
| 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 | */ |
| 143 | function 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 150 | } |