Chromium Code Reviews| 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..82037409e494b5f3a7bf00e8f9f7b82d795fd76d 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. |
|
Toscano
2013/04/19 01:58:21
Can you add a short desc about what it's used for.
Vitaly Buka (NO REVIEWS)
2013/04/19 06:18:47
Done.
|
| * @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. |
|
Toscano
2013/04/19 01:58:21
Some of these words can fit on the line above.
|
| + * @type {Dictionary} |
|
Toscano
2013/04/19 01:58:21
You can use {Object.<XXX: , YYY: >}
|
| * @private |
| */ |
| - this.xsrfToken_ = ''; |
| + this.xsrfTokens_ = {}; |
| + |
| + /** |
| + * Pending requests we are when waiting for access tokens. |
| + * @type {Dictionary} |
| + * @private |
| + */ |
| + this.pendingRequests_ = {}; |
| /** |
| * 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
Please move private methods after public methods.
|
| + 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) { |
|
Toscano
2013/04/19 01:58:21
Please move private methods after public methods.
|
| + var requests = this.pendingRequests_[event.authType]; |
| + this.pendingRequests_[event.authType] = []; |
| + for (var i in requests) { |
| + 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 = [ |
| + print_preview.Destination.Origin.COOKIES, |
| + // TODO(vitalybuka) Reenable when implemented. |
| + // ready print_preview.Destination.Origin.PROFILE, |
| + print_preview.Destination.Origin.DEVICE |
| + ]; |
| + for (var type in cloud_origins) { |
| + var origin = cloud_origins[type]; |
| + ++this.outstandingCloudSearchRequestCount_; |
| + this.sendRequest_('GET', 'search', params, origin, |
| + 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) { |
| 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,6 +303,25 @@ cr.define('cloudprint', function() { |
| return JSON.stringify(cjt); |
| }, |
| + // TODO |
| + 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. |
| @@ -254,12 +331,15 @@ cr.define('cloudprint', function() { |
| * @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) { |
| + 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) { |
| @@ -279,6 +359,9 @@ cr.define('cloudprint', function() { |
| var headers = {}; |
| headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
| + |
| + |
| + |
| if (method == 'GET') { |
| headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
| } else if (method == 'POST') { |
| @@ -286,14 +369,22 @@ 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) { |
| + console.log(body, xhr, headers, callback, origin, ''); |
| + 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 +412,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 +432,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 +483,13 @@ cr.define('cloudprint', function() { |
| * @param {Object} result JSON response. |
| * @private |
| */ |
| - onPrinterDone_: function(destinationId, status, result) { |
| + onPrinterDone_: function(destinationId, origin, status, result) { |
| + console.log(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)); |