OLD | NEW |
| (Empty) |
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 * 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. | |
12 * @constructor | |
13 * @class | |
14 */ | |
15 function ManagedUserListData() { | |
16 this.observers_ = []; | |
17 }; | |
18 | |
19 cr.addSingletonGetter(ManagedUserListData); | |
20 | |
21 /** | |
22 * Receives a list of managed users and resolves the promise. | |
23 * @param {Array.<Object>} managedUsers An array of managed user objects. | |
24 * Each object is of the form: | |
25 * managedUser = { | |
26 * id: "Managed User ID", | |
27 * name: "Managed User Name", | |
28 * iconURL: "chrome://path/to/icon/image", | |
29 * onCurrentDevice: true or false, | |
30 * needAvatar: true or false | |
31 * } | |
32 * @private | |
33 */ | |
34 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function( | |
35 managedUsers) { | |
36 if (!this.promise_) { | |
37 this.onDataChanged_(managedUsers); | |
38 return; | |
39 } | |
40 this.resolve_(managedUsers); | |
41 }; | |
42 | |
43 /** | |
44 * Called when there is a signin error when retrieving the list of managed | |
45 * users. Rejects the promise and resets the cached promise to null. | |
46 * @private | |
47 */ | |
48 ManagedUserListData.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 managed users by returning a | |
56 * 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 managed users. | |
59 * @private | |
60 */ | |
61 ManagedUserListData.prototype.requestExistingManagedUsers_ = function() { | |
62 if (this.promise_) | |
63 return this.promise_; | |
64 this.promise_ = this.createPromise_(); | |
65 chrome.send('requestManagedUserImportUpdate'); | |
66 return this.promise_; | |
67 }; | |
68 | |
69 /** | |
70 * Creates the promise containing the list of managed users. The promise is | |
71 * resolved in receiveExistingManagedUsers_() or rejected in | |
72 * onSigninError_(). The promise is cached, so that for future requests it can | |
73 * be resolved immediately. | |
74 * @return {Promise} The promise containing the list of managed users. | |
75 * @private | |
76 */ | |
77 ManagedUserListData.prototype.createPromise_ = function() { | |
78 var self = this; | |
79 return new Promise(function(resolve, reject) { | |
80 self.resolve_ = resolve; | |
81 self.reject_ = reject; | |
82 }); | |
83 }; | |
84 | |
85 /** | |
86 * Resets the promise to null in order to avoid stale data. For the next | |
87 * request, a new promise will be created. | |
88 * @private | |
89 */ | |
90 ManagedUserListData.prototype.resetPromise_ = function() { | |
91 this.promise_ = null; | |
92 }; | |
93 | |
94 /** | |
95 * Initializes |promise| with the new data and also passes the new data to | |
96 * observers. | |
97 * @param {Array.<Object>} managedUsers An array of managed user objects. | |
98 * For the format of the objects, see receiveExistingManagedUsers_(). | |
99 * @private | |
100 */ | |
101 ManagedUserListData.prototype.onDataChanged_ = function(managedUsers) { | |
102 this.promise_ = this.createPromise_(); | |
103 this.resolve_(managedUsers); | |
104 for (var i = 0; i < this.observers_.length; ++i) | |
105 this.observers_[i].receiveExistingManagedUsers_(managedUsers); | |
106 }; | |
107 | |
108 /** | |
109 * Adds an observer to the list of observers. | |
110 * @param {Object} observer The observer to be added. | |
111 * @private | |
112 */ | |
113 ManagedUserListData.prototype.addObserver_ = function(observer) { | |
114 for (var i = 0; i < this.observers_.length; ++i) | |
115 assert(this.observers_[i] != observer); | |
116 this.observers_.push(observer); | |
117 }; | |
118 | |
119 /** | |
120 * Removes an observer from the list of observers. | |
121 * @param {Object} observer The observer to be removed. | |
122 * @private | |
123 */ | |
124 ManagedUserListData.prototype.removeObserver_ = function(observer) { | |
125 for (var i = 0; i < this.observers_.length; ++i) { | |
126 if (this.observers_[i] == observer) { | |
127 this.observers_.splice(i, 1); | |
128 return; | |
129 } | |
130 } | |
131 }; | |
132 | |
133 // Forward public APIs to private implementations. | |
134 [ | |
135 'addObserver', | |
136 'onSigninError', | |
137 'receiveExistingManagedUsers', | |
138 'removeObserver', | |
139 'requestExistingManagedUsers', | |
140 'resetPromise', | |
141 ].forEach(function(name) { | |
142 ManagedUserListData[name] = function() { | |
143 var instance = ManagedUserListData.getInstance(); | |
144 return instance[name + '_'].apply(instance, arguments); | |
145 }; | |
146 }); | |
147 | |
148 // Export | |
149 return { | |
150 ManagedUserListData: ManagedUserListData, | |
151 }; | |
152 }); | |
OLD | NEW |