| 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 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 408 |
| 409 /** | 409 /** |
| 410 * @return {boolean} Whether a search for cloud destinations is in progress. | 410 * @return {boolean} Whether a search for cloud destinations is in progress. |
| 411 */ | 411 */ |
| 412 get isCloudDestinationSearchInProgress() { | 412 get isCloudDestinationSearchInProgress() { |
| 413 return !!this.cloudPrintInterface_ && | 413 return !!this.cloudPrintInterface_ && |
| 414 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; | 414 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; |
| 415 }, | 415 }, |
| 416 | 416 |
| 417 /** | 417 /** |
| 418 * @return {boolean} Whether the selected destination is valid. |
| 419 */ |
| 420 selectedDestinationValid_: function() { |
| 421 return this.appState_.selectedDestination && |
| 422 this.appState_.selectedDestination.id && |
| 423 this.appState_.selectedDestination.origin; |
| 424 }, |
| 425 |
| 426 /* |
| 418 * Initializes the destination store. Sets the initially selected | 427 * Initializes the destination store. Sets the initially selected |
| 419 * destination. If any inserted destinations match this ID, that destination | 428 * destination. If any inserted destinations match this ID, that destination |
| 420 * will be automatically selected. This method must be called after the | 429 * will be automatically selected. This method must be called after the |
| 421 * print_preview.AppState has been initialized. | 430 * print_preview.AppState has been initialized. |
| 422 * @param {boolean} isInAppKioskMode Whether the print preview is in App | 431 * @param {boolean} isInAppKioskMode Whether the print preview is in App |
| 423 * Kiosk mode. | 432 * Kiosk mode. |
| 424 * @param {?string} systemDefaultDestinationId ID of the system default | 433 * @param {?string} systemDefaultDestinationId ID of the system default |
| 425 * destination. | 434 * destination. |
| 426 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized | 435 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized |
| 427 * default destination selection rules. | 436 * default destination selection rules. |
| 428 */ | 437 */ |
| 429 init: function( | 438 init: function( |
| 430 isInAppKioskMode, | 439 isInAppKioskMode, |
| 431 systemDefaultDestinationId, | 440 systemDefaultDestinationId, |
| 432 serializedDefaultDestinationSelectionRulesStr) { | 441 serializedDefaultDestinationSelectionRulesStr) { |
| 433 this.pdfPrinterEnabled_ = !isInAppKioskMode; | 442 this.pdfPrinterEnabled_ = !isInAppKioskMode; |
| 434 this.systemDefaultDestinationId_ = systemDefaultDestinationId; | 443 this.systemDefaultDestinationId_ = systemDefaultDestinationId; |
| 435 this.createLocalPdfPrintDestination_(); | 444 this.createLocalPdfPrintDestination_(); |
| 436 | 445 |
| 437 if (!this.appState_.selectedDestinationId || | 446 if (!this.selectedDestinationValid_()) { |
| 438 !this.appState_.selectedDestinationOrigin) { | |
| 439 var destinationMatch = this.convertToDestinationMatch_( | 447 var destinationMatch = this.convertToDestinationMatch_( |
| 440 serializedDefaultDestinationSelectionRulesStr); | 448 serializedDefaultDestinationSelectionRulesStr); |
| 441 if (destinationMatch) { | 449 if (destinationMatch) { |
| 442 this.fetchMatchingDestination_(destinationMatch); | 450 this.fetchMatchingDestination_(destinationMatch); |
| 443 return; | 451 return; |
| 444 } | 452 } |
| 445 } | 453 } |
| 446 | 454 |
| 447 if (!this.systemDefaultDestinationId_ && | 455 if (!this.systemDefaultDestinationId_ && |
| 448 !(this.appState_.selectedDestinationId && | 456 !this.selectedDestinationValid_()) { |
| 449 this.appState_.selectedDestinationOrigin)) { | |
| 450 this.selectPdfDestination_(); | 457 this.selectPdfDestination_(); |
| 451 return; | 458 return; |
| 452 } | 459 } |
| 453 | 460 |
| 454 var origin = print_preview.Destination.Origin.LOCAL; | 461 var origin = print_preview.Destination.Origin.LOCAL; |
| 455 var id = this.systemDefaultDestinationId_; | 462 var id = this.systemDefaultDestinationId_; |
| 456 var account = ''; | 463 var account = ''; |
| 457 var name = ''; | 464 var name = ''; |
| 458 var capabilities = null; | 465 var capabilities = null; |
| 459 var extensionId = ''; | 466 var extensionId = ''; |
| 460 var extensionName = ''; | 467 var extensionName = ''; |
| 461 var foundDestination = false; | 468 var foundDestination = false; |
| 462 if (this.appState_.recentDestinations) { | 469 if (this.appState_.recentDestinations) { |
| 463 // Run through the destinations backwards the most recently used is set | 470 // Run through the destinations forward. As soon as we find a |
| 464 // as the initially selected destination. | 471 // destination, don't select any future destinations, just mark |
| 465 for (var i = this.appState_.recentDestinations.length - 1; i >= 0; | 472 // them recent. Otherwise, there is a race condition between selecting |
| 466 i--) { | 473 // destinations/updating the print ticket and this selecting a new |
| 474 // destination that causes random print preview errors. |
| 475 for (var i = 0; i < this.appState_.recentDestinations.length; i++) { |
| 467 origin = this.appState_.recentDestinations[i].origin; | 476 origin = this.appState_.recentDestinations[i].origin; |
| 468 id = this.appState_.recentDestinations[i].id; | 477 id = this.appState_.recentDestinations[i].id; |
| 469 account = this.appState_.recentDestinations[i].account || ''; | 478 account = this.appState_.recentDestinations[i].account || ''; |
| 470 name = this.appState_.recentDestinations[i].name || ''; | 479 name = this.appState_.recentDestinations[i].name || ''; |
| 471 capabilities = this.appState_.recentDestinations[i].capabilities; | 480 capabilities = this.appState_.recentDestinations[i].capabilities; |
| 472 extensionId = this.appState_.recentDestinations[i].extensionId || | 481 extensionId = this.appState_.recentDestinations[i].extensionId || |
| 473 ''; | 482 ''; |
| 474 extensionName = | 483 extensionName = |
| 475 this.appState_.recentDestinations[i].extensionName || ''; | 484 this.appState_.recentDestinations[i].extensionName || ''; |
| 476 var candidate = | 485 var candidate = |
| 477 this.destinationMap_[this.getDestinationKey_(origin, | 486 this.destinationMap_[this.getDestinationKey_(origin, |
| 478 id, account)]; | 487 id, account)]; |
| 479 if (candidate != null) { | 488 if (candidate != null) { |
| 480 this.selectDestination(candidate); | 489 if (!foundDestination) |
| 490 this.selectDestination(candidate); |
| 481 candidate.isRecent = true; | 491 candidate.isRecent = true; |
| 482 foundDestination = true; | 492 foundDestination = true; |
| 483 } else { | 493 } else if (!foundDestination) { |
| 484 foundDestination = this.fetchPreselectedDestination_( | 494 foundDestination = this.fetchPreselectedDestination_( |
| 485 origin, | 495 origin, |
| 486 id, | 496 id, |
| 487 account, | 497 account, |
| 488 name, | 498 name, |
| 489 capabilities, | 499 capabilities, |
| 490 extensionId, | 500 extensionId, |
| 491 extensionName); | 501 extensionName); |
| 492 } | 502 } |
| 493 } | 503 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 displayNameRegExp, | 701 displayNameRegExp, |
| 692 true /*skipVirtualDestinations*/); | 702 true /*skipVirtualDestinations*/); |
| 693 }, | 703 }, |
| 694 | 704 |
| 695 /** | 705 /** |
| 696 * @return {print_preview.DestinationMatch} Creates rules matching | 706 * @return {print_preview.DestinationMatch} Creates rules matching |
| 697 * previously selected destination. | 707 * previously selected destination. |
| 698 * @private | 708 * @private |
| 699 */ | 709 */ |
| 700 convertPreselectedToDestinationMatch_: function() { | 710 convertPreselectedToDestinationMatch_: function() { |
| 701 if (this.appState_.selectedDestinationId && | 711 if (this.selectedDestinationValid_()) { |
| 702 this.appState_.selectedDestinationOrigin) { | |
| 703 return this.createExactDestinationMatch_( | 712 return this.createExactDestinationMatch_( |
| 704 this.appState_.selectedDestinationOrigin, | 713 this.appState_.selectedDestination.origin, |
| 705 this.appState_.selectedDestinationId); | 714 this.appState_.selectedDestination.id); |
| 706 } | 715 } |
| 707 if (this.systemDefaultDestinationId_) { | 716 if (this.systemDefaultDestinationId_) { |
| 708 return this.createExactDestinationMatch_( | 717 return this.createExactDestinationMatch_( |
| 709 print_preview.Destination.Origin.LOCAL, | 718 print_preview.Destination.Origin.LOCAL, |
| 710 this.systemDefaultDestinationId_); | 719 this.systemDefaultDestinationId_); |
| 711 } | 720 } |
| 712 return null; | 721 return null; |
| 713 }, | 722 }, |
| 714 | 723 |
| 715 /** | 724 /** |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1094 */ | 1103 */ |
| 1095 updateDestination_: function(destination) { | 1104 updateDestination_: function(destination) { |
| 1096 assert(destination.constructor !== Array, 'Single printer expected'); | 1105 assert(destination.constructor !== Array, 'Single printer expected'); |
| 1097 var existingDestination = this.destinationMap_[this.getKey_(destination)]; | 1106 var existingDestination = this.destinationMap_[this.getKey_(destination)]; |
| 1098 if (existingDestination != null) { | 1107 if (existingDestination != null) { |
| 1099 existingDestination.capabilities = destination.capabilities; | 1108 existingDestination.capabilities = destination.capabilities; |
| 1100 } else { | 1109 } else { |
| 1101 this.insertDestination_(destination); | 1110 this.insertDestination_(destination); |
| 1102 } | 1111 } |
| 1103 | 1112 |
| 1104 if (existingDestination == this.selectedDestination_ || | 1113 if (this.selectedDestination_ && |
| 1105 destination == this.selectedDestination_) { | 1114 (existingDestination == this.selectedDestination_ || |
| 1115 destination == this.selectedDestination_)) { |
| 1106 this.appState_.persistSelectedDestination(this.selectedDestination_); | 1116 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 1107 cr.dispatchSimpleEvent( | 1117 cr.dispatchSimpleEvent( |
| 1108 this, | 1118 this, |
| 1109 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 1119 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 1110 } | 1120 } |
| 1111 | 1121 |
| 1112 return existingDestination; | 1122 return existingDestination; |
| 1113 }, | 1123 }, |
| 1114 | 1124 |
| 1115 /** | 1125 /** |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1503 return this.getDestinationKey_( | 1513 return this.getDestinationKey_( |
| 1504 destination.origin, destination.id, destination.account); | 1514 destination.origin, destination.id, destination.account); |
| 1505 } | 1515 } |
| 1506 }; | 1516 }; |
| 1507 | 1517 |
| 1508 // Export | 1518 // Export |
| 1509 return { | 1519 return { |
| 1510 DestinationStore: DestinationStore | 1520 DestinationStore: DestinationStore |
| 1511 }; | 1521 }; |
| 1512 }); | 1522 }); |
| OLD | NEW |