Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 2938073003: Change getAccessToken and getExtensionPrinterAccess to sendWithPromise (Closed)
Patch Set: Address comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 this.nativeLayer_.grantExtensionPrinterAccess(destination.id).then(
1017 function(destinationInfo) {
1018 /**
dpapad 2017/06/16 22:21:30 This comment needs +2 indent.
rbpotter 2017/06/19 22:19:30 Done.
1019 * Removes the destination from the store and replaces it with a
1020 * destination created from the resolved destination properties, if
1021 * any are reported. Then sends a PROVISIONAL_DESTINATION_RESOLVED
1022 * event.
1023 * @param {!print_preview.ProvisionalDestinationInfo} destinationInfo
1024 * Information about the resolved printer.
1025 */
1026 this.removeProvisionalDestination_(destination.id);
1027 var parsedDestination =
1028 print_preview.ExtensionDestinationParser.parse(destinationInfo);
1029 this.insertIntoStore_(parsedDestination);
1030 this.dispatchProvisionalDestinationResolvedEvent_(
1031 destination.id, parsedDestination);
1032 }.bind(this),
1033 function() {
1034 /**
1035 * The provisional destination is removed from the store and a
1036 * PROVISIONAL_DESTINATION_RESOLVED event is dispatched with a null
1037 * destination.
1038 */
1039 this.removeProvisionalDestination_(destination.id);
1040 this.dispatchProvisionalDestinationResolvedEvent_(destination.id,
1041 null);
1042 }.bind(this));
1017 }, 1043 },
1018 1044
1019 /** 1045 /**
1020 * Selects 'Save to PDF' destination (since it always exists). 1046 * Selects 'Save to PDF' destination (since it always exists).
1021 * @private 1047 * @private
1022 */ 1048 */
1023 selectPdfDestination_: function() { 1049 selectPdfDestination_: function() {
1024 var saveToPdfKey = this.getDestinationKey_( 1050 var saveToPdfKey = this.getDestinationKey_(
1025 print_preview.DestinationOrigin.LOCAL, 1051 print_preview.DestinationOrigin.LOCAL,
1026 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, 1052 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1142 /** 1168 /**
1143 * Wait for a privet device to be registered. 1169 * Wait for a privet device to be registered.
1144 */ 1170 */
1145 waitForRegister: function(id) { 1171 waitForRegister: function(id) {
1146 this.nativeLayer_.getPrivetPrinters().then( 1172 this.nativeLayer_.getPrivetPrinters().then(
1147 this.endPrivetPrinterSearch_.bind(this)); 1173 this.endPrivetPrinterSearch_.bind(this));
1148 this.waitForRegisterDestination_ = id; 1174 this.waitForRegisterDestination_ = id;
1149 }, 1175 },
1150 1176
1151 /** 1177 /**
1152 * Event handler for {@code 1178 * Removes the provisional destination with ID |provisionalId| from
1153 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}. 1179 * |destinationMap_| and |destinations_|.
1154 * Currently assumes the provisional destination is an extension 1180 * @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 1181 * @private
1166 */ 1182 */
1167 handleProvisionalDestinationResolved_: function(evt) { 1183 removeProvisionalDestination_: function(provisionalId) {
1168 var provisionalDestinationIndex = -1; 1184 this.destinations_ = this.destinations_.filter(
1169 var provisionalDestination = null; 1185 function(el, i, destinations) {
dpapad 2017/06/16 22:21:30 Do you need to pass |i| and |destinations| since t
rbpotter 2017/06/19 22:19:30 Done.
1170 for (var i = 0; i < this.destinations_.length; ++i) { 1186 if (el.id == provisionalId) {
1171 if (evt.provisionalId == this.destinations_[i].id) { 1187 delete this.destinationMap_[this.getKey_(el)];
1172 provisionalDestinationIndex = i; 1188 return false;
1173 provisionalDestination = this.destinations_[i]; 1189 }
1174 break; 1190 return true;
1175 } 1191 }, this);
1176 } 1192 },
1177 1193
1178 if (!provisionalDestination) 1194 /**
1179 return; 1195 * Dispatches the PROVISIONAL_DESTINATION_RESOLVED event for id
1180 1196 * |provisionalId| and destination |destination|.
1181 this.destinations_.splice(provisionalDestinationIndex, 1); 1197 * @param {string} provisionalId The ID of the destination that was
1182 delete this.destinationMap_[this.getKey_(provisionalDestination)]; 1198 * resolved.
1183 1199 * @param {?print_preview.Destination} destination Information about the
1184 var destination = evt.destination ? 1200 * destination if it was resolved successfully.
1185 print_preview.ExtensionDestinationParser.parse(evt.destination) : 1201 */
1186 null; 1202 dispatchProvisionalDestinationResolvedEvent_: function(provisionalId,
1187 1203 destination) {
1188 if (destination)
1189 this.insertIntoStore_(destination);
1190
1191 var event = new Event( 1204 var event = new Event(
1192 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED); 1205 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED);
1193 event.provisionalId = evt.provisionalId; 1206 event.provisionalId = provisionalId;
1194 event.destination = destination; 1207 event.destination = destination;
1195 this.dispatchEvent(event); 1208 this.dispatchEvent(event);
1196 }, 1209 },
1197 1210
1198 /** 1211 /**
1199 * Inserts {@code destination} to the data store and dispatches a 1212 * Inserts {@code destination} to the data store and dispatches a
1200 * DESTINATIONS_INSERTED event. 1213 * DESTINATIONS_INSERTED event.
1201 * @param {!print_preview.Destination} destination Print destination to 1214 * @param {!print_preview.Destination} destination Print destination to
1202 * insert. 1215 * insert.
1203 * @private 1216 * @private
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1341 /** 1354 /**
1342 * Binds handlers to events. 1355 * Binds handlers to events.
1343 * @private 1356 * @private
1344 */ 1357 */
1345 addEventListeners_: function() { 1358 addEventListeners_: function() {
1346 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); 1359 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
1347 this.tracker_.add( 1360 this.tracker_.add(
1348 nativeLayerEventTarget, 1361 nativeLayerEventTarget,
1349 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, 1362 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD,
1350 this.onDestinationsReload_.bind(this)); 1363 this.onDestinationsReload_.bind(this));
1351 this.tracker_.add(
1352 nativeLayerEventTarget,
1353 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED,
1354 this.handleProvisionalDestinationResolved_.bind(this));
1355 }, 1364 },
1356 1365
1357 /** 1366 /**
1358 * Creates a local PDF print destination. 1367 * Creates a local PDF print destination.
1359 * @private 1368 * @private
1360 */ 1369 */
1361 createLocalPdfPrintDestination_: function() { 1370 createLocalPdfPrintDestination_: function() {
1362 // TODO(alekseys): Create PDF printer in the native code and send its 1371 // TODO(alekseys): Create PDF printer in the native code and send its
1363 // capabilities back with other local printers. 1372 // capabilities back with other local printers.
1364 if (this.pdfPrinterEnabled_) { 1373 if (this.pdfPrinterEnabled_) {
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
1665 return this.getDestinationKey_( 1674 return this.getDestinationKey_(
1666 destination.origin, destination.id, destination.account); 1675 destination.origin, destination.id, destination.account);
1667 } 1676 }
1668 }; 1677 };
1669 1678
1670 // Export 1679 // Export
1671 return { 1680 return {
1672 DestinationStore: DestinationStore 1681 DestinationStore: DestinationStore
1673 }; 1682 };
1674 }); 1683 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698