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. |
| 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 |
| 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 and passes the list to each of the |
| 39 * callbacks. |
| 40 * @param {Array.<Object>} managedUsers An array of managed user objects. |
| 41 * Each object is of the form: |
| 42 * managedUser = { |
| 43 * id: "Managed User ID", |
| 44 * name: "Managed User Name", |
| 45 * iconURL: "chrome://path/to/icon/image", |
| 46 * onCurrentDevice: true or false, |
| 47 * needAvatar: true or false |
| 48 * } |
| 49 */ |
| 50 ManagedUserList.receiveExistingManagedUsers = function(managedUsers) { |
| 51 var instance = ManagedUserList.getInstance(); |
| 52 var i; |
| 53 for (i = 0; i < instance.callbacks_.length; i++) { |
| 54 instance.callbacks_[i](managedUsers); |
| 55 } |
| 56 this.managedUsers = managedUsers; |
| 57 instance.reset_(); |
| 58 }; |
| 59 |
| 60 /** |
| 61 * Called when there is a signin error when retrieving the list of managed |
| 62 * users. Calls the error callbacks which will display an appropriate error |
| 63 * message to the user. |
| 64 */ |
| 65 ManagedUserList.onSigninError = function() { |
| 66 var instance = ManagedUserList.getInstance(); |
| 67 var i; |
| 68 for (i = 0; i < instance.errbacks_.length; i++) { |
| 69 instance.errbacks_[i](); |
| 70 } |
| 71 // Reset the list of managed users in order to avoid showing stale data. |
| 72 instance.managedUsers = null; |
| 73 instance.reset_(); |
| 74 }; |
| 75 |
| 76 /** |
| 77 * Handles the request for the list of existing managed users. If the data is |
| 78 * already available, it will call |callback| immediately. Otherwise, it |
| 79 * retrieves the list of existing managed users which is then processed in |
| 80 * receiveExistingManagedUsers(). |
| 81 * @param {Object} callback The callback function which is called on success. |
| 82 * @param {Object} errback the callback function which is called on error. |
| 83 */ |
| 84 ManagedUserList.requestExistingManagedUsers = function(callback, errback) { |
| 85 var instance = ManagedUserList.getInstance(); |
| 86 instance.callbacks_.push(callback); |
| 87 instance.errbacks_.push(errback); |
| 88 if (!instance.requestInProgress_) { |
| 89 if (instance.managedUsers != null) { |
| 90 ManagedUserList.receiveExistingManagedUsers(instance.managedUsers); |
| 91 return; |
| 92 } |
| 93 instance.requestInProgress_ = true; |
| 94 chrome.send('requestManagedUserImportUpdate'); |
| 95 } |
| 96 }; |
| 97 |
| 98 /** |
10 * ManagedUserImportOverlay class. | 99 * ManagedUserImportOverlay class. |
11 * Encapsulated handling of the 'Import existing managed user' overlay page. | 100 * Encapsulated handling of the 'Import existing managed user' overlay page. |
12 * @constructor | 101 * @constructor |
13 * @class | 102 * @class |
14 */ | 103 */ |
15 function ManagedUserImportOverlay() { | 104 function ManagedUserImportOverlay() { |
16 var title = loadTimeData.getString('managedUserImportTitle'); | 105 var title = loadTimeData.getString('managedUserImportTitle'); |
17 OptionsPage.call(this, 'managedUserImport', | 106 OptionsPage.call(this, 'managedUserImport', |
18 title, 'managed-user-import'); | 107 title, 'managed-user-import'); |
19 }; | 108 }; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 $('create-new-user-link').onclick = function(event) { | 157 $('create-new-user-link').onclick = function(event) { |
69 OptionsPage.closeOverlay(); | 158 OptionsPage.closeOverlay(); |
70 OptionsPage.navigateToPage('createProfile'); | 159 OptionsPage.navigateToPage('createProfile'); |
71 }; | 160 }; |
72 }, | 161 }, |
73 | 162 |
74 /** | 163 /** |
75 * @override | 164 * @override |
76 */ | 165 */ |
77 didShowPage: function() { | 166 didShowPage: function() { |
78 chrome.send('requestManagedUserImportUpdate'); | 167 ManagedUserList.requestExistingManagedUsers( |
| 168 this.receiveExistingManagedUsers_, this.onSigninError_.bind(this)); |
79 | 169 |
80 this.updateImportInProgress_(false); | 170 this.updateImportInProgress_(false); |
81 $('managed-user-import-error-bubble').hidden = true; | 171 $('managed-user-import-error-bubble').hidden = true; |
82 $('managed-user-import-ok').disabled = true; | 172 $('managed-user-import-ok').disabled = true; |
83 $('select-avatar-grid').hidden = true; | 173 $('select-avatar-grid').hidden = true; |
84 $('managed-user-list').hidden = false; | 174 $('managed-user-list').hidden = false; |
85 | 175 |
86 $('managed-user-import-ok').textContent = | 176 $('managed-user-import-ok').textContent = |
87 loadTimeData.getString('managedUserImportOk'); | 177 loadTimeData.getString('managedUserImportOk'); |
88 $('managed-user-import-text').textContent = | 178 $('managed-user-import-text').textContent = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 */ | 240 */ |
151 updateImportInProgress_: function(inProgress) { | 241 updateImportInProgress_: function(inProgress) { |
152 $('managed-user-import-ok').disabled = inProgress; | 242 $('managed-user-import-ok').disabled = inProgress; |
153 $('managed-user-list').disabled = inProgress; | 243 $('managed-user-list').disabled = inProgress; |
154 $('select-avatar-grid').disabled = inProgress; | 244 $('select-avatar-grid').disabled = inProgress; |
155 $('create-new-user-link').disabled = inProgress; | 245 $('create-new-user-link').disabled = inProgress; |
156 $('managed-user-import-throbber').hidden = !inProgress; | 246 $('managed-user-import-throbber').hidden = !inProgress; |
157 }, | 247 }, |
158 | 248 |
159 /** | 249 /** |
160 * Adds all the existing |managedUsers| to the list. If |managedUsers| | 250 * 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. | 251 * @param {Array.<Object>} managedUsers An array of managed user objects. |
163 * Each object is of the form: | 252 * Each object is of the form: |
164 * managedUser = { | 253 * managedUser = { |
165 * id: "Managed User ID", | 254 * id: "Managed User ID", |
166 * name: "Managed User Name", | 255 * name: "Managed User Name", |
167 * iconURL: "chrome://path/to/icon/image", | 256 * iconURL: "chrome://path/to/icon/image", |
168 * onCurrentDevice: true or false, | 257 * onCurrentDevice: true or false, |
169 * needAvatar: true or false | 258 * needAvatar: true or false |
170 * } | 259 * } |
171 * @private | 260 * @private |
172 */ | 261 */ |
173 receiveExistingManagedUsers_: function(managedUsers) { | 262 receiveExistingManagedUsers_: function(managedUsers) { |
174 if (!managedUsers) { | |
175 $('managed-user-list').dataModel = null; | |
176 return; | |
177 } | |
178 | |
179 managedUsers.sort(function(a, b) { | 263 managedUsers.sort(function(a, b) { |
180 return a.name.localeCompare(b.name); | 264 return a.name.localeCompare(b.name); |
181 }); | 265 }); |
182 | 266 |
183 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers); | 267 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers); |
184 if (managedUsers.length == 0) { | 268 if (managedUsers.length == 0) { |
185 this.onError_(loadTimeData.getString('noExistingManagedUsers')); | 269 this.onError_(loadTimeData.getString('noExistingManagedUsers')); |
186 $('managed-user-import-ok').disabled = true; | 270 $('managed-user-import-ok').disabled = true; |
| 271 } else { |
| 272 // Hide the error bubble. |
| 273 $('managed-user-import-error-bubble').hidden = true; |
187 } | 274 } |
188 }, | 275 }, |
189 | 276 |
190 /** | 277 onSigninError_: function() { |
191 * @private | 278 $('managed-user-list').dataModel = null; |
192 */ | 279 this.onError_(loadTimeData.getString('managedUserImportSigninError')); |
193 hideErrorBubble_: function() { | |
194 $('managed-user-import-error-bubble').hidden = true; | |
195 }, | 280 }, |
196 | 281 |
197 /** | 282 /** |
198 * Displays an error message if an error occurs while | 283 * Displays an error message if an error occurs while |
199 * importing a managed user. | 284 * importing a managed user. |
200 * Called by BrowserOptions via the BrowserOptionsHandler. | 285 * Called by BrowserOptions via the BrowserOptionsHandler. |
201 * @param {string} error The error message to display. | 286 * @param {string} error The error message to display. |
202 * @private | 287 * @private |
203 */ | 288 */ |
204 onError_: function(error) { | 289 onError_: function(error) { |
205 var errorBubble = $('managed-user-import-error-bubble'); | 290 var errorBubble = $('managed-user-import-error-bubble'); |
206 errorBubble.hidden = false; | 291 errorBubble.hidden = false; |
207 errorBubble.textContent = error; | 292 errorBubble.textContent = error; |
208 this.updateImportInProgress_(false); | 293 this.updateImportInProgress_(false); |
209 }, | 294 }, |
210 | 295 |
211 /** | 296 /** |
212 * Closes the overlay if importing the managed user was successful. | 297 * Closes the overlay if importing the managed user was successful. |
213 * @private | 298 * @private |
214 */ | 299 */ |
215 onSuccess_: function() { | 300 onSuccess_: function() { |
216 this.updateImportInProgress_(false); | 301 this.updateImportInProgress_(false); |
217 OptionsPage.closeOverlay(); | 302 OptionsPage.closeOverlay(); |
218 }, | 303 }, |
219 }; | 304 }; |
220 | 305 |
221 // Forward public APIs to private implementations. | 306 // Forward public APIs to private implementations. |
222 [ | 307 [ |
223 'hideErrorBubble', | |
224 'onError', | |
225 'onSuccess', | 308 'onSuccess', |
226 'receiveExistingManagedUsers', | |
227 ].forEach(function(name) { | 309 ].forEach(function(name) { |
228 ManagedUserImportOverlay[name] = function() { | 310 ManagedUserImportOverlay[name] = function() { |
229 var instance = ManagedUserImportOverlay.getInstance(); | 311 var instance = ManagedUserImportOverlay.getInstance(); |
230 return instance[name + '_'].apply(instance, arguments); | 312 return instance[name + '_'].apply(instance, arguments); |
231 }; | 313 }; |
232 }); | 314 }); |
233 | 315 |
234 // Export | 316 // Export |
235 return { | 317 return { |
236 ManagedUserImportOverlay: ManagedUserImportOverlay, | 318 ManagedUserImportOverlay: ManagedUserImportOverlay, |
| 319 ManagedUserList: ManagedUserList, |
237 }; | 320 }; |
238 }); | 321 }); |
OLD | NEW |