browser/base/content/sync/addDevice.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cc = Components.classes;
michael@0 7 const Cu = Components.utils;
michael@0 8
michael@0 9 Cu.import("resource://services-sync/main.js");
michael@0 10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 11
michael@0 12 const PIN_PART_LENGTH = 4;
michael@0 13
michael@0 14 const ADD_DEVICE_PAGE = 0;
michael@0 15 const SYNC_KEY_PAGE = 1;
michael@0 16 const DEVICE_CONNECTED_PAGE = 2;
michael@0 17
michael@0 18 let gSyncAddDevice = {
michael@0 19
michael@0 20 init: function init() {
michael@0 21 this.pin1.setAttribute("maxlength", PIN_PART_LENGTH);
michael@0 22 this.pin2.setAttribute("maxlength", PIN_PART_LENGTH);
michael@0 23 this.pin3.setAttribute("maxlength", PIN_PART_LENGTH);
michael@0 24
michael@0 25 this.nextFocusEl = {pin1: this.pin2,
michael@0 26 pin2: this.pin3,
michael@0 27 pin3: this.wizard.getButton("next")};
michael@0 28
michael@0 29 this.throbber = document.getElementById("pairDeviceThrobber");
michael@0 30 this.errorRow = document.getElementById("errorRow");
michael@0 31
michael@0 32 // Kick off a sync. That way the server will have the most recent data from
michael@0 33 // this computer and it will show up immediately on the new device.
michael@0 34 Weave.Service.scheduler.scheduleNextSync(0);
michael@0 35 },
michael@0 36
michael@0 37 onPageShow: function onPageShow() {
michael@0 38 this.wizard.getButton("back").hidden = true;
michael@0 39
michael@0 40 switch (this.wizard.pageIndex) {
michael@0 41 case ADD_DEVICE_PAGE:
michael@0 42 this.onTextBoxInput();
michael@0 43 this.wizard.canRewind = false;
michael@0 44 this.wizard.getButton("next").hidden = false;
michael@0 45 this.pin1.focus();
michael@0 46 break;
michael@0 47 case SYNC_KEY_PAGE:
michael@0 48 this.wizard.canAdvance = false;
michael@0 49 this.wizard.canRewind = true;
michael@0 50 this.wizard.getButton("back").hidden = false;
michael@0 51 this.wizard.getButton("next").hidden = true;
michael@0 52 document.getElementById("weavePassphrase").value =
michael@0 53 Weave.Utils.hyphenatePassphrase(Weave.Service.identity.syncKey);
michael@0 54 break;
michael@0 55 case DEVICE_CONNECTED_PAGE:
michael@0 56 this.wizard.canAdvance = true;
michael@0 57 this.wizard.canRewind = false;
michael@0 58 this.wizard.getButton("cancel").hidden = true;
michael@0 59 break;
michael@0 60 }
michael@0 61 },
michael@0 62
michael@0 63 onWizardAdvance: function onWizardAdvance() {
michael@0 64 switch (this.wizard.pageIndex) {
michael@0 65 case ADD_DEVICE_PAGE:
michael@0 66 this.startTransfer();
michael@0 67 return false;
michael@0 68 case DEVICE_CONNECTED_PAGE:
michael@0 69 window.close();
michael@0 70 return false;
michael@0 71 }
michael@0 72 return true;
michael@0 73 },
michael@0 74
michael@0 75 startTransfer: function startTransfer() {
michael@0 76 this.errorRow.hidden = true;
michael@0 77 // When onAbort is called, Weave may already be gone.
michael@0 78 const JPAKE_ERROR_USERABORT = Weave.JPAKE_ERROR_USERABORT;
michael@0 79
michael@0 80 let self = this;
michael@0 81 let jpakeclient = this._jpakeclient = new Weave.JPAKEClient({
michael@0 82 onPaired: function onPaired() {
michael@0 83 let credentials = {account: Weave.Service.identity.account,
michael@0 84 password: Weave.Service.identity.basicPassword,
michael@0 85 synckey: Weave.Service.identity.syncKey,
michael@0 86 serverURL: Weave.Service.serverURL};
michael@0 87 jpakeclient.sendAndComplete(credentials);
michael@0 88 },
michael@0 89 onComplete: function onComplete() {
michael@0 90 delete self._jpakeclient;
michael@0 91 self.wizard.pageIndex = DEVICE_CONNECTED_PAGE;
michael@0 92
michael@0 93 // Schedule a Sync for soonish to fetch the data uploaded by the
michael@0 94 // device with which we just paired.
michael@0 95 Weave.Service.scheduler.scheduleNextSync(Weave.Service.scheduler.activeInterval);
michael@0 96 },
michael@0 97 onAbort: function onAbort(error) {
michael@0 98 delete self._jpakeclient;
michael@0 99
michael@0 100 // Aborted by user, ignore.
michael@0 101 if (error == JPAKE_ERROR_USERABORT) {
michael@0 102 return;
michael@0 103 }
michael@0 104
michael@0 105 self.errorRow.hidden = false;
michael@0 106 self.throbber.hidden = true;
michael@0 107 self.pin1.value = self.pin2.value = self.pin3.value = "";
michael@0 108 self.pin1.disabled = self.pin2.disabled = self.pin3.disabled = false;
michael@0 109 self.pin1.focus();
michael@0 110 }
michael@0 111 });
michael@0 112 this.throbber.hidden = false;
michael@0 113 this.pin1.disabled = this.pin2.disabled = this.pin3.disabled = true;
michael@0 114 this.wizard.canAdvance = false;
michael@0 115
michael@0 116 let pin = this.pin1.value + this.pin2.value + this.pin3.value;
michael@0 117 let expectDelay = false;
michael@0 118 jpakeclient.pairWithPIN(pin, expectDelay);
michael@0 119 },
michael@0 120
michael@0 121 onWizardBack: function onWizardBack() {
michael@0 122 if (this.wizard.pageIndex != SYNC_KEY_PAGE)
michael@0 123 return true;
michael@0 124
michael@0 125 this.wizard.pageIndex = ADD_DEVICE_PAGE;
michael@0 126 return false;
michael@0 127 },
michael@0 128
michael@0 129 onWizardCancel: function onWizardCancel() {
michael@0 130 if (this._jpakeclient) {
michael@0 131 this._jpakeclient.abort();
michael@0 132 delete this._jpakeclient;
michael@0 133 }
michael@0 134 return true;
michael@0 135 },
michael@0 136
michael@0 137 onTextBoxInput: function onTextBoxInput(textbox) {
michael@0 138 if (textbox && textbox.value.length == PIN_PART_LENGTH)
michael@0 139 this.nextFocusEl[textbox.id].focus();
michael@0 140
michael@0 141 this.wizard.canAdvance = (this.pin1.value.length == PIN_PART_LENGTH
michael@0 142 && this.pin2.value.length == PIN_PART_LENGTH
michael@0 143 && this.pin3.value.length == PIN_PART_LENGTH);
michael@0 144 },
michael@0 145
michael@0 146 goToSyncKeyPage: function goToSyncKeyPage() {
michael@0 147 this.wizard.pageIndex = SYNC_KEY_PAGE;
michael@0 148 }
michael@0 149
michael@0 150 };
michael@0 151 // onWizardAdvance() and onPageShow() are run before init() so we'll set
michael@0 152 // these up as lazy getters.
michael@0 153 ["wizard", "pin1", "pin2", "pin3"].forEach(function (id) {
michael@0 154 XPCOMUtils.defineLazyGetter(gSyncAddDevice, id, function() {
michael@0 155 return document.getElementById(id);
michael@0 156 });
michael@0 157 });

mercurial