blob: 3d000f318c7d251867ed3295df303e3ff2bd2e61 [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 Thompson02b9f4d2012-11-29 14:40:57 -080011 var spec = window._content.document.location.href.trim();
12 var preHash = spec.split('#', 1)[0];
13 var hash = spec.substr(preHash.length).trim();
14 var preSearch = preHash.split('?', 1)[0];
15 var search = preHash.substr(preSearch.length).trim();
16 // Set nameString to the preSearch without the protocol.
17 var nameString = preSearch.trim();
18 if (nameString.indexOf(':') >= 0)
19 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
20
21 var name = new Name(nameString);
22 var indexOfVersion = getIndexOfVersion(name);
23 if (indexOfVersion < 0) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080024 alert("The ndn address does not have a version");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080025 return;
26 }
27
28 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
Jeff Thompsonbd829262012-11-30 22:28:37 -080029 var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ndn.ChildSelector=1";
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080030
Jeff Thompsonbd829262012-11-30 22:28:37 -080031 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash;
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080032 window._content.document.location = uri;
33}
34
Jeff Thompson6ad5c362012-12-27 17:57:02 -080035/*
36 * Return the index of the last component that starts with 0xfd, or -1 if not found.
37 */
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080038function getIndexOfVersion(name) {
39 for (var i = name.components.length - 1; i >= 0; --i) {
40 if (name.components[i].length >= 1 && name.components[i][0] == 0xfd)
41 return i;
42 }
43
44 return -1;
45}
Jeff Thompson6ad5c362012-12-27 17:57:02 -080046
47/*
48 * This is called when the connected NDN hub changes.
49 */
50function onNdnHubChanged(host, port) {
51 document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port);
52}
53
54// Assume that addNdnHubChangedListener is defined since we imported NdnProtocolInfo.jsm above.
55addNdnHubChangedListener(onNdnHubChanged);