1 // Copyright 2014 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.
5 cr.define('options', function() {
7 * ManagedUserListData class.
8 * Handles requests for retrieving a list of existing managed users which are
9 * supervised by the current profile. For each request a promise is returned,
10 * which is cached in order to reuse the retrieved managed users for future
11 * requests. The first request will be handled asynchronously.
15 function ManagedUserListData() {};
17 cr.addSingletonGetter(ManagedUserListData);
20 * Receives a list of managed users and resolves the promise.
21 * @param {Array.<Object>} managedUsers An array of managed user objects.
22 * Each object is of the form:
24 * id: "Managed User ID",
25 * name: "Managed User Name",
26 * iconURL: "chrome://path/to/icon/image",
27 * onCurrentDevice: true or false,
28 * needAvatar: true or false
32 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function(
34 assert(this.promise_);
35 this.resolve_(managedUsers);
39 * Called when there is a signin error when retrieving the list of managed
40 * users. Rejects the promise and resets the cached promise to null.
43 ManagedUserListData.prototype.onSigninError_ = function() {
44 assert(this.promise_);
50 * Handles the request for the list of existing managed users by returning a
51 * promise for the requested data. If there is no cached promise yet, a new
52 * one will be created.
53 * @return {Promise} The promise containing the list of managed users.
56 ManagedUserListData.prototype.requestExistingManagedUsers_ = function() {
59 this.promise_ = this.createPromise_();
60 chrome.send('requestManagedUserImportUpdate');
65 * Creates the promise containing the list of managed users. The promise is
66 * resolved in receiveExistingManagedUsers_() or rejected in
67 * onSigninError_(). The promise is cached, so that for future requests it can
68 * be resolved immediately.
69 * @return {Promise} The promise containing the list of managed users.
72 ManagedUserListData.prototype.createPromise_ = function() {
74 return new Promise(function(resolve, reject) {
75 self.resolve_ = resolve;
76 self.reject_ = reject;
81 * Resets the promise to null in order to avoid stale data. For the next
82 * request, a new promise will be created.
85 ManagedUserListData.prototype.resetPromise_ = function() {
89 // Forward public APIs to private implementations.
92 'receiveExistingManagedUsers',
93 'requestExistingManagedUsers',
95 ].forEach(function(name) {
96 ManagedUserListData[name] = function() {
97 var instance = ManagedUserListData.getInstance();
98 return instance[name + '_'].apply(instance, arguments);
104 ManagedUserListData: ManagedUserListData,