| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * A data store that stores destinations and dispatches events when the data | 9 * A data store that stores destinations and dispatches events when the data |
| 10 * store changes. | 10 * store changes. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 this.destinationMap_ = {}; | 62 this.destinationMap_ = {}; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Currently selected destination. | 65 * Currently selected destination. |
| 66 * @type {print_preview.Destination} | 66 * @type {print_preview.Destination} |
| 67 * @private | 67 * @private |
| 68 */ | 68 */ |
| 69 this.selectedDestination_ = null; | 69 this.selectedDestination_ = null; |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Initial destination ID used to auto-select the first inserted destination | |
| 73 * that matches. If {@code null}, the first destination inserted into the | |
| 74 * store will be selected. | |
| 75 * @type {?string} | |
| 76 * @private | |
| 77 */ | |
| 78 this.initialDestinationId_ = null; | |
| 79 | |
| 80 /** | |
| 81 * Initial origin used to auto-select destination. | |
| 82 * @type {print_preview.Destination.Origin} | |
| 83 * @private | |
| 84 */ | |
| 85 this.initialDestinationOrigin_ = print_preview.Destination.Origin.LOCAL; | |
| 86 | |
| 87 /** | |
| 88 * Whether the destination store will auto select the destination that | 72 * Whether the destination store will auto select the destination that |
| 89 * matches the initial destination. | 73 * matches the last used destination stored in appState_. |
| 90 * @type {boolean} | 74 * @type {boolean} |
| 91 * @private | 75 * @private |
| 92 */ | 76 */ |
| 93 this.isInAutoSelectMode_ = false; | 77 this.isInAutoSelectMode_ = false; |
| 94 | 78 |
| 95 /** | 79 /** |
| 96 * Event tracker used to track event listeners of the destination store. | 80 * Event tracker used to track event listeners of the destination store. |
| 97 * @type {!EventTracker} | 81 * @type {!EventTracker} |
| 98 * @private | 82 * @private |
| 99 */ | 83 */ |
| 100 this.tracker_ = new EventTracker(); | 84 this.tracker_ = new EventTracker(); |
| 101 | 85 |
| 102 /** | 86 /** |
| 103 * Used to fetch cloud-based print destinations. | 87 * Used to fetch cloud-based print destinations. |
| 104 * @type {print_preview.CloudPrintInterface} | 88 * @type {print_preview.CloudPrintInterface} |
| 105 * @private | 89 * @private |
| 106 */ | 90 */ |
| 107 this.cloudPrintInterface_ = null; | 91 this.cloudPrintInterface_ = null; |
| 108 | 92 |
| 109 /** | 93 /** |
| 110 * Whether the destination store has already loaded or is loading all cloud | 94 * Maps user account to the list of origins for which destinations are |
| 111 * destinations. | 95 * already loaded. |
| 112 * @type {boolean} | 96 * @type {!Object.<string, Array.<print_preview.Destination.Origin>>} |
| 113 * @private | 97 * @private |
| 114 */ | 98 */ |
| 115 this.hasLoadedAllCloudDestinations_ = false; | 99 this.loadedCloudOrigins_ = {}; |
| 116 | 100 |
| 117 /** | 101 /** |
| 118 * ID of a timeout after the initial destination ID is set. If no inserted | 102 * ID of a timeout after the initial destination ID is set. If no inserted |
| 119 * destination matches the initial destination ID after the specified | 103 * destination matches the initial destination ID after the specified |
| 120 * timeout, the first destination in the store will be automatically | 104 * timeout, the first destination in the store will be automatically |
| 121 * selected. | 105 * selected. |
| 122 * @type {?number} | 106 * @type {?number} |
| 123 * @private | 107 * @private |
| 124 */ | 108 */ |
| 125 this.autoSelectTimeout_ = null; | 109 this.autoSelectTimeout_ = null; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 color: { option: [{type: 'STANDARD_COLOR', is_default: true}] } | 219 color: { option: [{type: 'STANDARD_COLOR', is_default: true}] } |
| 236 } | 220 } |
| 237 }; | 221 }; |
| 238 return dest; | 222 return dest; |
| 239 }; | 223 }; |
| 240 | 224 |
| 241 DestinationStore.prototype = { | 225 DestinationStore.prototype = { |
| 242 __proto__: cr.EventTarget.prototype, | 226 __proto__: cr.EventTarget.prototype, |
| 243 | 227 |
| 244 /** | 228 /** |
| 245 * @return {!Array.<!print_preview.Destination>} List of destinations in | 229 * @param {string=} opt_account Account to filter destinations by. When |
| 246 * the store. | 230 * omitted, all destinations are returned. |
| 231 * @return {!Array.<!print_preview.Destination>} List of destinations |
| 232 * accessible by the {@code account}. |
| 247 */ | 233 */ |
| 248 get destinations() { | 234 destinations: function(opt_account) { |
| 249 return this.destinations_.slice(0); | 235 if (opt_account) { |
| 236 return this.destinations_.filter(function(destination) { |
| 237 return !destination.account || destination.account == opt_account; |
| 238 }); |
| 239 } else { |
| 240 return this.destinations_.slice(0); |
| 241 } |
| 250 }, | 242 }, |
| 251 | 243 |
| 252 /** | 244 /** |
| 253 * @return {print_preview.Destination} The currently selected destination or | 245 * @return {print_preview.Destination} The currently selected destination or |
| 254 * {@code null} if none is selected. | 246 * {@code null} if none is selected. |
| 255 */ | 247 */ |
| 256 get selectedDestination() { | 248 get selectedDestination() { |
| 257 return this.selectedDestination_; | 249 return this.selectedDestination_; |
| 258 }, | 250 }, |
| 259 | 251 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 274 }, | 266 }, |
| 275 | 267 |
| 276 /** | 268 /** |
| 277 * Initializes the destination store. Sets the initially selected | 269 * Initializes the destination store. Sets the initially selected |
| 278 * destination. If any inserted destinations match this ID, that destination | 270 * destination. If any inserted destinations match this ID, that destination |
| 279 * will be automatically selected. This method must be called after the | 271 * will be automatically selected. This method must be called after the |
| 280 * print_preview.AppState has been initialized. | 272 * print_preview.AppState has been initialized. |
| 281 * @private | 273 * @private |
| 282 */ | 274 */ |
| 283 init: function() { | 275 init: function() { |
| 284 if (this.appState_.selectedDestinationId && | |
| 285 this.appState_.selectedDestinationOrigin) { | |
| 286 this.initialDestinationId_ = this.appState_.selectedDestinationId; | |
| 287 this.initialDestinationOrigin_ = | |
| 288 this.appState_.selectedDestinationOrigin; | |
| 289 } | |
| 290 this.isInAutoSelectMode_ = true; | 276 this.isInAutoSelectMode_ = true; |
| 291 if (!this.initialDestinationId_ || !this.initialDestinationOrigin_) { | 277 if (!this.appState_.selectedDestinationId || |
| 278 !this.appState_.selectedDestinationOrigin) { |
| 292 this.onAutoSelectFailed_(); | 279 this.onAutoSelectFailed_(); |
| 293 } else { | 280 } else { |
| 294 var key = this.getDestinationKey_(this.initialDestinationOrigin_, | 281 var key = this.getDestinationKey_( |
| 295 this.initialDestinationId_); | 282 this.appState_.selectedDestinationOrigin, |
| 283 this.appState_.selectedDestinationId, |
| 284 this.appState_.selectedDestinationAccount); |
| 296 var candidate = this.destinationMap_[key]; | 285 var candidate = this.destinationMap_[key]; |
| 297 if (candidate != null) { | 286 if (candidate != null) { |
| 298 this.selectDestination(candidate); | 287 this.selectDestination(candidate); |
| 299 } else if (this.initialDestinationOrigin_ == | 288 } else if (this.appState_.selectedDestinationOrigin == |
| 300 print_preview.Destination.Origin.LOCAL) { | 289 print_preview.Destination.Origin.LOCAL) { |
| 301 this.nativeLayer_.startGetLocalDestinationCapabilities( | 290 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 302 this.initialDestinationId_); | 291 this.appState_.selectedDestinationId); |
| 303 } else if (this.cloudPrintInterface_ && | 292 } else if (this.cloudPrintInterface_ && |
| 304 (this.initialDestinationOrigin_ == | 293 (this.appState_.selectedDestinationOrigin == |
| 305 print_preview.Destination.Origin.COOKIES || | 294 print_preview.Destination.Origin.COOKIES || |
| 306 this.initialDestinationOrigin_ == | 295 this.appState_.selectedDestinationOrigin == |
| 307 print_preview.Destination.Origin.DEVICE)) { | 296 print_preview.Destination.Origin.DEVICE)) { |
| 308 this.cloudPrintInterface_.printer(this.initialDestinationId_, | 297 this.cloudPrintInterface_.printer( |
| 309 this.initialDestinationOrigin_); | 298 this.appState_.selectedDestinationId, |
| 310 } else if (this.initialDestinationOrigin_ == | 299 this.appState_.selectedDestinationOrigin, |
| 300 this.appState_.selectedDestinationAccount); |
| 301 } else if (this.appState_.selectedDestinationOrigin == |
| 311 print_preview.Destination.Origin.PRIVET) { | 302 print_preview.Destination.Origin.PRIVET) { |
| 312 // TODO(noamsml): Resolve a specific printer instead of listing all | 303 // TODO(noamsml): Resolve a specific printer instead of listing all |
| 313 // privet printers in this case. | 304 // privet printers in this case. |
| 314 this.nativeLayer_.startGetPrivetDestinations(); | 305 this.nativeLayer_.startGetPrivetDestinations(); |
| 315 | 306 |
| 316 var destinationName = this.appState_.selectedDestinationName || ''; | 307 var destinationName = this.appState_.selectedDestinationName || ''; |
| 317 | 308 |
| 318 // Create a fake selectedDestination_ that is not actually in the | 309 // Create a fake selectedDestination_ that is not actually in the |
| 319 // destination store. When the real destination is created, this | 310 // destination store. When the real destination is created, this |
| 320 // destination will be overwritten. | 311 // destination will be overwritten. |
| 321 this.selectedDestination_ = new print_preview.Destination( | 312 this.selectedDestination_ = new print_preview.Destination( |
| 322 this.initialDestinationId_, | 313 this.appState_.selectedDestinationId, |
| 323 print_preview.Destination.Type.LOCAL, | 314 print_preview.Destination.Type.LOCAL, |
| 324 print_preview.Destination.Origin.PRIVET, | 315 print_preview.Destination.Origin.PRIVET, |
| 325 destinationName, | 316 destinationName, |
| 326 false /*isRecent*/, | 317 false /*isRecent*/, |
| 327 print_preview.Destination.ConnectionStatus.ONLINE); | 318 print_preview.Destination.ConnectionStatus.ONLINE); |
| 328 this.selectedDestination_.capabilities = | 319 this.selectedDestination_.capabilities = |
| 329 this.appState_.selectedDestinationCapabilities; | 320 this.appState_.selectedDestinationCapabilities; |
| 330 | 321 |
| 331 cr.dispatchSimpleEvent( | 322 cr.dispatchSimpleEvent( |
| 332 this, | 323 this, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 345 */ | 336 */ |
| 346 setCloudPrintInterface: function(cloudPrintInterface) { | 337 setCloudPrintInterface: function(cloudPrintInterface) { |
| 347 this.cloudPrintInterface_ = cloudPrintInterface; | 338 this.cloudPrintInterface_ = cloudPrintInterface; |
| 348 this.tracker_.add( | 339 this.tracker_.add( |
| 349 this.cloudPrintInterface_, | 340 this.cloudPrintInterface_, |
| 350 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, | 341 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, |
| 351 this.onCloudPrintSearchDone_.bind(this)); | 342 this.onCloudPrintSearchDone_.bind(this)); |
| 352 this.tracker_.add( | 343 this.tracker_.add( |
| 353 this.cloudPrintInterface_, | 344 this.cloudPrintInterface_, |
| 354 cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED, | 345 cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED, |
| 355 this.onCloudPrintSearchFailed_.bind(this)); | 346 this.onCloudPrintSearchDone_.bind(this)); |
| 356 this.tracker_.add( | 347 this.tracker_.add( |
| 357 this.cloudPrintInterface_, | 348 this.cloudPrintInterface_, |
| 358 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, | 349 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, |
| 359 this.onCloudPrintPrinterDone_.bind(this)); | 350 this.onCloudPrintPrinterDone_.bind(this)); |
| 360 this.tracker_.add( | 351 this.tracker_.add( |
| 361 this.cloudPrintInterface_, | 352 this.cloudPrintInterface_, |
| 362 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, | 353 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, |
| 363 this.onCloudPrintPrinterFailed_.bind(this)); | 354 this.onCloudPrintPrinterFailed_.bind(this)); |
| 364 }, | 355 }, |
| 365 | 356 |
| 366 /** | 357 /** |
| 367 * @return {boolean} Whether only default cloud destinations have been | 358 * @return {boolean} Whether only default cloud destinations have been |
| 368 * loaded. | 359 * loaded. |
| 369 */ | 360 */ |
| 370 hasOnlyDefaultCloudDestinations: function() { | 361 hasOnlyDefaultCloudDestinations: function() { |
| 362 // TODO: Move the logic to print_preview. |
| 371 return this.destinations_.every(function(dest) { | 363 return this.destinations_.every(function(dest) { |
| 372 return dest.isLocal || | 364 return dest.isLocal || |
| 373 dest.id == print_preview.Destination.GooglePromotedId.DOCS || | 365 dest.id == print_preview.Destination.GooglePromotedId.DOCS || |
| 374 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; | 366 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; |
| 375 }); | 367 }); |
| 376 }, | 368 }, |
| 377 | 369 |
| 378 /** @param {!print_preview.Destination} Destination to select. */ | 370 /** @param {!print_preview.Destination} Destination to select. */ |
| 379 selectDestination: function(destination) { | 371 selectDestination: function(destination) { |
| 372 this.isInAutoSelectMode_ = false; |
| 373 this.cancelAutoSelectTimeout_(); |
| 374 if (destination == this.selectedDestination_) { |
| 375 return; |
| 376 } |
| 377 if (destination == null) { |
| 378 this.selectedDestination_ = null; |
| 379 cr.dispatchSimpleEvent( |
| 380 this, DestinationStore.EventType.DESTINATION_SELECT); |
| 381 return; |
| 382 } |
| 383 // Update and persist selected destination. |
| 380 this.selectedDestination_ = destination; | 384 this.selectedDestination_ = destination; |
| 381 this.selectedDestination_.isRecent = true; | 385 this.selectedDestination_.isRecent = true; |
| 382 this.isInAutoSelectMode_ = false; | |
| 383 if (this.autoSelectTimeout_ != null) { | |
| 384 clearTimeout(this.autoSelectTimeout_); | |
| 385 this.autoSelectTimeout_ = null; | |
| 386 } | |
| 387 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && | 386 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && |
| 388 !destination.isTosAccepted) { | 387 !destination.isTosAccepted) { |
| 389 assert(this.cloudPrintInterface_ != null, | 388 assert(this.cloudPrintInterface_ != null, |
| 390 'Selected FedEx Office destination, but Google Cloud Print is ' + | 389 'Selected FedEx destination, but GCP API is not available'); |
| 391 'not enabled'); | |
| 392 destination.isTosAccepted = true; | 390 destination.isTosAccepted = true; |
| 393 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination.id, | 391 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true); |
| 394 destination.origin, | |
| 395 true); | |
| 396 } | 392 } |
| 397 this.appState_.persistSelectedDestination(this.selectedDestination_); | 393 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 398 | 394 // Adjust metrics. |
| 399 if (destination.cloudID && | 395 if (destination.cloudID && |
| 400 this.destinations.some(function(otherDestination) { | 396 this.destinations_.some(function(otherDestination) { |
| 401 return otherDestination.cloudID == destination.cloudID && | 397 return otherDestination.cloudID == destination.cloudID && |
| 402 otherDestination != destination; | 398 otherDestination != destination; |
| 403 })) { | 399 })) { |
| 404 if (destination.isPrivet) { | 400 if (destination.isPrivet) { |
| 405 this.metrics_.incrementDestinationSearchBucket( | 401 this.metrics_.incrementDestinationSearchBucket( |
| 406 print_preview.Metrics.DestinationSearchBucket. | 402 print_preview.Metrics.DestinationSearchBucket. |
| 407 PRIVET_DUPLICATE_SELECTED); | 403 PRIVET_DUPLICATE_SELECTED); |
| 408 } else { | 404 } else { |
| 409 this.metrics_.incrementDestinationSearchBucket( | 405 this.metrics_.incrementDestinationSearchBucket( |
| 410 print_preview.Metrics.DestinationSearchBucket. | 406 print_preview.Metrics.DestinationSearchBucket. |
| 411 CLOUD_DUPLICATE_SELECTED); | 407 CLOUD_DUPLICATE_SELECTED); |
| 412 } | 408 } |
| 413 } | 409 } |
| 414 | 410 // Notify about selected destination change. |
| 415 cr.dispatchSimpleEvent( | 411 cr.dispatchSimpleEvent( |
| 416 this, DestinationStore.EventType.DESTINATION_SELECT); | 412 this, DestinationStore.EventType.DESTINATION_SELECT); |
| 413 // Request destination capabilities, of not known yet. |
| 417 if (destination.capabilities == null) { | 414 if (destination.capabilities == null) { |
| 418 if (destination.isPrivet) { | 415 if (destination.isPrivet) { |
| 419 this.nativeLayer_.startGetPrivetDestinationCapabilities( | 416 this.nativeLayer_.startGetPrivetDestinationCapabilities( |
| 420 destination.id); | 417 destination.id); |
| 421 } | 418 } |
| 422 else if (destination.isLocal) { | 419 else if (destination.isLocal) { |
| 423 this.nativeLayer_.startGetLocalDestinationCapabilities( | 420 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 424 destination.id); | 421 destination.id); |
| 425 } else { | 422 } else { |
| 426 assert(this.cloudPrintInterface_ != null, | 423 assert(this.cloudPrintInterface_ != null, |
| 427 'Selected destination is a cloud destination, but Google ' + | 424 'Cloud destination selected, but GCP is not enabled'); |
| 428 'Cloud Print is not enabled'); | 425 this.cloudPrintInterface_.printer( |
| 429 this.cloudPrintInterface_.printer(destination.id, | 426 destination.id, destination.origin, destination.account); |
| 430 destination.origin); | |
| 431 } | 427 } |
| 432 } else { | 428 } else { |
| 433 cr.dispatchSimpleEvent( | 429 cr.dispatchSimpleEvent( |
| 434 this, | 430 this, |
| 435 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 431 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 436 } | 432 } |
| 437 }, | 433 }, |
| 438 | 434 |
| 439 /** | 435 /** |
| 440 * Inserts a print destination to the data store and dispatches a | 436 * Selects 'Save to PDF' destination (since it always exists). |
| 441 * DESTINATIONS_INSERTED event. If the destination matches the initial | 437 * @private |
| 442 * destination ID, then the destination will be automatically selected. | |
| 443 * @param {!print_preview.Destination} destination Print destination to | |
| 444 * insert. | |
| 445 */ | 438 */ |
| 446 insertDestination: function(destination) { | 439 selectDefaultDestination_: function() { |
| 447 if (this.insertDestination_(destination)) { | 440 var destination = this.destinationMap_[this.getDestinationKey_( |
| 448 cr.dispatchSimpleEvent( | 441 print_preview.Destination.Origin.LOCAL, |
| 449 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | 442 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
| 450 if (this.isInAutoSelectMode_ && | 443 '')] || null; |
| 451 this.matchInitialDestination_(destination.id, destination.origin)) { | 444 assert(destination != null, 'Save to PDF printer not found'); |
| 452 this.selectDestination(destination); | 445 this.selectDestination(destination); |
| 453 } | |
| 454 } | |
| 455 }, | |
| 456 | |
| 457 /** | |
| 458 * Inserts multiple print destinations to the data store and dispatches one | |
| 459 * DESTINATIONS_INSERTED event. If any of the destinations match the initial | |
| 460 * destination ID, then that destination will be automatically selected. | |
| 461 * @param {!Array.<print_preview.Destination>} destinations Print | |
| 462 * destinations to insert. | |
| 463 */ | |
| 464 insertDestinations: function(destinations) { | |
| 465 var insertedDestination = false; | |
| 466 var destinationToAutoSelect = null; | |
| 467 destinations.forEach(function(dest) { | |
| 468 if (this.insertDestination_(dest)) { | |
| 469 insertedDestination = true; | |
| 470 if (this.isInAutoSelectMode_ && | |
| 471 destinationToAutoSelect == null && | |
| 472 this.matchInitialDestination_(dest.id, dest.origin)) { | |
| 473 destinationToAutoSelect = dest; | |
| 474 } | |
| 475 } | |
| 476 }, this); | |
| 477 if (insertedDestination) { | |
| 478 cr.dispatchSimpleEvent( | |
| 479 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | |
| 480 } | |
| 481 if (destinationToAutoSelect != null) { | |
| 482 this.selectDestination(destinationToAutoSelect); | |
| 483 } | |
| 484 }, | |
| 485 | |
| 486 /** | |
| 487 * Updates an existing print destination with capabilities and display name | |
| 488 * information. If the destination doesn't already exist, it will be added. | |
| 489 * @param {!print_preview.Destination} destination Destination to update. | |
| 490 * @return {!print_preview.Destination} The existing destination that was | |
| 491 * updated or {@code null} if it was the new destination. | |
| 492 */ | |
| 493 updateDestination: function(destination) { | |
| 494 assert(destination.constructor !== Array, 'Single printer expected'); | |
| 495 var key = this.getDestinationKey_(destination.origin, destination.id); | |
| 496 var existingDestination = this.destinationMap_[key]; | |
| 497 if (existingDestination != null) { | |
| 498 existingDestination.capabilities = destination.capabilities; | |
| 499 } else { | |
| 500 this.insertDestination(destination); | |
| 501 } | |
| 502 | |
| 503 if (existingDestination == this.selectedDestination_ || | |
| 504 destination == this.selectedDestination_) { | |
| 505 this.appState_.persistSelectedDestination(this.selectedDestination_); | |
| 506 cr.dispatchSimpleEvent( | |
| 507 this, | |
| 508 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | |
| 509 } | |
| 510 | |
| 511 return existingDestination; | |
| 512 }, | 446 }, |
| 513 | 447 |
| 514 /** Initiates loading of local print destinations. */ | 448 /** Initiates loading of local print destinations. */ |
| 515 startLoadLocalDestinations: function() { | 449 startLoadLocalDestinations: function() { |
| 516 if (!this.hasLoadedAllLocalDestinations_) { | 450 if (!this.hasLoadedAllLocalDestinations_) { |
| 517 this.hasLoadedAllLocalDestinations_ = true; | 451 this.hasLoadedAllLocalDestinations_ = true; |
| 518 this.nativeLayer_.startGetLocalDestinations(); | 452 this.nativeLayer_.startGetLocalDestinations(); |
| 519 this.isLocalDestinationSearchInProgress_ = true; | 453 this.isLocalDestinationSearchInProgress_ = true; |
| 520 cr.dispatchSimpleEvent( | 454 cr.dispatchSimpleEvent( |
| 521 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 455 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 534 DestinationStore.PRIVET_SEARCH_DURATION_); | 468 DestinationStore.PRIVET_SEARCH_DURATION_); |
| 535 } | 469 } |
| 536 }, | 470 }, |
| 537 | 471 |
| 538 /** | 472 /** |
| 539 * Initiates loading of cloud destinations. | 473 * Initiates loading of cloud destinations. |
| 540 * @param {print_preview.Destination.Origin=} opt_origin Search destinations | 474 * @param {print_preview.Destination.Origin=} opt_origin Search destinations |
| 541 * for the specified origin only. | 475 * for the specified origin only. |
| 542 */ | 476 */ |
| 543 startLoadCloudDestinations: function(opt_origin) { | 477 startLoadCloudDestinations: function(opt_origin) { |
| 544 if (this.cloudPrintInterface_ != null && | 478 if (this.cloudPrintInterface_ != null) { |
| 545 !this.hasLoadedAllCloudDestinations_) { | 479 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; |
| 546 this.hasLoadedAllCloudDestinations_ = true; | 480 if (origins.length == 0 || |
| 547 this.cloudPrintInterface_.search(opt_origin); | 481 (opt_origin && origins.indexOf(opt_origin) < 0)) { |
| 548 cr.dispatchSimpleEvent( | 482 this.cloudPrintInterface_.search( |
| 549 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 483 this.userInfo_.activeUser, opt_origin); |
| 484 cr.dispatchSimpleEvent( |
| 485 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); |
| 486 } |
| 550 } | 487 } |
| 551 }, | 488 }, |
| 552 | 489 |
| 490 /** Requests load of COOKIE based cloud destinations. */ |
| 491 reloadUserCookieBasedDestinations: function() { |
| 492 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; |
| 493 if (origins.indexOf(print_preview.Destination.Origin.COOKIES) >= 0) { |
| 494 cr.dispatchSimpleEvent( |
| 495 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
| 496 } else { |
| 497 this.startLoadCloudDestinations( |
| 498 print_preview.Destination.Origin.COOKIES); |
| 499 } |
| 500 }, |
| 501 |
| 553 /** | 502 /** |
| 554 * Wait for a privet device to be registered. | 503 * Wait for a privet device to be registered. |
| 555 */ | 504 */ |
| 556 waitForRegister: function(id) { | 505 waitForRegister: function(id) { |
| 557 this.nativeLayer_.startGetPrivetDestinations(); | 506 this.nativeLayer_.startGetPrivetDestinations(); |
| 558 this.waitForRegisterDestination_ = id; | 507 this.waitForRegisterDestination_ = id; |
| 559 }, | 508 }, |
| 560 | 509 |
| 561 /** | 510 /** |
| 511 * Inserts {@code destination} to the data store and dispatches a |
| 512 * DESTINATIONS_INSERTED event. |
| 513 * @param {!print_preview.Destination} destination Print destination to |
| 514 * insert. |
| 515 * @private |
| 516 */ |
| 517 insertDestination_: function(destination) { |
| 518 if (this.insertIntoStore_(destination)) { |
| 519 this.destinationsInserted_(destination); |
| 520 } |
| 521 }, |
| 522 |
| 523 /** |
| 524 * Inserts multiple {@code destinations} to the data store and dispatches |
| 525 * single DESTINATIONS_INSERTED event. |
| 526 * @param {!Array.<print_preview.Destination>} destinations Print |
| 527 * destinations to insert. |
| 528 * @private |
| 529 */ |
| 530 insertDestinations_: function(destinations) { |
| 531 var inserted = false; |
| 532 destinations.forEach(function(destination) { |
| 533 inserted = this.insertIntoStore_(destination) || inserted; |
| 534 }, this); |
| 535 if (inserted) { |
| 536 this.destinationsInserted_(); |
| 537 } |
| 538 }, |
| 539 |
| 540 /** |
| 541 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to |
| 542 * update selected destination to match {@code appState_} settings. |
| 543 * @param {print_preview.Destination=} opt_destination The only destination |
| 544 * that was changed or skipped if possibly more than one destination was |
| 545 * changed. Used as a hint to limit destination search scope in |
| 546 * {@code isInAutoSelectMode_). |
| 547 */ |
| 548 destinationsInserted_: function(opt_destination) { |
| 549 cr.dispatchSimpleEvent( |
| 550 this, DestinationStore.EventType.DESTINATIONS_INSERTED); |
| 551 if (this.isInAutoSelectMode_) { |
| 552 var destinationsToSearch = |
| 553 opt_destination && [opt_destination] || this.destinations_; |
| 554 destinationsToSearch.some(function(destination) { |
| 555 if (this.matchPersistedDestination_(destination)) { |
| 556 this.selectDestination(destination); |
| 557 return true; |
| 558 } |
| 559 }, this); |
| 560 } |
| 561 }, |
| 562 |
| 563 /** |
| 564 * Updates an existing print destination with capabilities and display name |
| 565 * information. If the destination doesn't already exist, it will be added. |
| 566 * @param {!print_preview.Destination} destination Destination to update. |
| 567 * @return {!print_preview.Destination} The existing destination that was |
| 568 * updated or {@code null} if it was the new destination. |
| 569 * @private |
| 570 */ |
| 571 updateDestination_: function(destination) { |
| 572 assert(destination.constructor !== Array, 'Single printer expected'); |
| 573 var existingDestination = this.destinationMap_[this.getKey_(destination)]; |
| 574 if (existingDestination != null) { |
| 575 existingDestination.capabilities = destination.capabilities; |
| 576 } else { |
| 577 this.insertDestination_(destination); |
| 578 } |
| 579 |
| 580 if (existingDestination == this.selectedDestination_ || |
| 581 destination == this.selectedDestination_) { |
| 582 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 583 cr.dispatchSimpleEvent( |
| 584 this, |
| 585 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 586 } |
| 587 |
| 588 return existingDestination; |
| 589 }, |
| 590 |
| 591 /** |
| 562 * Called when the search for Privet printers is done. | 592 * Called when the search for Privet printers is done. |
| 563 * @private | 593 * @private |
| 564 */ | 594 */ |
| 565 endPrivetPrinterSearch_: function() { | 595 endPrivetPrinterSearch_: function() { |
| 566 this.nativeLayer_.stopGetPrivetDestinations(); | 596 this.nativeLayer_.stopGetPrivetDestinations(); |
| 567 this.isPrivetDestinationSearchInProgress_ = false; | 597 this.isPrivetDestinationSearchInProgress_ = false; |
| 568 this.hasLoadedAllPrivetDestinations_ = true; | 598 this.hasLoadedAllPrivetDestinations_ = true; |
| 569 cr.dispatchSimpleEvent( | 599 cr.dispatchSimpleEvent( |
| 570 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 600 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
| 571 }, | 601 }, |
| 572 | 602 |
| 573 /** | 603 /** |
| 574 * Inserts a destination into the store without dispatching any events. | 604 * Inserts a destination into the store without dispatching any events. |
| 575 * @return {boolean} Whether the inserted destination was not already in the | 605 * @return {boolean} Whether the inserted destination was not already in the |
| 576 * store. | 606 * store. |
| 577 * @private | 607 * @private |
| 578 */ | 608 */ |
| 579 insertDestination_: function(destination) { | 609 insertIntoStore_: function(destination) { |
| 580 var key = this.getDestinationKey_(destination.origin, destination.id); | 610 var key = this.getKey_(destination); |
| 581 var existingDestination = this.destinationMap_[key]; | 611 var existingDestination = this.destinationMap_[key]; |
| 582 if (existingDestination == null) { | 612 if (existingDestination == null) { |
| 583 this.destinations_.push(destination); | 613 this.destinations_.push(destination); |
| 584 this.destinationMap_[key] = destination; | 614 this.destinationMap_[key] = destination; |
| 585 return true; | 615 return true; |
| 586 } else if (existingDestination.connectionStatus == | 616 } else if (existingDestination.connectionStatus == |
| 587 print_preview.Destination.ConnectionStatus.UNKNOWN && | 617 print_preview.Destination.ConnectionStatus.UNKNOWN && |
| 588 destination.connectionStatus != | 618 destination.connectionStatus != |
| 589 print_preview.Destination.ConnectionStatus.UNKNOWN) { | 619 print_preview.Destination.ConnectionStatus.UNKNOWN) { |
| 590 existingDestination.connectionStatus = destination.connectionStatus; | 620 existingDestination.connectionStatus = destination.connectionStatus; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 616 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 646 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
| 617 this.onDestinationsReload_.bind(this)); | 647 this.onDestinationsReload_.bind(this)); |
| 618 this.tracker_.add( | 648 this.tracker_.add( |
| 619 this.nativeLayer_, | 649 this.nativeLayer_, |
| 620 print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED, | 650 print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED, |
| 621 this.onPrivetPrinterAdded_.bind(this)); | 651 this.onPrivetPrinterAdded_.bind(this)); |
| 622 this.tracker_.add( | 652 this.tracker_.add( |
| 623 this.nativeLayer_, | 653 this.nativeLayer_, |
| 624 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, | 654 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, |
| 625 this.onPrivetCapabilitiesSet_.bind(this)); | 655 this.onPrivetCapabilitiesSet_.bind(this)); |
| 626 this.tracker_.add( | |
| 627 this.userInfo_, | |
| 628 print_preview.UserInfo.EventType.ACTIVE_USER_CHANGED, | |
| 629 this.onActiveUserChanged_.bind(this)); | |
| 630 }, | 656 }, |
| 631 | 657 |
| 632 /** | 658 /** |
| 633 * Resets the state of the destination store to its initial state. | 659 * Resets the state of the destination store to its initial state. |
| 634 * @private | 660 * @private |
| 635 */ | 661 */ |
| 636 reset_: function() { | 662 reset_: function() { |
| 637 this.destinations_ = []; | 663 this.destinations_ = []; |
| 638 this.destinationMap_ = {}; | 664 this.destinationMap_ = {}; |
| 639 this.selectedDestination_ = null; | 665 this.selectDestination(null); |
| 640 this.hasLoadedAllCloudDestinations_ = false; | 666 this.loadedCloudOrigins_ = {}; |
| 641 this.hasLoadedAllLocalDestinations_ = false; | 667 this.hasLoadedAllLocalDestinations_ = false; |
| 642 this.insertDestination( | 668 this.insertDestination_( |
| 643 DestinationStore.createLocalPdfPrintDestination_()); | 669 DestinationStore.createLocalPdfPrintDestination_()); |
| 644 this.resetAutoSelectTimeout_(); | 670 this.resetAutoSelectTimeout_(); |
| 645 }, | 671 }, |
| 646 | 672 |
| 647 /** | 673 /** |
| 648 * Resets the state of the destination store to its initial state. | |
| 649 * @private | |
| 650 */ | |
| 651 resetCookiesDestinations_: function() { | |
| 652 // Forget all cookies based destinations. | |
| 653 this.destinations_ = this.destinations_.filter(function(destination) { | |
| 654 if (destination.origin == print_preview.Destination.Origin.COOKIES) { | |
| 655 delete this.destinationMap_[ | |
| 656 this.getDestinationKey_(destination.origin, destination.id)]; | |
| 657 return false; | |
| 658 } | |
| 659 return true; | |
| 660 }, this); | |
| 661 // Reset selected destination, if necessary. | |
| 662 if (this.selectedDestination_ && | |
| 663 this.selectedDestination_.origin == | |
| 664 print_preview.Destination.Origin.COOKIES) { | |
| 665 this.selectedDestination_ = null; | |
| 666 } | |
| 667 this.hasLoadedAllCloudDestinations_ = false; | |
| 668 this.resetAutoSelectTimeout_(); | |
| 669 }, | |
| 670 | |
| 671 /** | |
| 672 * Resets destination auto selection timeout. | 674 * Resets destination auto selection timeout. |
| 673 * @private | 675 * @private |
| 674 */ | 676 */ |
| 675 resetAutoSelectTimeout_: function() { | 677 resetAutoSelectTimeout_: function() { |
| 678 this.cancelAutoSelectTimeout_(); |
| 676 this.autoSelectTimeout_ = | 679 this.autoSelectTimeout_ = |
| 677 setTimeout(this.onAutoSelectFailed_.bind(this), | 680 setTimeout(this.onAutoSelectFailed_.bind(this), |
| 678 DestinationStore.AUTO_SELECT_TIMEOUT_); | 681 DestinationStore.AUTO_SELECT_TIMEOUT_); |
| 679 }, | 682 }, |
| 680 | 683 |
| 681 /** | 684 /** |
| 682 * Called when active user changes. Resets cookie based destinations | 685 * Cancels destination auto selection timeout. |
| 683 * and starts loading cloud destinations for the active user. | |
| 684 * @private | 686 * @private |
| 685 */ | 687 */ |
| 686 onActiveUserChanged_: function() { | 688 cancelAutoSelectTimeout_: function() { |
| 687 this.resetCookiesDestinations_(); | 689 if (this.autoSelectTimeout_ != null) { |
| 688 this.isInAutoSelectMode_ = true; | 690 clearTimeout(this.autoSelectTimeout_); |
| 689 this.startLoadCloudDestinations(print_preview.Destination.Origin.COOKIES); | 691 this.autoSelectTimeout_ = null; |
| 692 } |
| 690 }, | 693 }, |
| 691 | 694 |
| 692 /** | 695 /** |
| 693 * Called when the local destinations have been got from the native layer. | 696 * Called when the local destinations have been got from the native layer. |
| 694 * @param {Event} Contains the local destinations. | 697 * @param {Event} Contains the local destinations. |
| 695 * @private | 698 * @private |
| 696 */ | 699 */ |
| 697 onLocalDestinationsSet_: function(event) { | 700 onLocalDestinationsSet_: function(event) { |
| 698 var localDestinations = event.destinationInfos.map(function(destInfo) { | 701 var localDestinations = event.destinationInfos.map(function(destInfo) { |
| 699 return print_preview.LocalDestinationParser.parse(destInfo); | 702 return print_preview.LocalDestinationParser.parse(destInfo); |
| 700 }); | 703 }); |
| 701 this.insertDestinations(localDestinations); | 704 this.insertDestinations_(localDestinations); |
| 702 this.isLocalDestinationSearchInProgress_ = false; | 705 this.isLocalDestinationSearchInProgress_ = false; |
| 703 cr.dispatchSimpleEvent( | 706 cr.dispatchSimpleEvent( |
| 704 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 707 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
| 705 }, | 708 }, |
| 706 | 709 |
| 707 /** | 710 /** |
| 708 * Called when the native layer retrieves the capabilities for the selected | 711 * Called when the native layer retrieves the capabilities for the selected |
| 709 * local destination. Updates the destination with new capabilities if the | 712 * local destination. Updates the destination with new capabilities if the |
| 710 * destination already exists, otherwise it creates a new destination and | 713 * destination already exists, otherwise it creates a new destination and |
| 711 * then updates its capabilities. | 714 * then updates its capabilities. |
| 712 * @param {Event} event Contains the capabilities of the local print | 715 * @param {Event} event Contains the capabilities of the local print |
| 713 * destination. | 716 * destination. |
| 714 * @private | 717 * @private |
| 715 */ | 718 */ |
| 716 onLocalDestinationCapabilitiesSet_: function(event) { | 719 onLocalDestinationCapabilitiesSet_: function(event) { |
| 717 var destinationId = event.settingsInfo['printerId']; | 720 var destinationId = event.settingsInfo['printerId']; |
| 718 var key = | 721 var key = this.getDestinationKey_( |
| 719 this.getDestinationKey_(print_preview.Destination.Origin.LOCAL, | 722 print_preview.Destination.Origin.LOCAL, |
| 720 destinationId); | 723 destinationId, |
| 724 ''); |
| 721 var destination = this.destinationMap_[key]; | 725 var destination = this.destinationMap_[key]; |
| 722 var capabilities = print_preview.LocalCapabilitiesParser.parse( | 726 var capabilities = print_preview.LocalCapabilitiesParser.parse( |
| 723 event.settingsInfo); | 727 event.settingsInfo); |
| 724 if (destination) { | 728 if (destination) { |
| 725 // In case there were multiple capabilities request for this local | 729 // In case there were multiple capabilities request for this local |
| 726 // destination, just ignore the later ones. | 730 // destination, just ignore the later ones. |
| 727 if (destination.capabilities != null) { | 731 if (destination.capabilities != null) { |
| 728 return; | 732 return; |
| 729 } | 733 } |
| 730 destination.capabilities = capabilities; | 734 destination.capabilities = capabilities; |
| 731 } else { | 735 } else { |
| 732 // TODO(rltoscano): This makes the assumption that the "deviceName" is | 736 // TODO(rltoscano): This makes the assumption that the "deviceName" is |
| 733 // the same as "printerName". We should include the "printerName" in the | 737 // the same as "printerName". We should include the "printerName" in the |
| 734 // response. See http://crbug.com/132831. | 738 // response. See http://crbug.com/132831. |
| 735 destination = print_preview.LocalDestinationParser.parse( | 739 destination = print_preview.LocalDestinationParser.parse( |
| 736 {deviceName: destinationId, printerName: destinationId}); | 740 {deviceName: destinationId, printerName: destinationId}); |
| 737 destination.capabilities = capabilities; | 741 destination.capabilities = capabilities; |
| 738 this.insertDestination(destination); | 742 this.insertDestination_(destination); |
| 739 } | 743 } |
| 740 if (this.selectedDestination_ && | 744 if (this.selectedDestination_ && |
| 741 this.selectedDestination_.id == destinationId) { | 745 this.selectedDestination_.id == destinationId) { |
| 742 cr.dispatchSimpleEvent(this, | 746 cr.dispatchSimpleEvent(this, |
| 743 DestinationStore.EventType. | 747 DestinationStore.EventType. |
| 744 SELECTED_DESTINATION_CAPABILITIES_READY); | 748 SELECTED_DESTINATION_CAPABILITIES_READY); |
| 745 } | 749 } |
| 746 }, | 750 }, |
| 747 | 751 |
| 748 /** | 752 /** |
| 749 * Called when a request to get a local destination's print capabilities | 753 * Called when a request to get a local destination's print capabilities |
| 750 * fails. If the destination is the initial destination, auto-select another | 754 * fails. If the destination is the initial destination, auto-select another |
| 751 * destination instead. | 755 * destination instead. |
| 752 * @param {Event} event Contains the destination ID that failed. | 756 * @param {Event} event Contains the destination ID that failed. |
| 753 * @private | 757 * @private |
| 754 */ | 758 */ |
| 755 onGetCapabilitiesFail_: function(event) { | 759 onGetCapabilitiesFail_: function(event) { |
| 756 console.error('Failed to get print capabilities for printer ' + | 760 console.error('Failed to get print capabilities for printer ' + |
| 757 event.destinationId); | 761 event.destinationId); |
| 758 if (this.isInAutoSelectMode_ && | 762 if (this.isInAutoSelectMode_ && |
| 759 this.matchInitialDestinationStrict_(event.destinationId, | 763 this.sameAsPersistedDestination_(event.destinationId, |
| 760 event.destinationOrigin)) { | 764 event.destinationOrigin)) { |
| 761 assert(this.destinations_.length > 0, | 765 this.selectDefaultDestination_(); |
| 762 'No destinations were loaded when failed to get initial ' + | |
| 763 'destination'); | |
| 764 this.selectDestination(this.destinations_[0]); | |
| 765 } | 766 } |
| 766 }, | 767 }, |
| 767 | 768 |
| 768 /** | 769 /** |
| 769 * Called when the /search call completes. Adds the fetched destinations to | 770 * Called when the /search call completes, either successfully or not. |
| 770 * the destination store. | 771 * In case of success, stores fetched destinations. |
| 771 * @param {Event} event Contains the fetched destinations. | 772 * @param {Event} event Contains the request result. |
| 772 * @private | 773 * @private |
| 773 */ | 774 */ |
| 774 onCloudPrintSearchDone_: function(event) { | 775 onCloudPrintSearchDone_: function(event) { |
| 775 this.insertDestinations(event.printers); | 776 if (event.printers) { |
| 777 this.insertDestinations_(event.printers); |
| 778 } |
| 779 if (event.searchDone) { |
| 780 var origins = this.loadedCloudOrigins_[event.user] || []; |
| 781 if (origins.indexOf(event.origin) < 0) { |
| 782 this.loadedCloudOrigins_[event.user] = origins.concat([event.origin]); |
| 783 } |
| 784 } |
| 776 cr.dispatchSimpleEvent( | 785 cr.dispatchSimpleEvent( |
| 777 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 786 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
| 778 }, | 787 }, |
| 779 | |
| 780 /** | |
| 781 * Called when the /search call fails. Updates outstanding request count and | |
| 782 * dispatches CLOUD_DESTINATIONS_LOADED event. | |
| 783 * @private | |
| 784 */ | |
| 785 onCloudPrintSearchFailed_: function() { | |
| 786 cr.dispatchSimpleEvent( | |
| 787 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | |
| 788 }, | |
| 789 | 788 |
| 790 /** | 789 /** |
| 791 * Called when /printer call completes. Updates the specified destination's | 790 * Called when /printer call completes. Updates the specified destination's |
| 792 * print capabilities. | 791 * print capabilities. |
| 793 * @param {Event} event Contains detailed information about the | 792 * @param {Event} event Contains detailed information about the |
| 794 * destination. | 793 * destination. |
| 795 * @private | 794 * @private |
| 796 */ | 795 */ |
| 797 onCloudPrintPrinterDone_: function(event) { | 796 onCloudPrintPrinterDone_: function(event) { |
| 798 this.updateDestination(event.printer); | 797 this.updateDestination_(event.printer); |
| 799 }, | 798 }, |
| 800 | 799 |
| 801 /** | 800 /** |
| 802 * Called when the Google Cloud Print interface fails to lookup a | 801 * Called when the Google Cloud Print interface fails to lookup a |
| 803 * destination. Selects another destination if the failed destination was | 802 * destination. Selects another destination if the failed destination was |
| 804 * the initial destination. | 803 * the initial destination. |
| 805 * @param {object} event Contains the ID of the destination that was failed | 804 * @param {object} event Contains the ID of the destination that was failed |
| 806 * to be looked up. | 805 * to be looked up. |
| 807 * @private | 806 * @private |
| 808 */ | 807 */ |
| 809 onCloudPrintPrinterFailed_: function(event) { | 808 onCloudPrintPrinterFailed_: function(event) { |
| 810 if (this.isInAutoSelectMode_ && | 809 if (this.isInAutoSelectMode_ && |
| 811 this.matchInitialDestinationStrict_(event.destinationId, | 810 this.sameAsPersistedDestination_(event.destinationId, |
| 812 event.destinationOrigin)) { | 811 event.destinationOrigin)) { |
| 813 console.error('Could not find initial printer: ' + event.destinationId); | 812 console.error( |
| 814 assert(this.destinations_.length > 0, | 813 'Failed to fetch last used printer caps: ' + event.destinationId); |
| 815 'No destinations were loaded when failed to get initial ' + | 814 this.selectDefaultDestination_(); |
| 816 'destination'); | |
| 817 this.selectDestination(this.destinations_[0]); | |
| 818 } | 815 } |
| 819 }, | 816 }, |
| 820 | 817 |
| 821 /** | 818 /** |
| 822 * Called when a Privet printer is added to the local network. | 819 * Called when a Privet printer is added to the local network. |
| 823 * @param {object} event Contains information about the added printer. | 820 * @param {object} event Contains information about the added printer. |
| 824 * @private | 821 * @private |
| 825 */ | 822 */ |
| 826 onPrivetPrinterAdded_: function(event) { | 823 onPrivetPrinterAdded_: function(event) { |
| 827 if (event.printer.serviceName == this.waitForRegisterDestination_ && | 824 if (event.printer.serviceName == this.waitForRegisterDestination_ && |
| 828 !event.printer.isUnregistered) { | 825 !event.printer.isUnregistered) { |
| 829 this.waitForRegisterDestination_ = null; | 826 this.waitForRegisterDestination_ = null; |
| 830 this.onDestinationsReload_(); | 827 this.onDestinationsReload_(); |
| 831 } else { | 828 } else { |
| 832 this.insertDestinations( | 829 this.insertDestinations_( |
| 833 print_preview.PrivetDestinationParser.parse(event.printer)); | 830 print_preview.PrivetDestinationParser.parse(event.printer)); |
| 834 } | 831 } |
| 835 }, | 832 }, |
| 836 | 833 |
| 837 /** | 834 /** |
| 838 * Called when capabilities for a privet printer are set. | 835 * Called when capabilities for a privet printer are set. |
| 839 * @param {object} event Contains the capabilities and printer ID. | 836 * @param {object} event Contains the capabilities and printer ID. |
| 840 * @private | 837 * @private |
| 841 */ | 838 */ |
| 842 onPrivetCapabilitiesSet_: function(event) { | 839 onPrivetCapabilitiesSet_: function(event) { |
| 843 var destinationId = event.printerId; | 840 var destinationId = event.printerId; |
| 844 var destinations = | 841 var destinations = |
| 845 print_preview.PrivetDestinationParser.parse(event.printer); | 842 print_preview.PrivetDestinationParser.parse(event.printer); |
| 846 destinations.forEach(function(dest) { | 843 destinations.forEach(function(dest) { |
| 847 dest.capabilities = event.capabilities; | 844 dest.capabilities = event.capabilities; |
| 848 this.updateDestination(dest); | 845 this.updateDestination_(dest); |
| 849 }, this); | 846 }, this); |
| 850 }, | 847 }, |
| 851 | 848 |
| 852 /** | 849 /** |
| 853 * Called from native layer after the user was requested to sign in, and did | 850 * Called from native layer after the user was requested to sign in, and did |
| 854 * so successfully. | 851 * so successfully. |
| 855 * @private | 852 * @private |
| 856 */ | 853 */ |
| 857 onDestinationsReload_: function() { | 854 onDestinationsReload_: function() { |
| 858 this.reset_(); | 855 this.reset_(); |
| 859 this.isInAutoSelectMode_ = true; | 856 this.isInAutoSelectMode_ = true; |
| 860 this.startLoadLocalDestinations(); | 857 this.startLoadLocalDestinations(); |
| 861 this.startLoadCloudDestinations(); | 858 this.startLoadCloudDestinations(); |
| 862 this.startLoadPrivetDestinations(); | 859 this.startLoadPrivetDestinations(); |
| 863 }, | 860 }, |
| 864 | 861 |
| 865 /** | 862 /** |
| 866 * Called when auto-selection fails. Selects the first destination in store. | 863 * Called when auto-selection fails. Selects the first destination in store. |
| 867 * @private | 864 * @private |
| 868 */ | 865 */ |
| 869 onAutoSelectFailed_: function() { | 866 onAutoSelectFailed_: function() { |
| 870 this.autoSelectTimeout_ = null; | 867 this.cancelAutoSelectTimeout_(); |
| 871 assert(this.destinations_.length > 0, | 868 this.selectDefaultDestination_(); |
| 872 'No destinations were loaded before auto-select timeout expired'); | |
| 873 this.selectDestination(this.destinations_[0]); | |
| 874 }, | 869 }, |
| 875 | 870 |
| 876 // TODO(vitalybuka): Remove three next functions replacing Destination.id | 871 // TODO(vitalybuka): Remove three next functions replacing Destination.id |
| 877 // and Destination.origin by complex ID. | 872 // and Destination.origin by complex ID. |
| 878 /** | 873 /** |
| 879 * Returns key to be used with {@code destinationMap_}. | 874 * Returns key to be used with {@code destinationMap_}. |
| 880 * @param {!print_preview.Destination.Origin} origin Destination origin. | 875 * @param {!print_preview.Destination.Origin} origin Destination origin. |
| 881 * @return {!string} id Destination id. | 876 * @return {string} id Destination id. |
| 877 * @return {string} account User account destination is registered for. |
| 882 * @private | 878 * @private |
| 883 */ | 879 */ |
| 884 getDestinationKey_: function(origin, id) { | 880 getDestinationKey_: function(origin, id, account) { |
| 885 return origin + '/' + id; | 881 return origin + '/' + id + '/' + account; |
| 886 }, | 882 }, |
| 887 | 883 |
| 888 /** | 884 /** |
| 889 * @param {?string} id Id of the destination. | 885 * Returns key to be used with {@code destinationMap_}. |
| 890 * @param {?string} origin Oring of the destination. | 886 * @param {!print_preview.Destination} destination Destination. |
| 891 * @return {boolean} Whether a initial destination matches provided. | |
| 892 * @private | 887 * @private |
| 893 */ | 888 */ |
| 894 matchInitialDestination_: function(id, origin) { | 889 getKey_: function(destination) { |
| 895 return this.initialDestinationId_ == null || | 890 return this.getDestinationKey_( |
| 896 this.initialDestinationOrigin_ == null || | 891 destination.origin, destination.id, destination.account); |
| 897 this.matchInitialDestinationStrict_(id, origin); | |
| 898 }, | 892 }, |
| 899 | 893 |
| 900 /** | 894 /** |
| 895 * @param {!print_preview.Destination} destination Destination to match. |
| 896 * @return {boolean} Whether {@code destination} matches the last user |
| 897 * selected one. |
| 898 * @private |
| 899 */ |
| 900 matchPersistedDestination_: function(destination) { |
| 901 return !this.appState_.selectedDestinationId || |
| 902 !this.appState_.selectedDestinationOrigin || |
| 903 this.sameAsPersistedDestination_( |
| 904 destination.id, destination.origin); |
| 905 }, |
| 906 |
| 907 /** |
| 901 * @param {?string} id Id of the destination. | 908 * @param {?string} id Id of the destination. |
| 902 * @param {?string} origin Oring of the destination. | 909 * @param {?string} origin Oring of the destination. |
| 903 * @return {boolean} Whether destination is the same as initial. | 910 * @return {boolean} Whether destination is the same as initial. |
| 904 * @private | 911 * @private |
| 905 */ | 912 */ |
| 906 matchInitialDestinationStrict_: function(id, origin) { | 913 sameAsPersistedDestination_: function(id, origin) { |
| 907 return id == this.initialDestinationId_ && | 914 return id == this.appState_.selectedDestinationId && |
| 908 origin == this.initialDestinationOrigin_; | 915 origin == this.appState_.selectedDestinationOrigin; |
| 909 } | 916 } |
| 910 }; | 917 }; |
| 911 | 918 |
| 912 // Export | 919 // Export |
| 913 return { | 920 return { |
| 914 DestinationStore: DestinationStore | 921 DestinationStore: DestinationStore |
| 915 }; | 922 }; |
| 916 }); | 923 }); |
| OLD | NEW |