blob: 0c34fb2357b70d36dd8d8e0c6a3021971bbcd6c5 [file] [log] [blame]
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08001Components.utils.import("chrome://modules/content/ndn-js.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -08002Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08003
Jeff Thompson6ad5c362012-12-27 17:57:02 -08004function ndnToolbarGetLatest(event) {
Jeff Thompsonbd829262012-11-30 22:28:37 -08005 if (window._content.document.location.protocol != "ndn:") {
Jeff Thompson35cfc262012-12-16 12:06:40 -08006 alert("The address must start with ndn:");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08007 return;
8 }
9
Jeff Thompsonbd829262012-11-30 22:28:37 -080010 // Parse the same as in ndnProtocolService newChannel.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080011 var uriParts = NdnProtocolInfo.splitUri(window._content.document.location.href);
Jeff Thompson8107ec82013-01-12 21:53:27 -080012 var name = new Name(uriParts.name);
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080013 var indexOfVersion = getIndexOfVersion(name);
14 if (indexOfVersion < 0) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080015 alert("The ndn address does not have a version");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080016 return;
17 }
18
19 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
Jeff Thompson8107ec82013-01-12 21:53:27 -080020 var searchWithChildSelector =
21 (uriParts.search == "" ? "?" : uriParts.search + "&") + "ndn.ChildSelector=1";
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080022
Jeff Thompson8107ec82013-01-12 21:53:27 -080023 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080024 window._content.document.location = uri;
25}
26
Jeff Thompson6ad5c362012-12-27 17:57:02 -080027/*
28 * Return the index of the last component that starts with 0xfd, or -1 if not found.
29 */
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080030function getIndexOfVersion(name) {
31 for (var i = name.components.length - 1; i >= 0; --i) {
32 if (name.components[i].length >= 1 && name.components[i][0] == 0xfd)
33 return i;
34 }
35
36 return -1;
37}
Jeff Thompson6ad5c362012-12-27 17:57:02 -080038
39/*
40 * This is called when the connected NDN hub changes.
41 */
42function onNdnHubChanged(host, port) {
43 document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port);
44}
45
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080046window.addEventListener("load", function() { NdnProtocolInfo.addNdnHubChangedListener(onNdnHubChanged); }, false);