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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 this.tracker_ = new EventTracker(); | 87 this.tracker_ = new EventTracker(); |
88 | 88 |
89 this.addEventListeners_(); | 89 this.addEventListeners_(); |
90 }; | 90 }; |
91 | 91 |
92 /** | 92 /** |
93 * Event types dispatched by the interface. | 93 * Event types dispatched by the interface. |
94 * @enum {string} | 94 * @enum {string} |
95 */ | 95 */ |
96 CloudPrintInterface.EventType = { | 96 CloudPrintInterface.EventType = { |
| 97 INVITES_DONE: 'cloudprint.CloudPrintInterface.INVITES_DONE', |
| 98 INVITES_FAILED: 'cloudprint.CloudPrintInterface.INVITES_FAILED', |
97 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | 99 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', |
98 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', | 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', |
99 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | 104 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', |
100 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', | 105 SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED', |
101 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', | 106 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE', |
102 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', | 107 SUBMIT_FAILED: 'cloudprint.CloudPrintInterface.SUBMIT_FAILED', |
103 UPDATE_PRINTER_TOS_ACCEPTANCE_FAILED: | 108 UPDATE_PRINTER_TOS_ACCEPTANCE_FAILED: |
104 'cloudprint.CloudPrintInterface.UPDATE_PRINTER_TOS_ACCEPTANCE_FAILED' | 109 'cloudprint.CloudPrintInterface.UPDATE_PRINTER_TOS_ACCEPTANCE_FAILED' |
105 }; | 110 }; |
106 | 111 |
107 /** | 112 /** |
108 * Content type header value for a URL encoded HTTP request. | 113 * Content type header value for a URL encoded HTTP request. |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 params, | 232 params, |
228 origin, | 233 origin, |
229 account, | 234 account, |
230 this.onSearchDone_.bind(this, isRecent)); | 235 this.onSearchDone_.bind(this, isRecent)); |
231 this.outstandingCloudSearchRequests_.push(cpRequest); | 236 this.outstandingCloudSearchRequests_.push(cpRequest); |
232 this.sendOrQueueRequest_(cpRequest); | 237 this.sendOrQueueRequest_(cpRequest); |
233 }, this); | 238 }, this); |
234 }, | 239 }, |
235 | 240 |
236 /** | 241 /** |
| 242 * Sends Google Cloud Print printer sharing invitations API requests. |
| 243 * @param {string} account Account the request is sent for. |
| 244 */ |
| 245 invites: function(account) { |
| 246 var params = [ |
| 247 new HttpParam('client', 'chrome'), |
| 248 ]; |
| 249 this.sendOrQueueRequest_(this.buildRequest_( |
| 250 'GET', |
| 251 'invites', |
| 252 params, |
| 253 print_preview.Destination.Origin.COOKIES, |
| 254 account, |
| 255 this.onInvitesDone_.bind(this))); |
| 256 }, |
| 257 |
| 258 /** |
| 259 * Accepts or rejects printer sharing invitation. |
| 260 * @param {!print_preview.Invitation} invitation Invitation to process. |
| 261 * @param {boolean} accept Whether to accept this invitation. |
| 262 */ |
| 263 processInvite: function(invitation, accept) { |
| 264 var params = [ |
| 265 new HttpParam('printerid', invitation.destination.id), |
| 266 new HttpParam('email', invitation.scopeId), |
| 267 new HttpParam('accept', accept), |
| 268 new HttpParam('use_cdd', true), |
| 269 ]; |
| 270 this.sendOrQueueRequest_(this.buildRequest_( |
| 271 'POST', |
| 272 'processinvite', |
| 273 params, |
| 274 invitation.destination.origin, |
| 275 invitation.destination.account, |
| 276 this.onProcessInviteDone_.bind(this, invitation, accept))); |
| 277 }, |
| 278 |
| 279 /** |
237 * Sends a Google Cloud Print submit API request. | 280 * Sends a Google Cloud Print submit API request. |
238 * @param {!print_preview.Destination} destination Cloud destination to | 281 * @param {!print_preview.Destination} destination Cloud destination to |
239 * print to. | 282 * print to. |
240 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the | 283 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the |
241 * print ticket to print. | 284 * print ticket to print. |
242 * @param {!print_preview.DocumentInfo} documentInfo Document data model. | 285 * @param {!print_preview.DocumentInfo} documentInfo Document data model. |
243 * @param {string} data Base64 encoded data of the document. | 286 * @param {string} data Base64 encoded data of the document. |
244 */ | 287 */ |
245 submit: function(destination, printTicketStore, documentInfo, data) { | 288 submit: function(destination, printTicketStore, documentInfo, data) { |
246 var result = | 289 var result = |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 event = this.createErrorEvent_( | 611 event = this.createErrorEvent_( |
569 CloudPrintInterface.EventType.SEARCH_FAILED, | 612 CloudPrintInterface.EventType.SEARCH_FAILED, |
570 request); | 613 request); |
571 } | 614 } |
572 event.user = activeUser; | 615 event.user = activeUser; |
573 event.searchDone = lastRequestForThisOrigin; | 616 event.searchDone = lastRequestForThisOrigin; |
574 this.dispatchEvent(event); | 617 this.dispatchEvent(event); |
575 }, | 618 }, |
576 | 619 |
577 /** | 620 /** |
| 621 * Called when invitations search request completes. |
| 622 * @param {!CloudPrintRequest} request Request that has been completed. |
| 623 * @private |
| 624 */ |
| 625 onInvitesDone_: function(request) { |
| 626 var event = null; |
| 627 var activeUser = |
| 628 (request.result && |
| 629 request.result['request'] && |
| 630 request.result['request']['user']) || ''; |
| 631 if (request.xhr.status == 200 && request.result['success']) { |
| 632 // Extract invitations. |
| 633 var invitationListJson = request.result['invites'] || []; |
| 634 var invitationList = []; |
| 635 invitationListJson.forEach(function(invitationJson) { |
| 636 try { |
| 637 invitationList.push(cloudprint.InvitationParser.parse( |
| 638 invitationJson, activeUser)); |
| 639 } catch (e) { |
| 640 console.error('Unable to parse invitation: ' + e); |
| 641 } |
| 642 }); |
| 643 // Dispatch INVITES_DONE event. |
| 644 event = new Event(CloudPrintInterface.EventType.INVITES_DONE); |
| 645 event.invitations = invitationList; |
| 646 } else { |
| 647 event = this.createErrorEvent_( |
| 648 CloudPrintInterface.EventType.INVITES_FAILED, request); |
| 649 } |
| 650 event.user = activeUser; |
| 651 this.dispatchEvent(event); |
| 652 }, |
| 653 |
| 654 /** |
| 655 * Called when invitation processing request completes. |
| 656 * @param {!print_preview.Invitation} invitation Processed invitation. |
| 657 * @param {boolean} accept Whether this invitation was accepted or rejected. |
| 658 * @param {!CloudPrintRequest} request Request that has been completed. |
| 659 * @private |
| 660 */ |
| 661 onProcessInviteDone_: function(invitation, accept, request) { |
| 662 var event = null; |
| 663 var activeUser = |
| 664 (request.result && |
| 665 request.result['request'] && |
| 666 request.result['request']['user']) || ''; |
| 667 if (request.xhr.status == 200 && request.result['success']) { |
| 668 event = new Event(CloudPrintInterface.EventType.PROCESS_INVITE_DONE); |
| 669 if (accept) { |
| 670 try { |
| 671 event.printer = cloudprint.CloudDestinationParser.parse( |
| 672 request.result['printer'], request.origin, activeUser); |
| 673 } catch (e) { |
| 674 console.error('Failed to parse cloud print destination: ' + e); |
| 675 } |
| 676 } |
| 677 } else { |
| 678 event = this.createErrorEvent_( |
| 679 CloudPrintInterface.EventType.PROCESS_INVITE_FAILED, request); |
| 680 } |
| 681 event.invitation = invitation; |
| 682 event.accept = accept; |
| 683 event.user = activeUser; |
| 684 this.dispatchEvent(event); |
| 685 }, |
| 686 |
| 687 /** |
578 * Called when the submit request completes. | 688 * Called when the submit request completes. |
579 * @param {!CloudPrintRequest} request Request that has been completed. | 689 * @param {!CloudPrintRequest} request Request that has been completed. |
580 * @private | 690 * @private |
581 */ | 691 */ |
582 onSubmitDone_: function(request) { | 692 onSubmitDone_: function(request) { |
583 if (request.xhr.status == 200 && request.result['success']) { | 693 if (request.xhr.status == 200 && request.result['success']) { |
584 var submitDoneEvent = new Event( | 694 var submitDoneEvent = new Event( |
585 CloudPrintInterface.EventType.SUBMIT_DONE); | 695 CloudPrintInterface.EventType.SUBMIT_DONE); |
586 submitDoneEvent.jobId = request.result['job']['id']; | 696 submitDoneEvent.jobId = request.result['job']['id']; |
587 this.dispatchEvent(submitDoneEvent); | 697 this.dispatchEvent(submitDoneEvent); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 * @type {string} | 844 * @type {string} |
735 */ | 845 */ |
736 this.value = value; | 846 this.value = value; |
737 }; | 847 }; |
738 | 848 |
739 // Export | 849 // Export |
740 return { | 850 return { |
741 CloudPrintInterface: CloudPrintInterface | 851 CloudPrintInterface: CloudPrintInterface |
742 }; | 852 }; |
743 }); | 853 }); |
OLD | NEW |