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