OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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. This list is cached in order to make it | |
10 * possible to serve future requests immediately. The first request will be | |
11 * handled asynchronously. | |
12 * @constructor | |
13 * @class | |
14 */ | |
15 function ManagedUserListData() { | |
16 this.callbacks_ = []; | |
17 this.errbacks_ = []; | |
18 this.requestInProgress_ = false; | |
19 this.managedUsers_ = null; | |
20 }; | |
21 | |
22 cr.addSingletonGetter(ManagedUserListData); | |
23 | |
24 /** | |
25 * Resets to the initial state of no pending requests. | |
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. | |
38 * Each object is of the form: | |
39 * managedUser = { | |
40 * id: "Managed User ID", | |
41 * name: "Managed User Name", | |
42 * iconURL: "chrome://path/to/icon/image", | |
43 * onCurrentDevice: true or false, | |
44 * needAvatar: true or false | |
45 * } | |
46 */ | |
47 ManagedUserListData.receiveExistingManagedUsers = function(managedUsers) { | |
48 var instance = ManagedUserListData.getInstance(); | |
49 var i; | |
Bernhard Bauer
2014/01/09 13:39:08
Nit: You can inline the var into the for loop.
Adrian Kuegel
2014/01/09 14:12:42
Right. I am just not used to inline this into the
| |
50 for (i = 0; i < instance.callbacks_.length; i++) { | |
51 instance.callbacks_[i](managedUsers); | |
52 } | |
53 instance.managedUsers_ = managedUsers; | |
54 instance.reset_(); | |
55 }; | |
56 | |
57 /** | |
58 * 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 * message to the user. | |
61 */ | |
62 ManagedUserListData.onSigninError = function() { | |
63 var instance = ManagedUserListData.getInstance(); | |
64 var 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. | |
69 instance.managedUsers_ = null; | |
70 instance.reset_(); | |
71 }; | |
72 | |
73 /** | |
74 * Handles the request for the list of existing managed users. If the data is | |
75 * already available, it will call |callback| immediately. Otherwise, it | |
76 * retrieves the list of existing managed users which is then processed in | |
77 * receiveExistingManagedUsers(). | |
78 * @param {Object} callback The callback function which is called on success. | |
79 * @param {Object} errback the callback function which is called on error. | |
Bernhard Bauer
2014/01/09 13:39:08
It might be nice to do the whole thing with DOM fu
Adrian Kuegel
2014/01/09 14:12:42
I already have the caching in the code. I didn't k
Bernhard Bauer
2014/01/09 14:19:27
We can still do that; just throw away the future.
Adrian Kuegel
2014/01/09 15:34:17
From what I have read so far, it looks like it got
Bernhard Bauer
2014/01/09 16:25:11
Promises are available from Chrome 32 on, accordin
| |
80 */ | |
81 ManagedUserListData.requestExistingManagedUsers = function(callback, | |
82 errback) { | |
83 var instance = ManagedUserListData.getInstance(); | |
Bernhard Bauer
2014/01/09 13:39:08
The usual way to do this is to make this a private
Adrian Kuegel
2014/01/09 14:12:42
Ok, I didn't know this is a required pattern. I ca
Bernhard Bauer
2014/01/09 14:19:27
It's not required, but this is how it is usually d
| |
84 instance.callbacks_.push(callback); | |
85 instance.errbacks_.push(errback); | |
86 if (!instance.requestInProgress_) { | |
87 if (instance.managedUsers_ != null) { | |
88 ManagedUserListData.receiveExistingManagedUsers(instance.managedUsers_); | |
Bernhard Bauer
2014/01/09 13:39:08
Note that this will not only call |callback|, but
Adrian Kuegel
2014/01/09 14:12:42
I think that if |requestInProgress_| is false, the
| |
89 return; | |
90 } | |
91 instance.requestInProgress_ = true; | |
92 chrome.send('requestManagedUserImportUpdate'); | |
93 } | |
94 }; | |
95 | |
96 /** | |
97 * 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 | |
99 * deleted. | |
100 */ | |
101 ManagedUserListData.reloadExistingManagedUsers = function() { | |
102 var instance = ManagedUserListData.getInstance(); | |
103 if (instance.requestInProgress_) | |
104 return; | |
105 | |
106 instance.managedUsers_ = null; | |
107 instance.requestInProgress_ = true; | |
108 chrome.send('requestManagedUserImportUpdate'); | |
109 }; | |
110 | |
111 // Export | |
112 return { | |
113 ManagedUserListData: ManagedUserListData, | |
114 }; | |
115 }); | |
OLD | NEW |