| 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 new HttpParam('accept', accept ? 'true' : 'false'), |
| 266 new HttpParam('use_cdd', true), | 270 new HttpParam('use_cdd', 'true'), |
| 267 ]; | 271 ]; |
| 268 this.sendOrQueueRequest_(this.buildRequest_( | 272 this.sendOrQueueRequest_(this.buildRequest_( |
| 269 'POST', | 273 'POST', |
| 270 'processinvite', | 274 'processinvite', |
| 271 params, | 275 params, |
| 272 invitation.destination.origin, | 276 invitation.destination.origin, |
| 273 invitation.destination.account, | 277 invitation.destination.account, |
| 274 this.onProcessInviteDone_.bind(this, invitation, accept))); | 278 this.onProcessInviteDone_.bind(this, invitation, accept))); |
| 275 }, | 279 }, |
| 276 | 280 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 var params = [ | 329 var params = [ |
| 326 new HttpParam('printerid', printerId), | 330 new HttpParam('printerid', printerId), |
| 327 new HttpParam('use_cdd', 'true'), | 331 new HttpParam('use_cdd', 'true'), |
| 328 new HttpParam('printer_connection_status', 'true') | 332 new HttpParam('printer_connection_status', 'true') |
| 329 ]; | 333 ]; |
| 330 this.sendOrQueueRequest_(this.buildRequest_( | 334 this.sendOrQueueRequest_(this.buildRequest_( |
| 331 'GET', | 335 'GET', |
| 332 'printer', | 336 'printer', |
| 333 params, | 337 params, |
| 334 origin, | 338 origin, |
| 335 account, | 339 account || '', |
| 336 this.onPrinterDone_.bind(this, printerId))); | 340 this.onPrinterDone_.bind(this, printerId))); |
| 337 }, | 341 }, |
| 338 | 342 |
| 339 /** | 343 /** |
| 340 * Adds event listeners to relevant events. | 344 * Adds event listeners to relevant events. |
| 341 * @private | 345 * @private |
| 342 */ | 346 */ |
| 343 addEventListeners_: function() { | 347 addEventListeners_: function() { |
| 344 this.tracker_.add( | 348 this.tracker_.add( |
| 345 this.nativeLayer_, | 349 this.nativeLayer_, |
| 346 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | 350 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, |
| 347 this.onAccessTokenReady_.bind(this)); | 351 this.onAccessTokenReady_.bind(this)); |
| 348 }, | 352 }, |
| 349 | 353 |
| 350 /** | 354 /** |
| 351 * Builds request to the Google Cloud Print API. | 355 * Builds request to the Google Cloud Print API. |
| 352 * @param {string} method HTTP method of the request. | 356 * @param {string} method HTTP method of the request. |
| 353 * @param {string} action Google Cloud Print action to perform. | 357 * @param {string} action Google Cloud Print action to perform. |
| 354 * @param {Array<!HttpParam>} params HTTP parameters to include in the | 358 * @param {Array<!HttpParam>} params HTTP parameters to include in the |
| 355 * request. | 359 * request. |
| 356 * @param {!print_preview.DestinationOrigin} origin Origin for destination. | 360 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
| 357 * @param {?string} account Account the request is sent for. Can be | 361 * @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 | 362 * {@code null} or empty string if the request is not cookie bound or |
| 359 * is sent on behalf of the primary user. | 363 * is sent on behalf of the primary user. |
| 360 * @param {function(number, Object, !print_preview.DestinationOrigin)} | 364 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to |
| 361 * callback Callback to invoke when request completes. | 365 * invoke when request completes. |
| 362 * @return {!CloudPrintRequest} Partially prepared request. | 366 * @return {!cloudprint.CloudPrintRequest} Partially prepared request. |
| 363 * @private | 367 * @private |
| 364 */ | 368 */ |
| 365 buildRequest_: function(method, action, params, origin, account, callback) { | 369 buildRequest_: function(method, action, params, origin, account, callback) { |
| 366 var url = this.baseUrl_ + '/' + action + '?xsrf='; | 370 var url = this.baseUrl_ + '/' + action + '?xsrf='; |
| 367 if (origin == print_preview.DestinationOrigin.COOKIES) { | 371 if (origin == print_preview.DestinationOrigin.COOKIES) { |
| 368 var xsrfToken = this.xsrfTokens_[account]; | 372 var xsrfToken = this.xsrfTokens_[account]; |
| 369 if (!xsrfToken) { | 373 if (!xsrfToken) { |
| 370 // TODO(rltoscano): Should throw an error if not a read-only action or | 374 // TODO(rltoscano): Should throw an error if not a read-only action or |
| 371 // issue an xsrf token request. | 375 // issue an xsrf token request. |
| 372 } else { | 376 } else { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 } | 408 } |
| 405 | 409 |
| 406 var xhr = new XMLHttpRequest(); | 410 var xhr = new XMLHttpRequest(); |
| 407 xhr.open(method, url, true); | 411 xhr.open(method, url, true); |
| 408 xhr.withCredentials = | 412 xhr.withCredentials = |
| 409 (origin == print_preview.DestinationOrigin.COOKIES); | 413 (origin == print_preview.DestinationOrigin.COOKIES); |
| 410 for (var header in headers) { | 414 for (var header in headers) { |
| 411 xhr.setRequestHeader(header, headers[header]); | 415 xhr.setRequestHeader(header, headers[header]); |
| 412 } | 416 } |
| 413 | 417 |
| 414 return new CloudPrintRequest(xhr, body, origin, account, callback); | 418 return new cloudprint.CloudPrintRequest(xhr, body, origin, account, |
| 419 callback); |
| 415 }, | 420 }, |
| 416 | 421 |
| 417 /** | 422 /** |
| 418 * Sends a request to the Google Cloud Print API or queues if it needs to | 423 * Sends a request to the Google Cloud Print API or queues if it needs to |
| 419 * wait OAuth2 access token. | 424 * wait OAuth2 access token. |
| 420 * @param {!CloudPrintRequest} request Request to send or queue. | 425 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. |
| 421 * @private | 426 * @private |
| 422 */ | 427 */ |
| 423 sendOrQueueRequest_: function(request) { | 428 sendOrQueueRequest_: function(request) { |
| 424 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 429 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
| 425 return this.sendRequest_(request); | 430 return this.sendRequest_(request); |
| 426 } else { | 431 } else { |
| 427 this.requestQueue_.push(request); | 432 this.requestQueue_.push(request); |
| 428 this.nativeLayer_.startGetAccessToken(request.origin); | 433 this.nativeLayer_.startGetAccessToken(request.origin); |
| 429 } | 434 } |
| 430 }, | 435 }, |
| 431 | 436 |
| 432 /** | 437 /** |
| 433 * Sends a request to the Google Cloud Print API. | 438 * Sends a request to the Google Cloud Print API. |
| 434 * @param {!CloudPrintRequest} request Request to send. | 439 * @param {!cloudprint.CloudPrintRequest} request Request to send. |
| 435 * @private | 440 * @private |
| 436 */ | 441 */ |
| 437 sendRequest_: function(request) { | 442 sendRequest_: function(request) { |
| 438 request.xhr.onreadystatechange = | 443 request.xhr.onreadystatechange = |
| 439 this.onReadyStateChange_.bind(this, request); | 444 this.onReadyStateChange_.bind(this, request); |
| 440 request.xhr.send(request.body); | 445 request.xhr.send(request.body); |
| 441 }, | 446 }, |
| 442 | 447 |
| 443 /** | 448 /** |
| 444 * Creates a Google Cloud Print interface error that is ready to dispatch. | 449 * Creates a Google Cloud Print interface error that is ready to dispatch. |
| 445 * @param {!CloudPrintInterface.EventType} type Type of the error. | 450 * @param {!cloudprint.CloudPrintInterfaceEventType} type Type of the |
| 446 * @param {!CloudPrintRequest} request Request that has been completed. | 451 * error. |
| 452 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 453 * completed. |
| 447 * @return {!Event} Google Cloud Print interface error event. | 454 * @return {!Event} Google Cloud Print interface error event. |
| 448 * @private | 455 * @private |
| 449 */ | 456 */ |
| 450 createErrorEvent_: function(type, request) { | 457 createErrorEvent_: function(type, request) { |
| 451 var errorEvent = new Event(type); | 458 var errorEvent = new Event(type); |
| 452 errorEvent.status = request.xhr.status; | 459 errorEvent.status = request.xhr.status; |
| 453 if (request.xhr.status == 200) { | 460 if (request.xhr.status == 200) { |
| 454 errorEvent.errorCode = request.result['errorCode']; | 461 errorEvent.errorCode = request.result['errorCode']; |
| 455 errorEvent.message = request.result['message']; | 462 errorEvent.message = request.result['message']; |
| 456 } else { | 463 } else { |
| 457 errorEvent.errorCode = 0; | 464 errorEvent.errorCode = 0; |
| 458 errorEvent.message = ''; | 465 errorEvent.message = ''; |
| 459 } | 466 } |
| 460 errorEvent.origin = request.origin; | 467 errorEvent.origin = request.origin; |
| 461 return errorEvent; | 468 return errorEvent; |
| 462 }, | 469 }, |
| 463 | 470 |
| 464 /** | 471 /** |
| 465 * Updates user info and session index from the {@code request} response. | 472 * Updates user info and session index from the {@code request} response. |
| 466 * @param {!CloudPrintRequest} request Request to extract user info from. | 473 * @param {!cloudprint.CloudPrintRequest} request Request to extract user |
| 474 * info from. |
| 467 * @private | 475 * @private |
| 468 */ | 476 */ |
| 469 setUsers_: function(request) { | 477 setUsers_: function(request) { |
| 470 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 478 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
| 471 var users = request.result['request']['users'] || []; | 479 var users = request.result['request']['users'] || []; |
| 472 this.userSessionIndex_ = {}; | 480 this.userSessionIndex_ = {}; |
| 473 for (var i = 0; i < users.length; i++) { | 481 for (var i = 0; i < users.length; i++) { |
| 474 this.userSessionIndex_[users[i]] = i; | 482 this.userSessionIndex_[users[i]] = i; |
| 475 } | 483 } |
| 476 this.userInfo_.setUsers(request.result['request']['user'], users); | 484 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(); | 524 request.xhr.abort(); |
| 517 request.callback(request); | 525 request.callback(request); |
| 518 } | 526 } |
| 519 return false; | 527 return false; |
| 520 }, this); | 528 }, this); |
| 521 }, | 529 }, |
| 522 | 530 |
| 523 /** | 531 /** |
| 524 * Called when the ready-state of a XML http request changes. | 532 * Called when the ready-state of a XML http request changes. |
| 525 * Calls the successCallback with the result or dispatches an ERROR event. | 533 * Calls the successCallback with the result or dispatches an ERROR event. |
| 526 * @param {!CloudPrintRequest} request Request that was changed. | 534 * @param {!cloudprint.CloudPrintRequest} request Request that was changed. |
| 527 * @private | 535 * @private |
| 528 */ | 536 */ |
| 529 onReadyStateChange_: function(request) { | 537 onReadyStateChange_: function(request) { |
| 530 if (request.xhr.readyState == 4) { | 538 if (request.xhr.readyState == 4) { |
| 531 if (request.xhr.status == 200) { | 539 if (request.xhr.status == 200) { |
| 532 request.result = JSON.parse(request.xhr.responseText); | 540 request.result = /** @type {Object} */ ( |
| 541 JSON.parse(request.xhr.responseText)); |
| 533 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 542 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
| 534 request.result['success']) { | 543 request.result['success']) { |
| 535 this.xsrfTokens_[request.result['request']['user']] = | 544 this.xsrfTokens_[request.result['request']['user']] = |
| 536 request.result['xsrf_token']; | 545 request.result['xsrf_token']; |
| 537 } | 546 } |
| 538 } | 547 } |
| 539 request.status = request.xhr.status; | 548 request.status = request.xhr.status; |
| 540 request.callback(request); | 549 request.callback(request); |
| 541 } | 550 } |
| 542 }, | 551 }, |
| 543 | 552 |
| 544 /** | 553 /** |
| 545 * Called when the search request completes. | 554 * Called when the search request completes. |
| 546 * @param {boolean} isRecent Whether the search request was for recent | 555 * @param {boolean} isRecent Whether the search request was for recent |
| 547 * destinations. | 556 * destinations. |
| 548 * @param {!CloudPrintRequest} request Request that has been completed. | 557 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 558 * completed. |
| 549 * @private | 559 * @private |
| 550 */ | 560 */ |
| 551 onSearchDone_: function(isRecent, request) { | 561 onSearchDone_: function(isRecent, request) { |
| 552 var lastRequestForThisOrigin = true; | 562 var lastRequestForThisOrigin = true; |
| 553 this.outstandingCloudSearchRequests_ = | 563 this.outstandingCloudSearchRequests_ = |
| 554 this.outstandingCloudSearchRequests_.filter(function(item) { | 564 this.outstandingCloudSearchRequests_.filter(function(item) { |
| 555 if (item != request && item.origin == request.origin) { | 565 if (item != request && item.origin == request.origin) { |
| 556 lastRequestForThisOrigin = false; | 566 lastRequestForThisOrigin = false; |
| 557 } | 567 } |
| 558 return item != request; | 568 return item != request; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 573 try { | 583 try { |
| 574 printerList.push(cloudprint.CloudDestinationParser.parse( | 584 printerList.push(cloudprint.CloudDestinationParser.parse( |
| 575 printerJson, request.origin, activeUser)); | 585 printerJson, request.origin, activeUser)); |
| 576 } catch (err) { | 586 } catch (err) { |
| 577 console.error('Unable to parse cloud print destination: ' + err); | 587 console.error('Unable to parse cloud print destination: ' + err); |
| 578 } | 588 } |
| 579 }); | 589 }); |
| 580 // Extract and store users. | 590 // Extract and store users. |
| 581 this.setUsers_(request); | 591 this.setUsers_(request); |
| 582 // Dispatch SEARCH_DONE event. | 592 // Dispatch SEARCH_DONE event. |
| 583 event = new Event(CloudPrintInterface.EventType.SEARCH_DONE); | 593 event = new Event(CloudPrintInterfaceEventType.SEARCH_DONE); |
| 584 event.origin = request.origin; | 594 event.origin = request.origin; |
| 585 event.printers = printerList; | 595 event.printers = printerList; |
| 586 event.isRecent = isRecent; | 596 event.isRecent = isRecent; |
| 587 } else { | 597 } else { |
| 588 event = this.createErrorEvent_( | 598 event = this.createErrorEvent_( |
| 589 CloudPrintInterface.EventType.SEARCH_FAILED, | 599 CloudPrintInterfaceEventType.SEARCH_FAILED, |
| 590 request); | 600 request); |
| 591 } | 601 } |
| 592 event.user = activeUser; | 602 event.user = activeUser; |
| 593 event.searchDone = lastRequestForThisOrigin; | 603 event.searchDone = lastRequestForThisOrigin; |
| 594 this.dispatchEvent(event); | 604 this.dispatchEvent(event); |
| 595 }, | 605 }, |
| 596 | 606 |
| 597 /** | 607 /** |
| 598 * Called when invitations search request completes. | 608 * Called when invitations search request completes. |
| 599 * @param {!CloudPrintRequest} request Request that has been completed. | 609 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 610 * completed. |
| 600 * @private | 611 * @private |
| 601 */ | 612 */ |
| 602 onInvitesDone_: function(request) { | 613 onInvitesDone_: function(request) { |
| 603 var event = null; | 614 var event = null; |
| 604 var activeUser = | 615 var activeUser = |
| 605 (request.result && | 616 (request.result && |
| 606 request.result['request'] && | 617 request.result['request'] && |
| 607 request.result['request']['user']) || ''; | 618 request.result['request']['user']) || ''; |
| 608 if (request.xhr.status == 200 && request.result['success']) { | 619 if (request.xhr.status == 200 && request.result['success']) { |
| 609 // Extract invitations. | 620 // Extract invitations. |
| 610 var invitationListJson = request.result['invites'] || []; | 621 var invitationListJson = request.result['invites'] || []; |
| 611 var invitationList = []; | 622 var invitationList = []; |
| 612 invitationListJson.forEach(function(invitationJson) { | 623 invitationListJson.forEach(function(invitationJson) { |
| 613 try { | 624 try { |
| 614 invitationList.push(cloudprint.InvitationParser.parse( | 625 invitationList.push(cloudprint.InvitationParser.parse( |
| 615 invitationJson, activeUser)); | 626 invitationJson, activeUser)); |
| 616 } catch (e) { | 627 } catch (e) { |
| 617 console.error('Unable to parse invitation: ' + e); | 628 console.error('Unable to parse invitation: ' + e); |
| 618 } | 629 } |
| 619 }); | 630 }); |
| 620 // Dispatch INVITES_DONE event. | 631 // Dispatch INVITES_DONE event. |
| 621 event = new Event(CloudPrintInterface.EventType.INVITES_DONE); | 632 event = new Event(CloudPrintInterfaceEventType.INVITES_DONE); |
| 622 event.invitations = invitationList; | 633 event.invitations = invitationList; |
| 623 } else { | 634 } else { |
| 624 event = this.createErrorEvent_( | 635 event = this.createErrorEvent_( |
| 625 CloudPrintInterface.EventType.INVITES_FAILED, request); | 636 CloudPrintInterfaceEventType.INVITES_FAILED, request); |
| 626 } | 637 } |
| 627 event.user = activeUser; | 638 event.user = activeUser; |
| 628 this.dispatchEvent(event); | 639 this.dispatchEvent(event); |
| 629 }, | 640 }, |
| 630 | 641 |
| 631 /** | 642 /** |
| 632 * Called when invitation processing request completes. | 643 * Called when invitation processing request completes. |
| 633 * @param {!print_preview.Invitation} invitation Processed invitation. | 644 * @param {!print_preview.Invitation} invitation Processed invitation. |
| 634 * @param {boolean} accept Whether this invitation was accepted or rejected. | 645 * @param {boolean} accept Whether this invitation was accepted or rejected. |
| 635 * @param {!CloudPrintRequest} request Request that has been completed. | 646 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 647 * completed. |
| 636 * @private | 648 * @private |
| 637 */ | 649 */ |
| 638 onProcessInviteDone_: function(invitation, accept, request) { | 650 onProcessInviteDone_: function(invitation, accept, request) { |
| 639 var event = null; | 651 var event = null; |
| 640 var activeUser = | 652 var activeUser = |
| 641 (request.result && | 653 (request.result && |
| 642 request.result['request'] && | 654 request.result['request'] && |
| 643 request.result['request']['user']) || ''; | 655 request.result['request']['user']) || ''; |
| 644 if (request.xhr.status == 200 && request.result['success']) { | 656 if (request.xhr.status == 200 && request.result['success']) { |
| 645 event = new Event(CloudPrintInterface.EventType.PROCESS_INVITE_DONE); | 657 event = new Event(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE); |
| 646 if (accept) { | 658 if (accept) { |
| 647 try { | 659 try { |
| 648 event.printer = cloudprint.CloudDestinationParser.parse( | 660 event.printer = cloudprint.CloudDestinationParser.parse( |
| 649 request.result['printer'], request.origin, activeUser); | 661 request.result['printer'], request.origin, activeUser); |
| 650 } catch (e) { | 662 } catch (e) { |
| 651 console.error('Failed to parse cloud print destination: ' + e); | 663 console.error('Failed to parse cloud print destination: ' + e); |
| 652 } | 664 } |
| 653 } | 665 } |
| 654 } else { | 666 } else { |
| 655 event = this.createErrorEvent_( | 667 event = this.createErrorEvent_( |
| 656 CloudPrintInterface.EventType.PROCESS_INVITE_FAILED, request); | 668 CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED, request); |
| 657 } | 669 } |
| 658 event.invitation = invitation; | 670 event.invitation = invitation; |
| 659 event.accept = accept; | 671 event.accept = accept; |
| 660 event.user = activeUser; | 672 event.user = activeUser; |
| 661 this.dispatchEvent(event); | 673 this.dispatchEvent(event); |
| 662 }, | 674 }, |
| 663 | 675 |
| 664 /** | 676 /** |
| 665 * Called when the submit request completes. | 677 * Called when the submit request completes. |
| 666 * @param {!CloudPrintRequest} request Request that has been completed. | 678 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 679 * completed. |
| 667 * @private | 680 * @private |
| 668 */ | 681 */ |
| 669 onSubmitDone_: function(request) { | 682 onSubmitDone_: function(request) { |
| 670 if (request.xhr.status == 200 && request.result['success']) { | 683 if (request.xhr.status == 200 && request.result['success']) { |
| 671 var submitDoneEvent = new Event( | 684 var submitDoneEvent = new Event( |
| 672 CloudPrintInterface.EventType.SUBMIT_DONE); | 685 CloudPrintInterfaceEventType.SUBMIT_DONE); |
| 673 submitDoneEvent.jobId = request.result['job']['id']; | 686 submitDoneEvent.jobId = request.result['job']['id']; |
| 674 this.dispatchEvent(submitDoneEvent); | 687 this.dispatchEvent(submitDoneEvent); |
| 675 } else { | 688 } else { |
| 676 var errorEvent = this.createErrorEvent_( | 689 var errorEvent = this.createErrorEvent_( |
| 677 CloudPrintInterface.EventType.SUBMIT_FAILED, request); | 690 CloudPrintInterfaceEventType.SUBMIT_FAILED, request); |
| 678 this.dispatchEvent(errorEvent); | 691 this.dispatchEvent(errorEvent); |
| 679 } | 692 } |
| 680 }, | 693 }, |
| 681 | 694 |
| 682 /** | 695 /** |
| 683 * Called when the printer request completes. | 696 * Called when the printer request completes. |
| 684 * @param {string} destinationId ID of the destination that was looked up. | 697 * @param {string} destinationId ID of the destination that was looked up. |
| 685 * @param {!CloudPrintRequest} request Request that has been completed. | 698 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
| 699 * completed. |
| 686 * @private | 700 * @private |
| 687 */ | 701 */ |
| 688 onPrinterDone_: function(destinationId, request) { | 702 onPrinterDone_: function(destinationId, request) { |
| 689 // Special handling of the first printer request. It does not matter at | 703 // Special handling of the first printer request. It does not matter at |
| 690 // this point, whether printer was found or not. | 704 // this point, whether printer was found or not. |
| 691 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 705 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
| 692 request.result && | 706 request.result && |
| 693 request.account && | 707 request.account && |
| 694 request.result['request']['user'] && | 708 request.result['request']['user'] && |
| 695 request.result['request']['users'] && | 709 request.result['request']['users'] && |
| (...skipping 22 matching lines...) Expand all Loading... |
| 718 var printer; | 732 var printer; |
| 719 try { | 733 try { |
| 720 printer = cloudprint.CloudDestinationParser.parse( | 734 printer = cloudprint.CloudDestinationParser.parse( |
| 721 printerJson, request.origin, activeUser); | 735 printerJson, request.origin, activeUser); |
| 722 } catch (err) { | 736 } catch (err) { |
| 723 console.error('Failed to parse cloud print destination: ' + | 737 console.error('Failed to parse cloud print destination: ' + |
| 724 JSON.stringify(printerJson)); | 738 JSON.stringify(printerJson)); |
| 725 return; | 739 return; |
| 726 } | 740 } |
| 727 var printerDoneEvent = | 741 var printerDoneEvent = |
| 728 new Event(CloudPrintInterface.EventType.PRINTER_DONE); | 742 new Event(CloudPrintInterfaceEventType.PRINTER_DONE); |
| 729 printerDoneEvent.printer = printer; | 743 printerDoneEvent.printer = printer; |
| 730 this.dispatchEvent(printerDoneEvent); | 744 this.dispatchEvent(printerDoneEvent); |
| 731 } else { | 745 } else { |
| 732 var errorEvent = this.createErrorEvent_( | 746 var errorEvent = this.createErrorEvent_( |
| 733 CloudPrintInterface.EventType.PRINTER_FAILED, request); | 747 CloudPrintInterfaceEventType.PRINTER_FAILED, request); |
| 734 errorEvent.destinationId = destinationId; | 748 errorEvent.destinationId = destinationId; |
| 735 errorEvent.destinationOrigin = request.origin; | 749 errorEvent.destinationOrigin = request.origin; |
| 736 this.dispatchEvent(errorEvent); | 750 this.dispatchEvent(errorEvent); |
| 737 } | 751 } |
| 738 }, | 752 }, |
| 739 }; | 753 }; |
| 740 | 754 |
| 741 /** | 755 /** |
| 742 * Data structure that holds data for Cloud Print requests. | 756 * Data structure that holds data for Cloud Print requests. |
| 743 * @param {!XMLHttpRequest} xhr Partially prepared http request. | 757 * @param {!XMLHttpRequest} xhr Partially prepared http request. |
| 744 * @param {string} body Data to send with POST requests. | 758 * @param {string} body Data to send with POST requests. |
| 745 * @param {!print_preview.DestinationOrigin} origin Origin for destination. | 759 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
| 746 * @param {?string} account Account the request is sent for. Can be | 760 * @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 | 761 * {@code null} or empty string if the request is not cookie bound or |
| 748 * is sent on behalf of the primary user. | 762 * is sent on behalf of the primary user. |
| 749 * @param {function(!CloudPrintRequest)} callback Callback to invoke when | 763 * @param {function(!cloudprint.CloudPrintRequest)} callback Callback to |
| 750 * request completes. | 764 * invoke when request completes. |
| 751 * @constructor | 765 * @constructor |
| 752 */ | 766 */ |
| 753 function CloudPrintRequest(xhr, body, origin, account, callback) { | 767 function CloudPrintRequest(xhr, body, origin, account, callback) { |
| 754 /** | 768 /** |
| 755 * Partially prepared http request. | 769 * Partially prepared http request. |
| 756 * @type {!XMLHttpRequest} | 770 * @type {!XMLHttpRequest} |
| 757 */ | 771 */ |
| 758 this.xhr = xhr; | 772 this.xhr = xhr; |
| 759 | 773 |
| 760 /** | 774 /** |
| 761 * Data to send with POST requests. | 775 * Data to send with POST requests. |
| 762 * @type {string} | 776 * @type {string} |
| 763 */ | 777 */ |
| 764 this.body = body; | 778 this.body = body; |
| 765 | 779 |
| 766 /** | 780 /** |
| 767 * Origin for destination. | 781 * Origin for destination. |
| 768 * @type {!print_preview.DestinationOrigin} | 782 * @type {!print_preview.DestinationOrigin} |
| 769 */ | 783 */ |
| 770 this.origin = origin; | 784 this.origin = origin; |
| 771 | 785 |
| 772 /** | 786 /** |
| 773 * User account this request is expected to be executed for. | 787 * User account this request is expected to be executed for. |
| 774 * @type {?string} | 788 * @type {?string} |
| 775 */ | 789 */ |
| 776 this.account = account; | 790 this.account = account; |
| 777 | 791 |
| 778 /** | 792 /** |
| 779 * Callback to invoke when request completes. | 793 * Callback to invoke when request completes. |
| 780 * @type {function(!CloudPrintRequest)} | 794 * @type {function(!cloudprint.CloudPrintRequest)} |
| 781 */ | 795 */ |
| 782 this.callback = callback; | 796 this.callback = callback; |
| 783 | 797 |
| 784 /** | 798 /** |
| 785 * Result for requests. | 799 * Result for requests. |
| 786 * @type {Object} JSON response. | 800 * @type {Object} JSON response. |
| 787 */ | 801 */ |
| 788 this.result = null; | 802 this.result = null; |
| 789 }; | 803 } |
| 790 | 804 |
| 791 /** | 805 /** |
| 792 * Data structure that represents an HTTP parameter. | 806 * Data structure that represents an HTTP parameter. |
| 793 * @param {string} name Name of the parameter. | 807 * @param {string} name Name of the parameter. |
| 794 * @param {string} value Value of the parameter. | 808 * @param {string} value Value of the parameter. |
| 795 * @constructor | 809 * @constructor |
| 796 */ | 810 */ |
| 797 function HttpParam(name, value) { | 811 function HttpParam(name, value) { |
| 798 /** | 812 /** |
| 799 * Name of the parameter. | 813 * Name of the parameter. |
| 800 * @type {string} | 814 * @type {string} |
| 801 */ | 815 */ |
| 802 this.name = name; | 816 this.name = name; |
| 803 | 817 |
| 804 /** | 818 /** |
| 805 * Name of the value. | 819 * Name of the value. |
| 806 * @type {string} | 820 * @type {string} |
| 807 */ | 821 */ |
| 808 this.value = value; | 822 this.value = value; |
| 809 }; | 823 } |
| 810 | 824 |
| 811 // Export | 825 // Export |
| 812 return { | 826 return { |
| 813 CloudPrintInterface: CloudPrintInterface | 827 CloudPrintInterface: CloudPrintInterface, |
| 828 CloudPrintRequest: CloudPrintRequest |
| 814 }; | 829 }; |
| 815 }); | 830 }); |
| OLD | NEW |