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

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: Remove name conflict checks. 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_ = [];
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,
44 * needAvatar: true or false 28 * needAvatar: true or false
45 * } 29 * }
30 * @private
46 */ 31 */
47 ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { 32 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function(
48 var instance = ManagedUserListData.getInstance(); 33 managedUsers) {
49 var i; 34 assert(this.promise_);
50 for (i = 0; i < instance.callbacks_.length; i++) { 35 this.resolve_(managedUsers);
51 instance.callbacks_[i](managedUsers);
52 }
53 instance.managedUsers_ = managedUsers;
54 instance.reset_();
55 }; 36 };
56 37
57 /** 38 /**
58 * Called when there is a signin error when retrieving the list of managed 39 * 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 40 * users. Rejects the promise and resets the cached promise to null.
60 * message to the user. 41 * @private
61 */ 42 */
62 ManagedUserListData.onSigninError = function() { 43 ManagedUserListData.prototype.onSigninError_ = function() {
63 var instance = ManagedUserListData.getInstance(); 44 assert(this.promise_);
64 var i; 45 this.reject_();
65 for (i = 0; i < instance.errbacks_.length; i++) { 46 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 }; 47 };
72 48
73 /** 49 /**
74 * Handles the request for the list of existing managed users. If the data is 50 * Handles the request for the list of existing managed users by returning a
75 * already available, it will call |callback| immediately. Otherwise, it 51 * 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 52 * one will be created.
77 * receiveExistingManagedUsers(). 53 * @return {Promise} The promise containing the list of managed users.
78 * @param {Object} callback The callback function which is called on success. 54 * @private
79 * @param {Object} errback the callback function which is called on error.
80 */ 55 */
81 ManagedUserListData.requestExistingManagedUsers = function(callback, 56 ManagedUserListData.prototype.requestExistingManagedUsers_ = function() {
82 errback) { 57 if (this.promise_)
83 var instance = ManagedUserListData.getInstance(); 58 return this.promise_;
84 instance.callbacks_.push(callback); 59 this.promise_ = this.createPromise_();
85 instance.errbacks_.push(errback); 60 chrome.send('requestManagedUserImportUpdate');
86 if (!instance.requestInProgress_) { 61 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 }; 62 };
95 63
96 /** 64 /**
97 * Reload the list of existing managed users. Should be called when a new 65 * Creates the promise containing the list of managed users. The promise is
98 * supervised user profile was created or a supervised user profile was 66 * resolved in receiveExistingManagedUsers_() or rejected in
99 * deleted. 67 * onSigninError_(). The promise is cached, so that for future requests it can
68 * be resolved immediately.
69 * @return {Promise} The promise containing the list of managed users.
70 * @private
100 */ 71 */
101 ManagedUserListData.reloadExistingManagedUsers = function() { 72 ManagedUserListData.prototype.createPromise_ = function() {
102 var instance = ManagedUserListData.getInstance(); 73 var self = this;
103 if (instance.requestInProgress_) 74 return new Promise(function(resolve, reject) {
104 return; 75 self.resolve_ = resolve;
76 self.reject_ = reject;
77 });
78 };
105 79
106 instance.managedUsers_ = null; 80 /**
107 instance.requestInProgress_ = true; 81 * Resets the promise to null in order to avoid stale data. For the next
108 chrome.send('requestManagedUserImportUpdate'); 82 * request, a new promise will be created.
83 * @private
84 */
85 ManagedUserListData.prototype.resetPromise_ = function() {
86 this.promise_ = null;
109 }; 87 };
110 88
89 // Forward public APIs to private implementations.
90 [
91 'onSigninError',
92 'receiveExistingManagedUsers',
93 'requestExistingManagedUsers',
94 'resetPromise',
95 ].forEach(function(name) {
96 ManagedUserListData[name] = function() {
97 var instance = ManagedUserListData.getInstance();
98 return instance[name + '_'].apply(instance, arguments);
99 };
100 });
101
111 // Export 102 // Export
112 return { 103 return {
113 ManagedUserListData: ManagedUserListData, 104 ManagedUserListData: ManagedUserListData,
114 }; 105 };
115 }); 106 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698