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
3 // found in the LICENSE file.
5 cr
.define('print_preview', function() {
9 * Printer sharing invitations data store.
10 * @param {!print_preview.UserInfo} userInfo User information repository.
12 * @extends {cr.EventTarget}
14 function InvitationStore(userInfo
) {
15 cr
.EventTarget
.call(this);
18 * User information repository.
19 * @private {!print_preview.UserInfo}
21 this.userInfo_
= userInfo
;
24 * Maps user account to the list of invitations for this account.
25 * @private {!Object<!Array<!print_preview.Invitation>>}
27 this.invitations_
= {};
30 * Maps user account to the flag whether the invitations for this account
31 * were successfully loaded.
32 * @private {!Object<print_preview.InvitationStore.LoadStatus_>}
34 this.loadStatus_
= {};
37 * Event tracker used to track event listeners of the destination store.
38 * @private {!EventTracker}
40 this.tracker_
= new EventTracker();
43 * Used to fetch and process invitations.
44 * @private {print_preview.CloudPrintInterface}
46 this.cloudPrintInterface_
= null;
49 * Invitation being processed now. Only one invitation can be processed at
51 * @private {print_preview.Invitation}
53 this.invitationInProgress_
= null;
57 * Event types dispatched by the data store.
60 InvitationStore
.EventType
= {
62 'print_preview.InvitationStore.INVITATION_PROCESSED',
63 INVITATION_SEARCH_DONE
:
64 'print_preview.InvitationStore.INVITATION_SEARCH_DONE'
71 InvitationStore
.LoadStatus_
= {
77 InvitationStore
.prototype = {
78 __proto__
: cr
.EventTarget
.prototype,
81 * @return {print_preview.Invitation} Currently processed invitation or
84 get invitationInProgress() {
85 return this.invitationInProgress_
;
89 * @param {string} account Account to filter invitations by.
90 * @return {!Array<!print_preview.Invitation>} List of invitations for the
93 invitations: function(account
) {
94 return this.invitations_
[account
] || [];
98 * Sets the invitation store's Google Cloud Print interface.
99 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface
102 setCloudPrintInterface: function(cloudPrintInterface
) {
103 this.cloudPrintInterface_
= cloudPrintInterface
;
105 this.cloudPrintInterface_
,
106 cloudprint
.CloudPrintInterface
.EventType
.INVITES_DONE
,
107 this.onCloudPrintInvitesDone_
.bind(this));
109 this.cloudPrintInterface_
,
110 cloudprint
.CloudPrintInterface
.EventType
.INVITES_FAILED
,
111 this.onCloudPrintInvitesDone_
.bind(this));
113 this.cloudPrintInterface_
,
114 cloudprint
.CloudPrintInterface
.EventType
.PROCESS_INVITE_DONE
,
115 this.onCloudPrintProcessInviteDone_
.bind(this));
117 this.cloudPrintInterface_
,
118 cloudprint
.CloudPrintInterface
.EventType
.PROCESS_INVITE_FAILED
,
119 this.onCloudPrintProcessInviteFailed_
.bind(this));
122 /** Initiates loading of cloud printer sharing invitations. */
123 startLoadingInvitations: function() {
124 if (!this.cloudPrintInterface_
)
126 if (!this.userInfo_
.activeUser
)
128 if (this.loadStatus_
.hasOwnProperty(this.userInfo_
.activeUser
)) {
129 if (this.loadStatus_
[this.userInfo_
.activeUser
] ==
130 InvitationStore
.LoadStatus_
.DONE
) {
131 cr
.dispatchSimpleEvent(
132 this, InvitationStore
.EventType
.INVITATION_SEARCH_DONE
);
137 this.loadStatus_
[this.userInfo_
.activeUser
] =
138 InvitationStore
.LoadStatus_
.IN_PROGRESS
;
139 this.cloudPrintInterface_
.invites(this.userInfo_
.activeUser
);
143 * Accepts or rejects the {@code invitation}, based on {@code accept} value.
144 * @param {!print_preview.Invitation} invitation Invitation to process.
145 * @param {boolean} accept Whether to accept this invitation.
147 processInvitation: function(invitation
, accept
) {
148 if (!!this.invitationInProgress_
)
150 this.invitationInProgress_
= invitation
;
151 this.cloudPrintInterface_
.processInvite(invitation
, accept
);
155 * Removes processed invitation from the internal storage.
156 * @param {!print_preview.Invitation} invitation Processed invitation.
159 invitationProcessed_: function(invitation
) {
160 if (this.invitations_
.hasOwnProperty(invitation
.account
)) {
161 this.invitations_
[invitation
.account
] =
162 this.invitations_
[invitation
.account
].filter(function(i
) {
163 return i
!= invitation
;
166 if (this.invitationInProgress_
== invitation
)
167 this.invitationInProgress_
= null;
171 * Called when printer sharing invitations are fetched.
172 * @param {Event} event Contains the list of invitations.
175 onCloudPrintInvitesDone_: function(event
) {
176 this.loadStatus_
[event
.user
] = InvitationStore
.LoadStatus_
.DONE
;
177 this.invitations_
[event
.user
] = event
.invitations
;
179 cr
.dispatchSimpleEvent(
180 this, InvitationStore
.EventType
.INVITATION_SEARCH_DONE
);
184 * Called when printer sharing invitations fetch has failed.
185 * @param {Event} event Contains the reason of failure.
188 onCloudPrintInvitesFailed_: function(event
) {
189 this.loadStatus_
[event
.user
] = InvitationStore
.LoadStatus_
.FAILED
;
193 * Called when printer sharing invitation was processed successfully.
194 * @param {Event} event Contains detailed information about the invite and
195 * newly accepted destination.
198 onCloudPrintProcessInviteDone_: function(event
) {
199 this.invitationProcessed_(event
.invitation
);
200 cr
.dispatchSimpleEvent(
201 this, InvitationStore
.EventType
.INVITATION_PROCESSED
);
205 * Called when /printer call completes. Updates the specified destination's
206 * print capabilities.
207 * @param {Event} event Contains detailed information about the
211 onCloudPrintProcessInviteFailed_: function(event
) {
212 this.invitationProcessed_(event
.invitation
);
213 // TODO: Display an error.
214 cr
.dispatchSimpleEvent(
215 this, InvitationStore
.EventType
.INVITATION_PROCESSED
);
221 InvitationStore
: InvitationStore