[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / invitation_store.js
blobaeebd25658ef2e1233a23b3427d449dbd66437a3
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() {
6 'use strict';
8 /**
9 * Printer sharing invitations data store.
10 * @param {!print_preview.UserInfo} userInfo User information repository.
11 * @constructor
12 * @extends {cr.EventTarget}
14 function InvitationStore(userInfo) {
15 cr.EventTarget.call(this);
17 /**
18 * User information repository.
19 * @private {!print_preview.UserInfo}
21 this.userInfo_ = userInfo;
23 /**
24 * Maps user account to the list of invitations for this account.
25 * @private {!Object<!Array<!print_preview.Invitation>>}
27 this.invitations_ = {};
29 /**
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_ = {};
36 /**
37 * Event tracker used to track event listeners of the destination store.
38 * @private {!EventTracker}
40 this.tracker_ = new EventTracker();
42 /**
43 * Used to fetch and process invitations.
44 * @private {print_preview.CloudPrintInterface}
46 this.cloudPrintInterface_ = null;
48 /**
49 * Invitation being processed now. Only one invitation can be processed at
50 * a time.
51 * @private {print_preview.Invitation}
53 this.invitationInProgress_ = null;
56 /**
57 * Event types dispatched by the data store.
58 * @enum {string}
60 InvitationStore.EventType = {
61 INVITATION_PROCESSED:
62 'print_preview.InvitationStore.INVITATION_PROCESSED',
63 INVITATION_SEARCH_DONE:
64 'print_preview.InvitationStore.INVITATION_SEARCH_DONE'
67 /**
68 * @enum {number}
69 * @private
71 InvitationStore.LoadStatus_ = {
72 IN_PROGRESS: 1,
73 DONE: 2,
74 FAILED: 3
77 InvitationStore.prototype = {
78 __proto__: cr.EventTarget.prototype,
80 /**
81 * @return {print_preview.Invitation} Currently processed invitation or
82 * {@code null}.
84 get invitationInProgress() {
85 return this.invitationInProgress_;
88 /**
89 * @param {string} account Account to filter invitations by.
90 * @return {!Array<!print_preview.Invitation>} List of invitations for the
91 * {@code account}.
93 invitations: function(account) {
94 return this.invitations_[account] || [];
97 /**
98 * Sets the invitation store's Google Cloud Print interface.
99 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface
100 * to set.
102 setCloudPrintInterface: function(cloudPrintInterface) {
103 this.cloudPrintInterface_ = cloudPrintInterface;
104 this.tracker_.add(
105 this.cloudPrintInterface_,
106 cloudprint.CloudPrintInterface.EventType.INVITES_DONE,
107 this.onCloudPrintInvitesDone_.bind(this));
108 this.tracker_.add(
109 this.cloudPrintInterface_,
110 cloudprint.CloudPrintInterface.EventType.INVITES_FAILED,
111 this.onCloudPrintInvitesDone_.bind(this));
112 this.tracker_.add(
113 this.cloudPrintInterface_,
114 cloudprint.CloudPrintInterface.EventType.PROCESS_INVITE_DONE,
115 this.onCloudPrintProcessInviteDone_.bind(this));
116 this.tracker_.add(
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_)
125 return;
126 if (!this.userInfo_.activeUser)
127 return;
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);
134 return;
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_)
149 return;
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.
157 * @private
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.
173 * @private
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.
186 * @private
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.
196 * @private
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
208 * destination.
209 * @private
211 onCloudPrintProcessInviteFailed_: function(event) {
212 this.invitationProcessed_(event.invitation);
213 // TODO: Display an error.
214 cr.dispatchSimpleEvent(
215 this, InvitationStore.EventType.INVITATION_PROCESSED);
219 // Export
220 return {
221 InvitationStore: InvitationStore