OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options', function() { | 5 cr.define('options', function() { |
6 var OptionsPage = options.OptionsPage; | 6 var OptionsPage = options.OptionsPage; |
7 var ArrayDataModel = cr.ui.ArrayDataModel; | 7 var ArrayDataModel = cr.ui.ArrayDataModel; |
8 | 8 |
9 /** | 9 /** |
10 * ManagedUserList class. | |
Pam (message me for reviews)
2014/01/08 11:21:35
I'd suggest moving this into its own file.
Adrian Kuegel
2014/01/08 12:51:47
Yes, I thought about that, but I guess I should th
Pam (message me for reviews)
2014/01/08 14:36:45
Oh boy, what a batch of confusing class names. Oh
Adrian Kuegel
2014/01/08 16:49:08
I decided to use ManagedUserListData now.
| |
11 * Handles requests for retrieving a list of existing managed users which are | |
12 * supervised by the current profile. This list is cached in order to make it | |
13 * possible to serve future requests immediately. The first request will be | |
Pam (message me for reviews)
2014/01/08 11:21:35
Are we concerned about the cache becoming stale, e
Adrian Kuegel
2014/01/08 12:51:47
Yes, I think we should handle that. I think we cou
Pam (message me for reviews)
2014/01/08 14:36:45
Yes, since the failure case (managing to create a
Adrian Kuegel
2014/01/08 16:49:08
Ok, makes sense. Otherwise I think the import dial
| |
14 * handled asynchronously. | |
15 * @constructor | |
16 * @class | |
17 */ | |
18 function ManagedUserList() { | |
19 this.callbacks_ = []; | |
20 this.errbacks_ = []; | |
21 this.requestInProgress_ = false; | |
22 this.managedUsers = null; | |
23 }; | |
24 | |
25 cr.addSingletonGetter(ManagedUserList); | |
26 | |
27 /** | |
28 * Resets to the initial state of no pending requests. | |
29 * @private | |
30 */ | |
31 ManagedUserList.prototype.reset_ = function() { | |
32 this.callbacks_ = []; | |
33 this.errbacks_ = []; | |
34 this.requestInProgress_ = false; | |
35 } | |
36 | |
37 /** | |
38 * Receives a list of managed users. | |
Pam (message me for reviews)
2014/01/08 11:21:35
and passes the list to each of the callbacks.
Adrian Kuegel
2014/01/08 12:51:47
Done.
| |
39 * @param {Array.<Object>} managedUsers An array of managed user objects. | |
40 * Each object is of the form: | |
41 * managedUser = { | |
42 * id: "Managed User ID", | |
43 * name: "Managed User Name", | |
44 * iconURL: "chrome://path/to/icon/image", | |
45 * onCurrentDevice: true or false, | |
46 * needAvatar: true or false | |
47 * } | |
48 */ | |
49 ManagedUserList.receiveExistingManagedUsers = function(managedUsers) { | |
50 var instance = ManagedUserList.getInstance(); | |
51 var i; | |
52 for (i = 0; i < instance.callbacks_.length; i++) { | |
53 instance.callbacks_[i](managedUsers); | |
54 } | |
55 this.managedUsers = managedUsers; | |
56 instance.reset_(); | |
57 }; | |
58 | |
59 /** | |
60 * Called when there is a signin error when retrieving the list of managed | |
61 * users. Calls the error callbacks which will display an appropriate error | |
62 * message to the user. | |
63 */ | |
64 ManagedUserList.onSigninError = function() { | |
65 var instance = ManagedUserList.getInstance(); | |
66 var i; | |
67 for (i = 0; i < instance.errbacks_.length; i++) { | |
68 instance.errbacks_[i](); | |
69 } | |
70 // Reset the list of managed users in order to avoid showing stale data. | |
71 instance.managedUsers = null; | |
72 instance.reset_(); | |
73 }; | |
74 | |
75 /** | |
76 * Handles the request for the list of existing managed users. If the data is | |
77 * already available, it will call |callback| immediately. Otherwise, it | |
78 * retrieves the list of existing managed users which is then processed in | |
79 * receiveExistingManagedUsers(). | |
80 * @param {Object} callback The callback function which is called on success. | |
81 * @param {Object} errback the callback function which is called on error. | |
82 */ | |
83 ManagedUserList.requestExistingManagedUsers = function(callback, errback) { | |
84 var instance = ManagedUserList.getInstance(); | |
85 instance.callbacks_.push(callback); | |
86 instance.errbacks_.push(errback); | |
87 if (!instance.requestInProgress_) { | |
88 if (instance.managedUsers != null) { | |
89 ManagedUserList.receiveExistingManagedUsers(instance.managedUsers); | |
90 return; | |
91 } | |
92 instance.requestInProgress_ = true; | |
93 chrome.send('requestManagedUserImportUpdate'); | |
94 } | |
95 }; | |
96 | |
97 /** | |
10 * ManagedUserImportOverlay class. | 98 * ManagedUserImportOverlay class. |
11 * Encapsulated handling of the 'Import existing managed user' overlay page. | 99 * Encapsulated handling of the 'Import existing managed user' overlay page. |
12 * @constructor | 100 * @constructor |
13 * @class | 101 * @class |
14 */ | 102 */ |
15 function ManagedUserImportOverlay() { | 103 function ManagedUserImportOverlay() { |
16 var title = loadTimeData.getString('managedUserImportTitle'); | 104 var title = loadTimeData.getString('managedUserImportTitle'); |
17 OptionsPage.call(this, 'managedUserImport', | 105 OptionsPage.call(this, 'managedUserImport', |
18 title, 'managed-user-import'); | 106 title, 'managed-user-import'); |
19 }; | 107 }; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 $('create-new-user-link').onclick = function(event) { | 156 $('create-new-user-link').onclick = function(event) { |
69 OptionsPage.closeOverlay(); | 157 OptionsPage.closeOverlay(); |
70 OptionsPage.navigateToPage('createProfile'); | 158 OptionsPage.navigateToPage('createProfile'); |
71 }; | 159 }; |
72 }, | 160 }, |
73 | 161 |
74 /** | 162 /** |
75 * @override | 163 * @override |
76 */ | 164 */ |
77 didShowPage: function() { | 165 didShowPage: function() { |
78 chrome.send('requestManagedUserImportUpdate'); | 166 ManagedUserList.requestExistingManagedUsers( |
167 this.receiveExistingManagedUsers_, this.onSigninError_.bind(this)); | |
79 | 168 |
80 this.updateImportInProgress_(false); | 169 this.updateImportInProgress_(false); |
81 $('managed-user-import-error-bubble').hidden = true; | 170 $('managed-user-import-error-bubble').hidden = true; |
82 $('managed-user-import-ok').disabled = true; | 171 $('managed-user-import-ok').disabled = true; |
83 $('select-avatar-grid').hidden = true; | 172 $('select-avatar-grid').hidden = true; |
84 $('managed-user-list').hidden = false; | 173 $('managed-user-list').hidden = false; |
85 | 174 |
86 $('managed-user-import-ok').textContent = | 175 $('managed-user-import-ok').textContent = |
87 loadTimeData.getString('managedUserImportOk'); | 176 loadTimeData.getString('managedUserImportOk'); |
88 $('managed-user-import-text').textContent = | 177 $('managed-user-import-text').textContent = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 */ | 239 */ |
151 updateImportInProgress_: function(inProgress) { | 240 updateImportInProgress_: function(inProgress) { |
152 $('managed-user-import-ok').disabled = inProgress; | 241 $('managed-user-import-ok').disabled = inProgress; |
153 $('managed-user-list').disabled = inProgress; | 242 $('managed-user-list').disabled = inProgress; |
154 $('select-avatar-grid').disabled = inProgress; | 243 $('select-avatar-grid').disabled = inProgress; |
155 $('create-new-user-link').disabled = inProgress; | 244 $('create-new-user-link').disabled = inProgress; |
156 $('managed-user-import-throbber').hidden = !inProgress; | 245 $('managed-user-import-throbber').hidden = !inProgress; |
157 }, | 246 }, |
158 | 247 |
159 /** | 248 /** |
160 * Adds all the existing |managedUsers| to the list. If |managedUsers| | 249 * Sets the data model of the managed user list to |managedUsers|. |
161 * is undefined, then the list is cleared. | |
162 * @param {Array.<Object>} managedUsers An array of managed user objects. | 250 * @param {Array.<Object>} managedUsers An array of managed user objects. |
163 * Each object is of the form: | 251 * Each object is of the form: |
164 * managedUser = { | 252 * managedUser = { |
165 * id: "Managed User ID", | 253 * id: "Managed User ID", |
166 * name: "Managed User Name", | 254 * name: "Managed User Name", |
167 * iconURL: "chrome://path/to/icon/image", | 255 * iconURL: "chrome://path/to/icon/image", |
168 * onCurrentDevice: true or false, | 256 * onCurrentDevice: true or false, |
169 * needAvatar: true or false | 257 * needAvatar: true or false |
170 * } | 258 * } |
171 * @private | 259 * @private |
172 */ | 260 */ |
173 receiveExistingManagedUsers_: function(managedUsers) { | 261 receiveExistingManagedUsers_: function(managedUsers) { |
174 if (!managedUsers) { | |
175 $('managed-user-list').dataModel = null; | |
176 return; | |
177 } | |
178 | |
179 managedUsers.sort(function(a, b) { | 262 managedUsers.sort(function(a, b) { |
180 return a.name.localeCompare(b.name); | 263 return a.name.localeCompare(b.name); |
181 }); | 264 }); |
182 | 265 |
183 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers); | 266 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers); |
184 if (managedUsers.length == 0) { | 267 if (managedUsers.length == 0) { |
185 this.onError_(loadTimeData.getString('noExistingManagedUsers')); | 268 this.onError_(loadTimeData.getString('noExistingManagedUsers')); |
186 $('managed-user-import-ok').disabled = true; | 269 $('managed-user-import-ok').disabled = true; |
270 } else { | |
271 // Hide the error bubble. | |
272 $('managed-user-import-error-bubble').hidden = true; | |
187 } | 273 } |
188 }, | 274 }, |
189 | 275 |
190 /** | 276 onSigninError_: function() { |
191 * @private | 277 $('managed-user-list').dataModel = null; |
192 */ | 278 this.onError_(loadTimeData.getString('managedUserImportSigninError')); |
193 hideErrorBubble_: function() { | |
194 $('managed-user-import-error-bubble').hidden = true; | |
195 }, | 279 }, |
196 | 280 |
197 /** | 281 /** |
198 * Displays an error message if an error occurs while | 282 * Displays an error message if an error occurs while |
199 * importing a managed user. | 283 * importing a managed user. |
200 * Called by BrowserOptions via the BrowserOptionsHandler. | 284 * Called by BrowserOptions via the BrowserOptionsHandler. |
201 * @param {string} error The error message to display. | 285 * @param {string} error The error message to display. |
202 * @private | 286 * @private |
203 */ | 287 */ |
204 onError_: function(error) { | 288 onError_: function(error) { |
205 var errorBubble = $('managed-user-import-error-bubble'); | 289 var errorBubble = $('managed-user-import-error-bubble'); |
206 errorBubble.hidden = false; | 290 errorBubble.hidden = false; |
207 errorBubble.textContent = error; | 291 errorBubble.textContent = error; |
208 this.updateImportInProgress_(false); | 292 this.updateImportInProgress_(false); |
209 }, | 293 }, |
210 | 294 |
211 /** | 295 /** |
212 * Closes the overlay if importing the managed user was successful. | 296 * Closes the overlay if importing the managed user was successful. |
213 * @private | 297 * @private |
214 */ | 298 */ |
215 onSuccess_: function() { | 299 onSuccess_: function() { |
216 this.updateImportInProgress_(false); | 300 this.updateImportInProgress_(false); |
217 OptionsPage.closeOverlay(); | 301 OptionsPage.closeOverlay(); |
218 }, | 302 }, |
219 }; | 303 }; |
220 | 304 |
221 // Forward public APIs to private implementations. | 305 // Forward public APIs to private implementations. |
222 [ | 306 [ |
223 'hideErrorBubble', | |
224 'onError', | |
225 'onSuccess', | 307 'onSuccess', |
226 'receiveExistingManagedUsers', | |
227 ].forEach(function(name) { | 308 ].forEach(function(name) { |
228 ManagedUserImportOverlay[name] = function() { | 309 ManagedUserImportOverlay[name] = function() { |
229 var instance = ManagedUserImportOverlay.getInstance(); | 310 var instance = ManagedUserImportOverlay.getInstance(); |
230 return instance[name + '_'].apply(instance, arguments); | 311 return instance[name + '_'].apply(instance, arguments); |
231 }; | 312 }; |
232 }); | 313 }); |
233 | 314 |
234 // Export | 315 // Export |
235 return { | 316 return { |
236 ManagedUserImportOverlay: ManagedUserImportOverlay, | 317 ManagedUserImportOverlay: ManagedUserImportOverlay, |
318 ManagedUserList: ManagedUserList, | |
237 }; | 319 }; |
238 }); | 320 }); |
OLD | NEW |