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

Unified Diff: chrome/browser/resources/print_preview/cloud_print_interface.js

Issue 2938073003: Change getAccessToken and getExtensionPrinterAccess to sendWithPromise (Closed)
Patch Set: Address comments, remove extra code 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
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 cce11fc68825334b07bcd224c6b48a06639809b9..fef14c42d37558a662f4a21b9f53935f35ac2a0b 100644
--- a/chrome/browser/resources/print_preview/cloud_print_interface.js
+++ b/chrome/browser/resources/print_preview/cloud_print_interface.js
@@ -101,13 +101,13 @@ cr.define('cloudprint', function() {
this.outstandingCloudSearchRequests_ = [];
/**
- * Event tracker used to keep track of native layer events.
- * @type {!EventTracker}
- * @private
+ * Promise that will be resolved when the access token for
+ * DestinationOrigin.DEVICE is available. Null if there is no request
+ * currently pending.
+ * @private {?Promise}
*/
- this.tracker_ = new EventTracker();
+ this.accessTokenRequestPromise_ = null;
- this.addEventListeners_();
}
/**
@@ -163,8 +163,6 @@ cr.define('cloudprint', function() {
CloudPrintInterface.CLOUD_ORIGINS_ = [
print_preview.DestinationOrigin.COOKIES,
print_preview.DestinationOrigin.DEVICE
- // TODO(vitalybuka): Enable when implemented.
- // ready print_preview.DestinationOrigin.PROFILE
];
CloudPrintInterface.prototype = {
@@ -315,17 +313,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.
@@ -400,12 +387,16 @@ cr.define('cloudprint', function() {
* @private
*/
sendOrQueueRequest_: function(request) {
- if (request.origin == print_preview.DestinationOrigin.COOKIES) {
+ if (request.origin == print_preview.DestinationOrigin.COOKIES)
return this.sendRequest_(request);
dpapad 2017/06/20 16:09:09 This method is not declaring any @return. Should t
rbpotter 2017/06/21 03:52:28 I suspect this was done since sendRequest_ does no
- } else {
- this.requestQueue_.push(request);
- this.nativeLayer_.startGetAccessToken(request.origin);
- }
+
+ this.requestQueue_.push(request);
dpapad 2017/06/20 16:09:09 So, is the requestQueue_ necessary anymore? I thin
rbpotter 2017/06/21 03:52:28 Done. Queue is not needed anymore, thanks for the
+ if (this.accessTokenRequestPromise_ != null)
+ return;
+
+ this.accessTokenRequestPromise_ =
+ this.nativeLayer_.getAccessToken(request.origin).then(
+ this.onAccessTokenReady_.bind(this));
},
/**
@@ -477,29 +468,26 @@ cr.define('cloudprint', function() {
},
/**
- * Called when a native layer receives access token.
- * @param {Event} event Contains the authentication type and access token.
+ * Called when a native layer receives access token. Assumes that the
+ * destination type for this token is DestinationOrigin.DEVICE.
+ * @param {string} accessToken The access token obtained.
* @private
*/
- onAccessTokenReady_: function(event) {
- // TODO(vitalybuka): remove when other Origins implemented.
- assert(event.authType == print_preview.DestinationOrigin.DEVICE);
- this.requestQueue_ = this.requestQueue_.filter(function(request) {
+ onAccessTokenReady_: function(accessToken) {
+ this.requestQueue_ = this.requestQueue_.forEach(function(request) {
assert(request.origin == print_preview.DestinationOrigin.DEVICE);
- if (request.origin != event.authType) {
- return true;
- }
- if (event.accessToken) {
+ if (accessToken) {
request.xhr.setRequestHeader(
- 'Authorization', 'Bearer ' + event.accessToken);
+ 'Authorization', 'Bearer ' + accessToken);
this.sendRequest_(request);
} else { // No valid token.
// Without abort status does not exist.
request.xhr.abort();
request.callback(request);
}
- return false;
}, this);
+ this.requestQueue_ = [];
+ this.accessTokenRequestPromise_ = null;
},
/**
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698