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 function(destinationInfo) { | |
994 /** | |
995 * Removes the destination from the store and replaces it with a | |
996 * destination created from the resolved destination properties, if | |
997 * any are reported. Then sends a PROVISIONAL_DESTINATION_RESOLVED | |
998 * event. | |
999 * @param {!print_preview.ProvisionalDestinationInfo} | |
dpapad
2017/06/20 01:36:21
The @param comment should still be before the "fun
rbpotter
2017/06/20 02:47:54
Done.
| |
1000 * destinationInfo Information about the resolved printer. | |
1001 */ | |
1002 this.removeProvisionalDestination_(destination.id); | |
1003 var parsedDestination = | |
1004 print_preview.ExtensionDestinationParser.parse(destinationInfo); | |
1005 this.insertIntoStore_(parsedDestination); | |
1006 this.dispatchProvisionalDestinationResolvedEvent_( | |
1007 destination.id, parsedDestination); | |
1008 }.bind(this), | |
1009 function() { | |
1010 /** | |
1011 * The provisional destination is removed from the store and a | |
1012 * PROVISIONAL_DESTINATION_RESOLVED event is dispatched with a null | |
1013 * destination. | |
1014 */ | |
1015 this.removeProvisionalDestination_(destination.id); | |
1016 this.dispatchProvisionalDestinationResolvedEvent_(destination.id, | |
1017 null); | |
1018 }.bind(this)); | |
996 }, | 1019 }, |
997 | 1020 |
998 /** | 1021 /** |
999 * Selects 'Save to PDF' destination (since it always exists). | 1022 * Selects 'Save to PDF' destination (since it always exists). |
1000 * @private | 1023 * @private |
1001 */ | 1024 */ |
1002 selectPdfDestination_: function() { | 1025 selectPdfDestination_: function() { |
1003 var saveToPdfKey = this.getDestinationKey_( | 1026 var saveToPdfKey = this.getDestinationKey_( |
1004 print_preview.DestinationOrigin.LOCAL, | 1027 print_preview.DestinationOrigin.LOCAL, |
1005 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, ''); | 1028 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, ''); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1114 /** | 1137 /** |
1115 * Wait for a privet device to be registered. | 1138 * Wait for a privet device to be registered. |
1116 */ | 1139 */ |
1117 waitForRegister: function(id) { | 1140 waitForRegister: function(id) { |
1118 this.nativeLayer_.getPrivetPrinters().then( | 1141 this.nativeLayer_.getPrivetPrinters().then( |
1119 this.endPrivetPrinterSearch_.bind(this)); | 1142 this.endPrivetPrinterSearch_.bind(this)); |
1120 this.waitForRegisterDestination_ = id; | 1143 this.waitForRegisterDestination_ = id; |
1121 }, | 1144 }, |
1122 | 1145 |
1123 /** | 1146 /** |
1124 * Event handler for {@code | 1147 * Removes the provisional destination with ID |provisionalId| from |
1125 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}. | 1148 * |destinationMap_| and |destinations_|. |
1126 * Currently assumes the provisional destination is an extension | 1149 * @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 | 1150 * @private |
1138 */ | 1151 */ |
1139 handleProvisionalDestinationResolved_: function(evt) { | 1152 removeProvisionalDestination_: function(provisionalId) { |
1140 var provisionalDestinationIndex = -1; | 1153 this.destinations_ = this.destinations_.filter( |
1141 var provisionalDestination = null; | 1154 function(el) { |
1142 for (var i = 0; i < this.destinations_.length; ++i) { | 1155 if (el.id == provisionalId) { |
1143 if (evt.provisionalId == this.destinations_[i].id) { | 1156 delete this.destinationMap_[this.getKey_(el)]; |
1144 provisionalDestinationIndex = i; | 1157 return false; |
1145 provisionalDestination = this.destinations_[i]; | 1158 } |
1146 break; | 1159 return true; |
1147 } | 1160 }, this); |
1148 } | 1161 }, |
1149 | 1162 |
1150 if (!provisionalDestination) | 1163 /** |
1151 return; | 1164 * Dispatches the PROVISIONAL_DESTINATION_RESOLVED event for id |
1152 | 1165 * |provisionalId| and destination |destination|. |
1153 this.destinations_.splice(provisionalDestinationIndex, 1); | 1166 * @param {string} provisionalId The ID of the destination that was |
1154 delete this.destinationMap_[this.getKey_(provisionalDestination)]; | 1167 * resolved. |
1155 | 1168 * @param {?print_preview.Destination} destination Information about the |
1156 var destination = evt.destination ? | 1169 * destination if it was resolved successfully. |
1157 print_preview.ExtensionDestinationParser.parse(evt.destination) : | 1170 */ |
1158 null; | 1171 dispatchProvisionalDestinationResolvedEvent_: function(provisionalId, |
1159 | 1172 destination) { |
1160 if (destination) | |
1161 this.insertIntoStore_(destination); | |
1162 | |
1163 var event = new Event( | 1173 var event = new Event( |
1164 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); | 1174 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); |
1165 event.provisionalId = evt.provisionalId; | 1175 event.provisionalId = provisionalId; |
1166 event.destination = destination; | 1176 event.destination = destination; |
1167 this.dispatchEvent(event); | 1177 this.dispatchEvent(event); |
1168 }, | 1178 }, |
1169 | 1179 |
1170 /** | 1180 /** |
1171 * Inserts {@code destination} to the data store and dispatches a | 1181 * Inserts {@code destination} to the data store and dispatches a |
1172 * DESTINATIONS_INSERTED event. | 1182 * DESTINATIONS_INSERTED event. |
1173 * @param {!print_preview.Destination} destination Print destination to | 1183 * @param {!print_preview.Destination} destination Print destination to |
1174 * insert. | 1184 * insert. |
1175 * @private | 1185 * @private |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1314 /** | 1324 /** |
1315 * Binds handlers to events. | 1325 * Binds handlers to events. |
1316 * @private | 1326 * @private |
1317 */ | 1327 */ |
1318 addEventListeners_: function() { | 1328 addEventListeners_: function() { |
1319 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | 1329 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); |
1320 this.tracker_.add( | 1330 this.tracker_.add( |
1321 nativeLayerEventTarget, | 1331 nativeLayerEventTarget, |
1322 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 1332 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
1323 this.onDestinationsReload_.bind(this)); | 1333 this.onDestinationsReload_.bind(this)); |
1324 this.tracker_.add( | |
1325 nativeLayerEventTarget, | |
1326 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, | |
1327 this.handleProvisionalDestinationResolved_.bind(this)); | |
1328 }, | 1334 }, |
1329 | 1335 |
1330 /** | 1336 /** |
1331 * Creates a local PDF print destination. | 1337 * Creates a local PDF print destination. |
1332 * @private | 1338 * @private |
1333 */ | 1339 */ |
1334 createLocalPdfPrintDestination_: function() { | 1340 createLocalPdfPrintDestination_: function() { |
1335 // TODO(alekseys): Create PDF printer in the native code and send its | 1341 // TODO(alekseys): Create PDF printer in the native code and send its |
1336 // capabilities back with other local printers. | 1342 // capabilities back with other local printers. |
1337 if (this.pdfPrinterEnabled_) { | 1343 if (this.pdfPrinterEnabled_) { |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1633 */ | 1639 */ |
1634 getKey_: function(destination) { | 1640 getKey_: function(destination) { |
1635 return this.getDestinationKey_( | 1641 return this.getDestinationKey_( |
1636 destination.origin, destination.id, destination.account); | 1642 destination.origin, destination.id, destination.account); |
1637 } | 1643 } |
1638 }; | 1644 }; |
1639 | 1645 |
1640 // Export | 1646 // Export |
1641 return {DestinationStore: DestinationStore}; | 1647 return {DestinationStore: DestinationStore}; |
1642 }); | 1648 }); |
OLD | NEW |