- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / destination.js
1 // Copyright (c) 2012 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.
4
5 cr.define('print_preview', function() {
6   'use strict';
7
8   /**
9    * Print destination data object that holds data for both local and cloud
10    * destinations.
11    * @param {string} id ID of the destination.
12    * @param {!print_preview.Destination.Type} type Type of the destination.
13    * @param {!print_preview.Destination.Origin} origin Origin of the
14    *     destination.
15    * @param {string} displayName Display name of the destination.
16    * @param {boolean} isRecent Whether the destination has been used recently.
17    * @param {!print_preview.Destination.ConnectionStatus} connectionStatus
18    *     Connection status of the print destination.
19    * @param {{tags: Array.<string>,
20    *          isOwned: ?boolean,
21    *          lastAccessTime: ?number,
22    *          isTosAccepted: ?boolean}=} opt_params Optional parameters for the
23    *     destination.
24    * @constructor
25    */
26   function Destination(id, type, origin, displayName, isRecent,
27                        connectionStatus, opt_params) {
28     /**
29      * ID of the destination.
30      * @type {string}
31      * @private
32      */
33     this.id_ = id;
34
35     /**
36      * Type of the destination.
37      * @type {!print_preview.Destination.Type}
38      * @private
39      */
40     this.type_ = type;
41
42     /**
43      * Origin of the destination.
44      * @type {!print_preview.Destination.Origin}
45      * @private
46      */
47     this.origin_ = origin;
48
49     /**
50      * Display name of the destination.
51      * @type {string}
52      * @private
53      */
54     this.displayName_ = displayName;
55
56     /**
57      * Whether the destination has been used recently.
58      * @type {boolean}
59      * @private
60      */
61     this.isRecent_ = isRecent;
62
63     /**
64      * Tags associated with the destination.
65      * @type {!Array.<string>}
66      * @private
67      */
68     this.tags_ = (opt_params && opt_params.tags) || [];
69
70     /**
71      * Print capabilities of the destination.
72      * @type {print_preview.Cdd}
73      * @private
74      */
75     this.capabilities_ = null;
76
77     /**
78      * Whether the destination is owned by the user.
79      * @type {boolean}
80      * @private
81      */
82     this.isOwned_ = (opt_params && opt_params.isOwned) || false;
83
84     /**
85      * Cache of destination location fetched from tags.
86      * @type {?string}
87      * @private
88      */
89     this.location_ = null;
90
91     /**
92      * Connection status of the destination.
93      * @type {!print_preview.Destination.ConnectionStatus}
94      * @private
95      */
96     this.connectionStatus_ = connectionStatus;
97
98     /**
99      * Number of milliseconds since the epoch when the printer was last
100      * accessed.
101      * @type {number}
102      * @private
103      */
104     this.lastAccessTime_ = (opt_params && opt_params.lastAccessTime) ||
105                            Date.now();
106
107     /**
108      * Whether the user has accepted the terms-of-service for the print
109      * destination. Only applies to the FedEx Office cloud-based printer.
110      * {@code} null if terms-of-service does not apply to the print destination.
111      * @type {?boolean}
112      * @private
113      */
114     this.isTosAccepted_ = (opt_params && opt_params.isTosAccepted) || false;
115   };
116
117   /**
118    * Prefix of the location destination tag.
119    * @type {string}
120    * @const
121    */
122   Destination.LOCATION_TAG_PREFIX = '__cp__printer-location=';
123
124   /**
125    * Enumeration of Google-promoted destination IDs.
126    * @enum {string}
127    */
128   Destination.GooglePromotedId = {
129     DOCS: '__google__docs',
130     FEDEX: '__google__fedex',
131     SAVE_AS_PDF: 'Save as PDF'
132   };
133
134   /**
135    * Enumeration of the types of destinations.
136    * @enum {string}
137    */
138   Destination.Type = {
139     GOOGLE: 'google',
140     LOCAL: 'local',
141     MOBILE: 'mobile'
142   };
143
144   /**
145    * Enumeration of the origin types for cloud destinations.
146    * @enum {string}
147    */
148   Destination.Origin = {
149     LOCAL: 'local',
150     COOKIES: 'cookies',
151     PROFILE: 'profile',
152     DEVICE: 'device'
153   };
154
155   /**
156    * Enumeration of the connection statuses of printer destinations.
157    * @enum {string}
158    */
159   Destination.ConnectionStatus = {
160     DORMANT: 'DORMANT',
161     OFFLINE: 'OFFLINE',
162     ONLINE: 'ONLINE',
163     UNKNOWN: 'UNKNOWN'
164   };
165
166   /**
167    * Enumeration of relative icon URLs for various types of destinations.
168    * @enum {string}
169    * @private
170    */
171   Destination.IconUrl_ = {
172     CLOUD: 'images/printer.png',
173     CLOUD_SHARED: 'images/printer_shared.png',
174     LOCAL: 'images/printer.png',
175     MOBILE: 'images/mobile.png',
176     MOBILE_SHARED: 'images/mobile_shared.png',
177     THIRD_PARTY: 'images/third_party.png',
178     PDF: 'images/pdf.png',
179     DOCS: 'images/google_doc.png',
180     FEDEX: 'images/third_party_fedex.png'
181   };
182
183   Destination.prototype = {
184     /** @return {string} ID of the destination. */
185     get id() {
186       return this.id_;
187     },
188
189     /** @return {!print_preview.Destination.Type} Type of the destination. */
190     get type() {
191       return this.type_;
192     },
193
194     /**
195      * @return {!print_preview.Destination.Origin} Origin of the destination.
196      */
197     get origin() {
198       return this.origin_;
199     },
200
201     /** @return {string} Display name of the destination. */
202     get displayName() {
203       return this.displayName_;
204     },
205
206     /** @return {boolean} Whether the destination has been used recently. */
207     get isRecent() {
208       return this.isRecent_;
209     },
210
211     /**
212      * @param {boolean} isRecent Whether the destination has been used recently.
213      */
214     set isRecent(isRecent) {
215       this.isRecent_ = isRecent;
216     },
217
218     /**
219      * @return {boolean} Whether the user owns the destination. Only applies to
220      *     cloud-based destinations.
221      */
222     get isOwned() {
223       return this.isOwned_;
224     },
225
226     /** @return {boolean} Whether the destination is local or cloud-based. */
227     get isLocal() {
228       return this.origin_ == Destination.Origin.LOCAL;
229     },
230
231     /**
232      * @return {string} The location of the destination, or an empty string if
233      *     the location is unknown.
234      */
235     get location() {
236       if (this.location_ == null) {
237         for (var tag, i = 0; tag = this.tags_[i]; i++) {
238           if (tag.indexOf(Destination.LOCATION_TAG_PREFIX) == 0) {
239             this.location_ = tag.substring(
240                 Destination.LOCATION_TAG_PREFIX.length) || '';
241             break;
242           }
243         }
244       }
245       return this.location_;
246     },
247
248     /** @return {!Array.<string>} Tags associated with the destination. */
249     get tags() {
250       return this.tags_.slice(0);
251     },
252
253     /** @return {print_preview.Cdd} Print capabilities of the destination. */
254     get capabilities() {
255       return this.capabilities_;
256     },
257
258     /**
259      * @param {!print_preview.Cdd} capabilities Print capabilities of the
260      *     destination.
261      */
262     set capabilities(capabilities) {
263       this.capabilities_ = capabilities;
264     },
265
266     /**
267      * @return {!print_preview.Destination.ConnectionStatus} Connection status
268      *     of the print destination.
269      */
270     get connectionStatus() {
271       return this.connectionStatus_;
272     },
273
274     /**
275      * @param {!print_preview.Destination.ConnectionStatus} status Connection
276      *     status of the print destination.
277      */
278     set connectionStatus(status) {
279       this.connectionStatus_ = status;
280     },
281
282     /**
283      * @return {number} Number of milliseconds since the epoch when the printer
284      *     was last accessed.
285      */
286     get lastAccessTime() {
287       return this.lastAccessTime_;
288     },
289
290     /** @return {string} Relative URL of the destination's icon. */
291     get iconUrl() {
292       if (this.id_ == Destination.GooglePromotedId.DOCS) {
293         return Destination.IconUrl_.DOCS;
294       } else if (this.id_ == Destination.GooglePromotedId.FEDEX) {
295         return Destination.IconUrl_.FEDEX;
296       } else if (this.id_ == Destination.GooglePromotedId.SAVE_AS_PDF) {
297         return Destination.IconUrl_.PDF;
298       } else if (this.isLocal) {
299         return Destination.IconUrl_.LOCAL;
300       } else if (this.type_ == Destination.Type.MOBILE && this.isOwned_) {
301         return Destination.IconUrl_.MOBILE;
302       } else if (this.type_ == Destination.Type.MOBILE) {
303         return Destination.IconUrl_.MOBILE_SHARED;
304       } else if (this.isOwned_) {
305         return Destination.IconUrl_.CLOUD;
306       } else {
307         return Destination.IconUrl_.CLOUD_SHARED;
308       }
309     },
310
311     /**
312      * @return {?boolean} Whether the user has accepted the terms-of-service of
313      *     the print destination or {@code null} if a terms-of-service does not
314      *     apply.
315      */
316     get isTosAccepted() {
317       return this.isTosAccepted_;
318     },
319
320     /**
321      * @param {?boolean} Whether the user has accepted the terms-of-service of
322      *     the print destination or {@code null} if a terms-of-service does not
323      *     apply.
324      */
325     set isTosAccepted(isTosAccepted) {
326       this.isTosAccepted_ = isTosAccepted;
327     },
328
329     /**
330      * Matches a query against the destination.
331      * @param {string} query Query to match against the destination.
332      * @return {boolean} {@code true} if the query matches this destination,
333      *     {@code false} otherwise.
334      */
335     matches: function(query) {
336       return this.displayName_.toLowerCase().indexOf(
337           query.toLowerCase().trim()) != -1;
338     }
339   };
340
341   /**
342    * The CDD (Cloud Device Description) describes the capabilities of a print
343    * destination.
344    *
345    * @typedef {{
346    *   version: string,
347    *   printer: {
348    *     vendor_capability: !Array.<{Object}>,
349    *     collate: {default: boolean=}=,
350    *     color: {
351    *       option: !Array.<{
352    *         type: string=,
353    *         vendor_id: string=,
354    *         custom_display_name: string=,
355    *         is_default: boolean=
356    *       }>
357    *     }=,
358    *     copies: {default: number=, max: number=}=,
359    *     duplex: {option: !Array.<{type: string=, is_default: boolean=}>}=,
360    *     page_orientation: {
361    *       option: !Array.<{type: string=, is_default: boolean=}>
362    *     }=
363    *   }
364    * }}
365    */
366   var Cdd = Object;
367
368   // Export
369   return {
370     Destination: Destination,
371     Cdd: Cdd
372   };
373 });