Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / proximity_auth / webui / resources / local-state.js
blob865d203a5e8c9c7ff1eaa0650416bcb611c1e334
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.
5 Polymer({
6   is: 'local-state',
7   properties: {
8     /**
9      * The current CryptAuth enrollment status.
10      * @type {{
11      *   lastSuccessTime: ?number,
12      *   nextRefreshTime: ?number,
13      *   recoveringFromFailure: boolean,
14      *   operationInProgress: boolean,
15      * }} SyncState
16      */
17     enrollmentState_: {
18       type: Object,
19       value: {
20         lastSuccessTime: null,
21         nextRefreshTime: null,
22         recoveringFromFailure: true,
23         operationInProgress: false,
24       },
25     },
27     /**
28      * The current CryptAuth device sync status.
29      * @type {SyncState}
30      */
31     deviceSyncState_: {
32       type: Object,
33       value: {
34         lastSuccessTime: null,
35         nextRefreshTime: null,
36         recoveringFromFailure: true,
37         operationInProgress: false,
38       },
39     },
41     /**
42      * List of unlock keys that can unlock the local device.
43      * @type {Array<DeviceInfo>}
44      */
45     unlockKeys_: {
46       type: Array,
47       value: [
48         {
49          publicKey: 'CAESRQogOlH8DgPMQu7eAt-b6yoTXcazG8mAl6SPC5Ds-LTULIcSIQDZ' +
50                     'DMqsoYRO4tNMej1FBEl1sTiTiVDqrcGq-CkYCzDThw==',
51          friendlyDeviceName: 'LGE Nexus 4',
52          bluetoothAddress: 'C4:43:8F:12:07:07',
53          unlockKey: true,
54          unlockable: false,
55          connectionStatus: 'connected',
56          remoteState: {
57            userPresent: true,
58            secureScreenLock: true,
59            trustAgent: true
60          },
61         },
62       ],
63     },
64   },
66   /**
67    * Called when the page is about to be shown.
68    */
69   activate: function() {
70     LocalStateInterface = this;
71     chrome.send('getLocalState');
72   },
74   /**
75    * Immediately forces an enrollment attempt.
76    */
77   forceEnrollment_: function() {
78     chrome.send('forceEnrollment');
79   },
81   /**
82    * Immediately forces an device sync attempt.
83    */
84   forceDeviceSync_: function() {
85     chrome.send('forceDeviceSync');
86   },
88   /**
89    * Called when the enrollment state changes.
90    * @param {SyncState} enrollmentState
91    */
92   onEnrollmentStateChanged: function(enrollmentState) {
93     this.enrollmentState_ = enrollmentState;
94   },
96   /**
97    * Called when the device sync state changes.
98    * @param {SyncState} deviceSyncState
99    */
100   onDeviceSyncStateChanged: function(deviceSyncState) {
101     this.deviceSyncState_ = deviceSyncState;
102   },
104   /**
105    * Called when the locally stored unlock keys change.
106    * @param {Array<DeviceInfo>} unlockKeys
107    */
108   onUnlockKeysChanged: function(unlockKeys) {
109     this.unlockKeys_ = unlockKeys;
110   },
112   /**
113    * Called for the chrome.send('getSyncStates') response.
114    * @param {SyncState} enrollmentState
115    * @param {SyncState} deviceSyncState
116    * @param {Array<DeviceInfo>} unlockKeys
117    */
118   onGotLocalState: function(enrollmentState, deviceSyncState, unlockKeys) {
119     this.enrollmentState_ = enrollmentState;
120     this.deviceSyncState_ = deviceSyncState;
121     this.unlockKeys_ = unlockKeys;
122   },
124   /**
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.
129    */
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();
135   },
137   /**
138    * @param {SyncState} syncState The enrollment or device sync state.
139    * @return {string} The formatted string to be displayed.
140    */
141   getNextEnrollmentString_: function(syncState) {
142     var deltaMillis = syncState.nextRefreshTime;
143     if (deltaMillis == null)
144       return 'unknown';
145     if (deltaMillis == 0)
146       return 'sync in progress...';
148     var seconds = deltaMillis / 1000;
149     if (seconds < 60)
150       return Math.round(seconds) + ' seconds to refresh';
152     var minutes = seconds / 60;
153     if (minutes < 60)
154       return Math.round(minutes) + ' minutes to refresh';
156     var hours = minutes / 60;
157     if (hours < 24)
158       return Math.round(hours) + ' hours to refresh';
160     var days = hours / 24;
161     return Math.round(days) + ' days to refresh';
162   },
164   /**
165    * @param {SyncState} syncState The enrollment or device sync state.
166    * @return {string} The icon to show for the current state.
167    */
168   getNextSyncIcon_: function(syncState) {
169     return syncState.operationInProgress ? 'icons:refresh' : 'icons:schedule';
170  },
172   /**
173    * @param {SyncState} syncState The enrollment or device sync state.
174    * @return {string} The icon id representing whether the last sync is
175    *     successful.
176    */
177   getIconForSuccess_: function(syncState) {
178     return syncState.recoveringFromFailure ?
179         'icons:error' : 'icons:cloud-done';
180   },
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 = {
189   /**
190    * Called when the enrollment state changes. For example, when a new
191    * enrollment is initiated.
192    * @type {function(SyncState)}
193    */
194   onEnrollmentStateChanged: function(enrollmentState) {},
196   /**
197    * Called when the device state changes. For example, when a new device sync
198    * is initiated.
199    * @type {function(DeviceSyncState)}
200    */
201   onDeviceSyncStateChanged: function(deviceSyncState) {},
203   /**
204    * Called when the locally stored unlock keys changes.
205    * @type {function(Array<DeviceInfo>)}
206    */
207   onUnlockKeysChanged: function(unlockKeys) {},
209   /**
210    * Called in response to chrome.send('getLocalState') with the local state.
211    * @type {function(SyncState, SyncState, Array<DeviceInfo>)}
212    */
213   onGotLocalState: function(enrollmentState, deviceSyncState, unlockKeys) {},