Chromium Code Reviews| Index: chrome/browser/resources/options/managed_user_import.js |
| diff --git a/chrome/browser/resources/options/managed_user_import.js b/chrome/browser/resources/options/managed_user_import.js |
| index 70a54dd2cabfc54e54649f882a613e911981fca3..d36ff53143bd37a2f95dc479abe519342527e26e 100644 |
| --- a/chrome/browser/resources/options/managed_user_import.js |
| +++ b/chrome/browser/resources/options/managed_user_import.js |
| @@ -7,6 +7,94 @@ cr.define('options', function() { |
| var ArrayDataModel = cr.ui.ArrayDataModel; |
| /** |
| + * 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.
|
| + * Handles requests for retrieving a list of existing managed users which are |
| + * supervised by the current profile. This list is cached in order to make it |
| + * 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
|
| + * handled asynchronously. |
| + * @constructor |
| + * @class |
| + */ |
| + function ManagedUserList() { |
| + this.callbacks_ = []; |
| + this.errbacks_ = []; |
| + this.requestInProgress_ = false; |
| + this.managedUsers = null; |
| + }; |
| + |
| + cr.addSingletonGetter(ManagedUserList); |
| + |
| + /** |
| + * Resets to the initial state of no pending requests. |
| + * @private |
| + */ |
| + ManagedUserList.prototype.reset_ = function() { |
| + this.callbacks_ = []; |
| + this.errbacks_ = []; |
| + this.requestInProgress_ = false; |
| + } |
| + |
| + /** |
| + * 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.
|
| + * @param {Array.<Object>} managedUsers An array of managed user objects. |
| + * Each object is of the form: |
| + * managedUser = { |
| + * id: "Managed User ID", |
| + * name: "Managed User Name", |
| + * iconURL: "chrome://path/to/icon/image", |
| + * onCurrentDevice: true or false, |
| + * needAvatar: true or false |
| + * } |
| + */ |
| + ManagedUserList.receiveExistingManagedUsers = function(managedUsers) { |
| + var instance = ManagedUserList.getInstance(); |
| + var i; |
| + for (i = 0; i < instance.callbacks_.length; i++) { |
| + instance.callbacks_[i](managedUsers); |
| + } |
| + this.managedUsers = managedUsers; |
| + instance.reset_(); |
| + }; |
| + |
| + /** |
| + * Called when there is a signin error when retrieving the list of managed |
| + * users. Calls the error callbacks which will display an appropriate error |
| + * message to the user. |
| + */ |
| + ManagedUserList.onSigninError = function() { |
| + var instance = ManagedUserList.getInstance(); |
| + var i; |
| + for (i = 0; i < instance.errbacks_.length; i++) { |
| + instance.errbacks_[i](); |
| + } |
| + // Reset the list of managed users in order to avoid showing stale data. |
| + instance.managedUsers = null; |
| + instance.reset_(); |
| + }; |
| + |
| + /** |
| + * Handles the request for the list of existing managed users. If the data is |
| + * already available, it will call |callback| immediately. Otherwise, it |
| + * retrieves the list of existing managed users which is then processed in |
| + * receiveExistingManagedUsers(). |
| + * @param {Object} callback The callback function which is called on success. |
| + * @param {Object} errback the callback function which is called on error. |
| + */ |
| + ManagedUserList.requestExistingManagedUsers = function(callback, errback) { |
| + var instance = ManagedUserList.getInstance(); |
| + instance.callbacks_.push(callback); |
| + instance.errbacks_.push(errback); |
| + if (!instance.requestInProgress_) { |
| + if (instance.managedUsers != null) { |
| + ManagedUserList.receiveExistingManagedUsers(instance.managedUsers); |
| + return; |
| + } |
| + instance.requestInProgress_ = true; |
| + chrome.send('requestManagedUserImportUpdate'); |
| + } |
| + }; |
| + |
| + /** |
| * ManagedUserImportOverlay class. |
| * Encapsulated handling of the 'Import existing managed user' overlay page. |
| * @constructor |
| @@ -75,7 +163,8 @@ cr.define('options', function() { |
| * @override |
| */ |
| didShowPage: function() { |
| - chrome.send('requestManagedUserImportUpdate'); |
| + ManagedUserList.requestExistingManagedUsers( |
| + this.receiveExistingManagedUsers_, this.onSigninError_.bind(this)); |
| this.updateImportInProgress_(false); |
| $('managed-user-import-error-bubble').hidden = true; |
| @@ -157,8 +246,7 @@ cr.define('options', function() { |
| }, |
| /** |
| - * Adds all the existing |managedUsers| to the list. If |managedUsers| |
| - * is undefined, then the list is cleared. |
| + * Sets the data model of the managed user list to |managedUsers|. |
| * @param {Array.<Object>} managedUsers An array of managed user objects. |
| * Each object is of the form: |
| * managedUser = { |
| @@ -171,11 +259,6 @@ cr.define('options', function() { |
| * @private |
| */ |
| receiveExistingManagedUsers_: function(managedUsers) { |
| - if (!managedUsers) { |
| - $('managed-user-list').dataModel = null; |
| - return; |
| - } |
| - |
| managedUsers.sort(function(a, b) { |
| return a.name.localeCompare(b.name); |
| }); |
| @@ -184,14 +267,15 @@ cr.define('options', function() { |
| if (managedUsers.length == 0) { |
| this.onError_(loadTimeData.getString('noExistingManagedUsers')); |
| $('managed-user-import-ok').disabled = true; |
| + } else { |
| + // Hide the error bubble. |
| + $('managed-user-import-error-bubble').hidden = true; |
| } |
| }, |
| - /** |
| - * @private |
| - */ |
| - hideErrorBubble_: function() { |
| - $('managed-user-import-error-bubble').hidden = true; |
| + onSigninError_: function() { |
| + $('managed-user-list').dataModel = null; |
| + this.onError_(loadTimeData.getString('managedUserImportSigninError')); |
| }, |
| /** |
| @@ -220,10 +304,7 @@ cr.define('options', function() { |
| // Forward public APIs to private implementations. |
| [ |
| - 'hideErrorBubble', |
| - 'onError', |
| 'onSuccess', |
| - 'receiveExistingManagedUsers', |
| ].forEach(function(name) { |
| ManagedUserImportOverlay[name] = function() { |
| var instance = ManagedUserImportOverlay.getInstance(); |
| @@ -234,5 +315,6 @@ cr.define('options', function() { |
| // Export |
| return { |
| ManagedUserImportOverlay: ManagedUserImportOverlay, |
| + ManagedUserList: ManagedUserList, |
| }; |
| }); |