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_ = function(
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() {
49 assert(this.promise_);
55 * Handles the request for the list of existing supervised users by returning
56 * a promise for the requested data. If there is no cached promise yet, a new
57 * one will be created.
58 * @return {Promise} The promise containing the list of supervised users.
61 SupervisedUserListData.prototype.requestExistingSupervisedUsers_ =
65 this.promise_ = this.createPromise_();
66 chrome.send('requestSupervisedUserImportUpdate');
71 * Creates the promise containing the list of supervised users. The promise is
72 * resolved in receiveExistingSupervisedUsers_() or rejected in
73 * onSigninError_(). The promise is cached, so that for future requests it can
74 * be resolved immediately.
75 * @return {Promise} The promise containing the list of supervised users.
78 SupervisedUserListData.prototype.createPromise_ = function() {
80 return new Promise(function(resolve, reject) {
81 self.resolve_ = resolve;
82 self.reject_ = reject;
87 * Resets the promise to null in order to avoid stale data. For the next
88 * request, a new promise will be created.
91 SupervisedUserListData.prototype.resetPromise_ = function() {
96 * Initializes |promise| with the new data and also passes the new data to
98 * @param {Array.<Object>} supervisedUsers Array of supervised user objects.
99 * For the format of the objects, see receiveExistingSupervisedUsers_().
102 SupervisedUserListData.prototype.onDataChanged_ = function(supervisedUsers) {
103 this.promise_ = this.createPromise_();
104 this.resolve_(supervisedUsers);
105 for (var i = 0; i < this.observers_.length; ++i)
106 this.observers_[i].receiveExistingSupervisedUsers_(supervisedUsers);
110 * Adds an observer to the list of observers.
111 * @param {Object} observer The observer to be added.
114 SupervisedUserListData.prototype.addObserver_ = function(observer) {
115 for (var i = 0; i < this.observers_.length; ++i)
116 assert(this.observers_[i] != observer);
117 this.observers_.push(observer);
121 * Removes an observer from the list of observers.
122 * @param {Object} observer The observer to be removed.
125 SupervisedUserListData.prototype.removeObserver_ = function(observer) {
126 for (var i = 0; i < this.observers_.length; ++i) {
127 if (this.observers_[i] == observer) {
128 this.observers_.splice(i, 1);
134 // Forward public APIs to private implementations.
138 'receiveExistingSupervisedUsers',
140 'requestExistingSupervisedUsers',
142 ].forEach(function(name) {
143 SupervisedUserListData[name] = function() {
144 var instance = SupervisedUserListData.getInstance();
145 return instance[name + '_'].apply(instance, arguments);
151 SupervisedUserListData: SupervisedUserListData,