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 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 * @return {boolean} Whether capabilities fetch was successfully started. | 666 * @return {boolean} Whether capabilities fetch was successfully started. |
667 * @private | 667 * @private |
668 */ | 668 */ |
669 fetchPreselectedDestination_: function( | 669 fetchPreselectedDestination_: function( |
670 origin, id, account, name, capabilities, extensionId, extensionName) { | 670 origin, id, account, name, capabilities, extensionId, extensionName) { |
671 this.autoSelectMatchingDestination_ = | 671 this.autoSelectMatchingDestination_ = |
672 this.createExactDestinationMatch_(origin, id); | 672 this.createExactDestinationMatch_(origin, id); |
673 | 673 |
674 if (origin == print_preview.DestinationOrigin.LOCAL || | 674 if (origin == print_preview.DestinationOrigin.LOCAL || |
675 origin == print_preview.DestinationOrigin.CROS) { | 675 origin == print_preview.DestinationOrigin.CROS) { |
676 this.nativeLayer_.startGetLocalDestinationCapabilities(id); | 676 this.nativeLayer_.getPrinterCapabilities(id).then( |
| 677 this.onLocalDestinationCapabilitiesSet_.bind(this), |
| 678 this.onGetCapabilitiesFail_.bind(this, |
| 679 /** @type {print_preview.DestinationOrigin} */ (origin), |
| 680 id)); |
677 return true; | 681 return true; |
678 } | 682 } |
679 | 683 |
680 if (this.cloudPrintInterface_ && | 684 if (this.cloudPrintInterface_ && |
681 (origin == print_preview.DestinationOrigin.COOKIES || | 685 (origin == print_preview.DestinationOrigin.COOKIES || |
682 origin == print_preview.DestinationOrigin.DEVICE)) { | 686 origin == print_preview.DestinationOrigin.DEVICE)) { |
683 this.cloudPrintInterface_.printer( | 687 this.cloudPrintInterface_.printer( |
684 id, | 688 id, |
685 /** @type {print_preview.DestinationOrigin} */(origin), | 689 /** @type {print_preview.DestinationOrigin} */(origin), |
686 account); | 690 account); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 })) { | 956 })) { |
953 this.metrics_.record(destination.isPrivet ? | 957 this.metrics_.record(destination.isPrivet ? |
954 print_preview.Metrics.DestinationSearchBucket. | 958 print_preview.Metrics.DestinationSearchBucket. |
955 PRIVET_DUPLICATE_SELECTED : | 959 PRIVET_DUPLICATE_SELECTED : |
956 print_preview.Metrics.DestinationSearchBucket. | 960 print_preview.Metrics.DestinationSearchBucket. |
957 CLOUD_DUPLICATE_SELECTED); | 961 CLOUD_DUPLICATE_SELECTED); |
958 } | 962 } |
959 // Notify about selected destination change. | 963 // Notify about selected destination change. |
960 cr.dispatchSimpleEvent( | 964 cr.dispatchSimpleEvent( |
961 this, DestinationStore.EventType.DESTINATION_SELECT); | 965 this, DestinationStore.EventType.DESTINATION_SELECT); |
962 // Request destination capabilities, of not known yet. | 966 // Request destination capabilities from backend, since they are not |
| 967 // known yet. |
963 if (destination.capabilities == null) { | 968 if (destination.capabilities == null) { |
964 if (destination.isPrivet) { | 969 if (destination.isPrivet) { |
965 this.nativeLayer_.startGetPrivetDestinationCapabilities( | 970 this.nativeLayer_.getPrivetPrinterCapabilities(destination.id).then( |
966 destination.id); | 971 this.onPrivetCapabilitiesSet_.bind(this), |
| 972 this.onGetCapabilitiesFail_.bind(this, destination.origin, |
| 973 destination.id)); |
967 } else if (destination.isExtension) { | 974 } else if (destination.isExtension) { |
968 this.nativeLayer_.startGetExtensionDestinationCapabilities( | 975 this.nativeLayer_.getExtensionPrinterCapabilities(destination.id) |
969 destination.id); | 976 .then( |
| 977 this.onExtensionCapabilitiesSet_.bind(this, destination.id), |
| 978 this.onGetCapabilitiesFail_.bind(this, destination.origin, |
| 979 destination.id) |
| 980 ); |
970 } else if (destination.isLocal) { | 981 } else if (destination.isLocal) { |
971 this.nativeLayer_.startGetLocalDestinationCapabilities( | 982 this.nativeLayer_.getPrinterCapabilities(destination.id).then( |
972 destination.id); | 983 this.onLocalDestinationCapabilitiesSet_.bind(this), |
| 984 this.onGetCapabilitiesFail_.bind(this, destination.origin, |
| 985 destination.id)); |
973 } else { | 986 } else { |
974 assert(this.cloudPrintInterface_ != null, | 987 assert(this.cloudPrintInterface_ != null, |
975 'Cloud destination selected, but GCP is not enabled'); | 988 'Cloud destination selected, but GCP is not enabled'); |
976 this.cloudPrintInterface_.printer( | 989 this.cloudPrintInterface_.printer( |
977 destination.id, destination.origin, destination.account); | 990 destination.id, destination.origin, destination.account); |
978 } | 991 } |
979 } else { | 992 } else { |
980 cr.dispatchSimpleEvent( | 993 cr.dispatchSimpleEvent( |
981 this, | 994 this, |
982 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 995 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1330 }, | 1343 }, |
1331 | 1344 |
1332 /** | 1345 /** |
1333 * Binds handlers to events. | 1346 * Binds handlers to events. |
1334 * @private | 1347 * @private |
1335 */ | 1348 */ |
1336 addEventListeners_: function() { | 1349 addEventListeners_: function() { |
1337 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | 1350 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); |
1338 this.tracker_.add( | 1351 this.tracker_.add( |
1339 nativeLayerEventTarget, | 1352 nativeLayerEventTarget, |
1340 print_preview.NativeLayer.EventType.CAPABILITIES_SET, | |
1341 this.onLocalDestinationCapabilitiesSet_.bind(this)); | |
1342 this.tracker_.add( | |
1343 nativeLayerEventTarget, | |
1344 print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL, | |
1345 this.onGetCapabilitiesFail_.bind(this)); | |
1346 this.tracker_.add( | |
1347 nativeLayerEventTarget, | |
1348 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 1353 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
1349 this.onDestinationsReload_.bind(this)); | 1354 this.onDestinationsReload_.bind(this)); |
1350 this.tracker_.add( | 1355 this.tracker_.add( |
1351 nativeLayerEventTarget, | 1356 nativeLayerEventTarget, |
1352 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, | |
1353 this.onPrivetCapabilitiesSet_.bind(this)); | |
1354 this.tracker_.add( | |
1355 nativeLayerEventTarget, | |
1356 print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET, | |
1357 this.onExtensionCapabilitiesSet_.bind(this)); | |
1358 this.tracker_.add( | |
1359 nativeLayerEventTarget, | |
1360 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, | 1357 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, |
1361 this.handleProvisionalDestinationResolved_.bind(this)); | 1358 this.handleProvisionalDestinationResolved_.bind(this)); |
1362 }, | 1359 }, |
1363 | 1360 |
1364 /** | 1361 /** |
1365 * Creates a local PDF print destination. | 1362 * Creates a local PDF print destination. |
1366 * @private | 1363 * @private |
1367 */ | 1364 */ |
1368 createLocalPdfPrintDestination_: function() { | 1365 createLocalPdfPrintDestination_: function() { |
1369 // TODO(alekseys): Create PDF printer in the native code and send its | 1366 // TODO(alekseys): Create PDF printer in the native code and send its |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1412 this.isLocalDestinationSearchInProgress_ = false; | 1409 this.isLocalDestinationSearchInProgress_ = false; |
1413 cr.dispatchSimpleEvent( | 1410 cr.dispatchSimpleEvent( |
1414 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 1411 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
1415 }, | 1412 }, |
1416 | 1413 |
1417 /** | 1414 /** |
1418 * Called when the native layer retrieves the capabilities for the selected | 1415 * Called when the native layer retrieves the capabilities for the selected |
1419 * local destination. Updates the destination with new capabilities if the | 1416 * local destination. Updates the destination with new capabilities if the |
1420 * destination already exists, otherwise it creates a new destination and | 1417 * destination already exists, otherwise it creates a new destination and |
1421 * then updates its capabilities. | 1418 * then updates its capabilities. |
1422 * @param {Event} event Contains the capabilities of the local print | 1419 * @param {print_preview.PrinterCapabilitiesResponse} settingsInfo Contains |
1423 * destination. | 1420 * information about and capabilities of the local print destination. |
1424 * @private | 1421 * @private |
1425 */ | 1422 */ |
1426 onLocalDestinationCapabilitiesSet_: function(event) { | 1423 onLocalDestinationCapabilitiesSet_: function(settingsInfo) { |
1427 var destinationId = event.settingsInfo['printerId']; | 1424 var destinationId = settingsInfo['printerId']; |
1428 var printerName = event.settingsInfo['printerName']; | 1425 var printerName = settingsInfo['printerName']; |
1429 var printerDescription = event.settingsInfo['printerDescription']; | 1426 var printerDescription = settingsInfo['printerDescription']; |
1430 // PDF is special since we don't need to query the device for | 1427 // PDF is special since we don't need to query the device for |
1431 // capabilities. | 1428 // capabilities. |
1432 var origin = destinationId == | 1429 var origin = destinationId == |
1433 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? | 1430 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? |
1434 print_preview.DestinationOrigin.LOCAL : this.platformOrigin_; | 1431 print_preview.DestinationOrigin.LOCAL : this.platformOrigin_; |
1435 var key = this.getDestinationKey_( | 1432 var key = this.getDestinationKey_( |
1436 origin, | 1433 origin, |
1437 destinationId, | 1434 destinationId, |
1438 ''); | 1435 ''); |
1439 var destination = this.destinationMap_[key]; | 1436 var destination = this.destinationMap_[key]; |
1440 var capabilities = DestinationStore.localizeCapabilities_( | 1437 var capabilities = DestinationStore.localizeCapabilities_( |
1441 event.settingsInfo.capabilities); | 1438 settingsInfo.capabilities); |
1442 // Special case for PDF printer (until local printers capabilities are | 1439 // Special case for PDF printer (until local printers capabilities are |
1443 // reported in CDD format too). | 1440 // reported in CDD format too). |
1444 if (destinationId == | 1441 if (destinationId == |
1445 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { | 1442 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { |
1446 if (destination) { | 1443 if (destination) { |
1447 destination.capabilities = capabilities; | 1444 destination.capabilities = capabilities; |
1448 } | 1445 } |
1449 } else { | 1446 } else { |
1450 if (destination) { | 1447 if (destination) { |
1451 // In case there were multiple capabilities request for this local | 1448 // In case there were multiple capabilities request for this local |
1452 // destination, just ignore the later ones. | 1449 // destination, just ignore the later ones. |
1453 if (destination.capabilities != null) { | 1450 if (destination.capabilities != null) { |
1454 return; | 1451 return; |
1455 } | 1452 } |
1456 destination.capabilities = capabilities; | 1453 destination.capabilities = capabilities; |
1457 } else { | 1454 } else { |
1458 var isEnterprisePrinter = event.settingsInfo['cupsEnterprisePrinter']; | 1455 var isEnterprisePrinter = settingsInfo['cupsEnterprisePrinter']; |
1459 destination = print_preview.LocalDestinationParser.parse( | 1456 destination = print_preview.LocalDestinationParser.parse( |
1460 {deviceName: destinationId, | 1457 {deviceName: destinationId, |
1461 printerName: printerName, | 1458 printerName: printerName, |
1462 cupsEnterprisePrinter: isEnterprisePrinter, | 1459 cupsEnterprisePrinter: isEnterprisePrinter, |
1463 printerDescription: printerDescription}); | 1460 printerDescription: printerDescription}); |
1464 destination.capabilities = capabilities; | 1461 destination.capabilities = capabilities; |
1465 this.insertDestination_(destination); | 1462 this.insertDestination_(destination); |
1466 } | 1463 } |
1467 } | 1464 } |
1468 if (this.selectedDestination_ && | 1465 if (this.selectedDestination_ && |
1469 this.selectedDestination_.id == destinationId) { | 1466 this.selectedDestination_.id == destinationId) { |
1470 cr.dispatchSimpleEvent( | 1467 cr.dispatchSimpleEvent( |
1471 this, | 1468 this, |
1472 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 1469 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
1473 } | 1470 } |
1474 }, | 1471 }, |
1475 | 1472 |
1476 /** | 1473 /** |
1477 * Called when a request to get a local destination's print capabilities | 1474 * Called when a request to get a local destination's print capabilities |
1478 * fails. If the destination is the initial destination, auto-select another | 1475 * fails. If the destination is the initial destination, auto-select another |
1479 * destination instead. | 1476 * destination instead. |
1480 * @param {Event} event Contains the destination ID that failed. | 1477 * @param {print_preview.DestinationOrigin} origin The origin type of the |
| 1478 * failed destination. |
| 1479 * @param {string} destinationId The destination ID that failed. |
1481 * @private | 1480 * @private |
1482 */ | 1481 */ |
1483 onGetCapabilitiesFail_: function(event) { | 1482 onGetCapabilitiesFail_: function(origin, destinationId) { |
1484 console.warn('Failed to get print capabilities for printer ' + | 1483 console.warn('Failed to get print capabilities for printer ' + |
1485 event.destinationId); | 1484 destinationId); |
1486 if (this.autoSelectMatchingDestination_ && | 1485 if (this.autoSelectMatchingDestination_ && |
1487 this.autoSelectMatchingDestination_.matchIdAndOrigin( | 1486 this.autoSelectMatchingDestination_.matchIdAndOrigin( |
1488 event.destinationId, event.destinationOrigin)) { | 1487 destinationId, origin)) { |
1489 this.selectDefaultDestination_(); | 1488 this.selectDefaultDestination_(); |
1490 } | 1489 } |
1491 }, | 1490 }, |
1492 | 1491 |
1493 /** | 1492 /** |
1494 * Called when the /search call completes, either successfully or not. | 1493 * Called when the /search call completes, either successfully or not. |
1495 * In case of success, stores fetched destinations. | 1494 * In case of success, stores fetched destinations. |
1496 * @param {Event} event Contains the request result. | 1495 * @param {Event} event Contains the request result. |
1497 * @private | 1496 * @private |
1498 */ | 1497 */ |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1568 this.waitForRegisterDestination_ = null; | 1567 this.waitForRegisterDestination_ = null; |
1569 this.onDestinationsReload_(); | 1568 this.onDestinationsReload_(); |
1570 } else { | 1569 } else { |
1571 this.insertDestinations_( | 1570 this.insertDestinations_( |
1572 print_preview.PrivetDestinationParser.parse(printer)); | 1571 print_preview.PrivetDestinationParser.parse(printer)); |
1573 } | 1572 } |
1574 }, | 1573 }, |
1575 | 1574 |
1576 /** | 1575 /** |
1577 * Called when capabilities for a privet printer are set. | 1576 * Called when capabilities for a privet printer are set. |
1578 * @param {Object} event Contains the capabilities and printer ID. | 1577 * @param {!print_preview.PrivetPrinterCapabilitiesResponse} printerInfo |
| 1578 * Contains the privet printer's description and capabilities. |
1579 * @private | 1579 * @private |
1580 */ | 1580 */ |
1581 onPrivetCapabilitiesSet_: function(event) { | 1581 onPrivetCapabilitiesSet_: function(printerInfo) { |
1582 var destinations = | 1582 var destinations = |
1583 print_preview.PrivetDestinationParser.parse(event.printer); | 1583 print_preview.PrivetDestinationParser.parse(printerInfo.printer); |
1584 destinations.forEach(function(dest) { | 1584 destinations.forEach(function(dest) { |
1585 dest.capabilities = event.capabilities; | 1585 dest.capabilities = printerInfo.capabilities; |
1586 this.updateDestination_(dest); | 1586 this.updateDestination_(dest); |
1587 }, this); | 1587 }, this); |
1588 }, | 1588 }, |
1589 | 1589 |
1590 /** | 1590 /** |
1591 * Called when an extension responds to a getExtensionDestinations | 1591 * Called when an extension responds to a getExtensionDestinations |
1592 * request. | 1592 * request. |
1593 * @param {!Array<!{extensionId: string, | 1593 * @param {!Array<!{extensionId: string, |
1594 * extensionName: string, | 1594 * extensionName: string, |
1595 * id: string, | 1595 * id: string, |
(...skipping 14 matching lines...) Expand all Loading... |
1610 */ | 1610 */ |
1611 onExtensionPrintersDone_: function() { | 1611 onExtensionPrintersDone_: function() { |
1612 if (this.isExtensionDestinationSearchInProgress_) { | 1612 if (this.isExtensionDestinationSearchInProgress_) { |
1613 clearTimeout(this.extensionSearchTimeout_); | 1613 clearTimeout(this.extensionSearchTimeout_); |
1614 this.endExtensionPrinterSearch_(); | 1614 this.endExtensionPrinterSearch_(); |
1615 } | 1615 } |
1616 }, | 1616 }, |
1617 | 1617 |
1618 /** | 1618 /** |
1619 * Called when capabilities for an extension managed printer are set. | 1619 * Called when capabilities for an extension managed printer are set. |
1620 * @param {Object} event Contains the printer's capabilities and ID. | 1620 * @param {string} printerId The printer Id. |
| 1621 * @param {!print_preview.Cdd} capabilities The printer's capabilities. |
1621 * @private | 1622 * @private |
1622 */ | 1623 */ |
1623 onExtensionCapabilitiesSet_: function(event) { | 1624 onExtensionCapabilitiesSet_: function(printerId, capabilities) { |
1624 var destinationKey = this.getDestinationKey_( | 1625 var destinationKey = this.getDestinationKey_( |
1625 print_preview.DestinationOrigin.EXTENSION, | 1626 print_preview.DestinationOrigin.EXTENSION, |
1626 event.printerId, | 1627 printerId, |
1627 '' /* account */); | 1628 '' /* account */); |
1628 var destination = this.destinationMap_[destinationKey]; | 1629 var destination = this.destinationMap_[destinationKey]; |
1629 if (!destination) | 1630 if (!destination) |
1630 return; | 1631 return; |
1631 destination.capabilities = event.capabilities; | 1632 destination.capabilities = capabilities; |
1632 this.updateDestination_(destination); | 1633 this.updateDestination_(destination); |
1633 }, | 1634 }, |
1634 | 1635 |
1635 /** | 1636 /** |
1636 * Called from native layer after the user was requested to sign in, and did | 1637 * Called from native layer after the user was requested to sign in, and did |
1637 * so successfully. | 1638 * so successfully. |
1638 * @private | 1639 * @private |
1639 */ | 1640 */ |
1640 onDestinationsReload_: function() { | 1641 onDestinationsReload_: function() { |
1641 this.reset_(); | 1642 this.reset_(); |
(...skipping 26 matching lines...) Expand all Loading... |
1668 return this.getDestinationKey_( | 1669 return this.getDestinationKey_( |
1669 destination.origin, destination.id, destination.account); | 1670 destination.origin, destination.id, destination.account); |
1670 } | 1671 } |
1671 }; | 1672 }; |
1672 | 1673 |
1673 // Export | 1674 // Export |
1674 return { | 1675 return { |
1675 DestinationStore: DestinationStore | 1676 DestinationStore: DestinationStore |
1676 }; | 1677 }; |
1677 }); | 1678 }); |
OLD | NEW |