Add new certificateProvider extension API.
[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}
13    */
14   function InvitationStore(userInfo) {
15     cr.EventTarget.call(this);
17     /**
18      * User information repository.
19      * @private {!print_preview.UserInfo}
20      */
21     this.userInfo_ = userInfo;
23     /**
24      * Maps user account to the list of invitations for this account.
25      * @private {!Object<!Array<!print_preview.Invitation>>}
26      */
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_>}
33      */
34     this.loadStatus_ = {};
36     /**
37      * Event tracker used to track event listeners of the destination store.
38      * @private {!EventTracker}
39      */
40     this.tracker_ = new EventTracker();
42     /**
43      * Used to fetch and process invitations.
44      * @private {print_preview.CloudPrintInterface}
45      */
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}
52      */
53     this.invitationInProgress_ = null;
54   };
56   /**
57    * Event types dispatched by the data store.
58    * @enum {string}
59    */
60   InvitationStore.EventType = {
61     INVITATION_PROCESSED:
62         'print_preview.InvitationStore.INVITATION_PROCESSED',
63     INVITATION_SEARCH_DONE:
64         'print_preview.InvitationStore.INVITATION_SEARCH_DONE'
65   };
67   /**
68    * @enum {number}
69    * @private
70    */
71   InvitationStore.LoadStatus_ = {
72     IN_PROGRESS: 1,
73     DONE: 2,
74     FAILED: 3
75   };
77   InvitationStore.prototype = {
78     __proto__: cr.EventTarget.prototype,
80     /**
81      * @return {print_preview.Invitation} Currently processed invitation or
82      *     {@code null}.
83      */
84     get invitationInProgress() {
85       return this.invitationInProgress_;
86     },
88     /**
89      * @param {string} account Account to filter invitations by.
90      * @return {!Array<!print_preview.Invitation>} List of invitations for the
91      *     {@code account}.
92      */
93     invitations: function(account) {
94       return this.invitations_[account] || [];
95     },
97     /**
98      * Sets the invitation store's Google Cloud Print interface.
99      * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface
100      *     to set.
101      */
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));
120     },
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);
133         }
134         return;
135       }
137       this.loadStatus_[this.userInfo_.activeUser] =
138           InvitationStore.LoadStatus_.IN_PROGRESS;
139       this.cloudPrintInterface_.invites(this.userInfo_.activeUser);
140     },
142     /**
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.
146      */
147     processInvitation: function(invitation, accept) {
148       if (!!this.invitationInProgress_)
149         return;
150       this.invitationInProgress_ = invitation;
151       this.cloudPrintInterface_.processInvite(invitation, accept);
152     },
154     /**
155      * Removes processed invitation from the internal storage.
156      * @param {!print_preview.Invitation} invitation Processed invitation.
157      * @private
158      */
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;
164             });
165       }
166       if (this.invitationInProgress_ == invitation)
167         this.invitationInProgress_ = null;
168     },
170     /**
171      * Called when printer sharing invitations are fetched.
172      * @param {Event} event Contains the list of invitations.
173      * @private
174      */
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);
181     },
183     /**
184      * Called when printer sharing invitations fetch has failed.
185      * @param {Event} event Contains the reason of failure.
186      * @private
187      */
188     onCloudPrintInvitesFailed_: function(event) {
189       this.loadStatus_[event.user] = InvitationStore.LoadStatus_.FAILED;
190     },
192     /**
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
197      */
198     onCloudPrintProcessInviteDone_: function(event) {
199       this.invitationProcessed_(event.invitation);
200       cr.dispatchSimpleEvent(
201           this, InvitationStore.EventType.INVITATION_PROCESSED);
202     },
204     /**
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
210      */
211     onCloudPrintProcessInviteFailed_: function(event) {
212       this.invitationProcessed_(event.invitation);
213       // TODO: Display an error.
214       cr.dispatchSimpleEvent(
215           this, InvitationStore.EventType.INVITATION_PROCESSED);
216     }
217   };
219   // Export
220   return {
221     InvitationStore: InvitationStore
222   };