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

Unified Diff: chrome/browser/resources/print_preview/cloud_print_interface.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/cloud_print_interface.js
diff --git a/chrome/browser/resources/print_preview/cloud_print_interface.js b/chrome/browser/resources/print_preview/cloud_print_interface.js
index 4c40894b00a7246f83e64d3426ed83593c4883db..9af7d06704d39a5312d8bf5df8860341a24cab32 100644
--- a/chrome/browser/resources/print_preview/cloud_print_interface.js
+++ b/chrome/browser/resources/print_preview/cloud_print_interface.js
@@ -102,13 +102,12 @@ cr.define('cloudprint', function() {
this.outstandingCloudSearchRequests_ = [];
/**
- * Event tracker used to keep track of native layer events.
- * @type {!EventTracker}
- * @private
+ * Map from authentication types to promises that will be resolved when
+ * the corresponding access token is available.
+ * @private {!Map<string, !Promise>}
*/
- this.tracker_ = new EventTracker();
+ this.accessTokenRequestMap_ = new Map();
dpapad 2017/06/16 22:21:29 Let's wait for @vitalybuka's response to see if th
rbpotter 2017/06/19 22:19:29 Done. Removed as it seems the DestinationOrigin.PR
- this.addEventListeners_();
}
/**
@@ -340,17 +339,6 @@ cr.define('cloudprint', function() {
this.onPrinterDone_.bind(this, printerId)));
},
- /**
- * Adds event listeners to relevant events.
- * @private
- */
- addEventListeners_: function() {
- this.tracker_.add(
- this.nativeLayer_.getEventTarget(),
- print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY,
- this.onAccessTokenReady_.bind(this));
- },
-
/**
* Builds request to the Google Cloud Print API.
* @param {string} method HTTP method of the request.
@@ -430,7 +418,12 @@ cr.define('cloudprint', function() {
return this.sendRequest_(request);
} else {
this.requestQueue_.push(request);
- this.nativeLayer_.startGetAccessToken(request.origin);
+ if (!this.accessTokenRequestMap_.get(request.origin)) {
+ this.accessTokenRequestMap_.set(
+ request.origin,
dpapad 2017/06/16 22:21:29 Is this a string or a print_preview.DestinationOri
rbpotter 2017/06/19 22:19:30 Done.
+ this.nativeLayer_.getAccessToken(request.origin).then(
+ this.onAccessTokenReady_.bind(this, request.origin)));
+ }
}
},
@@ -504,20 +497,22 @@ cr.define('cloudprint', function() {
/**
* Called when a native layer receives access token.
- * @param {Event} event Contains the authentication type and access token.
+ * @param {!print_preview.DestinationOrigin} authType The authentication
+ * type.
+ * @param {string} accessToken The access token obtained for |authType|.
* @private
*/
- onAccessTokenReady_: function(event) {
+ onAccessTokenReady_: function(authType, accessToken) {
// TODO(vitalybuka): remove when other Origins implemented.
- assert(event.authType == print_preview.DestinationOrigin.DEVICE);
+ assert(authType == print_preview.DestinationOrigin.DEVICE);
this.requestQueue_ = this.requestQueue_.filter(function(request) {
assert(request.origin == print_preview.DestinationOrigin.DEVICE);
- if (request.origin != event.authType) {
+ if (request.origin != authType) {
return true;
}
- if (event.accessToken) {
+ if (accessToken) {
request.xhr.setRequestHeader('Authorization',
- 'Bearer ' + event.accessToken);
+ 'Bearer ' + accessToken);
this.sendRequest_(request);
} else { // No valid token.
// Without abort status does not exist.
@@ -526,6 +521,7 @@ cr.define('cloudprint', function() {
}
return false;
}, this);
+ this.accessTokenRequestMap_.delete(authType);
},
/**

Powered by Google App Engine
This is Rietveld 408576698