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

Unified Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 2938073003: Change getAccessToken and getExtensionPrinterAccess to sendWithPromise (Closed)
Patch Set: Cleanup 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..27872cb0f9015ed6f8d33d82ead4c27327079f8c 100644
--- a/chrome/browser/resources/print_preview/data/destination_store.js
+++ b/chrome/browser/resources/print_preview/data/destination_store.js
@@ -1013,7 +1013,10 @@ cr.define('print_preview', function() {
destination.provisionalType ==
print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION,
'Provisional type cannot be resolved.');
- this.nativeLayer_.grantExtensionPrinterAccess(destination.id);
+ var id = destination.id;
+ this.nativeLayer_.grantExtensionPrinterAccess(id).then(
+ this.handleProvisionalDestinationResolved_.bind(this, id),
dpapad 2017/06/15 22:20:13 handleProvisionalDestinationResolved_and handlePro
rbpotter 2017/06/16 02:19:36 Done.
+ this.handleProvisionalDestinationRejected_.bind(this, id));
},
/**
@@ -1149,52 +1152,78 @@ 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) {
+ 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);
},
+ /**
+ * Called when a provisional destination resolvement attempt finishes
+ * successfully. The provisional destination is removed from the store and
+ * replaced with a destination created from the resolved destination
+ * properties, if any are reported.
+ * Currently assumes the provisional destination is an extension
+ * destination.
+ * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED}
+ * event.
+ * @param {string} provisionalId The provisional destination ID
+ * @param {!print_preview.ProvisionalDestinationInfo} destinationInfo
+ * Information about the resolved printer.
+ * @private
+ */
+ handleProvisionalDestinationResolved_: function(provisionalId,
+ destinationInfo) {
+ this.removeProvisionalDestination_(provisionalId);
+ var destination =
+ print_preview.ExtensionDestinationParser.parse(destinationInfo);
+ this.insertIntoStore_(destination);
+ this.dispatchProvisionalDestinationResolvedEvent_(provisionalId,
+ destination);
+ },
+
+ /**
+ * Called when a provisional destination resolvement attempt fails.
+ * The provisional destination is removed from the store.
+ * Currently assumes the provisional destination is an extension
+ * destination.
+ * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED}
+ * event.
+ * @param {string} provisionalId The provisional destination ID
+ * @private
+ */
+ handleProvisionalDestinationRejected_: function(provisionalId) {
+ this.removeProvisionalDestination_(provisionalId);
+ this.dispatchProvisionalDestinationResolvedEvent_(provisionalId, null);
+ },
+
/**
* Inserts {@code destination} to the data store and dispatches a
* DESTINATIONS_INSERTED event.
@@ -1348,10 +1377,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