1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 * The current CryptAuth enrollment status.
11 * lastSuccessTime: ?number,
12 * nextRefreshTime: ?number,
13 * recoveringFromFailure: boolean,
14 * operationInProgress: boolean,
20 lastSuccessTime: null,
21 nextRefreshTime: null,
22 recoveringFromFailure: true,
23 operationInProgress: false,
28 * The current CryptAuth device sync status.
34 lastSuccessTime: null,
35 nextRefreshTime: null,
36 recoveringFromFailure: true,
37 operationInProgress: false,
42 * List of unlock keys that can unlock the local device.
43 * @type {Array<DeviceInfo>}
49 publicKey: 'CAESRQogOlH8DgPMQu7eAt-b6yoTXcazG8mAl6SPC5Ds-LTULIcSIQDZ' +
50 'DMqsoYRO4tNMej1FBEl1sTiTiVDqrcGq-CkYCzDThw==',
51 friendlyDeviceName: 'LGE Nexus 4',
52 bluetoothAddress: 'C4:43:8F:12:07:07',
55 connectionStatus: 'connected',
58 secureScreenLock: true,
67 * Called when the page is about to be shown.
69 activate: function() {
70 LocalStateInterface = this;
71 chrome.send('getLocalState');
75 * Immediately forces an enrollment attempt.
77 forceEnrollment_: function() {
78 chrome.send('forceEnrollment');
82 * Immediately forces an device sync attempt.
84 forceDeviceSync_: function() {
85 chrome.send('forceDeviceSync');
89 * Called when the enrollment state changes.
90 * @param {SyncState} enrollmentState
92 onEnrollmentStateChanged: function(enrollmentState) {
93 this.enrollmentState_ = enrollmentState;
97 * Called when the device sync state changes.
98 * @param {SyncState} deviceSyncState
100 onDeviceSyncStateChanged: function(deviceSyncState) {
101 this.deviceSyncState_ = deviceSyncState;
105 * Called when the locally stored unlock keys change.
106 * @param {Array<DeviceInfo>} unlockKeys
108 onUnlockKeysChanged: function(unlockKeys) {
109 this.unlockKeys_ = unlockKeys;
113 * Called for the chrome.send('getSyncStates') response.
114 * @param {SyncState} enrollmentState
115 * @param {SyncState} deviceSyncState
116 * @param {Array<DeviceInfo>} unlockKeys
118 onGotLocalState: function(enrollmentState, deviceSyncState, unlockKeys) {
119 this.enrollmentState_ = enrollmentState;
120 this.deviceSyncState_ = deviceSyncState;
121 this.unlockKeys_ = unlockKeys;
125 * @param {SyncState} syncState The enrollment or device sync state.
126 * @param {string} neverSyncedString String returned if there has never been a
127 * last successful sync.
128 * @return {string} The formatted string of the last successful sync time.
130 getLastSyncTimeString_: function(syncState, neverSyncedString) {
131 if (syncState.lastSuccessTime == 0)
132 return neverSyncedString;
133 var date = new Date(syncState.lastSuccessTime);
134 return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
138 * @param {SyncState} syncState The enrollment or device sync state.
139 * @return {string} The formatted string to be displayed.
141 getNextEnrollmentString_: function(syncState) {
142 var deltaMillis = syncState.nextRefreshTime;
143 if (deltaMillis == null)
145 if (deltaMillis == 0)
146 return 'sync in progress...';
148 var seconds = deltaMillis / 1000;
150 return Math.round(seconds) + ' seconds to refresh';
152 var minutes = seconds / 60;
154 return Math.round(minutes) + ' minutes to refresh';
156 var hours = minutes / 60;
158 return Math.round(hours) + ' hours to refresh';
160 var days = hours / 24;
161 return Math.round(days) + ' days to refresh';
165 * @param {SyncState} syncState The enrollment or device sync state.
166 * @return {string} The icon to show for the current state.
168 getNextSyncIcon_: function(syncState) {
169 return syncState.operationInProgress ? 'icons:refresh' : 'icons:schedule';
173 * @param {SyncState} syncState The enrollment or device sync state.
174 * @return {string} The icon id representing whether the last sync is
177 getIconForSuccess_: function(syncState) {
178 return syncState.recoveringFromFailure ?
179 'icons:error' : 'icons:cloud-done';
183 // Interface with the native WebUI component for getting the local state and
184 // being notified when the local state changes.
185 // The local state refers to state stored on the device rather than online in
186 // CryptAuth. This state includes the enrollment and device sync states, as well
187 // as the list of unlock keys.
188 LocalStateInterface = {
190 * Called when the enrollment state changes. For example, when a new
191 * enrollment is initiated.
192 * @type {function(SyncState)}
194 onEnrollmentStateChanged: function(enrollmentState) {},
197 * Called when the device state changes. For example, when a new device sync
199 * @type {function(DeviceSyncState)}
201 onDeviceSyncStateChanged: function(deviceSyncState) {},
204 * Called when the locally stored unlock keys changes.
205 * @type {function(Array<DeviceInfo>)}
207 onUnlockKeysChanged: function(unlockKeys) {},
210 * Called in response to chrome.send('getLocalState') with the local state.
211 * @type {function(SyncState, SyncState, Array<DeviceInfo>)}
213 onGotLocalState: function(enrollmentState, deviceSyncState, unlockKeys) {},