Chromium Code Reviews| Index: chrome/browser/resources/options/managed_user_list_data.js |
| diff --git a/chrome/browser/resources/options/managed_user_list_data.js b/chrome/browser/resources/options/managed_user_list_data.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f774f70751937fe5075157acf31d052a795d78b7 |
| --- /dev/null |
| +++ b/chrome/browser/resources/options/managed_user_list_data.js |
| @@ -0,0 +1,115 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +cr.define('options', function() { |
| + /** |
| + * ManagedUserListData class. |
| + * 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 |
| + * handled asynchronously. |
| + * @constructor |
| + * @class |
| + */ |
| + function ManagedUserListData() { |
| + this.callbacks_ = []; |
| + this.errbacks_ = []; |
| + this.requestInProgress_ = false; |
| + this.managedUsers_ = null; |
| + }; |
| + |
| + cr.addSingletonGetter(ManagedUserListData); |
| + |
| + /** |
| + * Resets to the initial state of no pending requests. |
| + * @private |
| + */ |
| + ManagedUserListData.prototype.reset_ = function() { |
| + this.callbacks_ = []; |
| + this.errbacks_ = []; |
| + this.requestInProgress_ = false; |
| + } |
| + |
| + /** |
| + * Receives a list of managed users and passes the list to each of the |
| + * callbacks. |
| + * @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 |
| + * } |
| + */ |
| + ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { |
| + var instance = ManagedUserListData.getInstance(); |
| + var i; |
|
Bernhard Bauer
2014/01/09 13:39:08
Nit: You can inline the var into the for loop.
Adrian Kuegel
2014/01/09 14:12:42
Right. I am just not used to inline this into the
|
| + for (i = 0; i < instance.callbacks_.length; i++) { |
| + instance.callbacks_[i](managedUsers); |
| + } |
| + instance.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. |
| + */ |
| + ManagedUserListData.onSigninError = function() { |
| + var instance = ManagedUserListData.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. |
|
Bernhard Bauer
2014/01/09 13:39:08
It might be nice to do the whole thing with DOM fu
Adrian Kuegel
2014/01/09 14:12:42
I already have the caching in the code. I didn't k
Bernhard Bauer
2014/01/09 14:19:27
We can still do that; just throw away the future.
Adrian Kuegel
2014/01/09 15:34:17
From what I have read so far, it looks like it got
Bernhard Bauer
2014/01/09 16:25:11
Promises are available from Chrome 32 on, accordin
|
| + */ |
| + ManagedUserListData.requestExistingManagedUsers = function(callback, |
| + errback) { |
| + var instance = ManagedUserListData.getInstance(); |
|
Bernhard Bauer
2014/01/09 13:39:08
The usual way to do this is to make this a private
Adrian Kuegel
2014/01/09 14:12:42
Ok, I didn't know this is a required pattern. I ca
Bernhard Bauer
2014/01/09 14:19:27
It's not required, but this is how it is usually d
|
| + instance.callbacks_.push(callback); |
| + instance.errbacks_.push(errback); |
| + if (!instance.requestInProgress_) { |
| + if (instance.managedUsers_ != null) { |
| + ManagedUserListData.receiveExistingManagedUsers(instance.managedUsers_); |
|
Bernhard Bauer
2014/01/09 13:39:08
Note that this will not only call |callback|, but
Adrian Kuegel
2014/01/09 14:12:42
I think that if |requestInProgress_| is false, the
|
| + return; |
| + } |
| + instance.requestInProgress_ = true; |
| + chrome.send('requestManagedUserImportUpdate'); |
| + } |
| + }; |
| + |
| + /** |
| + * Reload the list of existing managed users. Should be called when a new |
| + * supervised user profile was created or a supervised user profile was |
| + * deleted. |
| + */ |
| + ManagedUserListData.reloadExistingManagedUsers = function() { |
| + var instance = ManagedUserListData.getInstance(); |
| + if (instance.requestInProgress_) |
| + return; |
| + |
| + instance.managedUsers_ = null; |
| + instance.requestInProgress_ = true; |
| + chrome.send('requestManagedUserImportUpdate'); |
| + }; |
| + |
| + // Export |
| + return { |
| + ManagedUserListData: ManagedUserListData, |
| + }; |
| +}); |