| 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 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 print_preview.DestinationOrigin.PRIVET)) { | 741 print_preview.DestinationOrigin.PRIVET)) { |
| 742 this.startLoadPrivetDestinations(); | 742 this.startLoadPrivetDestinations(); |
| 743 } | 743 } |
| 744 if (destinationMatch.matchOrigin( | 744 if (destinationMatch.matchOrigin( |
| 745 print_preview.DestinationOrigin.EXTENSION)) { | 745 print_preview.DestinationOrigin.EXTENSION)) { |
| 746 this.startLoadExtensionDestinations(); | 746 this.startLoadExtensionDestinations(); |
| 747 } | 747 } |
| 748 if (destinationMatch.matchOrigin( | 748 if (destinationMatch.matchOrigin( |
| 749 print_preview.DestinationOrigin.COOKIES) || | 749 print_preview.DestinationOrigin.COOKIES) || |
| 750 destinationMatch.matchOrigin( | 750 destinationMatch.matchOrigin( |
| 751 print_preview.DestinationOrigin.DEVICE) || | 751 print_preview.DestinationOrigin.DEVICE)) { |
| 752 destinationMatch.matchOrigin( | |
| 753 print_preview.DestinationOrigin.PROFILE)) { | |
| 754 this.startLoadCloudDestinations(); | 752 this.startLoadCloudDestinations(); |
| 755 } | 753 } |
| 756 }, | 754 }, |
| 757 | 755 |
| 758 /** | 756 /** |
| 759 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized | 757 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized |
| 760 * default destination selection rules. | 758 * default destination selection rules. |
| 761 * @return {?print_preview.DestinationMatch} Creates rules matching | 759 * @return {?print_preview.DestinationMatch} Creates rules matching |
| 762 * previously selected destination. | 760 * previously selected destination. |
| 763 * @private | 761 * @private |
| (...skipping 22 matching lines...) Expand all Loading... |
| 786 var origins = []; | 784 var origins = []; |
| 787 if (isLocal) { | 785 if (isLocal) { |
| 788 origins.push(print_preview.DestinationOrigin.LOCAL); | 786 origins.push(print_preview.DestinationOrigin.LOCAL); |
| 789 origins.push(print_preview.DestinationOrigin.PRIVET); | 787 origins.push(print_preview.DestinationOrigin.PRIVET); |
| 790 origins.push(print_preview.DestinationOrigin.EXTENSION); | 788 origins.push(print_preview.DestinationOrigin.EXTENSION); |
| 791 origins.push(print_preview.DestinationOrigin.CROS); | 789 origins.push(print_preview.DestinationOrigin.CROS); |
| 792 } | 790 } |
| 793 if (isCloud) { | 791 if (isCloud) { |
| 794 origins.push(print_preview.DestinationOrigin.COOKIES); | 792 origins.push(print_preview.DestinationOrigin.COOKIES); |
| 795 origins.push(print_preview.DestinationOrigin.DEVICE); | 793 origins.push(print_preview.DestinationOrigin.DEVICE); |
| 796 origins.push(print_preview.DestinationOrigin.PROFILE); | |
| 797 } | 794 } |
| 798 | 795 |
| 799 var idRegExp = null; | 796 var idRegExp = null; |
| 800 try { | 797 try { |
| 801 if (matchRules.idPattern) { | 798 if (matchRules.idPattern) { |
| 802 idRegExp = new RegExp(matchRules.idPattern || '.*'); | 799 idRegExp = new RegExp(matchRules.idPattern || '.*'); |
| 803 } | 800 } |
| 804 } catch (e) { | 801 } catch (e) { |
| 805 console.error('Failed to parse regexp for "id": ' + e); | 802 console.error('Failed to parse regexp for "id": ' + e); |
| 806 } | 803 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 985 /** | 982 /** |
| 986 * Attempts to resolve a provisional destination. | 983 * Attempts to resolve a provisional destination. |
| 987 * @param {!print_preview.Destination} destination Provisional destination | 984 * @param {!print_preview.Destination} destination Provisional destination |
| 988 * that should be resolved. | 985 * that should be resolved. |
| 989 */ | 986 */ |
| 990 resolveProvisionalDestination: function(destination) { | 987 resolveProvisionalDestination: function(destination) { |
| 991 assert( | 988 assert( |
| 992 destination.provisionalType == | 989 destination.provisionalType == |
| 993 print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION, | 990 print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION, |
| 994 'Provisional type cannot be resolved.'); | 991 'Provisional type cannot be resolved.'); |
| 995 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); | 992 this.nativeLayer_.grantExtensionPrinterAccess(destination.id).then( |
| 993 /** |
| 994 * @param {!print_preview.ProvisionalDestinationInfo} |
| 995 * destinationInfo Information about the resolved printer. |
| 996 */ |
| 997 function(destinationInfo) { |
| 998 /** |
| 999 * Removes the destination from the store and replaces it with a |
| 1000 * destination created from the resolved destination properties, if |
| 1001 * any are reported. Then sends a PROVISIONAL_DESTINATION_RESOLVED |
| 1002 * event. |
| 1003 */ |
| 1004 this.removeProvisionalDestination_(destination.id); |
| 1005 var parsedDestination = |
| 1006 print_preview.ExtensionDestinationParser.parse(destinationInfo); |
| 1007 this.insertIntoStore_(parsedDestination); |
| 1008 this.dispatchProvisionalDestinationResolvedEvent_( |
| 1009 destination.id, parsedDestination); |
| 1010 }.bind(this), |
| 1011 function() { |
| 1012 /** |
| 1013 * The provisional destination is removed from the store and a |
| 1014 * PROVISIONAL_DESTINATION_RESOLVED event is dispatched with a null |
| 1015 * destination. |
| 1016 */ |
| 1017 this.removeProvisionalDestination_(destination.id); |
| 1018 this.dispatchProvisionalDestinationResolvedEvent_(destination.id, |
| 1019 null); |
| 1020 }.bind(this)); |
| 996 }, | 1021 }, |
| 997 | 1022 |
| 998 /** | 1023 /** |
| 999 * Selects 'Save to PDF' destination (since it always exists). | 1024 * Selects 'Save to PDF' destination (since it always exists). |
| 1000 * @private | 1025 * @private |
| 1001 */ | 1026 */ |
| 1002 selectPdfDestination_: function() { | 1027 selectPdfDestination_: function() { |
| 1003 var saveToPdfKey = this.getDestinationKey_( | 1028 var saveToPdfKey = this.getDestinationKey_( |
| 1004 print_preview.DestinationOrigin.LOCAL, | 1029 print_preview.DestinationOrigin.LOCAL, |
| 1005 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, ''); | 1030 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, ''); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1114 /** | 1139 /** |
| 1115 * Wait for a privet device to be registered. | 1140 * Wait for a privet device to be registered. |
| 1116 */ | 1141 */ |
| 1117 waitForRegister: function(id) { | 1142 waitForRegister: function(id) { |
| 1118 this.nativeLayer_.getPrivetPrinters().then( | 1143 this.nativeLayer_.getPrivetPrinters().then( |
| 1119 this.endPrivetPrinterSearch_.bind(this)); | 1144 this.endPrivetPrinterSearch_.bind(this)); |
| 1120 this.waitForRegisterDestination_ = id; | 1145 this.waitForRegisterDestination_ = id; |
| 1121 }, | 1146 }, |
| 1122 | 1147 |
| 1123 /** | 1148 /** |
| 1124 * Event handler for {@code | 1149 * Removes the provisional destination with ID |provisionalId| from |
| 1125 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}. | 1150 * |destinationMap_| and |destinations_|. |
| 1126 * Currently assumes the provisional destination is an extension | 1151 * @param{string} provisionalId The provisional destination ID. |
| 1127 * destination. | |
| 1128 * Called when a provisional destination resolvement attempt finishes. | |
| 1129 * The provisional destination is removed from the store and replaced with | |
| 1130 * a destination created from the resolved destination properties, if any | |
| 1131 * are reported. | |
| 1132 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED} | |
| 1133 * event. | |
| 1134 * @param {!Event} evt The event containing the provisional destination ID | |
| 1135 * and resolved destination description. If the destination was not | |
| 1136 * successfully resolved, the description will not be set. | |
| 1137 * @private | 1152 * @private |
| 1138 */ | 1153 */ |
| 1139 handleProvisionalDestinationResolved_: function(evt) { | 1154 removeProvisionalDestination_: function(provisionalId) { |
| 1140 var provisionalDestinationIndex = -1; | 1155 this.destinations_ = this.destinations_.filter( |
| 1141 var provisionalDestination = null; | 1156 function(el) { |
| 1142 for (var i = 0; i < this.destinations_.length; ++i) { | 1157 if (el.id == provisionalId) { |
| 1143 if (evt.provisionalId == this.destinations_[i].id) { | 1158 delete this.destinationMap_[this.getKey_(el)]; |
| 1144 provisionalDestinationIndex = i; | 1159 return false; |
| 1145 provisionalDestination = this.destinations_[i]; | 1160 } |
| 1146 break; | 1161 return true; |
| 1147 } | 1162 }, this); |
| 1148 } | 1163 }, |
| 1149 | 1164 |
| 1150 if (!provisionalDestination) | 1165 /** |
| 1151 return; | 1166 * Dispatches the PROVISIONAL_DESTINATION_RESOLVED event for id |
| 1152 | 1167 * |provisionalId| and destination |destination|. |
| 1153 this.destinations_.splice(provisionalDestinationIndex, 1); | 1168 * @param {string} provisionalId The ID of the destination that was |
| 1154 delete this.destinationMap_[this.getKey_(provisionalDestination)]; | 1169 * resolved. |
| 1155 | 1170 * @param {?print_preview.Destination} destination Information about the |
| 1156 var destination = evt.destination ? | 1171 * destination if it was resolved successfully. |
| 1157 print_preview.ExtensionDestinationParser.parse(evt.destination) : | 1172 */ |
| 1158 null; | 1173 dispatchProvisionalDestinationResolvedEvent_: function(provisionalId, |
| 1159 | 1174 destination) { |
| 1160 if (destination) | |
| 1161 this.insertIntoStore_(destination); | |
| 1162 | |
| 1163 var event = new Event( | 1175 var event = new Event( |
| 1164 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); | 1176 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); |
| 1165 event.provisionalId = evt.provisionalId; | 1177 event.provisionalId = provisionalId; |
| 1166 event.destination = destination; | 1178 event.destination = destination; |
| 1167 this.dispatchEvent(event); | 1179 this.dispatchEvent(event); |
| 1168 }, | 1180 }, |
| 1169 | 1181 |
| 1170 /** | 1182 /** |
| 1171 * Inserts {@code destination} to the data store and dispatches a | 1183 * Inserts {@code destination} to the data store and dispatches a |
| 1172 * DESTINATIONS_INSERTED event. | 1184 * DESTINATIONS_INSERTED event. |
| 1173 * @param {!print_preview.Destination} destination Print destination to | 1185 * @param {!print_preview.Destination} destination Print destination to |
| 1174 * insert. | 1186 * insert. |
| 1175 * @private | 1187 * @private |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 /** | 1326 /** |
| 1315 * Binds handlers to events. | 1327 * Binds handlers to events. |
| 1316 * @private | 1328 * @private |
| 1317 */ | 1329 */ |
| 1318 addEventListeners_: function() { | 1330 addEventListeners_: function() { |
| 1319 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | 1331 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); |
| 1320 this.tracker_.add( | 1332 this.tracker_.add( |
| 1321 nativeLayerEventTarget, | 1333 nativeLayerEventTarget, |
| 1322 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 1334 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
| 1323 this.onDestinationsReload_.bind(this)); | 1335 this.onDestinationsReload_.bind(this)); |
| 1324 this.tracker_.add( | |
| 1325 nativeLayerEventTarget, | |
| 1326 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, | |
| 1327 this.handleProvisionalDestinationResolved_.bind(this)); | |
| 1328 }, | 1336 }, |
| 1329 | 1337 |
| 1330 /** | 1338 /** |
| 1331 * Creates a local PDF print destination. | 1339 * Creates a local PDF print destination. |
| 1332 * @private | 1340 * @private |
| 1333 */ | 1341 */ |
| 1334 createLocalPdfPrintDestination_: function() { | 1342 createLocalPdfPrintDestination_: function() { |
| 1335 // TODO(alekseys): Create PDF printer in the native code and send its | 1343 // TODO(alekseys): Create PDF printer in the native code and send its |
| 1336 // capabilities back with other local printers. | 1344 // capabilities back with other local printers. |
| 1337 if (this.pdfPrinterEnabled_) { | 1345 if (this.pdfPrinterEnabled_) { |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1633 */ | 1641 */ |
| 1634 getKey_: function(destination) { | 1642 getKey_: function(destination) { |
| 1635 return this.getDestinationKey_( | 1643 return this.getDestinationKey_( |
| 1636 destination.origin, destination.id, destination.account); | 1644 destination.origin, destination.id, destination.account); |
| 1637 } | 1645 } |
| 1638 }; | 1646 }; |
| 1639 | 1647 |
| 1640 // Export | 1648 // Export |
| 1641 return {DestinationStore: DestinationStore}; | 1649 return {DestinationStore: DestinationStore}; |
| 1642 }); | 1650 }); |
| OLD | NEW |