OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.exportPath('cloudprint'); | 5 cr.exportPath('cloudprint'); |
6 | 6 |
7 /** | 7 /** |
8 * Event types dispatched by the cloudprint interface. | 8 * Event types dispatched by the cloudprint interface. |
9 * @enum {string} | 9 * @enum {string} |
10 */ | 10 */ |
11 cloudprint.CloudPrintInterfaceEventType = { | 11 cloudprint.CloudPrintInterfaceEventType = { |
12 INVITES_DONE: 'cloudprint.CloudPrintInterface.INVITES_DONE', | 12 INVITES_DONE: 'cloudprint.CloudPrintInterface.INVITES_DONE', |
13 INVITES_FAILED: 'cloudprint.CloudPrintInterface.INVITES_FAILED', | 13 INVITES_FAILED: 'cloudprint.CloudPrintInterface.INVITES_FAILED', |
14 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | 14 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', |
15 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', | 15 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', |
16 PROCESS_INVITE_DONE: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_DONE', | 16 PROCESS_INVITE_DONE: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_DONE', |
17 PROCESS_INVITE_FAILED: | 17 PROCESS_INVITE_FAILED: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_FAILED', |
18 'cloudprint.CloudPrintInterface.PROCESS_INVITE_FAILED', | |
19 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | 18 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', |
20 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', | 19 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', |
21 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', | 20 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', |
22 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', | 21 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', |
23 }; | 22 }; |
24 | 23 |
25 cr.define('cloudprint', function() { | 24 cr.define('cloudprint', function() { |
26 'use strict'; | 25 'use strict'; |
27 | 26 |
28 var CloudPrintInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; | 27 var CloudPrintInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 * @const | 145 * @const |
147 * @private | 146 * @private |
148 */ | 147 */ |
149 CloudPrintInterface.VERSION_REGEXP_ = /.*Chrome\/([\d\.]+)/i; | 148 CloudPrintInterface.VERSION_REGEXP_ = /.*Chrome\/([\d\.]+)/i; |
150 | 149 |
151 /** | 150 /** |
152 * Enumeration of JSON response fields from Google Cloud Print API. | 151 * Enumeration of JSON response fields from Google Cloud Print API. |
153 * @enum {string} | 152 * @enum {string} |
154 * @private | 153 * @private |
155 */ | 154 */ |
156 CloudPrintInterface.JsonFields_ = { | 155 CloudPrintInterface.JsonFields_ = {PRINTER: 'printer'}; |
157 PRINTER: 'printer' | |
158 }; | |
159 | 156 |
160 /** | 157 /** |
161 * Could Print origins used to search printers. | 158 * Could Print origins used to search printers. |
162 * @type {!Array<!print_preview.DestinationOrigin>} | 159 * @type {!Array<!print_preview.DestinationOrigin>} |
163 * @const | 160 * @const |
164 * @private | 161 * @private |
165 */ | 162 */ |
166 CloudPrintInterface.CLOUD_ORIGINS_ = [ | 163 CloudPrintInterface.CLOUD_ORIGINS_ = [ |
167 print_preview.DestinationOrigin.COOKIES, | 164 print_preview.DestinationOrigin.COOKIES, |
168 print_preview.DestinationOrigin.DEVICE | 165 print_preview.DestinationOrigin.DEVICE |
169 // TODO(vitalybuka): Enable when implemented. | 166 // TODO(vitalybuka): Enable when implemented. |
170 // ready print_preview.DestinationOrigin.PROFILE | 167 // ready print_preview.DestinationOrigin.PROFILE |
171 ]; | 168 ]; |
172 | 169 |
173 CloudPrintInterface.prototype = { | 170 CloudPrintInterface.prototype = { |
174 __proto__: cr.EventTarget.prototype, | 171 __proto__: cr.EventTarget.prototype, |
175 | 172 |
176 /** @return {string} Base URL of the Google Cloud Print service. */ | 173 /** @return {string} Base URL of the Google Cloud Print service. */ |
177 get baseUrl() { | 174 get baseUrl() { |
178 return this.baseUrl_; | 175 return this.baseUrl_; |
179 }, | 176 }, |
180 | 177 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 * @param {string} account Account the search is sent for. It matters for | 211 * @param {string} account Account the search is sent for. It matters for |
215 * COOKIES origin only, and can be empty (sent on behalf of the primary | 212 * COOKIES origin only, and can be empty (sent on behalf of the primary |
216 * user in this case). | 213 * user in this case). |
217 * @param {!Array<!print_preview.DestinationOrigin>} origins Origins to | 214 * @param {!Array<!print_preview.DestinationOrigin>} origins Origins to |
218 * search printers for. | 215 * search printers for. |
219 * @private | 216 * @private |
220 */ | 217 */ |
221 search_: function(isRecent, account, origins) { | 218 search_: function(isRecent, account, origins) { |
222 var params = [ | 219 var params = [ |
223 new HttpParam('connection_status', 'ALL'), | 220 new HttpParam('connection_status', 'ALL'), |
224 new HttpParam('client', 'chrome'), | 221 new HttpParam('client', 'chrome'), new HttpParam('use_cdd', 'true') |
225 new HttpParam('use_cdd', 'true') | |
226 ]; | 222 ]; |
227 if (isRecent) { | 223 if (isRecent) { |
228 params.push(new HttpParam('q', '^recent')); | 224 params.push(new HttpParam('q', '^recent')); |
229 } | 225 } |
230 origins.forEach(function(origin) { | 226 origins.forEach(function(origin) { |
231 var cpRequest = this.buildRequest_( | 227 var cpRequest = this.buildRequest_( |
232 'GET', | 228 'GET', 'search', params, origin, account, |
233 'search', | |
234 params, | |
235 origin, | |
236 account, | |
237 this.onSearchDone_.bind(this, isRecent)); | 229 this.onSearchDone_.bind(this, isRecent)); |
238 this.outstandingCloudSearchRequests_.push(cpRequest); | 230 this.outstandingCloudSearchRequests_.push(cpRequest); |
239 this.sendOrQueueRequest_(cpRequest); | 231 this.sendOrQueueRequest_(cpRequest); |
240 }, this); | 232 }, this); |
241 }, | 233 }, |
242 | 234 |
243 /** | 235 /** |
244 * Sends Google Cloud Print printer sharing invitations API requests. | 236 * Sends Google Cloud Print printer sharing invitations API requests. |
245 * @param {string} account Account the request is sent for. | 237 * @param {string} account Account the request is sent for. |
246 */ | 238 */ |
247 invites: function(account) { | 239 invites: function(account) { |
248 var params = [ | 240 var params = [ |
249 new HttpParam('client', 'chrome'), | 241 new HttpParam('client', 'chrome'), |
250 ]; | 242 ]; |
251 this.sendOrQueueRequest_(this.buildRequest_( | 243 this.sendOrQueueRequest_(this.buildRequest_( |
252 'GET', | 244 'GET', 'invites', params, print_preview.DestinationOrigin.COOKIES, |
253 'invites', | 245 account, this.onInvitesDone_.bind(this))); |
254 params, | |
255 print_preview.DestinationOrigin.COOKIES, | |
256 account, | |
257 this.onInvitesDone_.bind(this))); | |
258 }, | 246 }, |
259 | 247 |
260 /** | 248 /** |
261 * Accepts or rejects printer sharing invitation. | 249 * Accepts or rejects printer sharing invitation. |
262 * @param {!print_preview.Invitation} invitation Invitation to process. | 250 * @param {!print_preview.Invitation} invitation Invitation to process. |
263 * @param {boolean} accept Whether to accept this invitation. | 251 * @param {boolean} accept Whether to accept this invitation. |
264 */ | 252 */ |
265 processInvite: function(invitation, accept) { | 253 processInvite: function(invitation, accept) { |
266 var params = [ | 254 var params = [ |
267 new HttpParam('printerid', invitation.destination.id), | 255 new HttpParam('printerid', invitation.destination.id), |
268 new HttpParam('email', invitation.scopeId), | 256 new HttpParam('email', invitation.scopeId), |
269 new HttpParam('accept', accept ? 'true' : 'false'), | 257 new HttpParam('accept', accept ? 'true' : 'false'), |
270 new HttpParam('use_cdd', 'true'), | 258 new HttpParam('use_cdd', 'true'), |
271 ]; | 259 ]; |
272 this.sendOrQueueRequest_(this.buildRequest_( | 260 this.sendOrQueueRequest_(this.buildRequest_( |
273 'POST', | 261 'POST', 'processinvite', params, invitation.destination.origin, |
274 'processinvite', | |
275 params, | |
276 invitation.destination.origin, | |
277 invitation.destination.account, | 262 invitation.destination.account, |
278 this.onProcessInviteDone_.bind(this, invitation, accept))); | 263 this.onProcessInviteDone_.bind(this, invitation, accept))); |
279 }, | 264 }, |
280 | 265 |
281 /** | 266 /** |
282 * Sends a Google Cloud Print submit API request. | 267 * Sends a Google Cloud Print submit API request. |
283 * @param {!print_preview.Destination} destination Cloud destination to | 268 * @param {!print_preview.Destination} destination Cloud destination to |
284 * print to. | 269 * print to. |
285 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the | 270 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the |
286 * print ticket to print. | 271 * print ticket to print. |
287 * @param {!print_preview.DocumentInfo} documentInfo Document data model. | 272 * @param {!print_preview.DocumentInfo} documentInfo Document data model. |
288 * @param {string} data Base64 encoded data of the document. | 273 * @param {string} data Base64 encoded data of the document. |
289 */ | 274 */ |
290 submit: function(destination, printTicketStore, documentInfo, data) { | 275 submit: function(destination, printTicketStore, documentInfo, data) { |
291 var result = | 276 var result = |
292 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); | 277 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); |
293 var chromeVersion = 'unknown'; | 278 var chromeVersion = 'unknown'; |
294 if (result && result.length == 2) { | 279 if (result && result.length == 2) { |
295 chromeVersion = result[1]; | 280 chromeVersion = result[1]; |
296 } | 281 } |
297 var params = [ | 282 var params = [ |
298 new HttpParam('printerid', destination.id), | 283 new HttpParam('printerid', destination.id), |
299 new HttpParam('contentType', 'dataUrl'), | 284 new HttpParam('contentType', 'dataUrl'), |
300 new HttpParam('title', documentInfo.title), | 285 new HttpParam('title', documentInfo.title), |
301 new HttpParam('ticket', | 286 new HttpParam( |
302 printTicketStore.createPrintTicket(destination)), | 287 'ticket', printTicketStore.createPrintTicket(destination)), |
303 new HttpParam('content', 'data:application/pdf;base64,' + data), | 288 new HttpParam('content', 'data:application/pdf;base64,' + data), |
304 new HttpParam('tag', | 289 new HttpParam('tag', '__google__chrome_version=' + chromeVersion), |
305 '__google__chrome_version=' + chromeVersion), | |
306 new HttpParam('tag', '__google__os=' + navigator.platform) | 290 new HttpParam('tag', '__google__os=' + navigator.platform) |
307 ]; | 291 ]; |
308 var cpRequest = this.buildRequest_( | 292 var cpRequest = this.buildRequest_( |
309 'POST', | 293 'POST', 'submit', params, destination.origin, destination.account, |
310 'submit', | |
311 params, | |
312 destination.origin, | |
313 destination.account, | |
314 this.onSubmitDone_.bind(this)); | 294 this.onSubmitDone_.bind(this)); |
315 this.sendOrQueueRequest_(cpRequest); | 295 this.sendOrQueueRequest_(cpRequest); |
316 }, | 296 }, |
317 | 297 |
318 /** | 298 /** |
319 * Sends a Google Cloud Print printer API request. | 299 * Sends a Google Cloud Print printer API request. |
320 * @param {string} printerId ID of the printer to lookup. | 300 * @param {string} printerId ID of the printer to lookup. |
321 * @param {!print_preview.DestinationOrigin} origin Origin of the printer. | 301 * @param {!print_preview.DestinationOrigin} origin Origin of the printer. |
322 * @param {string=} account Account this printer is registered for. When | 302 * @param {string=} account Account this printer is registered for. When |
323 * provided for COOKIES {@code origin}, and users sessions are still not | 303 * provided for COOKIES {@code origin}, and users sessions are still not |
324 * known, will be checked against the response (both success and failure | 304 * known, will be checked against the response (both success and failure |
325 * to get printer) and, if the active user account is not the one | 305 * to get printer) and, if the active user account is not the one |
326 * requested, {@code account} is activated and printer request reissued. | 306 * requested, {@code account} is activated and printer request reissued. |
327 */ | 307 */ |
328 printer: function(printerId, origin, account) { | 308 printer: function(printerId, origin, account) { |
329 var params = [ | 309 var params = [ |
330 new HttpParam('printerid', printerId), | 310 new HttpParam('printerid', printerId), new HttpParam('use_cdd', 'true'), |
331 new HttpParam('use_cdd', 'true'), | |
332 new HttpParam('printer_connection_status', 'true') | 311 new HttpParam('printer_connection_status', 'true') |
333 ]; | 312 ]; |
334 this.sendOrQueueRequest_(this.buildRequest_( | 313 this.sendOrQueueRequest_(this.buildRequest_( |
335 'GET', | 314 'GET', 'printer', params, origin, account || '', |
336 'printer', | |
337 params, | |
338 origin, | |
339 account || '', | |
340 this.onPrinterDone_.bind(this, printerId))); | 315 this.onPrinterDone_.bind(this, printerId))); |
341 }, | 316 }, |
342 | 317 |
343 /** | 318 /** |
344 * Adds event listeners to relevant events. | 319 * Adds event listeners to relevant events. |
345 * @private | 320 * @private |
346 */ | 321 */ |
347 addEventListeners_: function() { | 322 addEventListeners_: function() { |
348 this.tracker_.add( | 323 this.tracker_.add( |
349 this.nativeLayer_.getEventTarget(), | 324 this.nativeLayer_.getEventTarget(), |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 var headers = {}; | 377 var headers = {}; |
403 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; | 378 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
404 if (method == 'GET') { | 379 if (method == 'GET') { |
405 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; | 380 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
406 } else if (method == 'POST') { | 381 } else if (method == 'POST') { |
407 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; | 382 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; |
408 } | 383 } |
409 | 384 |
410 var xhr = new XMLHttpRequest(); | 385 var xhr = new XMLHttpRequest(); |
411 xhr.open(method, url, true); | 386 xhr.open(method, url, true); |
412 xhr.withCredentials = | 387 xhr.withCredentials = (origin == print_preview.DestinationOrigin.COOKIES); |
413 (origin == print_preview.DestinationOrigin.COOKIES); | |
414 for (var header in headers) { | 388 for (var header in headers) { |
415 xhr.setRequestHeader(header, headers[header]); | 389 xhr.setRequestHeader(header, headers[header]); |
416 } | 390 } |
417 | 391 |
418 return new cloudprint.CloudPrintRequest(xhr, body, origin, account, | 392 return new cloudprint.CloudPrintRequest( |
419 callback); | 393 xhr, body, origin, account, callback); |
420 }, | 394 }, |
421 | 395 |
422 /** | 396 /** |
423 * Sends a request to the Google Cloud Print API or queues if it needs to | 397 * Sends a request to the Google Cloud Print API or queues if it needs to |
424 * wait OAuth2 access token. | 398 * wait OAuth2 access token. |
425 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. | 399 * @param {!cloudprint.CloudPrintRequest} request Request to send or queue. |
426 * @private | 400 * @private |
427 */ | 401 */ |
428 sendOrQueueRequest_: function(request) { | 402 sendOrQueueRequest_: function(request) { |
429 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 403 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 */ | 483 */ |
510 onAccessTokenReady_: function(event) { | 484 onAccessTokenReady_: function(event) { |
511 // TODO(vitalybuka): remove when other Origins implemented. | 485 // TODO(vitalybuka): remove when other Origins implemented. |
512 assert(event.authType == print_preview.DestinationOrigin.DEVICE); | 486 assert(event.authType == print_preview.DestinationOrigin.DEVICE); |
513 this.requestQueue_ = this.requestQueue_.filter(function(request) { | 487 this.requestQueue_ = this.requestQueue_.filter(function(request) { |
514 assert(request.origin == print_preview.DestinationOrigin.DEVICE); | 488 assert(request.origin == print_preview.DestinationOrigin.DEVICE); |
515 if (request.origin != event.authType) { | 489 if (request.origin != event.authType) { |
516 return true; | 490 return true; |
517 } | 491 } |
518 if (event.accessToken) { | 492 if (event.accessToken) { |
519 request.xhr.setRequestHeader('Authorization', | 493 request.xhr.setRequestHeader( |
520 'Bearer ' + event.accessToken); | 494 'Authorization', 'Bearer ' + event.accessToken); |
521 this.sendRequest_(request); | 495 this.sendRequest_(request); |
522 } else { // No valid token. | 496 } else { // No valid token. |
523 // Without abort status does not exist. | 497 // Without abort status does not exist. |
524 request.xhr.abort(); | 498 request.xhr.abort(); |
525 request.callback(request); | 499 request.callback(request); |
526 } | 500 } |
527 return false; | 501 return false; |
528 }, this); | 502 }, this); |
529 }, | 503 }, |
530 | 504 |
531 /** | 505 /** |
532 * Called when the ready-state of a XML http request changes. | 506 * Called when the ready-state of a XML http request changes. |
533 * Calls the successCallback with the result or dispatches an ERROR event. | 507 * Calls the successCallback with the result or dispatches an ERROR event. |
534 * @param {!cloudprint.CloudPrintRequest} request Request that was changed. | 508 * @param {!cloudprint.CloudPrintRequest} request Request that was changed. |
535 * @private | 509 * @private |
536 */ | 510 */ |
537 onReadyStateChange_: function(request) { | 511 onReadyStateChange_: function(request) { |
538 if (request.xhr.readyState == 4) { | 512 if (request.xhr.readyState == 4) { |
539 if (request.xhr.status == 200) { | 513 if (request.xhr.status == 200) { |
540 request.result = /** @type {Object} */ ( | 514 request.result = |
541 JSON.parse(request.xhr.responseText)); | 515 /** @type {Object} */ (JSON.parse(request.xhr.responseText)); |
542 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 516 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
543 request.result['success']) { | 517 request.result['success']) { |
544 this.xsrfTokens_[request.result['request']['user']] = | 518 this.xsrfTokens_[request.result['request']['user']] = |
545 request.result['xsrf_token']; | 519 request.result['xsrf_token']; |
546 } | 520 } |
547 } | 521 } |
548 request.status = request.xhr.status; | 522 request.status = request.xhr.status; |
549 request.callback(request); | 523 request.callback(request); |
550 } | 524 } |
551 }, | 525 }, |
(...skipping 10 matching lines...) Expand all Loading... |
562 var lastRequestForThisOrigin = true; | 536 var lastRequestForThisOrigin = true; |
563 this.outstandingCloudSearchRequests_ = | 537 this.outstandingCloudSearchRequests_ = |
564 this.outstandingCloudSearchRequests_.filter(function(item) { | 538 this.outstandingCloudSearchRequests_.filter(function(item) { |
565 if (item != request && item.origin == request.origin) { | 539 if (item != request && item.origin == request.origin) { |
566 lastRequestForThisOrigin = false; | 540 lastRequestForThisOrigin = false; |
567 } | 541 } |
568 return item != request; | 542 return item != request; |
569 }); | 543 }); |
570 var activeUser = ''; | 544 var activeUser = ''; |
571 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 545 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
572 activeUser = | 546 activeUser = request.result && request.result['request'] && |
573 request.result && | |
574 request.result['request'] && | |
575 request.result['request']['user']; | 547 request.result['request']['user']; |
576 } | 548 } |
577 var event = null; | 549 var event = null; |
578 if (request.xhr.status == 200 && request.result['success']) { | 550 if (request.xhr.status == 200 && request.result['success']) { |
579 // Extract printers. | 551 // Extract printers. |
580 var printerListJson = request.result['printers'] || []; | 552 var printerListJson = request.result['printers'] || []; |
581 var printerList = []; | 553 var printerList = []; |
582 printerListJson.forEach(function(printerJson) { | 554 printerListJson.forEach(function(printerJson) { |
583 try { | 555 try { |
584 printerList.push(cloudprint.CloudDestinationParser.parse( | 556 printerList.push(cloudprint.CloudDestinationParser.parse( |
585 printerJson, request.origin, activeUser)); | 557 printerJson, request.origin, activeUser)); |
586 } catch (err) { | 558 } catch (err) { |
587 console.error('Unable to parse cloud print destination: ' + err); | 559 console.error('Unable to parse cloud print destination: ' + err); |
588 } | 560 } |
589 }); | 561 }); |
590 // Extract and store users. | 562 // Extract and store users. |
591 this.setUsers_(request); | 563 this.setUsers_(request); |
592 // Dispatch SEARCH_DONE event. | 564 // Dispatch SEARCH_DONE event. |
593 event = new Event(CloudPrintInterfaceEventType.SEARCH_DONE); | 565 event = new Event(CloudPrintInterfaceEventType.SEARCH_DONE); |
594 event.origin = request.origin; | 566 event.origin = request.origin; |
595 event.printers = printerList; | 567 event.printers = printerList; |
596 event.isRecent = isRecent; | 568 event.isRecent = isRecent; |
597 } else { | 569 } else { |
598 event = this.createErrorEvent_( | 570 event = this.createErrorEvent_( |
599 CloudPrintInterfaceEventType.SEARCH_FAILED, | 571 CloudPrintInterfaceEventType.SEARCH_FAILED, request); |
600 request); | |
601 } | 572 } |
602 event.user = activeUser; | 573 event.user = activeUser; |
603 event.searchDone = lastRequestForThisOrigin; | 574 event.searchDone = lastRequestForThisOrigin; |
604 this.dispatchEvent(event); | 575 this.dispatchEvent(event); |
605 }, | 576 }, |
606 | 577 |
607 /** | 578 /** |
608 * Called when invitations search request completes. | 579 * Called when invitations search request completes. |
609 * @param {!cloudprint.CloudPrintRequest} request Request that has been | 580 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
610 * completed. | 581 * completed. |
611 * @private | 582 * @private |
612 */ | 583 */ |
613 onInvitesDone_: function(request) { | 584 onInvitesDone_: function(request) { |
614 var event = null; | 585 var event = null; |
615 var activeUser = | 586 var activeUser = (request.result && request.result['request'] && |
616 (request.result && | 587 request.result['request']['user']) || |
617 request.result['request'] && | 588 ''; |
618 request.result['request']['user']) || ''; | |
619 if (request.xhr.status == 200 && request.result['success']) { | 589 if (request.xhr.status == 200 && request.result['success']) { |
620 // Extract invitations. | 590 // Extract invitations. |
621 var invitationListJson = request.result['invites'] || []; | 591 var invitationListJson = request.result['invites'] || []; |
622 var invitationList = []; | 592 var invitationList = []; |
623 invitationListJson.forEach(function(invitationJson) { | 593 invitationListJson.forEach(function(invitationJson) { |
624 try { | 594 try { |
625 invitationList.push(cloudprint.InvitationParser.parse( | 595 invitationList.push( |
626 invitationJson, activeUser)); | 596 cloudprint.InvitationParser.parse(invitationJson, activeUser)); |
627 } catch (e) { | 597 } catch (e) { |
628 console.error('Unable to parse invitation: ' + e); | 598 console.error('Unable to parse invitation: ' + e); |
629 } | 599 } |
630 }); | 600 }); |
631 // Dispatch INVITES_DONE event. | 601 // Dispatch INVITES_DONE event. |
632 event = new Event(CloudPrintInterfaceEventType.INVITES_DONE); | 602 event = new Event(CloudPrintInterfaceEventType.INVITES_DONE); |
633 event.invitations = invitationList; | 603 event.invitations = invitationList; |
634 } else { | 604 } else { |
635 event = this.createErrorEvent_( | 605 event = this.createErrorEvent_( |
636 CloudPrintInterfaceEventType.INVITES_FAILED, request); | 606 CloudPrintInterfaceEventType.INVITES_FAILED, request); |
637 } | 607 } |
638 event.user = activeUser; | 608 event.user = activeUser; |
639 this.dispatchEvent(event); | 609 this.dispatchEvent(event); |
640 }, | 610 }, |
641 | 611 |
642 /** | 612 /** |
643 * Called when invitation processing request completes. | 613 * Called when invitation processing request completes. |
644 * @param {!print_preview.Invitation} invitation Processed invitation. | 614 * @param {!print_preview.Invitation} invitation Processed invitation. |
645 * @param {boolean} accept Whether this invitation was accepted or rejected. | 615 * @param {boolean} accept Whether this invitation was accepted or rejected. |
646 * @param {!cloudprint.CloudPrintRequest} request Request that has been | 616 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
647 * completed. | 617 * completed. |
648 * @private | 618 * @private |
649 */ | 619 */ |
650 onProcessInviteDone_: function(invitation, accept, request) { | 620 onProcessInviteDone_: function(invitation, accept, request) { |
651 var event = null; | 621 var event = null; |
652 var activeUser = | 622 var activeUser = (request.result && request.result['request'] && |
653 (request.result && | 623 request.result['request']['user']) || |
654 request.result['request'] && | 624 ''; |
655 request.result['request']['user']) || ''; | |
656 if (request.xhr.status == 200 && request.result['success']) { | 625 if (request.xhr.status == 200 && request.result['success']) { |
657 event = new Event(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE); | 626 event = new Event(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE); |
658 if (accept) { | 627 if (accept) { |
659 try { | 628 try { |
660 event.printer = cloudprint.CloudDestinationParser.parse( | 629 event.printer = cloudprint.CloudDestinationParser.parse( |
661 request.result['printer'], request.origin, activeUser); | 630 request.result['printer'], request.origin, activeUser); |
662 } catch (e) { | 631 } catch (e) { |
663 console.error('Failed to parse cloud print destination: ' + e); | 632 console.error('Failed to parse cloud print destination: ' + e); |
664 } | 633 } |
665 } | 634 } |
666 } else { | 635 } else { |
667 event = this.createErrorEvent_( | 636 event = this.createErrorEvent_( |
668 CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED, request); | 637 CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED, request); |
669 } | 638 } |
670 event.invitation = invitation; | 639 event.invitation = invitation; |
671 event.accept = accept; | 640 event.accept = accept; |
672 event.user = activeUser; | 641 event.user = activeUser; |
673 this.dispatchEvent(event); | 642 this.dispatchEvent(event); |
674 }, | 643 }, |
675 | 644 |
676 /** | 645 /** |
677 * Called when the submit request completes. | 646 * Called when the submit request completes. |
678 * @param {!cloudprint.CloudPrintRequest} request Request that has been | 647 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
679 * completed. | 648 * completed. |
680 * @private | 649 * @private |
681 */ | 650 */ |
682 onSubmitDone_: function(request) { | 651 onSubmitDone_: function(request) { |
683 if (request.xhr.status == 200 && request.result['success']) { | 652 if (request.xhr.status == 200 && request.result['success']) { |
684 var submitDoneEvent = new Event( | 653 var submitDoneEvent = |
685 CloudPrintInterfaceEventType.SUBMIT_DONE); | 654 new Event(CloudPrintInterfaceEventType.SUBMIT_DONE); |
686 submitDoneEvent.jobId = request.result['job']['id']; | 655 submitDoneEvent.jobId = request.result['job']['id']; |
687 this.dispatchEvent(submitDoneEvent); | 656 this.dispatchEvent(submitDoneEvent); |
688 } else { | 657 } else { |
689 var errorEvent = this.createErrorEvent_( | 658 var errorEvent = this.createErrorEvent_( |
690 CloudPrintInterfaceEventType.SUBMIT_FAILED, request); | 659 CloudPrintInterfaceEventType.SUBMIT_FAILED, request); |
691 this.dispatchEvent(errorEvent); | 660 this.dispatchEvent(errorEvent); |
692 } | 661 } |
693 }, | 662 }, |
694 | 663 |
695 /** | 664 /** |
696 * Called when the printer request completes. | 665 * Called when the printer request completes. |
697 * @param {string} destinationId ID of the destination that was looked up. | 666 * @param {string} destinationId ID of the destination that was looked up. |
698 * @param {!cloudprint.CloudPrintRequest} request Request that has been | 667 * @param {!cloudprint.CloudPrintRequest} request Request that has been |
699 * completed. | 668 * completed. |
700 * @private | 669 * @private |
701 */ | 670 */ |
702 onPrinterDone_: function(destinationId, request) { | 671 onPrinterDone_: function(destinationId, request) { |
703 // Special handling of the first printer request. It does not matter at | 672 // Special handling of the first printer request. It does not matter at |
704 // this point, whether printer was found or not. | 673 // this point, whether printer was found or not. |
705 if (request.origin == print_preview.DestinationOrigin.COOKIES && | 674 if (request.origin == print_preview.DestinationOrigin.COOKIES && |
706 request.result && | 675 request.result && request.account && |
707 request.account && | |
708 request.result['request']['user'] && | 676 request.result['request']['user'] && |
709 request.result['request']['users'] && | 677 request.result['request']['users'] && |
710 request.account != request.result['request']['user']) { | 678 request.account != request.result['request']['user']) { |
711 this.setUsers_(request); | 679 this.setUsers_(request); |
712 // In case the user account is known, but not the primary one, | 680 // In case the user account is known, but not the primary one, |
713 // activate it. | 681 // activate it. |
714 if (this.userSessionIndex_[request.account] > 0) { | 682 if (this.userSessionIndex_[request.account] > 0) { |
715 this.userInfo_.activeUser = request.account; | 683 this.userInfo_.activeUser = request.account; |
716 // Repeat the request for the newly activated account. | 684 // Repeat the request for the newly activated account. |
717 this.printer( | 685 this.printer( |
718 request.result['request']['params']['printerid'], | 686 request.result['request']['params']['printerid'], request.origin, |
719 request.origin, | |
720 request.account); | 687 request.account); |
721 // Stop processing this request, wait for the new response. | 688 // Stop processing this request, wait for the new response. |
722 return; | 689 return; |
723 } | 690 } |
724 } | 691 } |
725 // Process response. | 692 // Process response. |
726 if (request.xhr.status == 200 && request.result['success']) { | 693 if (request.xhr.status == 200 && request.result['success']) { |
727 var activeUser = ''; | 694 var activeUser = ''; |
728 if (request.origin == print_preview.DestinationOrigin.COOKIES) { | 695 if (request.origin == print_preview.DestinationOrigin.COOKIES) { |
729 activeUser = request.result['request']['user']; | 696 activeUser = request.result['request']['user']; |
730 } | 697 } |
731 var printerJson = request.result['printers'][0]; | 698 var printerJson = request.result['printers'][0]; |
732 var printer; | 699 var printer; |
733 try { | 700 try { |
734 printer = cloudprint.CloudDestinationParser.parse( | 701 printer = cloudprint.CloudDestinationParser.parse( |
735 printerJson, request.origin, activeUser); | 702 printerJson, request.origin, activeUser); |
736 } catch (err) { | 703 } catch (err) { |
737 console.error('Failed to parse cloud print destination: ' + | 704 console.error( |
| 705 'Failed to parse cloud print destination: ' + |
738 JSON.stringify(printerJson)); | 706 JSON.stringify(printerJson)); |
739 return; | 707 return; |
740 } | 708 } |
741 var printerDoneEvent = | 709 var printerDoneEvent = |
742 new Event(CloudPrintInterfaceEventType.PRINTER_DONE); | 710 new Event(CloudPrintInterfaceEventType.PRINTER_DONE); |
743 printerDoneEvent.printer = printer; | 711 printerDoneEvent.printer = printer; |
744 this.dispatchEvent(printerDoneEvent); | 712 this.dispatchEvent(printerDoneEvent); |
745 } else { | 713 } else { |
746 var errorEvent = this.createErrorEvent_( | 714 var errorEvent = this.createErrorEvent_( |
747 CloudPrintInterfaceEventType.PRINTER_FAILED, request); | 715 CloudPrintInterfaceEventType.PRINTER_FAILED, request); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 */ | 789 */ |
822 this.value = value; | 790 this.value = value; |
823 } | 791 } |
824 | 792 |
825 // Export | 793 // Export |
826 return { | 794 return { |
827 CloudPrintInterface: CloudPrintInterface, | 795 CloudPrintInterface: CloudPrintInterface, |
828 CloudPrintRequest: CloudPrintRequest | 796 CloudPrintRequest: CloudPrintRequest |
829 }; | 797 }; |
830 }); | 798 }); |
OLD | NEW |