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

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: 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. This list is cached in order to make it
10 * possible to serve future requests immediately. The first request will be 10 * possible to serve future requests immediately. The first request will be
11 * handled asynchronously. 11 * handled asynchronously.
12 * @constructor 12 * @constructor
13 * @class 13 * @class
14 */ 14 */
15 // TODO(akuegel): Refactor this class by using dom promises.
15 function ManagedUserListData() { 16 function ManagedUserListData() {
16 this.callbacks_ = []; 17 this.callbacks_ = [];
17 this.errbacks_ = []; 18 this.errbacks_ = [];
18 this.requestInProgress_ = false; 19 this.requestInProgress_ = false;
19 this.managedUsers_ = null; 20 this.managedUsers_ = null;
20 }; 21 };
21 22
22 cr.addSingletonGetter(ManagedUserListData); 23 cr.addSingletonGetter(ManagedUserListData);
23 24
24 /** 25 /**
25 * Resets to the initial state of no pending requests. 26 * Resets to the initial state of no pending requests.
26 * @private 27 * @private
27 */ 28 */
28 ManagedUserListData.prototype.reset_ = function() { 29 ManagedUserListData.prototype.reset_ = function() {
29 this.callbacks_ = []; 30 this.callbacks_ = [];
30 this.errbacks_ = []; 31 this.errbacks_ = [];
31 this.requestInProgress_ = false; 32 this.requestInProgress_ = false;
32 } 33 }
33 34
34 /** 35 /**
35 * Receives a list of managed users and passes the list to each of the 36 * Receives a list of managed users and passes the list to each of the
36 * callbacks. 37 * callbacks.
37 * @param {Array.<Object>} managedUsers An array of managed user objects. 38 * @param {Array.<Object>} managedUsers An array of managed user objects.
38 * Each object is of the form: 39 * Each object is of the form:
39 * managedUser = { 40 * managedUser = {
40 * id: "Managed User ID", 41 * id: "Managed User ID",
41 * name: "Managed User Name", 42 * name: "Managed User Name",
42 * iconURL: "chrome://path/to/icon/image", 43 * iconURL: "chrome://path/to/icon/image",
43 * onCurrentDevice: true or false, 44 * onCurrentDevice: true or false,
45 * nameConflict: true or false,
44 * needAvatar: true or false 46 * needAvatar: true or false
45 * } 47 * }
48 * @private
46 */ 49 */
47 ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { 50 ManagedUserListData.prototype.receiveExistingManagedUsers_ = function(
48 var instance = ManagedUserListData.getInstance(); 51 managedUsers) {
49 var i; 52 for (var i = 0; i < this.callbacks_.length; i++)
50 for (i = 0; i < instance.callbacks_.length; i++) { 53 this.callbacks_[i](managedUsers);
51 instance.callbacks_[i](managedUsers); 54 this.managedUsers_ = managedUsers;
52 } 55 this.reset_();
53 instance.managedUsers_ = managedUsers;
54 instance.reset_();
55 }; 56 };
56 57
57 /** 58 /**
58 * Called when there is a signin error when retrieving the list of managed 59 * 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 60 * users. Calls the error callbacks which will display an appropriate error
60 * message to the user. 61 * message to the user.
62 * @private
61 */ 63 */
62 ManagedUserListData.onSigninError = function() { 64 ManagedUserListData.prototype.onSigninError_ = function() {
63 var instance = ManagedUserListData.getInstance(); 65 for (var i = 0; i < this.errbacks_.length; i++)
64 var i; 66 this.errbacks_[i]();
65 for (i = 0; i < instance.errbacks_.length; i++) {
66 instance.errbacks_[i]();
67 }
68 // Reset the list of managed users in order to avoid showing stale data. 67 // Reset the list of managed users in order to avoid showing stale data.
69 instance.managedUsers_ = null; 68 this.managedUsers_ = null;
70 instance.reset_(); 69 this.reset_();
71 }; 70 };
72 71
73 /** 72 /**
74 * Handles the request for the list of existing managed users. If the data is 73 * Handles the request for the list of existing managed users. If the data is
75 * already available, it will call |callback| immediately. Otherwise, it 74 * already available, it will call |callback| immediately. Otherwise, it
76 * retrieves the list of existing managed users which is then processed in 75 * retrieves the list of existing managed users which is then processed in
77 * receiveExistingManagedUsers(). 76 * receiveExistingManagedUsers().
78 * @param {Object} callback The callback function which is called on success. 77 * @param {Object} callback The callback function which is called on success.
79 * @param {Object} errback the callback function which is called on error. 78 * @param {Object} errback the callback function which is called on error.
79 * @private
80 */ 80 */
81 ManagedUserListData.requestExistingManagedUsers = function(callback, 81 ManagedUserListData.prototype.requestExistingManagedUsers_ = function(
82 errback) { 82 callback, errback) {
83 var instance = ManagedUserListData.getInstance(); 83 if (!this.requestInProgress_) {
Bernhard Bauer 2014/01/09 16:33:18 Early-return if a request is in progress?
Adrian Kuegel 2014/01/10 09:53:43 This is obsolete now. I already replaced my code w
84 instance.callbacks_.push(callback); 84 if (this.managedUsers_ != null) {
85 instance.errbacks_.push(errback); 85 callback(this.managedUsers_);
86 if (!instance.requestInProgress_) {
87 if (instance.managedUsers_ != null) {
88 ManagedUserListData.receiveExistingManagedUsers(instance.managedUsers_);
89 return; 86 return;
90 } 87 }
91 instance.requestInProgress_ = true; 88 this.callbacks_.push(callback);
89 this.errbacks_.push(errback);
90 this.requestInProgress_ = true;
92 chrome.send('requestManagedUserImportUpdate'); 91 chrome.send('requestManagedUserImportUpdate');
93 } 92 }
94 }; 93 };
95 94
96 /** 95 /**
97 * Reload the list of existing managed users. Should be called when a new 96 * Reload the list of existing managed users. Should be called when a new
98 * supervised user profile was created or a supervised user profile was 97 * supervised user profile was created or a supervised user profile was
99 * deleted. 98 * deleted.
99 * @private
100 */ 100 */
101 ManagedUserListData.reloadExistingManagedUsers = function() { 101 ManagedUserListData.prototype.reloadExistingManagedUsers_ = function() {
102 var instance = ManagedUserListData.getInstance(); 102 if (this.requestInProgress_)
103 if (instance.requestInProgress_)
104 return; 103 return;
105 104
106 instance.managedUsers_ = null; 105 this.managedUsers_ = null;
107 instance.requestInProgress_ = true; 106 this.requestInProgress_ = true;
108 chrome.send('requestManagedUserImportUpdate'); 107 chrome.send('requestManagedUserImportUpdate');
109 }; 108 };
110 109
110 // Forward public APIs to private implementations.
111 [
112 'onSigninError',
113 'receiveExistingManagedUsers',
114 'reloadExistingManagedUsers',
115 'requestExistingManagedUsers',
116 ].forEach(function(name) {
117 ManagedUserListData[name] = function() {
118 var instance = ManagedUserListData.getInstance();
119 return instance[name + '_'].apply(instance, arguments);
120 };
121 });
122
111 // Export 123 // Export
112 return { 124 return {
113 ManagedUserListData: ManagedUserListData, 125 ManagedUserListData: ManagedUserListData,
114 }; 126 };
115 }); 127 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698