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.exportPath('print_preview'); | |
6 | |
7 /** | |
8 * @enum {number} | |
9 */ | |
10 print_preview.InvitationStoreLoadStatus = { | |
dpapad
2017/05/05 17:32:50
This enum was marked as private before. Can we sti
rbpotter
2017/05/05 18:25:46
Done.
| |
11 IN_PROGRESS: 1, | |
12 DONE: 2, | |
13 FAILED: 3 | |
14 }; | |
15 | |
5 cr.define('print_preview', function() { | 16 cr.define('print_preview', function() { |
6 'use strict'; | 17 'use strict'; |
7 | 18 |
8 /** | 19 /** |
9 * Printer sharing invitations data store. | 20 * Printer sharing invitations data store. |
10 * @param {!print_preview.UserInfo} userInfo User information repository. | 21 * @param {!print_preview.UserInfo} userInfo User information repository. |
11 * @constructor | 22 * @constructor |
12 * @extends {cr.EventTarget} | 23 * @extends {cr.EventTarget} |
13 */ | 24 */ |
14 function InvitationStore(userInfo) { | 25 function InvitationStore(userInfo) { |
15 cr.EventTarget.call(this); | 26 cr.EventTarget.call(this); |
16 | 27 |
17 /** | 28 /** |
18 * User information repository. | 29 * User information repository. |
19 * @private {!print_preview.UserInfo} | 30 * @private {!print_preview.UserInfo} |
20 */ | 31 */ |
21 this.userInfo_ = userInfo; | 32 this.userInfo_ = userInfo; |
22 | 33 |
23 /** | 34 /** |
24 * Maps user account to the list of invitations for this account. | 35 * Maps user account to the list of invitations for this account. |
25 * @private {!Object<!Array<!print_preview.Invitation>>} | 36 * @private {!Object<!Array<!print_preview.Invitation>>} |
26 */ | 37 */ |
27 this.invitations_ = {}; | 38 this.invitations_ = {}; |
28 | 39 |
29 /** | 40 /** |
30 * Maps user account to the flag whether the invitations for this account | 41 * Maps user account to the flag whether the invitations for this account |
31 * were successfully loaded. | 42 * were successfully loaded. |
32 * @private {!Object<print_preview.InvitationStore.LoadStatus_>} | 43 * @private {!Object<print_preview.InvitationStoreLoadStatus>} |
33 */ | 44 */ |
34 this.loadStatus_ = {}; | 45 this.loadStatus_ = {}; |
35 | 46 |
36 /** | 47 /** |
37 * Event tracker used to track event listeners of the destination store. | 48 * Event tracker used to track event listeners of the destination store. |
38 * @private {!EventTracker} | 49 * @private {!EventTracker} |
39 */ | 50 */ |
40 this.tracker_ = new EventTracker(); | 51 this.tracker_ = new EventTracker(); |
41 | 52 |
42 /** | 53 /** |
43 * Used to fetch and process invitations. | 54 * Used to fetch and process invitations. |
44 * @private {print_preview.CloudPrintInterface} | 55 * @private {cloudprint.CloudPrintInterface} |
45 */ | 56 */ |
46 this.cloudPrintInterface_ = null; | 57 this.cloudPrintInterface_ = null; |
47 | 58 |
48 /** | 59 /** |
49 * Invitation being processed now. Only one invitation can be processed at | 60 * Invitation being processed now. Only one invitation can be processed at |
50 * a time. | 61 * a time. |
51 * @private {print_preview.Invitation} | 62 * @private {print_preview.Invitation} |
52 */ | 63 */ |
53 this.invitationInProgress_ = null; | 64 this.invitationInProgress_ = null; |
54 }; | 65 } |
55 | 66 |
56 /** | 67 /** |
57 * Event types dispatched by the data store. | 68 * Event types dispatched by the data store. |
58 * @enum {string} | 69 * @enum {string} |
59 */ | 70 */ |
60 InvitationStore.EventType = { | 71 InvitationStore.EventType = { |
61 INVITATION_PROCESSED: | 72 INVITATION_PROCESSED: |
62 'print_preview.InvitationStore.INVITATION_PROCESSED', | 73 'print_preview.InvitationStore.INVITATION_PROCESSED', |
63 INVITATION_SEARCH_DONE: | 74 INVITATION_SEARCH_DONE: |
64 'print_preview.InvitationStore.INVITATION_SEARCH_DONE' | 75 'print_preview.InvitationStore.INVITATION_SEARCH_DONE' |
65 }; | 76 }; |
66 | 77 |
67 /** | |
68 * @enum {number} | |
69 * @private | |
70 */ | |
71 InvitationStore.LoadStatus_ = { | |
72 IN_PROGRESS: 1, | |
73 DONE: 2, | |
74 FAILED: 3 | |
75 }; | |
76 | |
77 InvitationStore.prototype = { | 78 InvitationStore.prototype = { |
78 __proto__: cr.EventTarget.prototype, | 79 __proto__: cr.EventTarget.prototype, |
79 | 80 |
80 /** | 81 /** |
81 * @return {print_preview.Invitation} Currently processed invitation or | 82 * @return {print_preview.Invitation} Currently processed invitation or |
82 * {@code null}. | 83 * {@code null}. |
83 */ | 84 */ |
84 get invitationInProgress() { | 85 get invitationInProgress() { |
85 return this.invitationInProgress_; | 86 return this.invitationInProgress_; |
86 }, | 87 }, |
87 | 88 |
88 /** | 89 /** |
89 * @param {string} account Account to filter invitations by. | 90 * @param {string} account Account to filter invitations by. |
90 * @return {!Array<!print_preview.Invitation>} List of invitations for the | 91 * @return {!Array<!print_preview.Invitation>} List of invitations for the |
91 * {@code account}. | 92 * {@code account}. |
92 */ | 93 */ |
93 invitations: function(account) { | 94 invitations: function(account) { |
94 return this.invitations_[account] || []; | 95 return this.invitations_[account] || []; |
95 }, | 96 }, |
96 | 97 |
97 /** | 98 /** |
98 * Sets the invitation store's Google Cloud Print interface. | 99 * Sets the invitation store's Google Cloud Print interface. |
99 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface | 100 * @param {!cloudprint.CloudPrintInterface} cloudPrintInterface Interface |
100 * to set. | 101 * to set. |
101 */ | 102 */ |
102 setCloudPrintInterface: function(cloudPrintInterface) { | 103 setCloudPrintInterface: function(cloudPrintInterface) { |
103 this.cloudPrintInterface_ = cloudPrintInterface; | 104 this.cloudPrintInterface_ = cloudPrintInterface; |
104 this.tracker_.add( | 105 this.tracker_.add( |
105 this.cloudPrintInterface_, | 106 this.cloudPrintInterface_, |
106 cloudprint.CloudPrintInterface.EventType.INVITES_DONE, | 107 cloudprint.CloudPrintInterface.EventType.INVITES_DONE, |
107 this.onCloudPrintInvitesDone_.bind(this)); | 108 this.onCloudPrintInvitesDone_.bind(this)); |
108 this.tracker_.add( | 109 this.tracker_.add( |
109 this.cloudPrintInterface_, | 110 this.cloudPrintInterface_, |
(...skipping 10 matching lines...) Expand all Loading... | |
120 }, | 121 }, |
121 | 122 |
122 /** Initiates loading of cloud printer sharing invitations. */ | 123 /** Initiates loading of cloud printer sharing invitations. */ |
123 startLoadingInvitations: function() { | 124 startLoadingInvitations: function() { |
124 if (!this.cloudPrintInterface_) | 125 if (!this.cloudPrintInterface_) |
125 return; | 126 return; |
126 if (!this.userInfo_.activeUser) | 127 if (!this.userInfo_.activeUser) |
127 return; | 128 return; |
128 if (this.loadStatus_.hasOwnProperty(this.userInfo_.activeUser)) { | 129 if (this.loadStatus_.hasOwnProperty(this.userInfo_.activeUser)) { |
129 if (this.loadStatus_[this.userInfo_.activeUser] == | 130 if (this.loadStatus_[this.userInfo_.activeUser] == |
130 InvitationStore.LoadStatus_.DONE) { | 131 print_preview.InvitationStoreLoadStatus.DONE) { |
131 cr.dispatchSimpleEvent( | 132 cr.dispatchSimpleEvent( |
132 this, InvitationStore.EventType.INVITATION_SEARCH_DONE); | 133 this, InvitationStore.EventType.INVITATION_SEARCH_DONE); |
133 } | 134 } |
134 return; | 135 return; |
135 } | 136 } |
136 | 137 |
137 this.loadStatus_[this.userInfo_.activeUser] = | 138 this.loadStatus_[this.userInfo_.activeUser] = |
138 InvitationStore.LoadStatus_.IN_PROGRESS; | 139 print_preview.InvitationStoreLoadStatus.IN_PROGRESS; |
139 this.cloudPrintInterface_.invites(this.userInfo_.activeUser); | 140 this.cloudPrintInterface_.invites(this.userInfo_.activeUser); |
140 }, | 141 }, |
141 | 142 |
142 /** | 143 /** |
143 * Accepts or rejects the {@code invitation}, based on {@code accept} value. | 144 * Accepts or rejects the {@code invitation}, based on {@code accept} value. |
144 * @param {!print_preview.Invitation} invitation Invitation to process. | 145 * @param {!print_preview.Invitation} invitation Invitation to process. |
145 * @param {boolean} accept Whether to accept this invitation. | 146 * @param {boolean} accept Whether to accept this invitation. |
146 */ | 147 */ |
147 processInvitation: function(invitation, accept) { | 148 processInvitation: function(invitation, accept) { |
148 if (!!this.invitationInProgress_) | 149 if (this.invitationInProgress_) |
149 return; | 150 return; |
150 this.invitationInProgress_ = invitation; | 151 this.invitationInProgress_ = invitation; |
151 this.cloudPrintInterface_.processInvite(invitation, accept); | 152 this.cloudPrintInterface_.processInvite(invitation, accept); |
152 }, | 153 }, |
153 | 154 |
154 /** | 155 /** |
155 * Removes processed invitation from the internal storage. | 156 * Removes processed invitation from the internal storage. |
156 * @param {!print_preview.Invitation} invitation Processed invitation. | 157 * @param {!print_preview.Invitation} invitation Processed invitation. |
157 * @private | 158 * @private |
158 */ | 159 */ |
159 invitationProcessed_: function(invitation) { | 160 invitationProcessed_: function(invitation) { |
160 if (this.invitations_.hasOwnProperty(invitation.account)) { | 161 if (this.invitations_.hasOwnProperty(invitation.account)) { |
161 this.invitations_[invitation.account] = | 162 this.invitations_[invitation.account] = |
162 this.invitations_[invitation.account].filter(function(i) { | 163 this.invitations_[invitation.account].filter(function(i) { |
163 return i != invitation; | 164 return i != invitation; |
164 }); | 165 }); |
165 } | 166 } |
166 if (this.invitationInProgress_ == invitation) | 167 if (this.invitationInProgress_ == invitation) |
167 this.invitationInProgress_ = null; | 168 this.invitationInProgress_ = null; |
168 }, | 169 }, |
169 | 170 |
170 /** | 171 /** |
171 * Called when printer sharing invitations are fetched. | 172 * Called when printer sharing invitations are fetched. |
172 * @param {Event} event Contains the list of invitations. | 173 * @param {Event} event Contains the list of invitations. |
173 * @private | 174 * @private |
174 */ | 175 */ |
175 onCloudPrintInvitesDone_: function(event) { | 176 onCloudPrintInvitesDone_: function(event) { |
176 this.loadStatus_[event.user] = InvitationStore.LoadStatus_.DONE; | 177 this.loadStatus_[event.user] = |
178 print_preview.InvitationStoreLoadStatus.DONE; | |
177 this.invitations_[event.user] = event.invitations; | 179 this.invitations_[event.user] = event.invitations; |
178 | 180 |
179 cr.dispatchSimpleEvent( | 181 cr.dispatchSimpleEvent( |
180 this, InvitationStore.EventType.INVITATION_SEARCH_DONE); | 182 this, InvitationStore.EventType.INVITATION_SEARCH_DONE); |
181 }, | 183 }, |
182 | 184 |
183 /** | 185 /** |
184 * Called when printer sharing invitations fetch has failed. | 186 * Called when printer sharing invitations fetch has failed. |
185 * @param {Event} event Contains the reason of failure. | 187 * @param {Event} event Contains the reason of failure. |
186 * @private | 188 * @private |
187 */ | 189 */ |
188 onCloudPrintInvitesFailed_: function(event) { | 190 onCloudPrintInvitesFailed_: function(event) { |
189 this.loadStatus_[event.user] = InvitationStore.LoadStatus_.FAILED; | 191 this.loadStatus_[event.user] = |
192 print_preview.InvitationStoreLoadStatus.FAILED; | |
190 }, | 193 }, |
191 | 194 |
192 /** | 195 /** |
193 * Called when printer sharing invitation was processed successfully. | 196 * Called when printer sharing invitation was processed successfully. |
194 * @param {Event} event Contains detailed information about the invite and | 197 * @param {Event} event Contains detailed information about the invite and |
195 * newly accepted destination. | 198 * newly accepted destination. |
196 * @private | 199 * @private |
197 */ | 200 */ |
198 onCloudPrintProcessInviteDone_: function(event) { | 201 onCloudPrintProcessInviteDone_: function(event) { |
199 this.invitationProcessed_(event.invitation); | 202 this.invitationProcessed_(event.invitation); |
(...skipping 14 matching lines...) Expand all Loading... | |
214 cr.dispatchSimpleEvent( | 217 cr.dispatchSimpleEvent( |
215 this, InvitationStore.EventType.INVITATION_PROCESSED); | 218 this, InvitationStore.EventType.INVITATION_PROCESSED); |
216 } | 219 } |
217 }; | 220 }; |
218 | 221 |
219 // Export | 222 // Export |
220 return { | 223 return { |
221 InvitationStore: InvitationStore | 224 InvitationStore: InvitationStore |
222 }; | 225 }; |
223 }); | 226 }); |
OLD | NEW |