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