Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / supervised_user_list_data.js
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.
4
5 cr.define('options', function() {
6   /**
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.
12    * @constructor
13    * @class
14    */
15   function SupervisedUserListData() {
16     this.observers_ = [];
17   };
18
19   cr.addSingletonGetter(SupervisedUserListData);
20
21   /**
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:
25    *       supervisedUser = {
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
31    *       }
32    * @private
33    */
34   SupervisedUserListData.prototype.receiveExistingSupervisedUsers_ = function(
35     supervisedUsers) {
36     if (!this.promise_) {
37       this.onDataChanged_(supervisedUsers);
38       return;
39     }
40     this.resolve_(supervisedUsers);
41   };
42
43   /**
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.
46    * @private
47    */
48   SupervisedUserListData.prototype.onSigninError_ = function() {
49     assert(this.promise_);
50     this.reject_();
51     this.resetPromise_();
52   };
53
54   /**
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.
59    * @private
60    */
61   SupervisedUserListData.prototype.requestExistingSupervisedUsers_ =
62       function() {
63     if (this.promise_)
64       return this.promise_;
65     this.promise_ = this.createPromise_();
66     chrome.send('requestSupervisedUserImportUpdate');
67     return this.promise_;
68   };
69
70   /**
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.
76    * @private
77    */
78   SupervisedUserListData.prototype.createPromise_ = function() {
79     var self = this;
80     return new Promise(function(resolve, reject) {
81       self.resolve_ = resolve;
82       self.reject_ = reject;
83     });
84   };
85
86   /**
87    * Resets the promise to null in order to avoid stale data. For the next
88    * request, a new promise will be created.
89    * @private
90    */
91   SupervisedUserListData.prototype.resetPromise_ = function() {
92     this.promise_ = null;
93   };
94
95   /**
96    * Initializes |promise| with the new data and also passes the new data to
97    * observers.
98    * @param {Array.<Object>} supervisedUsers Array of supervised user objects.
99    *     For the format of the objects, see receiveExistingSupervisedUsers_().
100    * @private
101    */
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);
107   };
108
109   /**
110    * Adds an observer to the list of observers.
111    * @param {Object} observer The observer to be added.
112    * @private
113    */
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);
118   };
119
120   /**
121    * Removes an observer from the list of observers.
122    * @param {Object} observer The observer to be removed.
123    * @private
124    */
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);
129         return;
130       }
131     }
132   };
133
134   // Forward public APIs to private implementations.
135   [
136     'addObserver',
137     'onSigninError',
138     'receiveExistingSupervisedUsers',
139     'removeObserver',
140     'requestExistingSupervisedUsers',
141     'resetPromise',
142   ].forEach(function(name) {
143     SupervisedUserListData[name] = function() {
144       var instance = SupervisedUserListData.getInstance();
145       return instance[name + '_'].apply(instance, arguments);
146     };
147   });
148
149   // Export
150   return {
151     SupervisedUserListData: SupervisedUserListData,
152   };
153 });