browser/metro/base/content/flyoutpanels/SyncFlyoutPanel.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 "use strict";
michael@0 6
michael@0 7 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 8
michael@0 9 let SyncFlyoutPanel = {
michael@0 10 init: function() {
michael@0 11 if (this._isInitialized) {
michael@0 12 Cu.reportError("Attempted to initialize SyncFlyoutPanel more than once");
michael@0 13 return;
michael@0 14 }
michael@0 15
michael@0 16 this._isInitialized = true;
michael@0 17 this._bundle = Services.strings.createBundle("chrome://browser/locale/sync.properties");
michael@0 18 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 19 let self = this;
michael@0 20
michael@0 21 this._elements = {};
michael@0 22 [
michael@0 23 ['outer', 'sync-flyoutpanel'],
michael@0 24 ['preSetup', 'sync-presetup-container'],
michael@0 25 ['easySetup', 'sync-setup-container'],
michael@0 26 ['manualSetup', 'sync-manualsetup-container'],
michael@0 27 ['setupSuccess', 'sync-setupsuccess-container'],
michael@0 28 ['setupFailure', 'sync-setupfailure-container'],
michael@0 29 ['connected', 'sync-connected-container'],
michael@0 30 ['pairNewDevice', 'sync-pair-container'],
michael@0 31 ['pairSuccess', 'sync-pair-success-container'],
michael@0 32 ['setupCode1', 'sync-setup-code1'],
michael@0 33 ['setupCode2', 'sync-setup-code2'],
michael@0 34 ['setupCode3', 'sync-setup-code3'],
michael@0 35 ['setupThrobber', 'sync-setup-throbber'],
michael@0 36 ['account', 'sync-manualsetup-account'],
michael@0 37 ['password', 'sync-manualsetup-password'],
michael@0 38 ['syncKey', 'sync-manualsetup-syncKey'],
michael@0 39 ['manualSetupConnect', 'sync-manualsetup-connect'],
michael@0 40 ['manualSetupFailure', 'sync-manualsetup-failure'],
michael@0 41 ['connectedAccount', 'sync-connected-account'],
michael@0 42 ['deviceName', 'sync-connected-device'],
michael@0 43 ['lastSync', 'sync-connected-lastSynced'],
michael@0 44 ['connectedThrobber', 'sync-connected-throbber'],
michael@0 45 ['disconnectLink', 'sync-disconnect-label'],
michael@0 46 ['disconnectWarning', 'sync-disconnect-warning'],
michael@0 47 ['pairCode1', 'sync-pair-entry1'],
michael@0 48 ['pairCode2', 'sync-pair-entry2'],
michael@0 49 ['pairCode3', 'sync-pair-entry3'],
michael@0 50 ['pairButton', 'sync-pair-button'],
michael@0 51 ['pairFailureMessage', 'sync-pair-failure'],
michael@0 52 ].forEach(function (aContainer) {
michael@0 53 let [name, id] = aContainer;
michael@0 54 XPCOMUtils.defineLazyGetter(self._elements, name, function() {
michael@0 55 return document.getElementById(id);
michael@0 56 });
michael@0 57 });
michael@0 58
michael@0 59 this._topmostElement = this._elements.outer;
michael@0 60
michael@0 61 let xps = Components.classes["@mozilla.org/weave/service;1"]
michael@0 62 .getService(Components.interfaces.nsISupports)
michael@0 63 .wrappedJSObject;
michael@0 64
michael@0 65 if (xps.ready) {
michael@0 66 this._onServiceReady();
michael@0 67 } else {
michael@0 68 Services.obs.addObserver(this._onServiceReady.bind(this),
michael@0 69 "weave:service:ready",
michael@0 70 false);
michael@0 71 xps.ensureLoaded();
michael@0 72 }
michael@0 73 },
michael@0 74
michael@0 75 _hide: function() {
michael@0 76 this._elements.outer.hide();
michael@0 77 this.showInitialScreen();
michael@0 78 },
michael@0 79
michael@0 80 _hideVisibleContainer: function() {
michael@0 81 if (this._currentlyVisibleContainer) {
michael@0 82 this._currentlyVisibleContainer.collapsed = true;
michael@0 83 delete this._currentlyVisibleContainer;
michael@0 84 delete this._onBackButton;
michael@0 85 }
michael@0 86 },
michael@0 87
michael@0 88 _onServiceReady: function(aEvent) {
michael@0 89 if (aEvent) {
michael@0 90 Services.obs.removeObserver(this._onServiceReady, "weave:service:ready");
michael@0 91 }
michael@0 92
michael@0 93 this.showInitialScreen();
michael@0 94 Services.obs.addObserver(this._onSyncStart.bind(this), "weave:service:sync:start", false);
michael@0 95 Services.obs.addObserver(this._onSyncEnd.bind(this), "weave:ui:sync:finish", false);
michael@0 96 Services.obs.addObserver(this._onSyncEnd.bind(this), "weave:ui:sync:error", false);
michael@0 97 Weave.Service.scheduler.scheduleNextSync(10*1000);
michael@0 98 },
michael@0 99
michael@0 100 _onSyncStart: function() {
michael@0 101 this._isSyncing = true;
michael@0 102 this._updateConnectedPage();
michael@0 103 },
michael@0 104
michael@0 105 _onSyncEnd: function() {
michael@0 106 this._isSyncing = false;
michael@0 107 this._updateConnectedPage();
michael@0 108 },
michael@0 109
michael@0 110 showInitialScreen: function() {
michael@0 111 if (Weave.Status.login == Weave.LOGIN_SUCCEEDED) {
michael@0 112 this.showConnected();
michael@0 113 } else {
michael@0 114 this.showPreSetup();
michael@0 115 }
michael@0 116 },
michael@0 117
michael@0 118 abortEasySetup: function() {
michael@0 119 if (this._setupJpake) {
michael@0 120 this._setupJpake.abort();
michael@0 121 }
michael@0 122 this._cleanUpEasySetup();
michael@0 123 },
michael@0 124
michael@0 125 _cleanUpEasySetup: function() {
michael@0 126 this._elements.setupCode1.value = "";
michael@0 127 this._elements.setupCode2.value = "";
michael@0 128 this._elements.setupCode3.value = "";
michael@0 129 delete this._setupJpake;
michael@0 130 this._elements.setupThrobber.collapsed = true;
michael@0 131 this._elements.setupThrobber.enabled = false;
michael@0 132 },
michael@0 133
michael@0 134 _updateConnectedPage: function() {
michael@0 135 // Show the day-of-week and time (HH:MM) of last sync
michael@0 136 let lastSync = Weave.Svc.Prefs.get("lastSync");
michael@0 137 let syncDate = '';
michael@0 138 if (lastSync != null) {
michael@0 139 syncDate = new Date(lastSync).toLocaleFormat("%A %I:%M %p");
michael@0 140 }
michael@0 141
michael@0 142 let device = Weave.Service.clientsEngine.localName;
michael@0 143 let account = Weave.Service.identity.account;
michael@0 144 this._elements.deviceName.textContent =
michael@0 145 this._bundle.formatStringFromName("sync.flyout.connected.device",
michael@0 146 [device], 1);
michael@0 147 this._elements.connectedAccount.textContent =
michael@0 148 this._bundle.formatStringFromName("sync.flyout.connected.account",
michael@0 149 [account], 1);
michael@0 150 this._elements.lastSync.textContent =
michael@0 151 this._bundle.formatStringFromName("sync.flyout.connected.lastSynced",
michael@0 152 [syncDate], 1);
michael@0 153
michael@0 154 if (this._currentlyVisibleContainer == this._elements.connected
michael@0 155 && this._isSyncing) {
michael@0 156 this._elements.connectedThrobber.collapsed = false;
michael@0 157 this._elements.connectedThrobber.enabled = true;
michael@0 158 } else {
michael@0 159 this._elements.connectedThrobber.collapsed = true;
michael@0 160 this._elements.connectedThrobber.enabled = false;
michael@0 161 }
michael@0 162 },
michael@0 163
michael@0 164 showConnected: function() {
michael@0 165 // Reset state of the connected screen
michael@0 166 this._elements.disconnectWarning.collapsed = true;
michael@0 167 this._elements.disconnectLink.collapsed = false;
michael@0 168
michael@0 169 this._updateConnectedPage();
michael@0 170 this._showContainer(this._elements.connected);
michael@0 171 },
michael@0 172
michael@0 173 startEasySetup: function() {
michael@0 174 let self = this;
michael@0 175
michael@0 176 this._showContainer(this._elements.easySetup);
michael@0 177
michael@0 178 // Set up our back button to do the appropriate action
michael@0 179 this._onBackButton = function() {
michael@0 180 self.abortEasySetup();
michael@0 181 self.showInitialScreen();
michael@0 182 };
michael@0 183
michael@0 184 this._setupJpake = new Weave.JPAKEClient({
michael@0 185 displayPIN: function displayPIN(aPin) {
michael@0 186 self._elements.setupCode1.value = aPin.slice(0, 4);
michael@0 187 self._elements.setupCode2.value = aPin.slice(4, 8);
michael@0 188 self._elements.setupCode3.value = aPin.slice(8);
michael@0 189 },
michael@0 190
michael@0 191 onPairingStart: function onPairingStart() {
michael@0 192 self._elements.setupThrobber.collapsed = false;
michael@0 193 self._elements.setupThrobber.enabled = true;
michael@0 194 },
michael@0 195
michael@0 196 onComplete: function onComplete(aCredentials) {
michael@0 197 Weave.Service.identity.account = aCredentials.account;
michael@0 198 Weave.Service.identity.basicPassword = aCredentials.password;
michael@0 199 Weave.Service.identity.syncKey = aCredentials.synckey;
michael@0 200 Weave.Service.serverURL = aCredentials.serverURL;
michael@0 201 Weave.Service.persistLogin();
michael@0 202 Weave.Service.scheduler.scheduleNextSync(0);
michael@0 203
michael@0 204 if (self._currentlyVisibleContainer == self._elements.easySetup) {
michael@0 205 self.showSetupSuccess();
michael@0 206 }
michael@0 207 self._cleanUpEasySetup();
michael@0 208 },
michael@0 209
michael@0 210 onAbort: function onAbort(aError) {
michael@0 211 if (aError == "jpake.error.userabort") {
michael@0 212 Services.obs.notifyObservers(null, "browser:sync:setup:userabort", "");
michael@0 213 self._cleanUpEasySetup();
michael@0 214 return;
michael@0 215 } else if (aError == "jpake.error.network") {
michael@0 216 Services.obs.notifyObservers(null, "browser:sync:setup:networkerror", "");
michael@0 217 }
michael@0 218
michael@0 219 if (self._currentlyVisibleContainer == self._elements.easySetup) {
michael@0 220 self.showSetupFailure();
michael@0 221 self._cleanUpEasySetup();
michael@0 222 }
michael@0 223 }
michael@0 224 });
michael@0 225
michael@0 226 this._setupJpake.receiveNoPIN();
michael@0 227 },
michael@0 228
michael@0 229 _showContainer: function(aContainer) {
michael@0 230 this._hideVisibleContainer();
michael@0 231 this._currentlyVisibleContainer = aContainer;
michael@0 232 this._currentlyVisibleContainer.collapsed = false;
michael@0 233 },
michael@0 234
michael@0 235 showSetupSuccess: function() {
michael@0 236 this._showContainer(this._elements.setupSuccess);
michael@0 237 this._onBackButton = this.showInitialScreen;
michael@0 238 },
michael@0 239
michael@0 240 showSetupFailure: function() {
michael@0 241 this._showContainer(this._elements.setupFailure);
michael@0 242 this._onBackButton = this.showInitialScreen;
michael@0 243 },
michael@0 244
michael@0 245 showPreSetup: function() {
michael@0 246 this._showContainer(this._elements.preSetup);
michael@0 247 delete this._onBackButton;
michael@0 248 },
michael@0 249
michael@0 250 showManualSetup: function() {
michael@0 251 this._showContainer(this._elements.manualSetup);
michael@0 252 this._onBackButton = this.showInitialScreen;
michael@0 253
michael@0 254 this._elements.account.value = Weave.Service.identity.account;
michael@0 255 this._elements.password.value = Weave.Service.identity.basicPassword;
michael@0 256 this._elements.syncKey.value =
michael@0 257 Weave.Utils.hyphenatePassphrase(Weave.Service.identity.syncKey);
michael@0 258 this.updateManualSetupConnectButtonState();
michael@0 259 },
michael@0 260
michael@0 261 updateManualSetupConnectButtonState: function() {
michael@0 262 this._elements.manualSetupConnect.disabled = !this._elements.account.value
michael@0 263 || !this._elements.password.value
michael@0 264 || !this._elements.syncKey.value;
michael@0 265 },
michael@0 266
michael@0 267 manualSetupConnect: function() {
michael@0 268 delete this._onBackButton;
michael@0 269 this._elements.manualSetupConnect.disabled = true;
michael@0 270 Weave.Service.identity.account = this._elements.account.value;
michael@0 271 Weave.Service.identity.basicPassword = this._elements.password.value;
michael@0 272 Weave.Service.identity.syncKey = Weave.Utils.normalizePassphrase(this._elements.syncKey.value);
michael@0 273 if (Weave.Service.login()) {
michael@0 274 Weave.Service.persistLogin();
michael@0 275 if (this._currentlyVisibleContainer == this._elements.manualSetup) {
michael@0 276 this.showSetupSuccess();
michael@0 277 }
michael@0 278 Weave.Service.scheduler.scheduleNextSync(0);
michael@0 279 } else {
michael@0 280 this._elements.manualSetupFailure.textContent = Weave.Utils.getErrorString(Weave.Status.login);
michael@0 281 this._elements.manualSetupFailure.collapsed = false;
michael@0 282 this.updateManualSetupConnectButtonState();
michael@0 283 }
michael@0 284 },
michael@0 285
michael@0 286 onDisconnectLink: function() {
michael@0 287 this._elements.disconnectWarning.collapsed = false;
michael@0 288 this._elements.disconnectLink.collapsed = true;
michael@0 289 },
michael@0 290
michael@0 291 onDisconnectCancel: function() {
michael@0 292 this._elements.disconnectWarning.collapsed = true;
michael@0 293 this._elements.disconnectLink.collapsed = false;
michael@0 294 },
michael@0 295
michael@0 296 onDisconnectButton: function() {
michael@0 297 Weave.Service.startOver();
michael@0 298 this.showInitialScreen();
michael@0 299 },
michael@0 300
michael@0 301 onPairDeviceLink: function() {
michael@0 302 // Reset state
michael@0 303 this._elements.pairCode1.value = "";
michael@0 304 this._elements.pairCode2.value = "";
michael@0 305 this._elements.pairCode3.value = "";
michael@0 306 this.updatePairButtonState();
michael@0 307 this._elements.pairFailureMessage.collapsed = true;
michael@0 308 this._elements.pairNewDevice.collapsed = false;
michael@0 309
michael@0 310 this._showContainer(this._elements.pairNewDevice);
michael@0 311 this._onBackButton = this.showInitialScreen;
michael@0 312 },
michael@0 313
michael@0 314 updatePairButtonState: function () {
michael@0 315 this._elements.pairButton.disabled = !this._elements.pairCode1.value
michael@0 316 || !this._elements.pairCode2.value
michael@0 317 || !this._elements.pairCode3.value;
michael@0 318 },
michael@0 319
michael@0 320 onCancelButton: function() {
michael@0 321 this.showInitialContainer();
michael@0 322 },
michael@0 323
michael@0 324 onTryAgainButton: function() {
michael@0 325 this.startEasySetup();
michael@0 326 },
michael@0 327
michael@0 328 onPairButton: function() {
michael@0 329 this._elements.pairButton.disabled = true;
michael@0 330 this._elements.pairFailureMessage.collapsed = true;
michael@0 331 let self = this;
michael@0 332 this._pairJpake = new Weave.JPAKEClient({
michael@0 333 onPaired: function() {
michael@0 334 self._pairJpake.sendAndComplete({
michael@0 335 account: Weave.Service.identity.account,
michael@0 336 password: Weave.Service.identity.basicPassword,
michael@0 337 synckey: Weave.Service.identity.syncKey,
michael@0 338 serverURL: Weave.Service.serverURL
michael@0 339 });
michael@0 340 },
michael@0 341
michael@0 342 onComplete: function() {
michael@0 343 delete self._pairJpake;
michael@0 344 Weave.Service.persistLogin();
michael@0 345 if (self._currentlyVisibleContainer == self._elements.pairNewDevice) {
michael@0 346 self._showContainer(self._elements.pairSuccess);
michael@0 347 }
michael@0 348 Weave.Service.scheduler.scheduleNextSync(Weave.Service.scheduler.activeInterval);
michael@0 349 },
michael@0 350
michael@0 351 onAbort: function(error) {
michael@0 352 delete self._pairJpake;
michael@0 353 if (error == Weave.JPAKE_ERROR_USERABORT) {
michael@0 354 return;
michael@0 355 }
michael@0 356
michael@0 357 self._elements.pairFailureMessage.collapsed = false;
michael@0 358 self.updatePairButtonState();
michael@0 359 }
michael@0 360 });
michael@0 361
michael@0 362 this._pairJpake.pairWithPIN(this._elements.pairCode1.value
michael@0 363 + this._elements.pairCode2.value
michael@0 364 + this._elements.pairCode3.value,
michael@0 365 false);
michael@0 366 },
michael@0 367 };

mercurial