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 995 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1006 /** | 1006 /** |
1007 * Attempts to resolve a provisional destination. | 1007 * Attempts to resolve a provisional destination. |
1008 * @param {!print_preview.Destination} destination Provisional destination | 1008 * @param {!print_preview.Destination} destination Provisional destination |
1009 * that should be resolved. | 1009 * that should be resolved. |
1010 */ | 1010 */ |
1011 resolveProvisionalDestination: function(destination) { | 1011 resolveProvisionalDestination: function(destination) { |
1012 assert( | 1012 assert( |
1013 destination.provisionalType == | 1013 destination.provisionalType == |
1014 print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION, | 1014 print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION, |
1015 'Provisional type cannot be resolved.'); | 1015 'Provisional type cannot be resolved.'); |
1016 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); | 1016 var id = destination.id; |
1017 this.nativeLayer_.grantExtensionPrinterAccess(id).then( | |
1018 this.handleProvisionalDestinationResolved_.bind(this, id), | |
dpapad
2017/06/15 22:20:13
handleProvisionalDestinationResolved_and handlePro
rbpotter
2017/06/16 02:19:36
Done.
| |
1019 this.handleProvisionalDestinationRejected_.bind(this, id)); | |
1017 }, | 1020 }, |
1018 | 1021 |
1019 /** | 1022 /** |
1020 * Selects 'Save to PDF' destination (since it always exists). | 1023 * Selects 'Save to PDF' destination (since it always exists). |
1021 * @private | 1024 * @private |
1022 */ | 1025 */ |
1023 selectPdfDestination_: function() { | 1026 selectPdfDestination_: function() { |
1024 var saveToPdfKey = this.getDestinationKey_( | 1027 var saveToPdfKey = this.getDestinationKey_( |
1025 print_preview.DestinationOrigin.LOCAL, | 1028 print_preview.DestinationOrigin.LOCAL, |
1026 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, | 1029 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1136 this.startLoadCloudDestinations(); | 1139 this.startLoadCloudDestinations(); |
1137 this.startLoadLocalDestinations(); | 1140 this.startLoadLocalDestinations(); |
1138 this.startLoadPrivetDestinations(); | 1141 this.startLoadPrivetDestinations(); |
1139 this.startLoadExtensionDestinations(); | 1142 this.startLoadExtensionDestinations(); |
1140 }, | 1143 }, |
1141 | 1144 |
1142 /** | 1145 /** |
1143 * Wait for a privet device to be registered. | 1146 * Wait for a privet device to be registered. |
1144 */ | 1147 */ |
1145 waitForRegister: function(id) { | 1148 waitForRegister: function(id) { |
1146 this.nativeLayer_.getPrivetPrinters().then( | 1149 this.nativeLayer_.getPrivetPrinters().then( |
dpapad
2017/06/15 22:20:13
Unrelated to this CL, but related to previous. Is
rbpotter
2017/06/16 02:19:36
I think the issue is that the promise returned by
| |
1147 this.endPrivetPrinterSearch_.bind(this)); | 1150 this.endPrivetPrinterSearch_.bind(this)); |
1148 this.waitForRegisterDestination_ = id; | 1151 this.waitForRegisterDestination_ = id; |
1149 }, | 1152 }, |
1150 | 1153 |
1151 /** | 1154 /** |
1152 * Event handler for {@code | 1155 * Removes the provisional destination with ID |provisionalId| from |
1153 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}. | 1156 * |destinationMap_| and |destinations_|. |
1154 * Currently assumes the provisional destination is an extension | 1157 * @param{string} provisionalId The provisional destination ID. |
1155 * destination. | |
1156 * Called when a provisional destination resolvement attempt finishes. | |
1157 * The provisional destination is removed from the store and replaced with | |
1158 * a destination created from the resolved destination properties, if any | |
1159 * are reported. | |
1160 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED} | |
1161 * event. | |
1162 * @param {!Event} evt The event containing the provisional destination ID | |
1163 * and resolved destination description. If the destination was not | |
1164 * successfully resolved, the description will not be set. | |
1165 * @private | 1158 * @private |
1166 */ | 1159 */ |
1167 handleProvisionalDestinationResolved_: function(evt) { | 1160 removeProvisionalDestination_: function(provisionalId) { |
1168 var provisionalDestinationIndex = -1; | 1161 this.destinations_ = this.destinations_.filter( |
1169 var provisionalDestination = null; | 1162 function(el, i, destinations) { |
1170 for (var i = 0; i < this.destinations_.length; ++i) { | 1163 if (el.id == provisionalId) { |
1171 if (evt.provisionalId == this.destinations_[i].id) { | 1164 delete this.destinationMap_[this.getKey_(el)]; |
1172 provisionalDestinationIndex = i; | 1165 return false; |
1173 provisionalDestination = this.destinations_[i]; | 1166 } |
1174 break; | 1167 return true; |
1175 } | 1168 }, this); |
1176 } | 1169 }, |
1177 | 1170 |
1178 if (!provisionalDestination) | 1171 /** |
1179 return; | 1172 * Dispatches the PROVISIONAL_DESTINATION_RESOLVED event for id |
1180 | 1173 * |provisionalId| and destination |destination|. |
1181 this.destinations_.splice(provisionalDestinationIndex, 1); | 1174 * @param {string} provisionalId The ID of the destination that was |
1182 delete this.destinationMap_[this.getKey_(provisionalDestination)]; | 1175 * resolved. |
1183 | 1176 * @param {?print_preview.Destination} destination Information about the |
1184 var destination = evt.destination ? | 1177 * destination if it was resolved successfully. |
1185 print_preview.ExtensionDestinationParser.parse(evt.destination) : | 1178 */ |
1186 null; | 1179 dispatchProvisionalDestinationResolvedEvent_: function(provisionalId, |
1187 | 1180 destination) { |
1188 if (destination) | |
1189 this.insertIntoStore_(destination); | |
1190 | |
1191 var event = new Event( | 1181 var event = new Event( |
1192 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); | 1182 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); |
1193 event.provisionalId = evt.provisionalId; | 1183 event.provisionalId = provisionalId; |
1194 event.destination = destination; | 1184 event.destination = destination; |
1195 this.dispatchEvent(event); | 1185 this.dispatchEvent(event); |
1196 }, | 1186 }, |
1197 | 1187 |
1198 /** | 1188 /** |
1189 * Called when a provisional destination resolvement attempt finishes | |
1190 * successfully. The provisional destination is removed from the store and | |
1191 * replaced with a destination created from the resolved destination | |
1192 * properties, if any are reported. | |
1193 * Currently assumes the provisional destination is an extension | |
1194 * destination. | |
1195 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED} | |
1196 * event. | |
1197 * @param {string} provisionalId The provisional destination ID | |
1198 * @param {!print_preview.ProvisionalDestinationInfo} destinationInfo | |
1199 * Information about the resolved printer. | |
1200 * @private | |
1201 */ | |
1202 handleProvisionalDestinationResolved_: function(provisionalId, | |
1203 destinationInfo) { | |
1204 this.removeProvisionalDestination_(provisionalId); | |
1205 var destination = | |
1206 print_preview.ExtensionDestinationParser.parse(destinationInfo); | |
1207 this.insertIntoStore_(destination); | |
1208 this.dispatchProvisionalDestinationResolvedEvent_(provisionalId, | |
1209 destination); | |
1210 }, | |
1211 | |
1212 /** | |
1213 * Called when a provisional destination resolvement attempt fails. | |
1214 * The provisional destination is removed from the store. | |
1215 * Currently assumes the provisional destination is an extension | |
1216 * destination. | |
1217 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED} | |
1218 * event. | |
1219 * @param {string} provisionalId The provisional destination ID | |
1220 * @private | |
1221 */ | |
1222 handleProvisionalDestinationRejected_: function(provisionalId) { | |
1223 this.removeProvisionalDestination_(provisionalId); | |
1224 this.dispatchProvisionalDestinationResolvedEvent_(provisionalId, null); | |
1225 }, | |
1226 | |
1227 /** | |
1199 * Inserts {@code destination} to the data store and dispatches a | 1228 * Inserts {@code destination} to the data store and dispatches a |
1200 * DESTINATIONS_INSERTED event. | 1229 * DESTINATIONS_INSERTED event. |
1201 * @param {!print_preview.Destination} destination Print destination to | 1230 * @param {!print_preview.Destination} destination Print destination to |
1202 * insert. | 1231 * insert. |
1203 * @private | 1232 * @private |
1204 */ | 1233 */ |
1205 insertDestination_: function(destination) { | 1234 insertDestination_: function(destination) { |
1206 if (this.insertIntoStore_(destination)) { | 1235 if (this.insertIntoStore_(destination)) { |
1207 this.destinationsInserted_(destination); | 1236 this.destinationsInserted_(destination); |
1208 } | 1237 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1341 /** | 1370 /** |
1342 * Binds handlers to events. | 1371 * Binds handlers to events. |
1343 * @private | 1372 * @private |
1344 */ | 1373 */ |
1345 addEventListeners_: function() { | 1374 addEventListeners_: function() { |
1346 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | 1375 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); |
1347 this.tracker_.add( | 1376 this.tracker_.add( |
1348 nativeLayerEventTarget, | 1377 nativeLayerEventTarget, |
1349 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 1378 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
1350 this.onDestinationsReload_.bind(this)); | 1379 this.onDestinationsReload_.bind(this)); |
1351 this.tracker_.add( | |
1352 nativeLayerEventTarget, | |
1353 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, | |
1354 this.handleProvisionalDestinationResolved_.bind(this)); | |
1355 }, | 1380 }, |
1356 | 1381 |
1357 /** | 1382 /** |
1358 * Creates a local PDF print destination. | 1383 * Creates a local PDF print destination. |
1359 * @private | 1384 * @private |
1360 */ | 1385 */ |
1361 createLocalPdfPrintDestination_: function() { | 1386 createLocalPdfPrintDestination_: function() { |
1362 // TODO(alekseys): Create PDF printer in the native code and send its | 1387 // TODO(alekseys): Create PDF printer in the native code and send its |
1363 // capabilities back with other local printers. | 1388 // capabilities back with other local printers. |
1364 if (this.pdfPrinterEnabled_) { | 1389 if (this.pdfPrinterEnabled_) { |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1665 return this.getDestinationKey_( | 1690 return this.getDestinationKey_( |
1666 destination.origin, destination.id, destination.account); | 1691 destination.origin, destination.id, destination.account); |
1667 } | 1692 } |
1668 }; | 1693 }; |
1669 | 1694 |
1670 // Export | 1695 // Export |
1671 return { | 1696 return { |
1672 DestinationStore: DestinationStore | 1697 DestinationStore: DestinationStore |
1673 }; | 1698 }; |
1674 }); | 1699 }); |
OLD | NEW |