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

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

Issue 14370003: Use device Robot Account to access Cloud Print. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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/print_preview.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 ea6b271bb31bb0f2ffcd11a020c55d0a3937b8bd..a2eac6ad9267f2dc796ec2d6d509e1e2ae6c7b23 100644
--- a/chrome/browser/resources/print_preview/cloud_print_interface.js
+++ b/chrome/browser/resources/print_preview/cloud_print_interface.js
@@ -10,10 +10,11 @@ cr.define('cloudprint', function() {
* @param {string} baseUrl Base part of the Google Cloud Print service URL
* with no trailing slash. For example,
* 'https://www.google.com/cloudprint'.
+ * @param {!print_preview.NativeLayer} nativeLayer Native layer.
* @constructor
* @extends {cr.EventTarget}
*/
- function CloudPrintInterface(baseUrl) {
+ function CloudPrintInterface(baseUrl, nativeLayer) {
/**
* The base URL of the Google Cloud Print API.
* @type {string}
@@ -22,11 +23,26 @@ cr.define('cloudprint', function() {
this.baseUrl_ = baseUrl;
/**
- * Last received XSRF token. Sent as a parameter in every request.
- * @type {string}
+ * Used to get Auth2 tokens.
+ * @type {!print_preview.NativeLayer}
+ * @private
+ */
+ this.nativeLayer_ = nativeLayer;
+
+ /**
+ * Last received XSRF tokens for each origin type. Sent as a
+ * parameter in every request.
+ * @type {Dictionary}
* @private
*/
- this.xsrfToken_ = '';
+ this.xsrfTokens_ = {};
Toscano 2013/04/19 01:58:21 Get rid of this cache.
Vitaly Buka (NO REVIEWS) 2013/04/19 06:18:47 Done.
+
+ /**
+ * Pending requests we are when waiting for access tokens.
+ * @type {Dictionary}
+ * @private
+ */
+ this.pendingRequests_ = {};
Toscano 2013/04/19 01:58:21 this.deviceRequestQueue_
Vitaly Buka (NO REVIEWS) 2013/04/19 06:18:47 Done.
/**
* Number of outstanding cloud destination search requests.
@@ -34,6 +50,15 @@ cr.define('cloudprint', function() {
* @private
*/
this.outstandingCloudSearchRequestCount_ = 0;
+
+ /**
+ * Event tracker used to keep track of native layer events.
+ * @type {!EventTracker}
+ * @private
+ */
+ this.tracker_ = new EventTracker();
+
+ this.addEventListeners_();
};
/**
@@ -106,6 +131,30 @@ cr.define('cloudprint', function() {
},
/**
+ * Adds event listeners to the relevant native layer events.
+ * @private
+ */
+ addEventListeners_: function() {
Toscano 2013/04/19 01:58:21 Move after public methods.
Vitaly Buka (NO REVIEWS) 2013/04/19 06:18:47 Done.
+ this.tracker_.add(
+ this.nativeLayer_,
+ print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY,
+ this.onAccessTokenReady_.bind(this));
+ },
+
+ /**
+ * Called when a native layer received access token.
+ * @param {cr.Event} evt Contains the access token.
+ * @private
+ */
+ onAccessTokenReady_: function(event) {
+ var requests = this.pendingRequests_[event.authType];
+ this.pendingRequests_[event.authType] = [];
+ for (var i in requests) {
Toscano 2013/04/19 01:58:21 requests.forEach(function(request) {});
Vitaly Buka (NO REVIEWS) 2013/04/19 06:18:47 Done.
+ requests[i](event.accessToken);
+ }
+ },
+
+ /**
* @return {boolean} Whether a search for cloud destinations is in progress.
*/
get isCloudDestinationSearchInProgress() {
@@ -126,9 +175,18 @@ cr.define('cloudprint', function() {
if (isRecent) {
params.push(new HttpParam('q', '^recent'));
}
- ++this.outstandingCloudSearchRequestCount_;
- this.sendRequest_('GET', 'search', params,
- this.onSearchDone_.bind(this, isRecent));
+ var cloud_origins = [
Toscano 2013/04/19 01:58:21 Move this into a static constant.
+ print_preview.Destination.Origin.COOKIES,
+ // TODO(vitalybuka) Enable when implemented.
+ // ready print_preview.Destination.Origin.PROFILE,
+ print_preview.Destination.Origin.DEVICE
+ ];
+ for (var type in cloud_origins) {
Toscano 2013/04/19 01:58:21 cloud_origins.forEach(function(type) { });
+ var origin = cloud_origins[type];
+ ++this.outstandingCloudSearchRequestCount_;
+ this.sendRequest_('GET', 'search', params, origin,
Toscano 2013/04/19 01:58:21 var cpRequest = this.buildRequest_('GET', 'search'
+ this.onSearchDone_.bind(this, isRecent, origin));
+ };
},
/**
@@ -157,7 +215,7 @@ cr.define('cloudprint', function() {
'__google__chrome_version=' + chromeVersion),
new HttpParam('tag', '__google__os=' + navigator.platform)
];
- this.sendRequest_('POST', 'submit', params,
+ this.sendRequest_('POST', 'submit', params, destination.origin,
this.onSubmitDone_.bind(this));
},
@@ -165,13 +223,13 @@ cr.define('cloudprint', function() {
* Sends a Google Cloud Print printer API request.
* @param {string} printerId ID of the printer to lookup.
*/
- printer: function(printerId) {
+ printer: function(printerId, origin) {
var params = [
new HttpParam('printerid', printerId),
new HttpParam('use_cdd', 'true')
];
- this.sendRequest_('GET', 'printer', params,
- this.onPrinterDone_.bind(this, printerId));
+ this.sendRequest_('GET', 'printer', params, origin,
+ this.onPrinterDone_.bind(this, printerId, origin));
},
/**
@@ -182,12 +240,12 @@ cr.define('cloudprint', function() {
* @param {boolean} isAccepted Whether the user accepted the
* terms-of-service.
*/
- updatePrinterTosAcceptance: function(printerId, isAccepted) {
+ updatePrinterTosAcceptance: function(printerId, origin, isAccepted) {
Toscano 2013/04/19 01:58:21 1. Change order so that origin is at the end. 2. U
var params = [
new HttpParam('printerid', printerId),
new HttpParam('is_tos_accepted', isAccepted)
];
- this.sendRequest_('POST', 'update', params,
+ this.sendRequest_('POST', 'update', params, origin,
this.onUpdatePrinterTosAcceptanceDone_.bind(this));
},
@@ -245,21 +303,44 @@ cr.define('cloudprint', function() {
return JSON.stringify(cjt);
},
+ // TODO docs
+ sendRequestWithToken_: function(body, xhr, callback, origin, accessToken) {
+ if (origin != print_preview.Destination.Origin.COOKIES) {
+ if (!accessToken) {
+ // No valid token.
+ callback(401, {
+ 'errorCode' : -1,
+ 'message' : 'No access token.'
+ });
+ retrun;
+ }
+ xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
+ xhr.withCredentials = false;
+ }
+ xhr.onreadystatechange =
+ this.onReadyStateChange_.bind(this, xhr, origin, callback);
+ xhr.send(body);
+ },
+
/**
* Sends a request to the Google Cloud Print API.
* @param {string} method HTTP method of the request.
* @param {string} action Google Cloud Print action to perform.
* @param {Array.<!HttpParam>} params HTTP parameters to include in the
* request.
+ * TODO origin
* @param {function(number, Object)} callback Callback to invoke when
* request completes.
*/
- sendRequest_: function(method, action, params, callback) {
- if (!this.xsrfToken_) {
+ sendRequest_: function(method, action, params, origin, callback) {
Toscano 2013/04/19 01:58:21 Change this method to buildRequest_(...); with out
+ if (!this.xsrfTokens_[origin]) {
// TODO(rltoscano): Should throw an error if not a read-only action or
// issue an xsrf token request.
}
- var url = this.baseUrl_ + '/' + action + '?xsrf=' + this.xsrfToken_;
+
+ var url = this.baseUrl_ + '/' + action + '?xsrf=';
+ if (this.xsrfTokens_[origin])
+ url = url + this.xsrfTokens_[origin];
var body = null;
if (params) {
@@ -286,14 +367,21 @@ cr.define('cloudprint', function() {
}
var xhr = new XMLHttpRequest();
- xhr.onreadystatechange =
- this.onReadyStateChange_.bind(this, xhr, callback);
xhr.open(method, url, true);
xhr.withCredentials = true;
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
- xhr.send(body);
+
+ if (origin == print_preview.Destination.Origin.COOKIES) {
+ this.sendRequestWithToken_(body, xhr, callback, origin, '');
+ } else {
+ if (!this.pendingRequests_[origin])
+ this.pendingRequests_[origin] = [];
+ this.pendingRequests_[origin].push(
+ this.sendRequestWithToken_.bind(this, body, xhr, callback, origin));
+ this.nativeLayer_.startGetAccessToken(origin);
+ }
},
/**
@@ -321,12 +409,12 @@ cr.define('cloudprint', function() {
* request completes.
* @private
*/
- onReadyStateChange_: function(xhr, callback) {
+ onReadyStateChange_: function(xhr, origin, callback) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
if (result['success']) {
- this.xsrfToken_ = result['xsrf_token'];
+ this.xsrfTokens_[origin] = result['xsrf_token'];
}
}
callback(xhr.status, result);
@@ -341,16 +429,14 @@ cr.define('cloudprint', function() {
* @param {Object} result JSON response.
* @private
*/
- onSearchDone_: function(isRecent, status, result) {
+ onSearchDone_: function(isRecent, origin, status, result) {
--this.outstandingCloudSearchRequestCount_;
if (status == 200 && result['success']) {
var printerListJson = result['printers'] || [];
var printerList = [];
printerListJson.forEach(function(printerJson) {
try {
- printerList.push(
- cloudprint.CloudDestinationParser.parse(
- printerJson, print_preview.Destination.Origin.COOKIES));
+ printerList.push(cloudprint.CloudDestinationParser.parse(printerJson, origin));
} catch (err) {
console.error('Unable to parse cloud print destination: ' + err);
}
@@ -394,13 +480,12 @@ cr.define('cloudprint', function() {
* @param {Object} result JSON response.
* @private
*/
- onPrinterDone_: function(destinationId, status, result) {
+ onPrinterDone_: function(destinationId, origin, status, result) {
if (status == 200 && result['success']) {
var printerJson = result['printers'][0];
var printer;
try {
- printer = cloudprint.CloudDestinationParser.parse(
- printerJson, print_preview.Destination.Origin.COOKIES);
+ printer = cloudprint.CloudDestinationParser.parse(printerJson, origin);
} catch (err) {
console.error('Failed to parse cloud print destination: ' +
JSON.stringify(printerJson));
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/print_preview.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698