OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options', function() { | 5 cr.define('options', function() { |
6 /** | 6 /** |
7 * ManagedUserListData class. | 7 * ManagedUserListData class. |
8 * Handles requests for retrieving a list of existing managed users which are | 8 * Handles requests for retrieving a list of existing managed users which are |
9 * supervised by the current profile. This list is cached in order to make it | 9 * supervised by the current profile. For each request a promise is returned, |
10 * possible to serve future requests immediately. The first request will be | 10 * which is cached in order to reuse the retrieved managed users for future |
11 * handled asynchronously. | 11 * requests. The first request will be handled asynchronously. |
12 * @constructor | 12 * @constructor |
13 * @class | 13 * @class |
14 */ | 14 */ |
15 function ManagedUserListData() { | 15 function ManagedUserListData() {}; |
16 this.callbacks_ = []; | |
17 this.errbacks_ = []; | |
18 this.requestInProgress_ = false; | |
19 this.managedUsers_ = null; | |
20 }; | |
21 | 16 |
22 cr.addSingletonGetter(ManagedUserListData); | 17 cr.addSingletonGetter(ManagedUserListData); |
23 | 18 |
24 /** | 19 /** |
25 * Resets to the initial state of no pending requests. | 20 * Receives a list of managed users and resolves the promise. |
26 * @private | |
27 */ | |
28 ManagedUserListData.prototype.reset_ = function() { | |
29 this.callbacks_ = []; | |
30 this.errbacks_ = []; | |
31 this.requestInProgress_ = false; | |
32 } | |
33 | |
34 /** | |
35 * Receives a list of managed users and passes the list to each of the | |
36 * callbacks. | |
37 * @param {Array.<Object>} managedUsers An array of managed user objects. | 21 * @param {Array.<Object>} managedUsers An array of managed user objects. |
38 * Each object is of the form: | 22 * Each object is of the form: |
39 * managedUser = { | 23 * managedUser = { |
40 * id: "Managed User ID", | 24 * id: "Managed User ID", |
41 * name: "Managed User Name", | 25 * name: "Managed User Name", |
42 * iconURL: "chrome://path/to/icon/image", | 26 * iconURL: "chrome://path/to/icon/image", |
43 * onCurrentDevice: true or false, | 27 * onCurrentDevice: true or false, |
| 28 * nameConflict: true or false, |
44 * needAvatar: true or false | 29 * needAvatar: true or false |
45 * } | 30 * } |
| 31 * @private |
46 */ | 32 */ |
47 ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { | 33 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function( |
48 var instance = ManagedUserListData.getInstance(); | 34 managedUsers) { |
49 var i; | 35 assert(this.promise_); |
50 for (i = 0; i < instance.callbacks_.length; i++) { | 36 this.resolve_(managedUsers); |
51 instance.callbacks_[i](managedUsers); | |
52 } | |
53 instance.managedUsers_ = managedUsers; | |
54 instance.reset_(); | |
55 }; | 37 }; |
56 | 38 |
57 /** | 39 /** |
58 * Called when there is a signin error when retrieving the list of managed | 40 * Called when there is a signin error when retrieving the list of managed |
59 * users. Calls the error callbacks which will display an appropriate error | 41 * users. Rejects the promise and resets the cached promise to null. |
60 * message to the user. | 42 * @private |
61 */ | 43 */ |
62 ManagedUserListData.onSigninError = function() { | 44 ManagedUserListData.prototype.onSigninError_ = function() { |
63 var instance = ManagedUserListData.getInstance(); | 45 assert(this.promise_); |
64 var i; | 46 this.reject_(); |
65 for (i = 0; i < instance.errbacks_.length; i++) { | 47 this.resetPromise_(); |
66 instance.errbacks_[i](); | |
67 } | |
68 // Reset the list of managed users in order to avoid showing stale data. | |
69 instance.managedUsers_ = null; | |
70 instance.reset_(); | |
71 }; | 48 }; |
72 | 49 |
73 /** | 50 /** |
74 * Handles the request for the list of existing managed users. If the data is | 51 * Handles the request for the list of existing managed users by returning a |
75 * already available, it will call |callback| immediately. Otherwise, it | 52 * promise for the requested data. If there is no cached promise yet, a new |
76 * retrieves the list of existing managed users which is then processed in | 53 * one will be created. |
77 * receiveExistingManagedUsers(). | 54 * @return {Promise} The promise containing the list of managed users. |
78 * @param {Object} callback The callback function which is called on success. | 55 * @private |
79 * @param {Object} errback the callback function which is called on error. | |
80 */ | 56 */ |
81 ManagedUserListData.requestExistingManagedUsers = function(callback, | 57 ManagedUserListData.prototype.requestExistingManagedUsers_ = function() { |
82 errback) { | 58 if (this.promise_) |
83 var instance = ManagedUserListData.getInstance(); | 59 return this.promise_; |
84 instance.callbacks_.push(callback); | 60 this.promise_ = this.createPromise_(); |
85 instance.errbacks_.push(errback); | 61 chrome.send('requestManagedUserImportUpdate'); |
86 if (!instance.requestInProgress_) { | 62 return this.promise_; |
87 if (instance.managedUsers_ != null) { | |
88 ManagedUserListData.receiveExistingManagedUsers(instance.managedUsers_); | |
89 return; | |
90 } | |
91 instance.requestInProgress_ = true; | |
92 chrome.send('requestManagedUserImportUpdate'); | |
93 } | |
94 }; | 63 }; |
95 | 64 |
96 /** | 65 /** |
97 * Reload the list of existing managed users. Should be called when a new | 66 * Creates the promise containing the list of managed users. The promise is |
98 * supervised user profile was created or a supervised user profile was | 67 * resolved in receiveExistingManagedUsers_() or rejected in |
99 * deleted. | 68 * onSigninError_(). The promise is cached, so that for future requests it can |
| 69 * be resolved immediately. |
| 70 * @return {Promise} The promise containing the list of managed users. |
| 71 * @private |
100 */ | 72 */ |
101 ManagedUserListData.reloadExistingManagedUsers = function() { | 73 ManagedUserListData.prototype.createPromise_ = function() { |
102 var instance = ManagedUserListData.getInstance(); | 74 var self = this; |
103 if (instance.requestInProgress_) | 75 return new Promise(function(resolve, reject) { |
104 return; | 76 self.resolve_ = resolve; |
| 77 self.reject_ = reject; |
| 78 }); |
| 79 }; |
105 | 80 |
106 instance.managedUsers_ = null; | 81 /** |
107 instance.requestInProgress_ = true; | 82 * Resets the promise to null in order to avoid stale data. For the next |
108 chrome.send('requestManagedUserImportUpdate'); | 83 * request, a new promise will be created. |
| 84 * @private |
| 85 */ |
| 86 ManagedUserListData.prototype.resetPromise_ = function() { |
| 87 this.promise_ = null; |
109 }; | 88 }; |
110 | 89 |
| 90 // Forward public APIs to private implementations. |
| 91 [ |
| 92 'onSigninError', |
| 93 'receiveExistingManagedUsers', |
| 94 'requestExistingManagedUsers', |
| 95 'resetPromise', |
| 96 ].forEach(function(name) { |
| 97 ManagedUserListData[name] = function() { |
| 98 var instance = ManagedUserListData.getInstance(); |
| 99 return instance[name + '_'].apply(instance, arguments); |
| 100 }; |
| 101 }); |
| 102 |
111 // Export | 103 // Export |
112 return { | 104 return { |
113 ManagedUserListData: ManagedUserListData, | 105 ManagedUserListData: ManagedUserListData, |
114 }; | 106 }; |
115 }); | 107 }); |
OLD | NEW |