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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/data/destination_store.js
diff --git a/chrome/browser/resources/print_preview/data/destination_store.js b/chrome/browser/resources/print_preview/data/destination_store.js
index a52b6d034e2bac20e43daad7120ed99e98533161..69397e8c1fe80f812cdda01be2f35b98e1fd027f 100644
--- a/chrome/browser/resources/print_preview/data/destination_store.js
+++ b/chrome/browser/resources/print_preview/data/destination_store.js
@@ -1013,7 +1013,33 @@ cr.define('print_preview', function() {
destination.provisionalType ==
print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION,
'Provisional type cannot be resolved.');
- this.nativeLayer_.grantExtensionPrinterAccess(destination.id);
+ this.nativeLayer_.grantExtensionPrinterAccess(destination.id).then(
+ function(destinationInfo) {
+ /**
dpapad 2017/06/16 22:21:30 This comment needs +2 indent.
rbpotter 2017/06/19 22:19:30 Done.
+ * Removes the destination from the store and replaces it with a
+ * destination created from the resolved destination properties, if
+ * any are reported. Then sends a PROVISIONAL_DESTINATION_RESOLVED
+ * event.
+ * @param {!print_preview.ProvisionalDestinationInfo} destinationInfo
+ * Information about the resolved printer.
+ */
+ this.removeProvisionalDestination_(destination.id);
+ var parsedDestination =
+ print_preview.ExtensionDestinationParser.parse(destinationInfo);
+ this.insertIntoStore_(parsedDestination);
+ this.dispatchProvisionalDestinationResolvedEvent_(
+ destination.id, parsedDestination);
+ }.bind(this),
+ function() {
+ /**
+ * The provisional destination is removed from the store and a
+ * PROVISIONAL_DESTINATION_RESOLVED event is dispatched with a null
+ * destination.
+ */
+ this.removeProvisionalDestination_(destination.id);
+ this.dispatchProvisionalDestinationResolvedEvent_(destination.id,
+ null);
+ }.bind(this));
},
/**
@@ -1149,48 +1175,35 @@ cr.define('print_preview', function() {
},
/**
- * Event handler for {@code
- * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}.
- * Currently assumes the provisional destination is an extension
- * destination.
- * Called when a provisional destination resolvement attempt finishes.
- * The provisional destination is removed from the store and replaced with
- * a destination created from the resolved destination properties, if any
- * are reported.
- * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED}
- * event.
- * @param {!Event} evt The event containing the provisional destination ID
- * and resolved destination description. If the destination was not
- * successfully resolved, the description will not be set.
+ * Removes the provisional destination with ID |provisionalId| from
+ * |destinationMap_| and |destinations_|.
+ * @param{string} provisionalId The provisional destination ID.
* @private
*/
- handleProvisionalDestinationResolved_: function(evt) {
- var provisionalDestinationIndex = -1;
- var provisionalDestination = null;
- for (var i = 0; i < this.destinations_.length; ++i) {
- if (evt.provisionalId == this.destinations_[i].id) {
- provisionalDestinationIndex = i;
- provisionalDestination = this.destinations_[i];
- break;
- }
- }
-
- if (!provisionalDestination)
- return;
-
- this.destinations_.splice(provisionalDestinationIndex, 1);
- delete this.destinationMap_[this.getKey_(provisionalDestination)];
-
- var destination = evt.destination ?
- print_preview.ExtensionDestinationParser.parse(evt.destination) :
- null;
-
- if (destination)
- this.insertIntoStore_(destination);
+ removeProvisionalDestination_: function(provisionalId) {
+ this.destinations_ = this.destinations_.filter(
+ 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.
+ if (el.id == provisionalId) {
+ delete this.destinationMap_[this.getKey_(el)];
+ return false;
+ }
+ return true;
+ }, this);
+ },
+ /**
+ * Dispatches the PROVISIONAL_DESTINATION_RESOLVED event for id
+ * |provisionalId| and destination |destination|.
+ * @param {string} provisionalId The ID of the destination that was
+ * resolved.
+ * @param {?print_preview.Destination} destination Information about the
+ * destination if it was resolved successfully.
+ */
+ dispatchProvisionalDestinationResolvedEvent_: function(provisionalId,
+ destination) {
var event = new Event(
DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED);
- event.provisionalId = evt.provisionalId;
+ event.provisionalId = provisionalId;
event.destination = destination;
this.dispatchEvent(event);
},
@@ -1348,10 +1361,6 @@ cr.define('print_preview', function() {
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD,
this.onDestinationsReload_.bind(this));
- this.tracker_.add(
- nativeLayerEventTarget,
- print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED,
- this.handleProvisionalDestinationResolved_.bind(this));
},
/**

Powered by Google App Engine
This is Rietveld 408576698