OLD | NEW |
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 * @private | 93 * @private |
94 */ | 94 */ |
95 this.requestQueue_ = []; | 95 this.requestQueue_ = []; |
96 | 96 |
97 /** | 97 /** |
98 * Outstanding cloud destination search requests. | 98 * Outstanding cloud destination search requests. |
99 * @type {!Array<!cloudprint.CloudPrintRequest>} | 99 * @type {!Array<!cloudprint.CloudPrintRequest>} |
100 * @private | 100 * @private |
101 */ | 101 */ |
102 this.outstandingCloudSearchRequests_ = []; | 102 this.outstandingCloudSearchRequests_ = []; |
103 | |
104 /** | |
105 * Event tracker used to keep track of native layer events. | |
106 * @type {!EventTracker} | |
107 * @private | |
108 */ | |
109 this.tracker_ = new EventTracker(); | |
110 | |
111 this.addEventListeners_(); | |
112 } | 103 } |
113 | 104 |
114 /** | 105 /** |
115 * Content type header value for a URL encoded HTTP request. | 106 * Content type header value for a URL encoded HTTP request. |
116 * @type {string} | 107 * @type {string} |
117 * @const | 108 * @const |
118 * @private | 109 * @private |
119 */ | 110 */ |
120 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = | 111 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = |
121 'application/x-www-form-urlencoded'; | 112 'application/x-www-form-urlencoded'; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 this.sendOrQueueRequest_(this.buildRequest_( | 325 this.sendOrQueueRequest_(this.buildRequest_( |
335 'GET', | 326 'GET', |
336 'printer', | 327 'printer', |
337 params, | 328 params, |
338 origin, | 329 origin, |
339 account || '', | 330 account || '', |
340 this.onPrinterDone_.bind(this, printerId))); | 331 this.onPrinterDone_.bind(this, printerId))); |
341 }, | 332 }, |
342 | 333 |
343 /** | 334 /** |
344 * Adds event listeners to relevant events. | |
345 * @private | |
346 */ | |
347 addEventListeners_: function() { | |
348 this.tracker_.add( | |
349 this.nativeLayer_.getEventTarget(), | |
350 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | |
351 this.onAccessTokenReady_.bind(this)); | |
352 }, | |
353 | |
354 /** | |
355 * Builds request to the Google Cloud Print API. | 335 * Builds request to the Google Cloud Print API. |
356 * @param {string} method HTTP method of the request. | 336 * @param {string} method HTTP method of the request. |
357 * @param {string} action Google Cloud Print action to perform. | 337 * @param {string} action Google Cloud Print action to perform. |
358 * @param {Array<!HttpParam>} params HTTP parameters to include in the | 338 * @param {Array<!HttpParam>} params HTTP parameters to include in the |
359 * request. | 339 * request. |
360 * @param {!print_preview.DestinationOrigin} origin Origin for destination. | 340 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
361 * @param {?string} account Account the request is sent for. Can be | 341 * @param {?string} account Account the request is sent for. Can be |
362 * {@code null} or empty string if the request is not cookie bound or | 342 * {@code null} or empty string if the request is not cookie bound or |
363 * is sent on behalf of the primary user. | 343 * is sent on behalf of the primary user. |
364 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to | 344 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 * Sends a request to the Google Cloud Print API or queues if it needs to | 403 * Sends a request to the Google Cloud Print API or queues if it needs to |
424 * wait OAuth2 access token. | 404 * wait OAuth2 access token. |
425 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. | 405 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. |
426 * @private | 406 * @private |
427 */ | 407 */ |
428 sendOrQueueRequest_: function(request) { | 408 sendOrQueueRequest_: function(request) { |
429 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 409 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
430 return this.sendRequest_(request); | 410 return this.sendRequest_(request); |
431 } else { | 411 } else { |
432 this.requestQueue_.push(request); | 412 this.requestQueue_.push(request); |
433 this.nativeLayer_.startGetAccessToken(request.origin); | 413 this.nativeLayer_.getAccessToken(request.origin).then( |
| 414 this.onAccessTokenReady_.bind(this, request.origin)); |
434 } | 415 } |
435 }, | 416 }, |
436 | 417 |
437 /** | 418 /** |
438 * Sends a request to the Google Cloud Print API. | 419 * Sends a request to the Google Cloud Print API. |
439 * @param {!cloudprint.CloudPrintRequest} request Request to send. | 420 * @param {!cloudprint.CloudPrintRequest} request Request to send. |
440 * @private | 421 * @private |
441 */ | 422 */ |
442 sendRequest_: function(request) { | 423 sendRequest_: function(request) { |
443 request.xhr.onreadystatechange = | 424 request.xhr.onreadystatechange = |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 if (origins.indexOf(request.origin) >= 0) { | 478 if (origins.indexOf(request.origin) >= 0) { |
498 request.xhr.abort(); | 479 request.xhr.abort(); |
499 return false; | 480 return false; |
500 } | 481 } |
501 return true; | 482 return true; |
502 }); | 483 }); |
503 }, | 484 }, |
504 | 485 |
505 /** | 486 /** |
506 * Called when a native layer receives access token. | 487 * Called when a native layer receives access token. |
507 * @param {Event} event Contains the authentication type and access token. | 488 * @param {!print_preview.DestinationOrigin} authType The authentication |
| 489 * type. |
| 490 * @param {!print_preview.AccessTokenResponse} response Contains the access |
| 491 * token and an indicator of whether the token request is complete (if |
| 492 * not, another request for this authentication type was already in |
| 493 * progress). |
508 * @private | 494 * @private |
509 */ | 495 */ |
510 onAccessTokenReady_: function(event) { | 496 onAccessTokenReady_: function(authType, response) { |
| 497 if (!response.completed) |
| 498 return; |
511 // TODO(vitalybuka): remove when other Origins implemented. | 499 // TODO(vitalybuka): remove when other Origins implemented. |
512 assert(event.authType == print_preview.DestinationOrigin.DEVICE); | 500 assert(authType == print_preview.DestinationOrigin.DEVICE); |
513 this.requestQueue_ = this.requestQueue_.filter(function(request) { | 501 this.requestQueue_ = this.requestQueue_.filter(function(request) { |
514 assert(request.origin == print_preview.DestinationOrigin.DEVICE); | 502 assert(request.origin == print_preview.DestinationOrigin.DEVICE); |
515 if (request.origin != event.authType) { | 503 if (request.origin != authType) { |
516 return true; | 504 return true; |
517 } | 505 } |
518 if (event.accessToken) { | 506 if (response.accessToken) { |
519 request.xhr.setRequestHeader('Authorization', | 507 request.xhr.setRequestHeader('Authorization', |
520 'Bearer ' + event.accessToken); | 508 'Bearer ' + response.accessToken); |
521 this.sendRequest_(request); | 509 this.sendRequest_(request); |
522 } else { // No valid token. | 510 } else { // No valid token. |
523 // Without abort status does not exist. | 511 // Without abort status does not exist. |
524 request.xhr.abort(); | 512 request.xhr.abort(); |
525 request.callback(request); | 513 request.callback(request); |
526 } | 514 } |
527 return false; | 515 return false; |
528 }, this); | 516 }, this); |
529 }, | 517 }, |
530 | 518 |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 */ | 809 */ |
822 this.value = value; | 810 this.value = value; |
823 } | 811 } |
824 | 812 |
825 // Export | 813 // Export |
826 return { | 814 return { |
827 CloudPrintInterface: CloudPrintInterface, | 815 CloudPrintInterface: CloudPrintInterface, |
828 CloudPrintRequest: CloudPrintRequest | 816 CloudPrintRequest: CloudPrintRequest |
829 }; | 817 }; |
830 }); | 818 }); |
OLD | NEW |