Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: chrome/browser/resources/options/managed_user_list_data.js

Issue 132013002: Replace own callback handling with Promises. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_ = []; 16 this.promise_;
17 this.errbacks_ = []; 17 this.resolve_;
18 this.requestInProgress_ = false; 18 this.reject_;
19 this.managedUsers_ = null;
20 }; 19 };
21 20
22 cr.addSingletonGetter(ManagedUserListData); 21 cr.addSingletonGetter(ManagedUserListData);
23 22
24 /** 23 /**
25 * Resets to the initial state of no pending requests. 24 * 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. 25 * @param {Array.<Object>} managedUsers An array of managed user objects.
38 * Each object is of the form: 26 * Each object is of the form:
39 * managedUser = { 27 * managedUser = {
40 * id: "Managed User ID", 28 * id: "Managed User ID",
41 * name: "Managed User Name", 29 * name: "Managed User Name",
42 * iconURL: "chrome://path/to/icon/image", 30 * iconURL: "chrome://path/to/icon/image",
43 * onCurrentDevice: true or false, 31 * onCurrentDevice: true or false,
32 * nameConflict: true or false,
44 * needAvatar: true or false 33 * needAvatar: true or false
45 * } 34 * }
35 * @private
46 */ 36 */
47 ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { 37 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function(
48 var instance = ManagedUserListData.getInstance(); 38 managedUsers) {
49 var i; 39 assert(this.promise_);
50 for (i = 0; i < instance.callbacks_.length; i++) { 40 this.resolve_(managedUsers);
51 instance.callbacks_[i](managedUsers);
52 }
53 instance.managedUsers_ = managedUsers;
54 instance.reset_();
55 }; 41 };
56 42
57 /** 43 /**
58 * Called when there is a signin error when retrieving the list of managed 44 * 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 45 * users. Rejects the promise and resets the cached promise to null.
60 * message to the user. 46 * @private
61 */ 47 */
62 ManagedUserListData.onSigninError = function() { 48 ManagedUserListData.prototype.onSigninError_ = function() {
63 var instance = ManagedUserListData.getInstance(); 49 assert(this.promise_);
64 var i; 50 this.reject_();
65 for (i = 0; i < instance.errbacks_.length; i++) { 51 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 }; 52 };
72 53
73 /** 54 /**
74 * Handles the request for the list of existing managed users. If the data is 55 * Handles the request for the list of existing managed users by returning a
75 * already available, it will call |callback| immediately. Otherwise, it 56 * 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 57 * one will be created.
77 * receiveExistingManagedUsers(). 58 * @return {Promise} The promise containing the list of managed users.
78 * @param {Object} callback The callback function which is called on success. 59 * @private
79 * @param {Object} errback the callback function which is called on error.
80 */ 60 */
81 ManagedUserListData.requestExistingManagedUsers = function(callback, 61 ManagedUserListData.prototype.requestExistingManagedUsers_ = function() {
82 errback) { 62 if (this.promise_)
83 var instance = ManagedUserListData.getInstance(); 63 return this.promise_;
84 instance.callbacks_.push(callback); 64 this.promise_ = this.createPromise_();
85 instance.errbacks_.push(errback); 65 chrome.send('requestManagedUserImportUpdate');
86 if (!instance.requestInProgress_) { 66 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 }; 67 };
95 68
96 /** 69 /**
97 * Reload the list of existing managed users. Should be called when a new 70 * Creates the promise containing the list of managed users. The promise is
98 * supervised user profile was created or a supervised user profile was 71 * resolved in receiveExistingManagedUsers_() or rejected in
99 * deleted. 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
100 */ 76 */
101 ManagedUserListData.reloadExistingManagedUsers = function() { 77 ManagedUserListData.prototype.createPromise_ = function() {
102 var instance = ManagedUserListData.getInstance(); 78 var self = this;
103 if (instance.requestInProgress_) 79 return new Promise(function(resolve, reject) {
104 return; 80 self.resolve_ = resolve;
81 self.reject_ = reject;
82 });
83 };
105 84
106 instance.managedUsers_ = null; 85 /**
107 instance.requestInProgress_ = true; 86 * Resets the promise to null in order to avoid stale data. For the next
108 chrome.send('requestManagedUserImportUpdate'); 87 * request, a new promise will be created.
88 * @private
89 */
90 ManagedUserListData.prototype.resetPromise_ = function() {
91 this.promise_ = null;
109 }; 92 };
110 93
94 // Forward public APIs to private implementations.
95 [
96 'onSigninError',
97 'receiveExistingManagedUsers',
98 'requestExistingManagedUsers',
99 'resetPromise',
100 ].forEach(function(name) {
101 ManagedUserListData[name] = function() {
102 var instance = ManagedUserListData.getInstance();
103 return instance[name + '_'].apply(instance, arguments);
104 };
105 });
106
111 // Export 107 // Export
112 return { 108 return {
113 ManagedUserListData: ManagedUserListData, 109 ManagedUserListData: ManagedUserListData,
114 }; 110 };
115 }); 111 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698