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