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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.exportPath('cloudprint'); 5 cr.exportPath('cloudprint');
6 6
7 /** 7 /**
8 * Event types dispatched by the cloudprint interface. 8 * Event types dispatched by the cloudprint interface.
9 * @enum {string} 9 * @enum {string}
10 */ 10 */
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 this.requestQueue_ = []; 94 this.requestQueue_ = [];
95 95
96 /** 96 /**
97 * Outstanding cloud destination search requests. 97 * Outstanding cloud destination search requests.
98 * @type {!Array<!cloudprint.CloudPrintRequest>} 98 * @type {!Array<!cloudprint.CloudPrintRequest>}
99 * @private 99 * @private
100 */ 100 */
101 this.outstandingCloudSearchRequests_ = []; 101 this.outstandingCloudSearchRequests_ = [];
102 102
103 /** 103 /**
104 * Event tracker used to keep track of native layer events. 104 * Promise that will be resolved when the access token for
105 * @type {!EventTracker} 105 * DestinationOrigin.DEVICE is available. Null if there is no request
106 * @private 106 * currently pending.
107 * @private {?Promise}
107 */ 108 */
108 this.tracker_ = new EventTracker(); 109 this.accessTokenRequestPromise_ = null;
109 110
110 this.addEventListeners_();
111 } 111 }
112 112
113 /** 113 /**
114 * Content type header value for a URL encoded HTTP request. 114 * Content type header value for a URL encoded HTTP request.
115 * @type {string} 115 * @type {string}
116 * @const 116 * @const
117 * @private 117 * @private
118 */ 118 */
119 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = 119 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ =
120 'application/x-www-form-urlencoded'; 120 'application/x-www-form-urlencoded';
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 156
157 /** 157 /**
158 * Could Print origins used to search printers. 158 * Could Print origins used to search printers.
159 * @type {!Array<!print_preview.DestinationOrigin>} 159 * @type {!Array<!print_preview.DestinationOrigin>}
160 * @const 160 * @const
161 * @private 161 * @private
162 */ 162 */
163 CloudPrintInterface.CLOUD_ORIGINS_ = [ 163 CloudPrintInterface.CLOUD_ORIGINS_ = [
164 print_preview.DestinationOrigin.COOKIES, 164 print_preview.DestinationOrigin.COOKIES,
165 print_preview.DestinationOrigin.DEVICE 165 print_preview.DestinationOrigin.DEVICE
166 // TODO(vitalybuka): Enable when implemented.
167 // ready print_preview.DestinationOrigin.PROFILE
168 ]; 166 ];
169 167
170 CloudPrintInterface.prototype = { 168 CloudPrintInterface.prototype = {
171 __proto__: cr.EventTarget.prototype, 169 __proto__: cr.EventTarget.prototype,
172 170
173 /** @return {string} Base URL of the Google Cloud Print service. */ 171 /** @return {string} Base URL of the Google Cloud Print service. */
174 get baseUrl() { 172 get baseUrl() {
175 return this.baseUrl_; 173 return this.baseUrl_;
176 }, 174 },
177 175
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 var params = [ 307 var params = [
310 new HttpParam('printerid', printerId), new HttpParam('use_cdd', 'true'), 308 new HttpParam('printerid', printerId), new HttpParam('use_cdd', 'true'),
311 new HttpParam('printer_connection_status', 'true') 309 new HttpParam('printer_connection_status', 'true')
312 ]; 310 ];
313 this.sendOrQueueRequest_(this.buildRequest_( 311 this.sendOrQueueRequest_(this.buildRequest_(
314 'GET', 'printer', params, origin, account || '', 312 'GET', 'printer', params, origin, account || '',
315 this.onPrinterDone_.bind(this, printerId))); 313 this.onPrinterDone_.bind(this, printerId)));
316 }, 314 },
317 315
318 /** 316 /**
319 * Adds event listeners to relevant events.
320 * @private
321 */
322 addEventListeners_: function() {
323 this.tracker_.add(
324 this.nativeLayer_.getEventTarget(),
325 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY,
326 this.onAccessTokenReady_.bind(this));
327 },
328
329 /**
330 * Builds request to the Google Cloud Print API. 317 * Builds request to the Google Cloud Print API.
331 * @param {string} method HTTP method of the request. 318 * @param {string} method HTTP method of the request.
332 * @param {string} action Google Cloud Print action to perform. 319 * @param {string} action Google Cloud Print action to perform.
333 * @param {Array<!HttpParam>} params HTTP parameters to include in the 320 * @param {Array<!HttpParam>} params HTTP parameters to include in the
334 * request. 321 * request.
335 * @param {!print_preview.DestinationOrigin} origin Origin for destination. 322 * @param {!print_preview.DestinationOrigin} origin Origin for destination.
336 * @param {?string} account Account the request is sent for. Can be 323 * @param {?string} account Account the request is sent for. Can be
337 * {@code null} or empty string if the request is not cookie bound or 324 * {@code null} or empty string if the request is not cookie bound or
338 * is sent on behalf of the primary user. 325 * is sent on behalf of the primary user.
339 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to 326 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 xhr, body, origin, account, callback); 380 xhr, body, origin, account, callback);
394 }, 381 },
395 382
396 /** 383 /**
397 * Sends a request to the Google Cloud Print API or queues if it needs to 384 * Sends a request to the Google Cloud Print API or queues if it needs to
398 * wait OAuth2 access token. 385 * wait OAuth2 access token.
399 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. 386 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue.
400 * @private 387 * @private
401 */ 388 */
402 sendOrQueueRequest_: function(request) { 389 sendOrQueueRequest_: function(request) {
403 if (request.origin == print_preview.DestinationOrigin.COOKIES) { 390 if (request.origin == print_preview.DestinationOrigin.COOKIES)
404 return this.sendRequest_(request); 391 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
405 } else { 392
406 this.requestQueue_.push(request); 393 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
407 this.nativeLayer_.startGetAccessToken(request.origin); 394 if (this.accessTokenRequestPromise_ != null)
408 } 395 return;
396
397 this.accessTokenRequestPromise_ =
398 this.nativeLayer_.getAccessToken(request.origin).then(
399 this.onAccessTokenReady_.bind(this));
409 }, 400 },
410 401
411 /** 402 /**
412 * Sends a request to the Google Cloud Print API. 403 * Sends a request to the Google Cloud Print API.
413 * @param {!cloudprint.CloudPrintRequest} request Request to send. 404 * @param {!cloudprint.CloudPrintRequest} request Request to send.
414 * @private 405 * @private
415 */ 406 */
416 sendRequest_: function(request) { 407 sendRequest_: function(request) {
417 request.xhr.onreadystatechange = 408 request.xhr.onreadystatechange =
418 this.onReadyStateChange_.bind(this, request); 409 this.onReadyStateChange_.bind(this, request);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 this.outstandingCloudSearchRequests_.filter(function(request) { 461 this.outstandingCloudSearchRequests_.filter(function(request) {
471 if (origins.indexOf(request.origin) >= 0) { 462 if (origins.indexOf(request.origin) >= 0) {
472 request.xhr.abort(); 463 request.xhr.abort();
473 return false; 464 return false;
474 } 465 }
475 return true; 466 return true;
476 }); 467 });
477 }, 468 },
478 469
479 /** 470 /**
480 * Called when a native layer receives access token. 471 * Called when a native layer receives access token. Assumes that the
481 * @param {Event} event Contains the authentication type and access token. 472 * destination type for this token is DestinationOrigin.DEVICE.
473 * @param {string} accessToken The access token obtained.
482 * @private 474 * @private
483 */ 475 */
484 onAccessTokenReady_: function(event) { 476 onAccessTokenReady_: function(accessToken) {
485 // TODO(vitalybuka): remove when other Origins implemented. 477 this.requestQueue_ = this.requestQueue_.forEach(function(request) {
486 assert(event.authType == print_preview.DestinationOrigin.DEVICE);
487 this.requestQueue_ = this.requestQueue_.filter(function(request) {
488 assert(request.origin == print_preview.DestinationOrigin.DEVICE); 478 assert(request.origin == print_preview.DestinationOrigin.DEVICE);
489 if (request.origin != event.authType) { 479 if (accessToken) {
490 return true;
491 }
492 if (event.accessToken) {
493 request.xhr.setRequestHeader( 480 request.xhr.setRequestHeader(
494 'Authorization', 'Bearer ' + event.accessToken); 481 'Authorization', 'Bearer ' + accessToken);
495 this.sendRequest_(request); 482 this.sendRequest_(request);
496 } else { // No valid token. 483 } else { // No valid token.
497 // Without abort status does not exist. 484 // Without abort status does not exist.
498 request.xhr.abort(); 485 request.xhr.abort();
499 request.callback(request); 486 request.callback(request);
500 } 487 }
501 return false;
502 }, this); 488 }, this);
489 this.requestQueue_ = [];
490 this.accessTokenRequestPromise_ = null;
503 }, 491 },
504 492
505 /** 493 /**
506 * Called when the ready-state of a XML http request changes. 494 * Called when the ready-state of a XML http request changes.
507 * Calls the successCallback with the result or dispatches an ERROR event. 495 * Calls the successCallback with the result or dispatches an ERROR event.
508 * @param {!cloudprint.CloudPrintRequest} request Request that was changed. 496 * @param {!cloudprint.CloudPrintRequest} request Request that was changed.
509 * @private 497 * @private
510 */ 498 */
511 onReadyStateChange_: function(request) { 499 onReadyStateChange_: function(request) {
512 if (request.xhr.readyState == 4) { 500 if (request.xhr.readyState == 4) {
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 */ 777 */
790 this.value = value; 778 this.value = value;
791 } 779 }
792 780
793 // Export 781 // Export
794 return { 782 return {
795 CloudPrintInterface: CloudPrintInterface, 783 CloudPrintInterface: CloudPrintInterface,
796 CloudPrintRequest: CloudPrintRequest 784 CloudPrintRequest: CloudPrintRequest
797 }; 785 };
798 }); 786 });
OLDNEW
« 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