Chromium Code Reviews| 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'); | |
| 6 | |
| 7 /** | |
| 8 * Event types dispatched by the cloudprint interface. | |
| 9 * @enum {string} | |
| 10 */ | |
| 11 cloudprint.CloudPrintInterfaceEventType = { | |
| 12 INVITES_DONE: 'cloudprint.CloudPrintInterface.INVITES_DONE', | |
| 13 INVITES_FAILED: 'cloudprint.CloudPrintInterface.INVITES_FAILED', | |
| 14 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | |
| 15 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', | |
| 16 PROCESS_INVITE_DONE: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_DONE', | |
| 17 PROCESS_INVITE_FAILED: | |
| 18 'cloudprint.CloudPrintInterface.PROCESS_INVITE_FAILED', | |
| 19 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | |
| 20 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', | |
| 21 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', | |
| 22 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', | |
| 23 }; | |
| 24 | |
| 5 cr.define('cloudprint', function() { | 25 cr.define('cloudprint', function() { |
| 6 'use strict'; | 26 'use strict'; |
| 7 | 27 |
| 28 var CloudPrintInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; | |
| 29 | |
| 8 /** | 30 /** |
| 9 * API to the Google Cloud Print service. | 31 * API to the Google Cloud Print service. |
| 10 * @param {string} baseUrl Base part of the Google Cloud Print service URL | 32 * @param {string} baseUrl Base part of the Google Cloud Print service URL |
| 11 * with no trailing slash. For example, | 33 * with no trailing slash. For example, |
| 12 * 'https://www.google.com/cloudprint'. | 34 * 'https://www.google.com/cloudprint'. |
| 13 * @param {!print_preview.NativeLayer} nativeLayer Native layer used to get | 35 * @param {!print_preview.NativeLayer} nativeLayer Native layer used to get |
| 14 * Auth2 tokens. | 36 * Auth2 tokens. |
| 15 * @param {!print_preview.UserInfo} userInfo User information repository. | 37 * @param {!print_preview.UserInfo} userInfo User information repository. |
| 16 * @param {boolean} isInAppKioskMode Whether the print preview is in App | 38 * @param {boolean} isInAppKioskMode Whether the print preview is in App |
| 17 * Kiosk mode. | 39 * Kiosk mode. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 /** | 82 /** |
| 61 * Stores last received XSRF tokens for each user account. Sent as | 83 * Stores last received XSRF tokens for each user account. Sent as |
| 62 * a parameter with every request. | 84 * a parameter with every request. |
| 63 * @type {!Object<string>} | 85 * @type {!Object<string>} |
| 64 * @private | 86 * @private |
| 65 */ | 87 */ |
| 66 this.xsrfTokens_ = {}; | 88 this.xsrfTokens_ = {}; |
| 67 | 89 |
| 68 /** | 90 /** |
| 69 * Pending requests delayed until we get access token. | 91 * Pending requests delayed until we get access token. |
| 70 * @type {!Array<!CloudPrintRequest>} | 92 * @type {!Array<!cloudprint.CloudPrintRequest>} |
| 71 * @private | 93 * @private |
| 72 */ | 94 */ |
| 73 this.requestQueue_ = []; | 95 this.requestQueue_ = []; |
| 74 | 96 |
| 75 /** | 97 /** |
| 76 * Outstanding cloud destination search requests. | 98 * Outstanding cloud destination search requests. |
| 77 * @type {!Array<!CloudPrintRequest>} | 99 * @type {!Array<!cloudprint.CloudPrintRequest>} |
| 78 * @private | 100 * @private |
| 79 */ | 101 */ |
| 80 this.outstandingCloudSearchRequests_ = []; | 102 this.outstandingCloudSearchRequests_ = []; |
| 81 | 103 |
| 82 /** | 104 /** |
| 83 * Event tracker used to keep track of native layer events. | 105 * Event tracker used to keep track of native layer events. |
| 84 * @type {!EventTracker} | 106 * @type {!EventTracker} |
| 85 * @private | 107 * @private |
| 86 */ | 108 */ |
| 87 this.tracker_ = new EventTracker(); | 109 this.tracker_ = new EventTracker(); |
| 88 | 110 |
| 89 this.addEventListeners_(); | 111 this.addEventListeners_(); |
| 90 }; | 112 } |
| 91 | |
| 92 /** | |
| 93 * Event types dispatched by the interface. | |
| 94 * @enum {string} | |
| 95 */ | |
| 96 CloudPrintInterface.EventType = { | |
| 97 INVITES_DONE: 'cloudprint.CloudPrintInterface.INVITES_DONE', | |
| 98 INVITES_FAILED: 'cloudprint.CloudPrintInterface.INVITES_FAILED', | |
| 99 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | |
| 100 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', | |
| 101 PROCESS_INVITE_DONE: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_DONE', | |
| 102 PROCESS_INVITE_FAILED: | |
| 103 'cloudprint.CloudPrintInterface.PROCESS_INVITE_FAILED', | |
| 104 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | |
| 105 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', | |
| 106 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', | |
| 107 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', | |
| 108 }; | |
| 109 | 113 |
| 110 /** | 114 /** |
| 111 * Content type header value for a URL encoded HTTP request. | 115 * Content type header value for a URL encoded HTTP request. |
| 112 * @type {string} | 116 * @type {string} |
| 113 * @const | 117 * @const |
| 114 * @private | 118 * @private |
| 115 */ | 119 */ |
| 116 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = | 120 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = |
| 117 'application/x-www-form-urlencoded'; | 121 'application/x-www-form-urlencoded'; |
| 118 | 122 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 180 |
| 177 /** | 181 /** |
| 178 * @return {boolean} Whether a search for cloud destinations is in progress. | 182 * @return {boolean} Whether a search for cloud destinations is in progress. |
| 179 */ | 183 */ |
| 180 get isCloudDestinationSearchInProgress() { | 184 get isCloudDestinationSearchInProgress() { |
| 181 return this.outstandingCloudSearchRequests_.length > 0; | 185 return this.outstandingCloudSearchRequests_.length > 0; |
| 182 }, | 186 }, |
| 183 | 187 |
| 184 /** | 188 /** |
| 185 * Sends Google Cloud Print search API request. | 189 * Sends Google Cloud Print search API request. |
| 186 * @param {string=} opt_account Account the search is sent for. When | 190 * @param {?string=} opt_account Account the search is sent for. When |
| 187 * omitted, the search is done on behalf of the primary user. | 191 * null or omitted, the search is done on behalf of the primary user. |
| 188 * @param {print_preview.DestinationOrigin=} opt_origin When specified, | 192 * @param {print_preview.DestinationOrigin=} opt_origin When specified, |
| 189 * searches destinations for {@code opt_origin} only, otherwise starts | 193 * searches destinations for {@code opt_origin} only, otherwise starts |
| 190 * searches for all origins. | 194 * searches for all origins. |
| 191 */ | 195 */ |
| 192 search: function(opt_account, opt_origin) { | 196 search: function(opt_account, opt_origin) { |
| 193 var account = opt_account || ''; | 197 var account = opt_account || ''; |
| 194 var origins = | 198 var origins = |
| 195 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_; | 199 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_; |
| 196 if (this.isInAppKioskMode_) { | 200 if (this.isInAppKioskMode_) { |
| 197 origins = origins.filter(function(origin) { | 201 origins = origins.filter(function(origin) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 | 259 |
| 256 /** | 260 /** |
| 257 * Accepts or rejects printer sharing invitation. | 261 * Accepts or rejects printer sharing invitation. |
| 258 * @param {!print_preview.Invitation} invitation Invitation to process. | 262 * @param {!print_preview.Invitation} invitation Invitation to process. |
| 259 * @param {boolean} accept Whether to accept this invitation. | 263 * @param {boolean} accept Whether to accept this invitation. |
| 260 */ | 264 */ |
| 261 processInvite: function(invitation, accept) { | 265 processInvite: function(invitation, accept) { |
| 262 var params = [ | 266 var params = [ |
| 263 new HttpParam('printerid', invitation.destination.id), | 267 new HttpParam('printerid', invitation.destination.id), |
| 264 new HttpParam('email', invitation.scopeId), | 268 new HttpParam('email', invitation.scopeId), |
| 265 new HttpParam('accept', accept), | 269 accept ? new HttpParam('accept', 'true') : |
|
dpapad
2017/05/05 23:30:29
You can compact (and satisfy the compiler) as foll
rbpotter
2017/05/06 00:02:19
Done.
| |
| 266 new HttpParam('use_cdd', true), | 270 new HttpParam('accept', 'false'), |
| 271 new HttpParam('use_cdd', 'true'), | |
| 267 ]; | 272 ]; |
| 268 this.sendOrQueueRequest_(this.buildRequest_( | 273 this.sendOrQueueRequest_(this.buildRequest_( |
| 269 'POST', | 274 'POST', |
| 270 'processinvite', | 275 'processinvite', |
| 271 params, | 276 params, |
| 272 invitation.destination.origin, | 277 invitation.destination.origin, |
| 273 invitation.destination.account, | 278 invitation.destination.account, |
| 274 this.onProcessInviteDone_.bind(this, invitation, accept))); | 279 this.onProcessInviteDone_.bind(this, invitation, accept))); |
| 275 }, | 280 }, |
| 276 | 281 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 var params = [ | 330 var params = [ |
| 326 new HttpParam('printerid', printerId), | 331 new HttpParam('printerid', printerId), |
| 327 new HttpParam('use_cdd', 'true'), | 332 new HttpParam('use_cdd', 'true'), |
| 328 new HttpParam('printer_connection_status', 'true') | 333 new HttpParam('printer_connection_status', 'true') |
| 329 ]; | 334 ]; |
| 330 this.sendOrQueueRequest_(this.buildRequest_( | 335 this.sendOrQueueRequest_(this.buildRequest_( |
| 331 'GET', | 336 'GET', |
| 332 'printer', | 337 'printer', |
| 333 params, | 338 params, |
| 334 origin, | 339 origin, |
| 335 account, | 340 account || '', |
| 336 this.onPrinterDone_.bind(this, printerId))); | 341 this.onPrinterDone_.bind(this, printerId))); |
| 337 }, | 342 }, |
| 338 | 343 |
| 339 /** | 344 /** |
| 340 * Adds event listeners to relevant events. | 345 * Adds event listeners to relevant events. |
| 341 * @private | 346 * @private |
| 342 */ | 347 */ |
| 343 addEventListeners_: function() { | 348 addEventListeners_: function() { |
| 344 this.tracker_.add( | 349 this.tracker_.add( |
| 345 this.nativeLayer_, | 350 this.nativeLayer_, |
| 346 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | 351 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, |
| 347 this.onAccessTokenReady_.bind(this)); | 352 this.onAccessTokenReady_.bind(this)); |
| 348 }, | 353 }, |
| 349 | 354 |
| 350 /** | 355 /** |
| 351 * Builds request to the Google Cloud Print API. | 356 * Builds request to the Google Cloud Print API. |
| 352 * @param {string} method HTTP method of the request. | 357 * @param {string} method HTTP method of the request. |
| 353 * @param {string} action Google Cloud Print action to perform. | 358 * @param {string} action Google Cloud Print action to perform. |
| 354 * @param {Array<!HttpParam>} params HTTP parameters to include in the | 359 * @param {Array<!HttpParam>} params HTTP parameters to include in the |
| 355 * request. | 360 * request. |
| 356 * @param {!print_preview.DestinationOrigin} origin Origin for destination. | 361 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
| 357 * @param {?string} account Account the request is sent for. Can be | 362 * @param {?string} account Account the request is sent for. Can be |
| 358 * {@code null} or empty string if the request is not cookie bound or | 363 * {@code null} or empty string if the request is not cookie bound or |
| 359 * is sent on behalf of the primary user. | 364 * is sent on behalf of the primary user. |
| 360 * @param {function(number, Object, !print_preview.DestinationOrigin)} | 365 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to |
| 361 * callback Callback to invoke when request completes. | 366 * invoke when request completes. |
| 362 * @return {!CloudPrintRequest} Partially prepared request. | 367 * @return {!cloudprint.CloudPrintRequest} Partially prepared request. |
| 363 * @private | 368 * @private |
| 364 */ | 369 */ |
| 365 buildRequest_: function(method, action, params, origin, account, callback) { | 370 buildRequest_: function(method, action, params, origin, account, callback) { |
| 366 var url = this.baseUrl_ + '/' + action + '?xsrf='; | 371 var url = this.baseUrl_ + '/' + action + '?xsrf='; |
| 367 if (origin == print_preview.DestinationOrigin.COOKIES) { | 372 if (origin == print_preview.DestinationOrigin.COOKIES) { |
| 368 var xsrfToken = this.xsrfTokens_[account]; | 373 var xsrfToken = this.xsrfTokens_[account]; |
| 369 if (!xsrfToken) { | 374 if (!xsrfToken) { |
| 370 // TODO(rltoscano): Should throw an error if not a read-only action or | 375 // TODO(rltoscano): Should throw an error if not a read-only action or |
| 371 // issue an xsrf token request. | 376 // issue an xsrf token request. |
| 372 } else { | 377 } else { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 } | 409 } |
| 405 | 410 |
| 406 var xhr = new XMLHttpRequest(); | 411 var xhr = new XMLHttpRequest(); |
| 407 xhr.open(method, url, true); | 412 xhr.open(method, url, true); |
| 408 xhr.withCredentials = | 413 xhr.withCredentials = |
| 409 (origin == print_preview.DestinationOrigin.COOKIES); | 414 (origin == print_preview.DestinationOrigin.COOKIES); |
| 410 for (var header in headers) { | 415 for (var header in headers) { |
| 411 xhr.setRequestHeader(header, headers[header]); | 416 xhr.setRequestHeader(header, headers[header]); |
| 412 } | 417 } |
| 413 | 418 |
| 414 return new CloudPrintRequest(xhr, body, origin, account, callback); | 419 return new cloudprint.CloudPrintRequest(xhr, body, origin, account, |
| 420 callback); | |
| 415 }, | 421 }, |
| 416 | 422 |
| 417 /** | 423 /** |
| 418 * Sends a request to the Google Cloud Print API or queues if it needs to | 424 * Sends a request to the Google Cloud Print API or queues if it needs to |
| 419 * wait OAuth2 access token. | 425 * wait OAuth2 access token. |
| 420 * @param {!CloudPrintRequest} request Request to send or queue. | 426 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. |
| 421 * @private | 427 * @private |
| 422 */ | 428 */ |
| 423 sendOrQueueRequest_: function(request) { | 429 sendOrQueueRequest_: function(request) { |
| 424 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 430 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
| 425 return this.sendRequest_(request); | 431 return this.sendRequest_(request); |
| 426 } else { | 432 } else { |
| 427 this.requestQueue_.push(request); | 433 this.requestQueue_.push(request); |
| 428 this.nativeLayer_.startGetAccessToken(request.origin); | 434 this.nativeLayer_.startGetAccessToken(request.origin); |
| 429 } | 435 } |
| 430 }, | 436 }, |
| 431 | 437 |
| 432 /** | 438 /** |
| 433 * Sends a request to the Google Cloud Print API. | 439 * Sends a request to the Google Cloud Print API. |
| 434 * @param {!CloudPrintRequest} request Request to send. | 440 * @param {!cloudprint.CloudPrintRequest} request Request to send. |
| 435 * @private | 441 * @private |
| 436 */ | 442 */ |
| 437 sendRequest_: function(request) { | 443 sendRequest_: function(request) { |
| 438 request.xhr.onreadystatechange = | 444 request.xhr.onreadystatechange = |
| 439 this.onReadyStateChange_.bind(this, request); | 445 this.onReadyStateChange_.bind(this, request); |
| 440 request.xhr.send(request.body); | 446 request.xhr.send(request.body); |
| 441 }, | 447 }, |
| 442 | 448 |
| 443 /** | 449 /** |
| 444 * Creates a Google Cloud Print interface error that is ready to dispatch. | 450 * Creates a Google Cloud Print interface error that is ready to dispatch. |
| 445 * @param {!CloudPrintInterface.EventType} type Type of the error. | 451 * @param {!cloudprint.CloudPrintInterfaceEventType} type Type of the |
| 446 * @param {!CloudPrintRequest} request Request that has been completed. | 452 * error. |
| 453 * @param {!cloudprint.CloudPrintRequest} request Request that has been | |
| 454 * completed. | |
| 447 * @return {!Event} Google Cloud Print interface error event. | 455 * @return {!Event} Google Cloud Print interface error event. |
| 448 * @private | 456 * @private |
| 449 */ | 457 */ |
| 450 createErrorEvent_: function(type, request) { | 458 createErrorEvent_: function(type, request) { |
| 451 var errorEvent = new Event(type); | 459 var errorEvent = new Event(type); |
| 452 errorEvent.status = request.xhr.status; | 460 errorEvent.status = request.xhr.status; |
| 453 if (request.xhr.status == 200) { | 461 if (request.xhr.status == 200) { |
| 454 errorEvent.errorCode = request.result['errorCode']; | 462 errorEvent.errorCode = request.result['errorCode']; |
| 455 errorEvent.message = request.result['message']; | 463 errorEvent.message = request.result['message']; |
| 456 } else { | 464 } else { |
| 457 errorEvent.errorCode = 0; | 465 errorEvent.errorCode = 0; |
| 458 errorEvent.message = ''; | 466 errorEvent.message = ''; |
| 459 } | 467 } |
| 460 errorEvent.origin = request.origin; | 468 errorEvent.origin = request.origin; |
| 461 return errorEvent; | 469 return errorEvent; |
| 462 }, | 470 }, |
| 463 | 471 |
| 464 /** | 472 /** |
| 465 * Updates user info and session index from the {@code request} response. | 473 * Updates user info and session index from the {@code request} response. |
| 466 * @param {!CloudPrintRequest} request Request to extract user info from. | 474 * @param {!cloudprint.CloudPrintRequest} request Request to extract user |
| 475 * info from. | |
| 467 * @private | 476 * @private |
| 468 */ | 477 */ |
| 469 setUsers_: function(request) { | 478 setUsers_: function(request) { |
| 470 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 479 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
| 471 var users = request.result['request']['users'] || []; | 480 var users = request.result['request']['users'] || []; |
| 472 this.userSessionIndex_ = {}; | 481 this.userSessionIndex_ = {}; |
| 473 for (var i = 0; i < users.length; i++) { | 482 for (var i = 0; i < users.length; i++) { |
| 474 this.userSessionIndex_[users[i]] = i; | 483 this.userSessionIndex_[users[i]] = i; |
| 475 } | 484 } |
| 476 this.userInfo_.setUsers(request.result['request']['user'], users); | 485 this.userInfo_.setUsers(request.result['request']['user'], users); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 request.xhr.abort(); | 525 request.xhr.abort(); |
| 517 request.callback(request); | 526 request.callback(request); |
| 518 } | 527 } |
| 519 return false; | 528 return false; |
| 520 }, this); | 529 }, this); |
| 521 }, | 530 }, |
| 522 | 531 |
| 523 /** | 532 /** |
| 524 * Called when the ready-state of a XML http request changes. | 533 * Called when the ready-state of a XML http request changes. |
| 525 * Calls the successCallback with the result or dispatches an ERROR event. | 534 * Calls the successCallback with the result or dispatches an ERROR event. |
| 526 * @param {!CloudPrintRequest} request Request that was changed. | 535 * @param {!cloudprint.CloudPrintRequest} request Request that was changed. |
| 527 * @private | 536 * @private |
| 528 */ | 537 */ |
| 529 onReadyStateChange_: function(request) { | 538 onReadyStateChange_: function(request) { |
| 530 if (request.xhr.readyState == 4) { | 539 if (request.xhr.readyState == 4) { |
| 531 if (request.xhr.status == 200) { | 540 if (request.xhr.status == 200) { |
| 532 request.result = JSON.parse(request.xhr.responseText); | 541 request.result = /** @type {Object} */ ( |
| 542 JSON.parse(request.xhr.responseText)); | |
| 533 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 543 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
| 534 request.result['success']) { | 544 request.result['success']) { |
| 535 this.xsrfTokens_[request.result['request']['user']] = | 545 this.xsrfTokens_[request.result['request']['user']] = |
| 536 request.result['xsrf_token']; | 546 request.result['xsrf_token']; |
| 537 } | 547 } |
| 538 } | 548 } |
| 539 request.status = request.xhr.status; | 549 request.status = request.xhr.status; |
| 540 request.callback(request); | 550 request.callback(request); |
| 541 } | 551 } |
| 542 }, | 552 }, |
| 543 | 553 |
| 544 /** | 554 /** |
| 545 * Called when the search request completes. | 555 * Called when the search request completes. |
| 546 * @param {boolean} isRecent Whether the search request was for recent | 556 * @param {boolean} isRecent Whether the search request was for recent |
| 547 * destinations. | 557 * destinations. |
| 548 * @param {!CloudPrintRequest} request Request that has been completed. | 558 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 559 * completed. | |
| 549 * @private | 560 * @private |
| 550 */ | 561 */ |
| 551 onSearchDone_: function(isRecent, request) { | 562 onSearchDone_: function(isRecent, request) { |
| 552 var lastRequestForThisOrigin = true; | 563 var lastRequestForThisOrigin = true; |
| 553 this.outstandingCloudSearchRequests_ = | 564 this.outstandingCloudSearchRequests_ = |
| 554 this.outstandingCloudSearchRequests_.filter(function(item) { | 565 this.outstandingCloudSearchRequests_.filter(function(item) { |
| 555 if (item != request && item.origin == request.origin) { | 566 if (item != request && item.origin == request.origin) { |
| 556 lastRequestForThisOrigin = false; | 567 lastRequestForThisOrigin = false; |
| 557 } | 568 } |
| 558 return item != request; | 569 return item != request; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 573 try { | 584 try { |
| 574 printerList.push(cloudprint.CloudDestinationParser.parse( | 585 printerList.push(cloudprint.CloudDestinationParser.parse( |
| 575 printerJson, request.origin, activeUser)); | 586 printerJson, request.origin, activeUser)); |
| 576 } catch (err) { | 587 } catch (err) { |
| 577 console.error('Unable to parse cloud print destination: ' + err); | 588 console.error('Unable to parse cloud print destination: ' + err); |
| 578 } | 589 } |
| 579 }); | 590 }); |
| 580 // Extract and store users. | 591 // Extract and store users. |
| 581 this.setUsers_(request); | 592 this.setUsers_(request); |
| 582 // Dispatch SEARCH_DONE event. | 593 // Dispatch SEARCH_DONE event. |
| 583 event = new Event(CloudPrintInterface.EventType.SEARCH_DONE); | 594 event = new Event(CloudPrintInterfaceEventType.SEARCH_DONE); |
| 584 event.origin = request.origin; | 595 event.origin = request.origin; |
| 585 event.printers = printerList; | 596 event.printers = printerList; |
| 586 event.isRecent = isRecent; | 597 event.isRecent = isRecent; |
| 587 } else { | 598 } else { |
| 588 event = this.createErrorEvent_( | 599 event = this.createErrorEvent_( |
| 589 CloudPrintInterface.EventType.SEARCH_FAILED, | 600 CloudPrintInterfaceEventType.SEARCH_FAILED, |
| 590 request); | 601 request); |
| 591 } | 602 } |
| 592 event.user = activeUser; | 603 event.user = activeUser; |
| 593 event.searchDone = lastRequestForThisOrigin; | 604 event.searchDone = lastRequestForThisOrigin; |
| 594 this.dispatchEvent(event); | 605 this.dispatchEvent(event); |
| 595 }, | 606 }, |
| 596 | 607 |
| 597 /** | 608 /** |
| 598 * Called when invitations search request completes. | 609 * Called when invitations search request completes. |
| 599 * @param {!CloudPrintRequest} request Request that has been completed. | 610 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 611 * completed. | |
| 600 * @private | 612 * @private |
| 601 */ | 613 */ |
| 602 onInvitesDone_: function(request) { | 614 onInvitesDone_: function(request) { |
| 603 var event = null; | 615 var event = null; |
| 604 var activeUser = | 616 var activeUser = |
| 605 (request.result && | 617 (request.result && |
| 606 request.result['request'] && | 618 request.result['request'] && |
| 607 request.result['request']['user']) || ''; | 619 request.result['request']['user']) || ''; |
| 608 if (request.xhr.status == 200 && request.result['success']) { | 620 if (request.xhr.status == 200 && request.result['success']) { |
| 609 // Extract invitations. | 621 // Extract invitations. |
| 610 var invitationListJson = request.result['invites'] || []; | 622 var invitationListJson = request.result['invites'] || []; |
| 611 var invitationList = []; | 623 var invitationList = []; |
| 612 invitationListJson.forEach(function(invitationJson) { | 624 invitationListJson.forEach(function(invitationJson) { |
| 613 try { | 625 try { |
| 614 invitationList.push(cloudprint.InvitationParser.parse( | 626 invitationList.push(cloudprint.InvitationParser.parse( |
| 615 invitationJson, activeUser)); | 627 invitationJson, activeUser)); |
| 616 } catch (e) { | 628 } catch (e) { |
| 617 console.error('Unable to parse invitation: ' + e); | 629 console.error('Unable to parse invitation: ' + e); |
| 618 } | 630 } |
| 619 }); | 631 }); |
| 620 // Dispatch INVITES_DONE event. | 632 // Dispatch INVITES_DONE event. |
| 621 event = new Event(CloudPrintInterface.EventType.INVITES_DONE); | 633 event = new Event(CloudPrintInterfaceEventType.INVITES_DONE); |
| 622 event.invitations = invitationList; | 634 event.invitations = invitationList; |
| 623 } else { | 635 } else { |
| 624 event = this.createErrorEvent_( | 636 event = this.createErrorEvent_( |
| 625 CloudPrintInterface.EventType.INVITES_FAILED, request); | 637 CloudPrintInterfaceEventType.INVITES_FAILED, request); |
| 626 } | 638 } |
| 627 event.user = activeUser; | 639 event.user = activeUser; |
| 628 this.dispatchEvent(event); | 640 this.dispatchEvent(event); |
| 629 }, | 641 }, |
| 630 | 642 |
| 631 /** | 643 /** |
| 632 * Called when invitation processing request completes. | 644 * Called when invitation processing request completes. |
| 633 * @param {!print_preview.Invitation} invitation Processed invitation. | 645 * @param {!print_preview.Invitation} invitation Processed invitation. |
| 634 * @param {boolean} accept Whether this invitation was accepted or rejected. | 646 * @param {boolean} accept Whether this invitation was accepted or rejected. |
| 635 * @param {!CloudPrintRequest} request Request that has been completed. | 647 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 648 * completed. | |
| 636 * @private | 649 * @private |
| 637 */ | 650 */ |
| 638 onProcessInviteDone_: function(invitation, accept, request) { | 651 onProcessInviteDone_: function(invitation, accept, request) { |
| 639 var event = null; | 652 var event = null; |
| 640 var activeUser = | 653 var activeUser = |
| 641 (request.result && | 654 (request.result && |
| 642 request.result['request'] && | 655 request.result['request'] && |
| 643 request.result['request']['user']) || ''; | 656 request.result['request']['user']) || ''; |
| 644 if (request.xhr.status == 200 && request.result['success']) { | 657 if (request.xhr.status == 200 && request.result['success']) { |
| 645 event = new Event(CloudPrintInterface.EventType.PROCESS_INVITE_DONE); | 658 event = new Event(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE); |
| 646 if (accept) { | 659 if (accept) { |
| 647 try { | 660 try { |
| 648 event.printer = cloudprint.CloudDestinationParser.parse( | 661 event.printer = cloudprint.CloudDestinationParser.parse( |
| 649 request.result['printer'], request.origin, activeUser); | 662 request.result['printer'], request.origin, activeUser); |
| 650 } catch (e) { | 663 } catch (e) { |
| 651 console.error('Failed to parse cloud print destination: ' + e); | 664 console.error('Failed to parse cloud print destination: ' + e); |
| 652 } | 665 } |
| 653 } | 666 } |
| 654 } else { | 667 } else { |
| 655 event = this.createErrorEvent_( | 668 event = this.createErrorEvent_( |
| 656 CloudPrintInterface.EventType.PROCESS_INVITE_FAILED, request); | 669 CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED, request); |
| 657 } | 670 } |
| 658 event.invitation = invitation; | 671 event.invitation = invitation; |
| 659 event.accept = accept; | 672 event.accept = accept; |
| 660 event.user = activeUser; | 673 event.user = activeUser; |
| 661 this.dispatchEvent(event); | 674 this.dispatchEvent(event); |
| 662 }, | 675 }, |
| 663 | 676 |
| 664 /** | 677 /** |
| 665 * Called when the submit request completes. | 678 * Called when the submit request completes. |
| 666 * @param {!CloudPrintRequest} request Request that has been completed. | 679 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 680 * completed. | |
| 667 * @private | 681 * @private |
| 668 */ | 682 */ |
| 669 onSubmitDone_: function(request) { | 683 onSubmitDone_: function(request) { |
| 670 if (request.xhr.status == 200 && request.result['success']) { | 684 if (request.xhr.status == 200 && request.result['success']) { |
| 671 var submitDoneEvent = new Event( | 685 var submitDoneEvent = new Event( |
| 672 CloudPrintInterface.EventType.SUBMIT_DONE); | 686 CloudPrintInterfaceEventType.SUBMIT_DONE); |
| 673 submitDoneEvent.jobId = request.result['job']['id']; | 687 submitDoneEvent.jobId = request.result['job']['id']; |
| 674 this.dispatchEvent(submitDoneEvent); | 688 this.dispatchEvent(submitDoneEvent); |
| 675 } else { | 689 } else { |
| 676 var errorEvent = this.createErrorEvent_( | 690 var errorEvent = this.createErrorEvent_( |
| 677 CloudPrintInterface.EventType.SUBMIT_FAILED, request); | 691 CloudPrintInterfaceEventType.SUBMIT_FAILED, request); |
| 678 this.dispatchEvent(errorEvent); | 692 this.dispatchEvent(errorEvent); |
| 679 } | 693 } |
| 680 }, | 694 }, |
| 681 | 695 |
| 682 /** | 696 /** |
| 683 * Called when the printer request completes. | 697 * Called when the printer request completes. |
| 684 * @param {string} destinationId ID of the destination that was looked up. | 698 * @param {string} destinationId ID of the destination that was looked up. |
| 685 * @param {!CloudPrintRequest} request Request that has been completed. | 699 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 700 * completed. | |
| 686 * @private | 701 * @private |
| 687 */ | 702 */ |
| 688 onPrinterDone_: function(destinationId, request) { | 703 onPrinterDone_: function(destinationId, request) { |
| 689 // Special handling of the first printer request. It does not matter at | 704 // Special handling of the first printer request. It does not matter at |
| 690 // this point, whether printer was found or not. | 705 // this point, whether printer was found or not. |
| 691 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 706 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
| 692 request.result && | 707 request.result && |
| 693 request.account && | 708 request.account && |
| 694 request.result['request']['user'] && | 709 request.result['request']['user'] && |
| 695 request.result['request']['users'] && | 710 request.result['request']['users'] && |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 718 var printer; | 733 var printer; |
| 719 try { | 734 try { |
| 720 printer = cloudprint.CloudDestinationParser.parse( | 735 printer = cloudprint.CloudDestinationParser.parse( |
| 721 printerJson, request.origin, activeUser); | 736 printerJson, request.origin, activeUser); |
| 722 } catch (err) { | 737 } catch (err) { |
| 723 console.error('Failed to parse cloud print destination: ' + | 738 console.error('Failed to parse cloud print destination: ' + |
| 724 JSON.stringify(printerJson)); | 739 JSON.stringify(printerJson)); |
| 725 return; | 740 return; |
| 726 } | 741 } |
| 727 var printerDoneEvent = | 742 var printerDoneEvent = |
| 728 new Event(CloudPrintInterface.EventType.PRINTER_DONE); | 743 new Event(CloudPrintInterfaceEventType.PRINTER_DONE); |
| 729 printerDoneEvent.printer = printer; | 744 printerDoneEvent.printer = printer; |
| 730 this.dispatchEvent(printerDoneEvent); | 745 this.dispatchEvent(printerDoneEvent); |
| 731 } else { | 746 } else { |
| 732 var errorEvent = this.createErrorEvent_( | 747 var errorEvent = this.createErrorEvent_( |
| 733 CloudPrintInterface.EventType.PRINTER_FAILED, request); | 748 CloudPrintInterfaceEventType.PRINTER_FAILED, request); |
| 734 errorEvent.destinationId = destinationId; | 749 errorEvent.destinationId = destinationId; |
| 735 errorEvent.destinationOrigin = request.origin; | 750 errorEvent.destinationOrigin = request.origin; |
| 736 this.dispatchEvent(errorEvent); | 751 this.dispatchEvent(errorEvent); |
| 737 } | 752 } |
| 738 }, | 753 }, |
| 739 }; | 754 }; |
| 740 | 755 |
| 741 /** | 756 /** |
| 742 * Data structure that holds data for Cloud Print requests. | 757 * Data structure that holds data for Cloud Print requests. |
| 743 * @param {!XMLHttpRequest} xhr Partially prepared http request. | 758 * @param {!XMLHttpRequest} xhr Partially prepared http request. |
| 744 * @param {string} body Data to send with POST requests. | 759 * @param {string} body Data to send with POST requests. |
| 745 * @param {!print_preview.DestinationOrigin} origin Origin for destination. | 760 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
| 746 * @param {?string} account Account the request is sent for. Can be | 761 * @param {?string} account Account the request is sent for. Can be |
| 747 * {@code null} or empty string if the request is not cookie bound or | 762 * {@code null} or empty string if the request is not cookie bound or |
| 748 * is sent on behalf of the primary user. | 763 * is sent on behalf of the primary user. |
| 749 * @param {function(!CloudPrintRequest)} callback Callback to invoke when | 764 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to |
| 750 * request completes. | 765 * invoke when request completes. |
| 751 * @constructor | 766 * @constructor |
| 752 */ | 767 */ |
| 753 function CloudPrintRequest(xhr, body, origin, account, callback) { | 768 function CloudPrintRequest(xhr, body, origin, account, callback) { |
| 754 /** | 769 /** |
| 755 * Partially prepared http request. | 770 * Partially prepared http request. |
| 756 * @type {!XMLHttpRequest} | 771 * @type {!XMLHttpRequest} |
| 757 */ | 772 */ |
| 758 this.xhr = xhr; | 773 this.xhr = xhr; |
| 759 | 774 |
| 760 /** | 775 /** |
| 761 * Data to send with POST requests. | 776 * Data to send with POST requests. |
| 762 * @type {string} | 777 * @type {string} |
| 763 */ | 778 */ |
| 764 this.body = body; | 779 this.body = body; |
| 765 | 780 |
| 766 /** | 781 /** |
| 767 * Origin for destination. | 782 * Origin for destination. |
| 768 * @type {!print_preview.DestinationOrigin} | 783 * @type {!print_preview.DestinationOrigin} |
| 769 */ | 784 */ |
| 770 this.origin = origin; | 785 this.origin = origin; |
| 771 | 786 |
| 772 /** | 787 /** |
| 773 * User account this request is expected to be executed for. | 788 * User account this request is expected to be executed for. |
| 774 * @type {?string} | 789 * @type {?string} |
| 775 */ | 790 */ |
| 776 this.account = account; | 791 this.account = account; |
| 777 | 792 |
| 778 /** | 793 /** |
| 779 * Callback to invoke when request completes. | 794 * Callback to invoke when request completes. |
| 780 * @type {function(!CloudPrintRequest)} | 795 * @type {function(!cloudprint.CloudPrintRequest)} |
| 781 */ | 796 */ |
| 782 this.callback = callback; | 797 this.callback = callback; |
| 783 | 798 |
| 784 /** | 799 /** |
| 785 * Result for requests. | 800 * Result for requests. |
| 786 * @type {Object} JSON response. | 801 * @type {Object} JSON response. |
| 787 */ | 802 */ |
| 788 this.result = null; | 803 this.result = null; |
| 789 }; | 804 } |
| 790 | 805 |
| 791 /** | 806 /** |
| 792 * Data structure that represents an HTTP parameter. | 807 * Data structure that represents an HTTP parameter. |
| 793 * @param {string} name Name of the parameter. | 808 * @param {string} name Name of the parameter. |
| 794 * @param {string} value Value of the parameter. | 809 * @param {string} value Value of the parameter. |
| 795 * @constructor | 810 * @constructor |
| 796 */ | 811 */ |
| 797 function HttpParam(name, value) { | 812 function HttpParam(name, value) { |
| 798 /** | 813 /** |
| 799 * Name of the parameter. | 814 * Name of the parameter. |
| 800 * @type {string} | 815 * @type {string} |
| 801 */ | 816 */ |
| 802 this.name = name; | 817 this.name = name; |
| 803 | 818 |
| 804 /** | 819 /** |
| 805 * Name of the value. | 820 * Name of the value. |
| 806 * @type {string} | 821 * @type {string} |
| 807 */ | 822 */ |
| 808 this.value = value; | 823 this.value = value; |
| 809 }; | 824 } |
| 810 | 825 |
| 811 // Export | 826 // Export |
| 812 return { | 827 return { |
| 813 CloudPrintInterface: CloudPrintInterface | 828 CloudPrintInterface: CloudPrintInterface, |
| 829 CloudPrintRequest: CloudPrintRequest | |
| 814 }; | 830 }; |
| 815 }); | 831 }); |
| OLD | NEW |