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.define('cloudprint', function() { | 5 cr.define('cloudprint', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * API to the Google Cloud Print service. | 9 * API to the Google Cloud Print service. |
10 * @param {string} baseUrl Base part of the Google Cloud Print service URL | 10 * @param {string} baseUrl Base part of the Google Cloud Print service URL |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 * Enumeration of JSON response fields from Google Cloud Print API. | 148 * Enumeration of JSON response fields from Google Cloud Print API. |
149 * @enum {string} | 149 * @enum {string} |
150 * @private | 150 * @private |
151 */ | 151 */ |
152 CloudPrintInterface.JsonFields_ = { | 152 CloudPrintInterface.JsonFields_ = { |
153 PRINTER: 'printer' | 153 PRINTER: 'printer' |
154 }; | 154 }; |
155 | 155 |
156 /** | 156 /** |
157 * Could Print origins used to search printers. | 157 * Could Print origins used to search printers. |
158 * @type {!Array<!print_preview.Destination.Origin>} | 158 * @type {!Array<!print_preview.DestinationOrigin>} |
159 * @const | 159 * @const |
160 * @private | 160 * @private |
161 */ | 161 */ |
162 CloudPrintInterface.CLOUD_ORIGINS_ = [ | 162 CloudPrintInterface.CLOUD_ORIGINS_ = [ |
163 print_preview.Destination.Origin.COOKIES, | 163 print_preview.DestinationOrigin.COOKIES, |
164 print_preview.Destination.Origin.DEVICE | 164 print_preview.DestinationOrigin.DEVICE |
165 // TODO(vitalybuka): Enable when implemented. | 165 // TODO(vitalybuka): Enable when implemented. |
166 // ready print_preview.Destination.Origin.PROFILE | 166 // ready print_preview.DestinationOrigin.PROFILE |
167 ]; | 167 ]; |
168 | 168 |
169 CloudPrintInterface.prototype = { | 169 CloudPrintInterface.prototype = { |
170 __proto__: cr.EventTarget.prototype, | 170 __proto__: cr.EventTarget.prototype, |
171 | 171 |
172 /** @return {string} Base URL of the Google Cloud Print service. */ | 172 /** @return {string} Base URL of the Google Cloud Print service. */ |
173 get baseUrl() { | 173 get baseUrl() { |
174 return this.baseUrl_; | 174 return this.baseUrl_; |
175 }, | 175 }, |
176 | 176 |
177 /** | 177 /** |
178 * @return {boolean} Whether a search for cloud destinations is in progress. | 178 * @return {boolean} Whether a search for cloud destinations is in progress. |
179 */ | 179 */ |
180 get isCloudDestinationSearchInProgress() { | 180 get isCloudDestinationSearchInProgress() { |
181 return this.outstandingCloudSearchRequests_.length > 0; | 181 return this.outstandingCloudSearchRequests_.length > 0; |
182 }, | 182 }, |
183 | 183 |
184 /** | 184 /** |
185 * Sends Google Cloud Print search API request. | 185 * Sends Google Cloud Print search API request. |
186 * @param {string=} opt_account Account the search is sent for. When | 186 * @param {string=} opt_account Account the search is sent for. When |
187 * omitted, the search is done on behalf of the primary user. | 187 * omitted, the search is done on behalf of the primary user. |
188 * @param {print_preview.Destination.Origin=} opt_origin When specified, | 188 * @param {print_preview.DestinationOrigin=} opt_origin When specified, |
189 * searches destinations for {@code opt_origin} only, otherwise starts | 189 * searches destinations for {@code opt_origin} only, otherwise starts |
190 * searches for all origins. | 190 * searches for all origins. |
191 */ | 191 */ |
192 search: function(opt_account, opt_origin) { | 192 search: function(opt_account, opt_origin) { |
193 var account = opt_account || ''; | 193 var account = opt_account || ''; |
194 var origins = | 194 var origins = |
195 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_; | 195 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_; |
196 if (this.isInAppKioskMode_) { | 196 if (this.isInAppKioskMode_) { |
197 origins = origins.filter(function(origin) { | 197 origins = origins.filter(function(origin) { |
198 return origin != print_preview.Destination.Origin.COOKIES; | 198 return origin != print_preview.DestinationOrigin.COOKIES; |
199 }); | 199 }); |
200 } | 200 } |
201 this.abortSearchRequests_(origins); | 201 this.abortSearchRequests_(origins); |
202 this.search_(true, account, origins); | 202 this.search_(true, account, origins); |
203 this.search_(false, account, origins); | 203 this.search_(false, account, origins); |
204 }, | 204 }, |
205 | 205 |
206 /** | 206 /** |
207 * Sends Google Cloud Print search API requests. | 207 * Sends Google Cloud Print search API requests. |
208 * @param {boolean} isRecent Whether to search for only recently used | 208 * @param {boolean} isRecent Whether to search for only recently used |
209 * printers. | 209 * printers. |
210 * @param {string} account Account the search is sent for. It matters for | 210 * @param {string} account Account the search is sent for. It matters for |
211 * COOKIES origin only, and can be empty (sent on behalf of the primary | 211 * COOKIES origin only, and can be empty (sent on behalf of the primary |
212 * user in this case). | 212 * user in this case). |
213 * @param {!Array<!print_preview.Destination.Origin>} origins Origins to | 213 * @param {!Array<!print_preview.DestinationOrigin>} origins Origins to |
214 * search printers for. | 214 * search printers for. |
215 * @private | 215 * @private |
216 */ | 216 */ |
217 search_: function(isRecent, account, origins) { | 217 search_: function(isRecent, account, origins) { |
218 var params = [ | 218 var params = [ |
219 new HttpParam('connection_status', 'ALL'), | 219 new HttpParam('connection_status', 'ALL'), |
220 new HttpParam('client', 'chrome'), | 220 new HttpParam('client', 'chrome'), |
221 new HttpParam('use_cdd', 'true') | 221 new HttpParam('use_cdd', 'true') |
222 ]; | 222 ]; |
223 if (isRecent) { | 223 if (isRecent) { |
(...skipping 17 matching lines...) Expand all Loading... |
241 * @param {string} account Account the request is sent for. | 241 * @param {string} account Account the request is sent for. |
242 */ | 242 */ |
243 invites: function(account) { | 243 invites: function(account) { |
244 var params = [ | 244 var params = [ |
245 new HttpParam('client', 'chrome'), | 245 new HttpParam('client', 'chrome'), |
246 ]; | 246 ]; |
247 this.sendOrQueueRequest_(this.buildRequest_( | 247 this.sendOrQueueRequest_(this.buildRequest_( |
248 'GET', | 248 'GET', |
249 'invites', | 249 'invites', |
250 params, | 250 params, |
251 print_preview.Destination.Origin.COOKIES, | 251 print_preview.DestinationOrigin.COOKIES, |
252 account, | 252 account, |
253 this.onInvitesDone_.bind(this))); | 253 this.onInvitesDone_.bind(this))); |
254 }, | 254 }, |
255 | 255 |
256 /** | 256 /** |
257 * Accepts or rejects printer sharing invitation. | 257 * Accepts or rejects printer sharing invitation. |
258 * @param {!print_preview.Invitation} invitation Invitation to process. | 258 * @param {!print_preview.Invitation} invitation Invitation to process. |
259 * @param {boolean} accept Whether to accept this invitation. | 259 * @param {boolean} accept Whether to accept this invitation. |
260 */ | 260 */ |
261 processInvite: function(invitation, accept) { | 261 processInvite: function(invitation, accept) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 params, | 307 params, |
308 destination.origin, | 308 destination.origin, |
309 destination.account, | 309 destination.account, |
310 this.onSubmitDone_.bind(this)); | 310 this.onSubmitDone_.bind(this)); |
311 this.sendOrQueueRequest_(cpRequest); | 311 this.sendOrQueueRequest_(cpRequest); |
312 }, | 312 }, |
313 | 313 |
314 /** | 314 /** |
315 * Sends a Google Cloud Print printer API request. | 315 * Sends a Google Cloud Print printer API request. |
316 * @param {string} printerId ID of the printer to lookup. | 316 * @param {string} printerId ID of the printer to lookup. |
317 * @param {!print_preview.Destination.Origin} origin Origin of the printer. | 317 * @param {!print_preview.DestinationOrigin} origin Origin of the printer. |
318 * @param {string=} account Account this printer is registered for. When | 318 * @param {string=} account Account this printer is registered for. When |
319 * provided for COOKIES {@code origin}, and users sessions are still not | 319 * provided for COOKIES {@code origin}, and users sessions are still not |
320 * known, will be checked against the response (both success and failure | 320 * known, will be checked against the response (both success and failure |
321 * to get printer) and, if the active user account is not the one | 321 * to get printer) and, if the active user account is not the one |
322 * requested, {@code account} is activated and printer request reissued. | 322 * requested, {@code account} is activated and printer request reissued. |
323 */ | 323 */ |
324 printer: function(printerId, origin, account) { | 324 printer: function(printerId, origin, account) { |
325 var params = [ | 325 var params = [ |
326 new HttpParam('printerid', printerId), | 326 new HttpParam('printerid', printerId), |
327 new HttpParam('use_cdd', 'true'), | 327 new HttpParam('use_cdd', 'true'), |
(...skipping 18 matching lines...) Expand all Loading... |
346 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | 346 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, |
347 this.onAccessTokenReady_.bind(this)); | 347 this.onAccessTokenReady_.bind(this)); |
348 }, | 348 }, |
349 | 349 |
350 /** | 350 /** |
351 * Builds request to the Google Cloud Print API. | 351 * Builds request to the Google Cloud Print API. |
352 * @param {string} method HTTP method of the request. | 352 * @param {string} method HTTP method of the request. |
353 * @param {string} action Google Cloud Print action to perform. | 353 * @param {string} action Google Cloud Print action to perform. |
354 * @param {Array<!HttpParam>} params HTTP parameters to include in the | 354 * @param {Array<!HttpParam>} params HTTP parameters to include in the |
355 * request. | 355 * request. |
356 * @param {!print_preview.Destination.Origin} origin Origin for destination. | 356 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
357 * @param {?string} account Account the request is sent for. Can be | 357 * @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 | 358 * {@code null} or empty string if the request is not cookie bound or |
359 * is sent on behalf of the primary user. | 359 * is sent on behalf of the primary user. |
360 * @param {function(number, Object, !print_preview.Destination.Origin)} | 360 * @param {function(number, Object, !print_preview.DestinationOrigin)} |
361 * callback Callback to invoke when request completes. | 361 * callback Callback to invoke when request completes. |
362 * @return {!CloudPrintRequest} Partially prepared request. | 362 * @return {!CloudPrintRequest} Partially prepared request. |
363 * @private | 363 * @private |
364 */ | 364 */ |
365 buildRequest_: function(method, action, params, origin, account, callback) { | 365 buildRequest_: function(method, action, params, origin, account, callback) { |
366 var url = this.baseUrl_ + '/' + action + '?xsrf='; | 366 var url = this.baseUrl_ + '/' + action + '?xsrf='; |
367 if (origin == print_preview.Destination.Origin.COOKIES) { | 367 if (origin == print_preview.DestinationOrigin.COOKIES) { |
368 var xsrfToken = this.xsrfTokens_[account]; | 368 var xsrfToken = this.xsrfTokens_[account]; |
369 if (!xsrfToken) { | 369 if (!xsrfToken) { |
370 // TODO(rltoscano): Should throw an error if not a read-only action or | 370 // TODO(rltoscano): Should throw an error if not a read-only action or |
371 // issue an xsrf token request. | 371 // issue an xsrf token request. |
372 } else { | 372 } else { |
373 url = url + xsrfToken; | 373 url = url + xsrfToken; |
374 } | 374 } |
375 if (account) { | 375 if (account) { |
376 var index = this.userSessionIndex_[account] || 0; | 376 var index = this.userSessionIndex_[account] || 0; |
377 if (index > 0) { | 377 if (index > 0) { |
(...skipping 21 matching lines...) Expand all Loading... |
399 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; | 399 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
400 if (method == 'GET') { | 400 if (method == 'GET') { |
401 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; | 401 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
402 } else if (method == 'POST') { | 402 } else if (method == 'POST') { |
403 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; | 403 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; |
404 } | 404 } |
405 | 405 |
406 var xhr = new XMLHttpRequest(); | 406 var xhr = new XMLHttpRequest(); |
407 xhr.open(method, url, true); | 407 xhr.open(method, url, true); |
408 xhr.withCredentials = | 408 xhr.withCredentials = |
409 (origin == print_preview.Destination.Origin.COOKIES); | 409 (origin == print_preview.DestinationOrigin.COOKIES); |
410 for (var header in headers) { | 410 for (var header in headers) { |
411 xhr.setRequestHeader(header, headers[header]); | 411 xhr.setRequestHeader(header, headers[header]); |
412 } | 412 } |
413 | 413 |
414 return new CloudPrintRequest(xhr, body, origin, account, callback); | 414 return new CloudPrintRequest(xhr, body, origin, account, callback); |
415 }, | 415 }, |
416 | 416 |
417 /** | 417 /** |
418 * Sends a request to the Google Cloud Print API or queues if it needs to | 418 * Sends a request to the Google Cloud Print API or queues if it needs to |
419 * wait OAuth2 access token. | 419 * wait OAuth2 access token. |
420 * @param {!CloudPrintRequest} request Request to send or queue. | 420 * @param {!CloudPrintRequest} request Request to send or queue. |
421 * @private | 421 * @private |
422 */ | 422 */ |
423 sendOrQueueRequest_: function(request) { | 423 sendOrQueueRequest_: function(request) { |
424 if (request.origin == print_preview.Destination.Origin.COOKIES) { | 424 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
425 return this.sendRequest_(request); | 425 return this.sendRequest_(request); |
426 } else { | 426 } else { |
427 this.requestQueue_.push(request); | 427 this.requestQueue_.push(request); |
428 this.nativeLayer_.startGetAccessToken(request.origin); | 428 this.nativeLayer_.startGetAccessToken(request.origin); |
429 } | 429 } |
430 }, | 430 }, |
431 | 431 |
432 /** | 432 /** |
433 * Sends a request to the Google Cloud Print API. | 433 * Sends a request to the Google Cloud Print API. |
434 * @param {!CloudPrintRequest} request Request to send. | 434 * @param {!CloudPrintRequest} request Request to send. |
(...skipping 25 matching lines...) Expand all Loading... |
460 errorEvent.origin = request.origin; | 460 errorEvent.origin = request.origin; |
461 return errorEvent; | 461 return errorEvent; |
462 }, | 462 }, |
463 | 463 |
464 /** | 464 /** |
465 * Updates user info and session index from the {@code request} response. | 465 * Updates user info and session index from the {@code request} response. |
466 * @param {!CloudPrintRequest} request Request to extract user info from. | 466 * @param {!CloudPrintRequest} request Request to extract user info from. |
467 * @private | 467 * @private |
468 */ | 468 */ |
469 setUsers_: function(request) { | 469 setUsers_: function(request) { |
470 if (request.origin == print_preview.Destination.Origin.COOKIES) { | 470 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
471 var users = request.result['request']['users'] || []; | 471 var users = request.result['request']['users'] || []; |
472 this.userSessionIndex_ = {}; | 472 this.userSessionIndex_ = {}; |
473 for (var i = 0; i < users.length; i++) { | 473 for (var i = 0; i < users.length; i++) { |
474 this.userSessionIndex_[users[i]] = i; | 474 this.userSessionIndex_[users[i]] = i; |
475 } | 475 } |
476 this.userInfo_.setUsers(request.result['request']['user'], users); | 476 this.userInfo_.setUsers(request.result['request']['user'], users); |
477 } | 477 } |
478 }, | 478 }, |
479 | 479 |
480 /** | 480 /** |
481 * Terminates search requests for requested {@code origins}. | 481 * Terminates search requests for requested {@code origins}. |
482 * @param {!Array<print_preview.Destination.Origin>} origins Origins | 482 * @param {!Array<print_preview.DestinationOrigin>} origins Origins |
483 * to terminate search requests for. | 483 * to terminate search requests for. |
484 * @private | 484 * @private |
485 */ | 485 */ |
486 abortSearchRequests_: function(origins) { | 486 abortSearchRequests_: function(origins) { |
487 this.outstandingCloudSearchRequests_ = | 487 this.outstandingCloudSearchRequests_ = |
488 this.outstandingCloudSearchRequests_.filter(function(request) { | 488 this.outstandingCloudSearchRequests_.filter(function(request) { |
489 if (origins.indexOf(request.origin) >= 0) { | 489 if (origins.indexOf(request.origin) >= 0) { |
490 request.xhr.abort(); | 490 request.xhr.abort(); |
491 return false; | 491 return false; |
492 } | 492 } |
493 return true; | 493 return true; |
494 }); | 494 }); |
495 }, | 495 }, |
496 | 496 |
497 /** | 497 /** |
498 * Called when a native layer receives access token. | 498 * Called when a native layer receives access token. |
499 * @param {Event} event Contains the authentication type and access token. | 499 * @param {Event} event Contains the authentication type and access token. |
500 * @private | 500 * @private |
501 */ | 501 */ |
502 onAccessTokenReady_: function(event) { | 502 onAccessTokenReady_: function(event) { |
503 // TODO(vitalybuka): remove when other Origins implemented. | 503 // TODO(vitalybuka): remove when other Origins implemented. |
504 assert(event.authType == print_preview.Destination.Origin.DEVICE); | 504 assert(event.authType == print_preview.DestinationOrigin.DEVICE); |
505 this.requestQueue_ = this.requestQueue_.filter(function(request) { | 505 this.requestQueue_ = this.requestQueue_.filter(function(request) { |
506 assert(request.origin == print_preview.Destination.Origin.DEVICE); | 506 assert(request.origin == print_preview.DestinationOrigin.DEVICE); |
507 if (request.origin != event.authType) { | 507 if (request.origin != event.authType) { |
508 return true; | 508 return true; |
509 } | 509 } |
510 if (event.accessToken) { | 510 if (event.accessToken) { |
511 request.xhr.setRequestHeader('Authorization', | 511 request.xhr.setRequestHeader('Authorization', |
512 'Bearer ' + event.accessToken); | 512 'Bearer ' + event.accessToken); |
513 this.sendRequest_(request); | 513 this.sendRequest_(request); |
514 } else { // No valid token. | 514 } else { // No valid token. |
515 // Without abort status does not exist. | 515 // Without abort status does not exist. |
516 request.xhr.abort(); | 516 request.xhr.abort(); |
517 request.callback(request); | 517 request.callback(request); |
518 } | 518 } |
519 return false; | 519 return false; |
520 }, this); | 520 }, this); |
521 }, | 521 }, |
522 | 522 |
523 /** | 523 /** |
524 * Called when the ready-state of a XML http request changes. | 524 * Called when the ready-state of a XML http request changes. |
525 * Calls the successCallback with the result or dispatches an ERROR event. | 525 * Calls the successCallback with the result or dispatches an ERROR event. |
526 * @param {!CloudPrintRequest} request Request that was changed. | 526 * @param {!CloudPrintRequest} request Request that was changed. |
527 * @private | 527 * @private |
528 */ | 528 */ |
529 onReadyStateChange_: function(request) { | 529 onReadyStateChange_: function(request) { |
530 if (request.xhr.readyState == 4) { | 530 if (request.xhr.readyState == 4) { |
531 if (request.xhr.status == 200) { | 531 if (request.xhr.status == 200) { |
532 request.result = JSON.parse(request.xhr.responseText); | 532 request.result = JSON.parse(request.xhr.responseText); |
533 if (request.origin == print_preview.Destination.Origin.COOKIES && | 533 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
534 request.result['success']) { | 534 request.result['success']) { |
535 this.xsrfTokens_[request.result['request']['user']] = | 535 this.xsrfTokens_[request.result['request']['user']] = |
536 request.result['xsrf_token']; | 536 request.result['xsrf_token']; |
537 } | 537 } |
538 } | 538 } |
539 request.status = request.xhr.status; | 539 request.status = request.xhr.status; |
540 request.callback(request); | 540 request.callback(request); |
541 } | 541 } |
542 }, | 542 }, |
543 | 543 |
544 /** | 544 /** |
545 * Called when the search request completes. | 545 * Called when the search request completes. |
546 * @param {boolean} isRecent Whether the search request was for recent | 546 * @param {boolean} isRecent Whether the search request was for recent |
547 * destinations. | 547 * destinations. |
548 * @param {!CloudPrintRequest} request Request that has been completed. | 548 * @param {!CloudPrintRequest} request Request that has been completed. |
549 * @private | 549 * @private |
550 */ | 550 */ |
551 onSearchDone_: function(isRecent, request) { | 551 onSearchDone_: function(isRecent, request) { |
552 var lastRequestForThisOrigin = true; | 552 var lastRequestForThisOrigin = true; |
553 this.outstandingCloudSearchRequests_ = | 553 this.outstandingCloudSearchRequests_ = |
554 this.outstandingCloudSearchRequests_.filter(function(item) { | 554 this.outstandingCloudSearchRequests_.filter(function(item) { |
555 if (item != request && item.origin == request.origin) { | 555 if (item != request && item.origin == request.origin) { |
556 lastRequestForThisOrigin = false; | 556 lastRequestForThisOrigin = false; |
557 } | 557 } |
558 return item != request; | 558 return item != request; |
559 }); | 559 }); |
560 var activeUser = ''; | 560 var activeUser = ''; |
561 if (request.origin == print_preview.Destination.Origin.COOKIES) { | 561 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
562 activeUser = | 562 activeUser = |
563 request.result && | 563 request.result && |
564 request.result['request'] && | 564 request.result['request'] && |
565 request.result['request']['user']; | 565 request.result['request']['user']; |
566 } | 566 } |
567 var event = null; | 567 var event = null; |
568 if (request.xhr.status == 200 && request.result['success']) { | 568 if (request.xhr.status == 200 && request.result['success']) { |
569 // Extract printers. | 569 // Extract printers. |
570 var printerListJson = request.result['printers'] || []; | 570 var printerListJson = request.result['printers'] || []; |
571 var printerList = []; | 571 var printerList = []; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 | 681 |
682 /** | 682 /** |
683 * Called when the printer request completes. | 683 * Called when the printer request completes. |
684 * @param {string} destinationId ID of the destination that was looked up. | 684 * @param {string} destinationId ID of the destination that was looked up. |
685 * @param {!CloudPrintRequest} request Request that has been completed. | 685 * @param {!CloudPrintRequest} request Request that has been completed. |
686 * @private | 686 * @private |
687 */ | 687 */ |
688 onPrinterDone_: function(destinationId, request) { | 688 onPrinterDone_: function(destinationId, request) { |
689 // Special handling of the first printer request. It does not matter at | 689 // Special handling of the first printer request. It does not matter at |
690 // this point, whether printer was found or not. | 690 // this point, whether printer was found or not. |
691 if (request.origin == print_preview.Destination.Origin.COOKIES && | 691 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
692 request.result && | 692 request.result && |
693 request.account && | 693 request.account && |
694 request.result['request']['user'] && | 694 request.result['request']['user'] && |
695 request.result['request']['users'] && | 695 request.result['request']['users'] && |
696 request.account != request.result['request']['user']) { | 696 request.account != request.result['request']['user']) { |
697 this.setUsers_(request); | 697 this.setUsers_(request); |
698 // In case the user account is known, but not the primary one, | 698 // In case the user account is known, but not the primary one, |
699 // activate it. | 699 // activate it. |
700 if (this.userSessionIndex_[request.account] > 0) { | 700 if (this.userSessionIndex_[request.account] > 0) { |
701 this.userInfo_.activeUser = request.account; | 701 this.userInfo_.activeUser = request.account; |
702 // Repeat the request for the newly activated account. | 702 // Repeat the request for the newly activated account. |
703 this.printer( | 703 this.printer( |
704 request.result['request']['params']['printerid'], | 704 request.result['request']['params']['printerid'], |
705 request.origin, | 705 request.origin, |
706 request.account); | 706 request.account); |
707 // Stop processing this request, wait for the new response. | 707 // Stop processing this request, wait for the new response. |
708 return; | 708 return; |
709 } | 709 } |
710 } | 710 } |
711 // Process response. | 711 // Process response. |
712 if (request.xhr.status == 200 && request.result['success']) { | 712 if (request.xhr.status == 200 && request.result['success']) { |
713 var activeUser = ''; | 713 var activeUser = ''; |
714 if (request.origin == print_preview.Destination.Origin.COOKIES) { | 714 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
715 activeUser = request.result['request']['user']; | 715 activeUser = request.result['request']['user']; |
716 } | 716 } |
717 var printerJson = request.result['printers'][0]; | 717 var printerJson = request.result['printers'][0]; |
718 var printer; | 718 var printer; |
719 try { | 719 try { |
720 printer = cloudprint.CloudDestinationParser.parse( | 720 printer = cloudprint.CloudDestinationParser.parse( |
721 printerJson, request.origin, activeUser); | 721 printerJson, request.origin, activeUser); |
722 } catch (err) { | 722 } catch (err) { |
723 console.error('Failed to parse cloud print destination: ' + | 723 console.error('Failed to parse cloud print destination: ' + |
724 JSON.stringify(printerJson)); | 724 JSON.stringify(printerJson)); |
(...skipping 10 matching lines...) Expand all Loading... |
735 errorEvent.destinationOrigin = request.origin; | 735 errorEvent.destinationOrigin = request.origin; |
736 this.dispatchEvent(errorEvent); | 736 this.dispatchEvent(errorEvent); |
737 } | 737 } |
738 }, | 738 }, |
739 }; | 739 }; |
740 | 740 |
741 /** | 741 /** |
742 * Data structure that holds data for Cloud Print requests. | 742 * Data structure that holds data for Cloud Print requests. |
743 * @param {!XMLHttpRequest} xhr Partially prepared http request. | 743 * @param {!XMLHttpRequest} xhr Partially prepared http request. |
744 * @param {string} body Data to send with POST requests. | 744 * @param {string} body Data to send with POST requests. |
745 * @param {!print_preview.Destination.Origin} origin Origin for destination. | 745 * @param {!print_preview.DestinationOrigin} origin Origin for destination. |
746 * @param {?string} account Account the request is sent for. Can be | 746 * @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 | 747 * {@code null} or empty string if the request is not cookie bound or |
748 * is sent on behalf of the primary user. | 748 * is sent on behalf of the primary user. |
749 * @param {function(!CloudPrintRequest)} callback Callback to invoke when | 749 * @param {function(!CloudPrintRequest)} callback Callback to invoke when |
750 * request completes. | 750 * request completes. |
751 * @constructor | 751 * @constructor |
752 */ | 752 */ |
753 function CloudPrintRequest(xhr, body, origin, account, callback) { | 753 function CloudPrintRequest(xhr, body, origin, account, callback) { |
754 /** | 754 /** |
755 * Partially prepared http request. | 755 * Partially prepared http request. |
756 * @type {!XMLHttpRequest} | 756 * @type {!XMLHttpRequest} |
757 */ | 757 */ |
758 this.xhr = xhr; | 758 this.xhr = xhr; |
759 | 759 |
760 /** | 760 /** |
761 * Data to send with POST requests. | 761 * Data to send with POST requests. |
762 * @type {string} | 762 * @type {string} |
763 */ | 763 */ |
764 this.body = body; | 764 this.body = body; |
765 | 765 |
766 /** | 766 /** |
767 * Origin for destination. | 767 * Origin for destination. |
768 * @type {!print_preview.Destination.Origin} | 768 * @type {!print_preview.DestinationOrigin} |
769 */ | 769 */ |
770 this.origin = origin; | 770 this.origin = origin; |
771 | 771 |
772 /** | 772 /** |
773 * User account this request is expected to be executed for. | 773 * User account this request is expected to be executed for. |
774 * @type {?string} | 774 * @type {?string} |
775 */ | 775 */ |
776 this.account = account; | 776 this.account = account; |
777 | 777 |
778 /** | 778 /** |
(...skipping 27 matching lines...) Expand all Loading... |
806 * @type {string} | 806 * @type {string} |
807 */ | 807 */ |
808 this.value = value; | 808 this.value = value; |
809 }; | 809 }; |
810 | 810 |
811 // Export | 811 // Export |
812 return { | 812 return { |
813 CloudPrintInterface: CloudPrintInterface | 813 CloudPrintInterface: CloudPrintInterface |
814 }; | 814 }; |
815 }); | 815 }); |
OLD | NEW |