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