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 * SupervisedUserListData class.
8 * Handles requests for retrieving a list of existing supervised users which
9 * are supervised by the current profile. For each request a promise is
10 * returned, which is cached in order to reuse the retrieved supervised users
11 * for future requests. The first request will be handled asynchronously.
15 function SupervisedUserListData() {
19 cr.addSingletonGetter(SupervisedUserListData);
22 * Receives a list of supervised users and resolves the promise.
23 * @param {Array<Object>} supervisedUsers Array of supervised user objects.
24 * Each object is of the form:
26 * id: "Supervised User ID",
27 * name: "Supervised User Name",
28 * iconURL: "chrome://path/to/icon/image",
29 * onCurrentDevice: true or false,
30 * needAvatar: true or false
34 SupervisedUserListData.prototype.receiveExistingSupervisedUsers_ =
35 function(supervisedUsers) {
37 this.onDataChanged_(supervisedUsers);
40 this.resolve_(supervisedUsers);
44 * Called when there is a signin error when retrieving the list of supervised
45 * users. Rejects the promise and resets the cached promise to null.
48 SupervisedUserListData.prototype.onSigninError_ = function() {
57 * Handles the request for the list of existing supervised users by returning
58 * a promise for the requested data. If there is no cached promise yet, a new
59 * one will be created.
60 * @return {Promise} The promise containing the list of supervised users.
63 SupervisedUserListData.prototype.requestExistingSupervisedUsers_ =
67 this.promise_ = this.createPromise_();
68 chrome.send('requestSupervisedUserImportUpdate');
73 * Creates the promise containing the list of supervised users. The promise is
74 * resolved in receiveExistingSupervisedUsers_() or rejected in
75 * onSigninError_(). The promise is cached, so that for future requests it can
76 * be resolved immediately.
77 * @return {Promise} The promise containing the list of supervised users.
80 SupervisedUserListData.prototype.createPromise_ = function() {
82 return new Promise(function(resolve, reject) {
83 self.resolve_ = resolve;
84 self.reject_ = reject;
89 * Resets the promise to null in order to avoid stale data. For the next
90 * request, a new promise will be created.
93 SupervisedUserListData.prototype.resetPromise_ = function() {
98 * Initializes |promise| with the new data and also passes the new data to
100 * @param {Array<Object>} supervisedUsers Array of supervised user objects.
101 * For the format of the objects, see receiveExistingSupervisedUsers_().
104 SupervisedUserListData.prototype.onDataChanged_ = function(supervisedUsers) {
105 this.promise_ = this.createPromise_();
106 this.resolve_(supervisedUsers);
107 for (var i = 0; i < this.observers_.length; ++i)
108 this.observers_[i].receiveExistingSupervisedUsers_(supervisedUsers);
112 * Adds an observer to the list of observers.
113 * @param {Object} observer The observer to be added.
116 SupervisedUserListData.prototype.addObserver_ = function(observer) {
117 for (var i = 0; i < this.observers_.length; ++i)
118 assert(this.observers_[i] != observer);
119 this.observers_.push(observer);
123 * Removes an observer from the list of observers.
124 * @param {Object} observer The observer to be removed.
127 SupervisedUserListData.prototype.removeObserver_ = function(observer) {
128 for (var i = 0; i < this.observers_.length; ++i) {
129 if (this.observers_[i] == observer) {
130 this.observers_.splice(i, 1);
136 // Forward public APIs to private implementations.
137 cr.makePublic(SupervisedUserListData, [
140 'receiveExistingSupervisedUsers',
142 'requestExistingSupervisedUsers',
148 SupervisedUserListData: SupervisedUserListData,