| 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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 */ | 273 */ |
| 274 this.extensionSearchTimeout_ = null; | 274 this.extensionSearchTimeout_ = null; |
| 275 | 275 |
| 276 /** | 276 /** |
| 277 * MDNS service name of destination that we are waiting to register. | 277 * MDNS service name of destination that we are waiting to register. |
| 278 * @type {?string} | 278 * @type {?string} |
| 279 * @private | 279 * @private |
| 280 */ | 280 */ |
| 281 this.waitForRegisterDestination_ = null; | 281 this.waitForRegisterDestination_ = null; |
| 282 | 282 |
| 283 /** |
| 284 * Local destinations are CROS destinations on ChromeOS because they require |
| 285 * extra setup. |
| 286 * @type {!print_preview.Destination.Origin} |
| 287 * @private |
| 288 */ |
| 289 this.platformOrigin_ = cr.isChromeOS ? |
| 290 print_preview.Destination.Origin.CROS : |
| 291 print_preview.Destination.Origin.LOCAL; |
| 292 |
| 283 this.addEventListeners_(); | 293 this.addEventListeners_(); |
| 284 this.reset_(); | 294 this.reset_(); |
| 285 }; | 295 }; |
| 286 | 296 |
| 287 /** | 297 /** |
| 288 * Event types dispatched by the data store. | 298 * Event types dispatched by the data store. |
| 289 * @enum {string} | 299 * @enum {string} |
| 290 */ | 300 */ |
| 291 DestinationStore.EventType = { | 301 DestinationStore.EventType = { |
| 292 DESTINATION_SEARCH_DONE: | 302 DESTINATION_SEARCH_DONE: |
| 293 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE', | 303 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE', |
| 294 DESTINATION_SEARCH_STARTED: | 304 DESTINATION_SEARCH_STARTED: |
| 295 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED', | 305 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED', |
| 296 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT', | 306 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT', |
| 297 DESTINATIONS_INSERTED: | 307 DESTINATIONS_INSERTED: |
| 298 'print_preview.DestinationStore.DESTINATIONS_INSERTED', | 308 'print_preview.DestinationStore.DESTINATIONS_INSERTED', |
| 299 PROVISIONAL_DESTINATION_RESOLVED: | 309 PROVISIONAL_DESTINATION_RESOLVED: |
| 300 'print_preview.DestinationStore.PROVISIONAL_DESTINATION_RESOLVED', | 310 'print_preview.DestinationStore.PROVISIONAL_DESTINATION_RESOLVED', |
| 301 CACHED_SELECTED_DESTINATION_INFO_READY: | 311 CACHED_SELECTED_DESTINATION_INFO_READY: |
| 302 'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY', | 312 'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY', |
| 303 SELECTED_DESTINATION_CAPABILITIES_READY: | 313 SELECTED_DESTINATION_CAPABILITIES_READY: |
| 304 'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY' | 314 'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY'
, |
| 315 PRINTER_CONFIGURED: |
| 316 'print_preview.DestinationStore.PRINTER_CONFIGURED', |
| 305 }; | 317 }; |
| 306 | 318 |
| 307 /** | 319 /** |
| 308 * Delay in milliseconds before the destination store ignores the initial | 320 * Delay in milliseconds before the destination store ignores the initial |
| 309 * destination ID and just selects any printer (since the initial destination | 321 * destination ID and just selects any printer (since the initial destination |
| 310 * was not found). | 322 * was not found). |
| 311 * @private {number} | 323 * @private {number} |
| 312 * @const | 324 * @const |
| 313 */ | 325 */ |
| 314 DestinationStore.AUTO_SELECT_TIMEOUT_ = 15000; | 326 DestinationStore.AUTO_SELECT_TIMEOUT_ = 15000; |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 return; | 695 return; |
| 684 } | 696 } |
| 685 } | 697 } |
| 686 | 698 |
| 687 if (!this.systemDefaultDestinationId_ && | 699 if (!this.systemDefaultDestinationId_ && |
| 688 !this.appState_.isSelectedDestinationValid()) { | 700 !this.appState_.isSelectedDestinationValid()) { |
| 689 this.selectPdfDestination_(); | 701 this.selectPdfDestination_(); |
| 690 return; | 702 return; |
| 691 } | 703 } |
| 692 | 704 |
| 693 var origin = print_preview.Destination.Origin.LOCAL; | 705 var origin = null; |
| 694 var id = this.systemDefaultDestinationId_; | 706 var id = ''; |
| 695 var account = ''; | 707 var account = ''; |
| 696 var name = ''; | 708 var name = ''; |
| 697 var capabilities = null; | 709 var capabilities = null; |
| 698 var extensionId = ''; | 710 var extensionId = ''; |
| 699 var extensionName = ''; | 711 var extensionName = ''; |
| 700 var foundDestination = false; | 712 var foundDestination = false; |
| 701 if (this.appState_.recentDestinations) { | 713 if (this.appState_.recentDestinations) { |
| 702 // Run through the destinations forward. As soon as we find a | 714 // Run through the destinations forward. As soon as we find a |
| 703 // destination, don't select any future destinations, just mark | 715 // destination, don't select any future destinations, just mark |
| 704 // them recent. Otherwise, there is a race condition between selecting | 716 // them recent. Otherwise, there is a race condition between selecting |
| (...skipping 23 matching lines...) Expand all Loading... |
| 728 id, | 740 id, |
| 729 account, | 741 account, |
| 730 name, | 742 name, |
| 731 capabilities, | 743 capabilities, |
| 732 extensionId, | 744 extensionId, |
| 733 extensionName); | 745 extensionName); |
| 734 } | 746 } |
| 735 } | 747 } |
| 736 } | 748 } |
| 737 if (foundDestination) return; | 749 if (foundDestination) return; |
| 750 |
| 738 // Try the system default | 751 // Try the system default |
| 752 id = this.systemDefaultDestinationId_; |
| 753 origin = id == print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? |
| 754 print_preview.Destination.Origin.LOCAL : |
| 755 this.platformOrigin_; |
| 756 account = ''; |
| 739 var candidate = | 757 var candidate = |
| 740 this.destinationMap_[this.getDestinationKey_(origin, id, account)]; | 758 this.destinationMap_[this.getDestinationKey_(origin, id, account)]; |
| 741 if (candidate != null) { | 759 if (candidate != null) { |
| 742 this.selectDestination(candidate); | 760 this.selectDestination(candidate); |
| 743 return; | 761 return; |
| 744 } | 762 } |
| 745 | 763 |
| 746 if (this.fetchPreselectedDestination_( | 764 if (this.fetchPreselectedDestination_( |
| 747 origin, | 765 origin, |
| 748 id, | 766 id, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 769 * destination. | 787 * destination. |
| 770 * @param {string} extensionName Extension name associated with this | 788 * @param {string} extensionName Extension name associated with this |
| 771 * destination. | 789 * destination. |
| 772 * @private | 790 * @private |
| 773 */ | 791 */ |
| 774 fetchPreselectedDestination_: function( | 792 fetchPreselectedDestination_: function( |
| 775 origin, id, account, name, capabilities, extensionId, extensionName) { | 793 origin, id, account, name, capabilities, extensionId, extensionName) { |
| 776 this.autoSelectMatchingDestination_ = | 794 this.autoSelectMatchingDestination_ = |
| 777 this.createExactDestinationMatch_(origin, id); | 795 this.createExactDestinationMatch_(origin, id); |
| 778 | 796 |
| 779 if (origin == print_preview.Destination.Origin.LOCAL) { | 797 if (origin == print_preview.Destination.Origin.LOCAL || |
| 798 origin == print_preview.Destination.Origin.CROS) { |
| 780 this.nativeLayer_.startGetLocalDestinationCapabilities(id); | 799 this.nativeLayer_.startGetLocalDestinationCapabilities(id); |
| 781 return true; | 800 return true; |
| 782 } | 801 } |
| 783 | 802 |
| 784 if (this.cloudPrintInterface_ && | 803 if (this.cloudPrintInterface_ && |
| 785 (origin == print_preview.Destination.Origin.COOKIES || | 804 (origin == print_preview.Destination.Origin.COOKIES || |
| 786 origin == print_preview.Destination.Origin.DEVICE)) { | 805 origin == print_preview.Destination.Origin.DEVICE)) { |
| 787 this.cloudPrintInterface_.printer(id, origin, account); | 806 this.cloudPrintInterface_.printer(id, origin, account); |
| 788 return true; | 807 return true; |
| 789 } | 808 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 | 859 |
| 841 /** | 860 /** |
| 842 * Attempts to find a destination matching the provided rules. | 861 * Attempts to find a destination matching the provided rules. |
| 843 * @param {!print_preview.DestinationMatch} destinationMatch Rules to match. | 862 * @param {!print_preview.DestinationMatch} destinationMatch Rules to match. |
| 844 * @private | 863 * @private |
| 845 */ | 864 */ |
| 846 fetchMatchingDestination_: function(destinationMatch) { | 865 fetchMatchingDestination_: function(destinationMatch) { |
| 847 this.autoSelectMatchingDestination_ = destinationMatch; | 866 this.autoSelectMatchingDestination_ = destinationMatch; |
| 848 | 867 |
| 849 if (destinationMatch.matchOrigin( | 868 if (destinationMatch.matchOrigin( |
| 850 print_preview.Destination.Origin.LOCAL)) { | 869 print_preview.Destination.Origin.LOCAL) || |
| 870 destinationMatch.matchOrigin( |
| 871 print_preview.Destination.Origin.CROS)) { |
| 851 this.startLoadLocalDestinations(); | 872 this.startLoadLocalDestinations(); |
| 852 } | 873 } |
| 853 if (destinationMatch.matchOrigin( | 874 if (destinationMatch.matchOrigin( |
| 854 print_preview.Destination.Origin.PRIVET)) { | 875 print_preview.Destination.Origin.PRIVET)) { |
| 855 this.startLoadPrivetDestinations(); | 876 this.startLoadPrivetDestinations(); |
| 856 } | 877 } |
| 857 if (destinationMatch.matchOrigin( | 878 if (destinationMatch.matchOrigin( |
| 858 print_preview.Destination.Origin.EXTENSION)) { | 879 print_preview.Destination.Origin.EXTENSION)) { |
| 859 this.startLoadExtensionDestinations(); | 880 this.startLoadExtensionDestinations(); |
| 860 } | 881 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 if (!isLocal && !isCloud) { | 916 if (!isLocal && !isCloud) { |
| 896 console.error('Unsupported type: "' + matchRules.kind + '"'); | 917 console.error('Unsupported type: "' + matchRules.kind + '"'); |
| 897 return null; | 918 return null; |
| 898 } | 919 } |
| 899 | 920 |
| 900 var origins = []; | 921 var origins = []; |
| 901 if (isLocal) { | 922 if (isLocal) { |
| 902 origins.push(print_preview.Destination.Origin.LOCAL); | 923 origins.push(print_preview.Destination.Origin.LOCAL); |
| 903 origins.push(print_preview.Destination.Origin.PRIVET); | 924 origins.push(print_preview.Destination.Origin.PRIVET); |
| 904 origins.push(print_preview.Destination.Origin.EXTENSION); | 925 origins.push(print_preview.Destination.Origin.EXTENSION); |
| 926 origins.push(print_preview.Destination.Origin.CROS); |
| 905 } | 927 } |
| 906 if (isCloud) { | 928 if (isCloud) { |
| 907 origins.push(print_preview.Destination.Origin.COOKIES); | 929 origins.push(print_preview.Destination.Origin.COOKIES); |
| 908 origins.push(print_preview.Destination.Origin.DEVICE); | 930 origins.push(print_preview.Destination.Origin.DEVICE); |
| 909 origins.push(print_preview.Destination.Origin.PROFILE); | 931 origins.push(print_preview.Destination.Origin.PROFILE); |
| 910 } | 932 } |
| 911 | 933 |
| 912 var idRegExp = null; | 934 var idRegExp = null; |
| 913 try { | 935 try { |
| 914 if (matchRules.idPattern) { | 936 if (matchRules.idPattern) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 940 * @private | 962 * @private |
| 941 */ | 963 */ |
| 942 convertPreselectedToDestinationMatch_: function() { | 964 convertPreselectedToDestinationMatch_: function() { |
| 943 if (this.appState_.isSelectedDestinationValid()) { | 965 if (this.appState_.isSelectedDestinationValid()) { |
| 944 return this.createExactDestinationMatch_( | 966 return this.createExactDestinationMatch_( |
| 945 this.appState_.selectedDestination.origin, | 967 this.appState_.selectedDestination.origin, |
| 946 this.appState_.selectedDestination.id); | 968 this.appState_.selectedDestination.id); |
| 947 } | 969 } |
| 948 if (this.systemDefaultDestinationId_) { | 970 if (this.systemDefaultDestinationId_) { |
| 949 return this.createExactDestinationMatch_( | 971 return this.createExactDestinationMatch_( |
| 950 print_preview.Destination.Origin.LOCAL, | 972 this.platformOrigin_, |
| 951 this.systemDefaultDestinationId_); | 973 this.systemDefaultDestinationId_); |
| 952 } | 974 } |
| 953 return null; | 975 return null; |
| 954 }, | 976 }, |
| 955 | 977 |
| 956 /** | 978 /** |
| 957 * @param {!print_preview.Destination.Origin} origin Destination origin. | 979 * @param {!print_preview.Destination.Origin} origin Destination origin. |
| 958 * @param {string} id Destination id. | 980 * @param {string} id Destination id. |
| 959 * @return {!print_preview.DestinationMatch} Creates rules matching | 981 * @return {!print_preview.DestinationMatch} Creates rules matching |
| 960 * provided destination. | 982 * provided destination. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1077 destination.id, destination.origin, destination.account); | 1099 destination.id, destination.origin, destination.account); |
| 1078 } | 1100 } |
| 1079 } else { | 1101 } else { |
| 1080 cr.dispatchSimpleEvent( | 1102 cr.dispatchSimpleEvent( |
| 1081 this, | 1103 this, |
| 1082 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 1104 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 1083 } | 1105 } |
| 1084 }, | 1106 }, |
| 1085 | 1107 |
| 1086 /** | 1108 /** |
| 1109 * Attempt to resolve the capabilities for a Chrome OS printer. |
| 1110 * @param {!print_preview.Destination} destination The destination which |
| 1111 * requires resolution. |
| 1112 */ |
| 1113 resolveCrosDestination: function(destination) { |
| 1114 assert(destination.origin == print_preview.Destination.Origin.CROS); |
| 1115 this.nativeLayer_.setupPrinter(destination.id).then( |
| 1116 /** |
| 1117 * Handle the result of a successful PRINTER_SETUP request. |
| 1118 * @param {!print_preview.PrinterSetupResponse} response. |
| 1119 */ |
| 1120 function(response) { |
| 1121 this.dispatchEvent(new CustomEvent( |
| 1122 DestinationStore.EventType.PRINTER_CONFIGURED, { |
| 1123 detail: response |
| 1124 })); |
| 1125 }.bind(this), |
| 1126 /** |
| 1127 * Calling printer setup failed. |
| 1128 */ |
| 1129 function() { |
| 1130 this.dispatchEvent(new CustomEvent( |
| 1131 DestinationStore.EventType.PRINTER_CONFIGURED, |
| 1132 {detail: {printerId: destination.id, success: false}})); |
| 1133 }.bind(this)); |
| 1134 }, |
| 1135 |
| 1136 /** |
| 1087 * Attempts to resolve a provisional destination. | 1137 * Attempts to resolve a provisional destination. |
| 1088 * @param {!print_preview.Destination} destinaion Provisional destination | 1138 * @param {!print_preview.Destination} destinaion Provisional destination |
| 1089 * that should be resolved. | 1139 * that should be resolved. |
| 1090 */ | 1140 */ |
| 1091 resolveProvisionalDestination: function(destination) { | 1141 resolveProvisionalDestination: function(destination) { |
| 1092 assert( | 1142 assert( |
| 1093 destination.provisionalType == | 1143 destination.provisionalType == |
| 1094 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION, | 1144 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION, |
| 1095 'Provisional type cannot be resolved.'); | 1145 'Provisional type cannot be resolved.'); |
| 1096 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); | 1146 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1112 /** | 1162 /** |
| 1113 * Attempts to select system default destination with a fallback to | 1163 * Attempts to select system default destination with a fallback to |
| 1114 * 'Save to PDF' destination. | 1164 * 'Save to PDF' destination. |
| 1115 * @private | 1165 * @private |
| 1116 */ | 1166 */ |
| 1117 selectDefaultDestination_: function() { | 1167 selectDefaultDestination_: function() { |
| 1118 if (this.systemDefaultDestinationId_) { | 1168 if (this.systemDefaultDestinationId_) { |
| 1119 if (this.autoSelectMatchingDestination_ && | 1169 if (this.autoSelectMatchingDestination_ && |
| 1120 !this.autoSelectMatchingDestination_.matchIdAndOrigin( | 1170 !this.autoSelectMatchingDestination_.matchIdAndOrigin( |
| 1121 this.systemDefaultDestinationId_, | 1171 this.systemDefaultDestinationId_, |
| 1122 print_preview.Destination.Origin.LOCAL)) { | 1172 this.plaformOrigin_)) { |
| 1123 if (this.fetchPreselectedDestination_( | 1173 if (this.fetchPreselectedDestination_( |
| 1124 print_preview.Destination.Origin.LOCAL, | 1174 this.platformOrigin_, |
| 1125 this.systemDefaultDestinationId_, | 1175 this.systemDefaultDestinationId_, |
| 1126 '' /*account*/, | 1176 '' /*account*/, |
| 1127 '' /*name*/, | 1177 '' /*name*/, |
| 1128 null /*capabilities*/, | 1178 null /*capabilities*/, |
| 1129 '' /*extensionId*/, | 1179 '' /*extensionId*/, |
| 1130 '' /*extensionName*/)) { | 1180 '' /*extensionName*/)) { |
| 1131 return; | 1181 return; |
| 1132 } | 1182 } |
| 1133 } | 1183 } |
| 1134 } | 1184 } |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1517 * destination already exists, otherwise it creates a new destination and | 1567 * destination already exists, otherwise it creates a new destination and |
| 1518 * then updates its capabilities. | 1568 * then updates its capabilities. |
| 1519 * @param {Event} event Contains the capabilities of the local print | 1569 * @param {Event} event Contains the capabilities of the local print |
| 1520 * destination. | 1570 * destination. |
| 1521 * @private | 1571 * @private |
| 1522 */ | 1572 */ |
| 1523 onLocalDestinationCapabilitiesSet_: function(event) { | 1573 onLocalDestinationCapabilitiesSet_: function(event) { |
| 1524 var destinationId = event.settingsInfo['printerId']; | 1574 var destinationId = event.settingsInfo['printerId']; |
| 1525 var printerName = event.settingsInfo['printerName']; | 1575 var printerName = event.settingsInfo['printerName']; |
| 1526 var printerDescription = event.settingsInfo['printerDescription']; | 1576 var printerDescription = event.settingsInfo['printerDescription']; |
| 1577 // PDF is special since we don't need to query the device for |
| 1578 // capabilities. |
| 1579 var origin = destinationId == |
| 1580 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? |
| 1581 print_preview.Destination.Origin.LOCAL : this.platformOrigin_; |
| 1527 var key = this.getDestinationKey_( | 1582 var key = this.getDestinationKey_( |
| 1528 print_preview.Destination.Origin.LOCAL, | 1583 origin, |
| 1529 destinationId, | 1584 destinationId, |
| 1530 ''); | 1585 ''); |
| 1531 var destination = this.destinationMap_[key]; | 1586 var destination = this.destinationMap_[key]; |
| 1532 var capabilities = DestinationStore.localizeCapabilities_( | 1587 var capabilities = DestinationStore.localizeCapabilities_( |
| 1533 event.settingsInfo.capabilities); | 1588 event.settingsInfo.capabilities); |
| 1534 // Special case for PDF printer (until local printers capabilities are | 1589 // Special case for PDF printer (until local printers capabilities are |
| 1535 // reported in CDD format too). | 1590 // reported in CDD format too). |
| 1536 if (destinationId == | 1591 if (destinationId == |
| 1537 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { | 1592 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { |
| 1538 if (destination) { | 1593 if (destination) { |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1745 return this.getDestinationKey_( | 1800 return this.getDestinationKey_( |
| 1746 destination.origin, destination.id, destination.account); | 1801 destination.origin, destination.id, destination.account); |
| 1747 } | 1802 } |
| 1748 }; | 1803 }; |
| 1749 | 1804 |
| 1750 // Export | 1805 // Export |
| 1751 return { | 1806 return { |
| 1752 DestinationStore: DestinationStore | 1807 DestinationStore: DestinationStore |
| 1753 }; | 1808 }; |
| 1754 }); | 1809 }); |
| OLD | NEW |