OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * 'settings-quick-unlock-choose-method' allows the user to change how they | |
8 * unlock their device. Note that setting up the unlock method is delegated | |
9 * to other elements. | |
10 * | |
11 * Example: | |
12 * | |
13 * <settings-quick-unlock-choose-method | |
14 * set-modes="[[quickUnlockSetModes]]" | |
15 * current-route="{{currentRoute}}" | |
16 * prefs="{{prefs}}"> | |
17 * </settings-quick-unlock-choose-method> | |
18 */ | |
19 | |
20 (function() { | |
21 'use strict'; | |
22 | |
23 /** @const */ var ENABLE_LOCK_SCREEN_PREF = 'settings.enable_screen_lock'; | |
24 | |
25 /** @enum {string} */ | |
26 var QuickUnlockUnlockType = { | |
27 VALUE_PENDING: 'value_pending', | |
28 NONE: 'none', | |
29 PASSWORD: 'password', | |
30 PIN_PASSWORD: 'pin+password' | |
31 }; | |
32 | |
33 Polymer({ | |
34 is: 'settings-quick-unlock-choose-method', | |
35 | |
36 behaviors: [PrefsBehavior, QuickUnlockPasswordDetectBehavior], | |
37 | |
38 properties: { | |
39 /** @type {!settings.Route} */ | |
40 currentRoute: { | |
41 type: Object, | |
42 observer: 'onRouteChanged_', | |
43 }, | |
44 | |
45 /** Preferences state. */ | |
46 prefs: { | |
47 type: Object, | |
48 notify: true, | |
49 }, | |
50 | |
51 /** | |
52 * The currently selected unlock type. | |
53 * @type {!QuickUnlockUnlockType} | |
54 * @private | |
55 */ | |
56 selectedUnlockType_: { | |
57 type: String, | |
58 notify: true, | |
59 value: QuickUnlockUnlockType.VALUE_PENDING, | |
60 observer: 'selectedUnlockTypeChanged_' | |
61 } | |
62 }, | |
63 | |
64 observers: ['onSetModesChanged_(setModes)'], | |
65 | |
66 /** @override */ | |
67 attached: function() { | |
68 CrSettingsPrefs.initialized.then(this.updateUnlockType_.bind(this)); | |
69 | |
70 this.boundOnPrefsChanged_ = function(prefs) { | |
71 for (var i = 0; i < prefs.length; ++i) { | |
72 if (prefs[i].key == ENABLE_LOCK_SCREEN_PREF) | |
73 this.updateUnlockType_(); | |
74 } | |
75 }.bind(this); | |
76 this.boundOnActiveModesChanged_ = this.updateUnlockType_.bind(this); | |
77 | |
78 chrome.settingsPrivate.onPrefsChanged.addListener( | |
79 this.boundOnPrefsChanged_); | |
80 chrome.quickUnlockPrivate.onActiveModesChanged.addListener( | |
81 this.boundOnActiveModesChanged_); | |
82 | |
83 if (this.currentRoute == settings.Route.QUICK_UNLOCK_CHOOSE_METHOD) | |
84 this.askForPasswordIfUnset(); | |
85 }, | |
86 | |
87 /** @override */ | |
88 detached: function() { | |
89 chrome.settingsPrivate.onPrefsChanged.removeListener( | |
90 this.boundOnPrefsChanged_); | |
91 chrome.quickUnlockPrivate.onActiveModesChanged.removeListener( | |
92 this.boundOnActiveModesChanged_); | |
93 }, | |
94 | |
95 /** @private */ | |
96 onRouteChanged_: function() { | |
97 if (this.currentRoute == settings.Route.QUICK_UNLOCK_CHOOSE_METHOD) | |
98 this.askForPasswordIfUnset(); | |
99 }, | |
100 | |
101 /** @private */ | |
102 onSetModesChanged_: function() { | |
103 if (this.currentRoute == settings.Route.QUICK_UNLOCK_CHOOSE_METHOD) | |
104 this.askForPasswordIfUnset(); | |
105 }, | |
106 | |
107 /** | |
108 * Updates the selected unlock type radio group. This function will get called | |
109 * after preferences are initialized, after the quick unlock mode has been | |
110 * changed, and after the lockscreen preference has changed. | |
111 * | |
112 * @private | |
113 */ | |
114 updateUnlockType_: function() { | |
115 // The quickUnlockPrivate.onActiveModesChanged event could trigger this | |
116 // function before CrSettingsPrefs is initialized if another settings page | |
117 // changes the quick unlock state. | |
118 if (!CrSettingsPrefs.isInitialized) | |
119 return; | |
120 | |
121 if (!this.getPref(ENABLE_LOCK_SCREEN_PREF).value) { | |
122 this.selectedUnlockType_ = QuickUnlockUnlockType.NONE; | |
123 return; | |
124 } | |
125 | |
126 chrome.quickUnlockPrivate.getActiveModes(function(modes) { | |
127 if (modes.includes(chrome.quickUnlockPrivate.QuickUnlockMode.PIN)) { | |
128 this.selectedUnlockType_ = QuickUnlockUnlockType.PIN_PASSWORD; | |
129 } else if (this.selectedUnlockType_ != | |
130 QuickUnlockUnlockType.PIN_PASSWORD) { | |
131 // We don't want to clobber an existing PIN+PASSWORD state because the | |
132 // user needs to configure that option before actually using it. | |
133 // | |
134 // Specifically, this check is needed because this function gets called | |
135 // by the pref system after changing the unlock type from NONE to | |
136 // PIN+PASSWORD. Without the conditional assignment, this function would | |
137 // change the PIN+PASSWORD lock type to PASSWORD because the PIN hasn't | |
138 // been configured yet. | |
139 this.selectedUnlockType_ = QuickUnlockUnlockType.PASSWORD; | |
140 } | |
141 }.bind(this)); | |
142 }, | |
143 | |
144 /** | |
145 * Called when the unlock type has changed. | |
146 * @param {!string} selected The current unlock type. | |
147 * @param {?string} previous The old unlock type. undefined if not present. | |
148 * @private | |
149 */ | |
150 selectedUnlockTypeChanged_: function(selected, previous) { | |
151 // This method gets invoked when setting the initial value from the existing | |
152 // state. In that case, we don't need to bother updating the prefs. | |
153 if (!previous) | |
154 return; | |
155 | |
156 this.setPrefValue(ENABLE_LOCK_SCREEN_PREF, | |
157 selected != QuickUnlockUnlockType.NONE); | |
158 if (selected != QuickUnlockUnlockType.PIN_PASSWORD && this.setModes) { | |
159 this.setModes.call(null, [], [], function(didSet) { | |
160 assert(didSet, 'Failed to clear quick unlock modes'); | |
161 }); | |
162 } | |
163 }, | |
164 | |
165 /** | |
166 * Retruns true if the setup pin section should be shown. | |
167 * @param {!string} selectedUnlockType The current unlock type. Used to let | |
168 * polymer know about the dependency. | |
169 * @private | |
170 */ | |
171 showSetupPin_: function(selectedUnlockType) { | |
172 return selectedUnlockType === QuickUnlockUnlockType.PIN_PASSWORD; | |
173 }, | |
174 | |
175 /** @private */ | |
176 onConfigurePin_: function() { | |
177 settings.navigateTo(settings.Route.QUICK_UNLOCK_SETUP_PIN); | |
178 }, | |
179 }); | |
180 | |
181 })(); | |
OLD | NEW |