Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: chrome/browser/resources/print_preview/cloud_print_interface.js

Issue 10971003: Moves HTTP POST handling to JS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge with trunk. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/print_ticket_store.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 /** 47 /**
48 * Content type header value for a URL encoded HTTP request. 48 * Content type header value for a URL encoded HTTP request.
49 * @type {string} 49 * @type {string}
50 * @private 50 * @private
51 */ 51 */
52 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = 52 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ =
53 'application/x-www-form-urlencoded'; 53 'application/x-www-form-urlencoded';
54 54
55 /** 55 /**
56 * Multi-part POST request boundary used in communication with Google
57 * Cloud Print.
58 * @type {string}
59 * @private
60 */
61 CloudPrintInterface.MULTIPART_BOUNDARY_ =
62 '----CloudPrintFormBoundaryjc9wuprokl8i';
63
64 /**
56 * Content type header value for a multipart HTTP request. 65 * Content type header value for a multipart HTTP request.
57 * @type {string} 66 * @type {string}
58 * @private 67 * @private
59 */ 68 */
60 CloudPrintInterface.MULTIPART_CONTENT_TYPE_ = 69 CloudPrintInterface.MULTIPART_CONTENT_TYPE_ =
61 'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i'; 70 'multipart/form-data; boundary=' +
71 CloudPrintInterface.MULTIPART_BOUNDARY_;
62 72
63 /** 73 /**
64 * Enumeration of JSON response fields from Google Cloud Print API. 74 * Enumeration of JSON response fields from Google Cloud Print API.
65 * @enum {string} 75 * @enum {string}
66 * @private 76 * @private
67 */ 77 */
68 CloudPrintInterface.JsonFields_ = { 78 CloudPrintInterface.JsonFields_ = {
69 PRINTER: 'printer' 79 PRINTER: 'printer'
70 }; 80 };
71 81
72 CloudPrintInterface.prototype = { 82 CloudPrintInterface.prototype = {
73 __proto__: cr.EventTarget.prototype, 83 __proto__: cr.EventTarget.prototype,
74 84
75 /** 85 /**
76 * Sends a Google Cloud Print search API request. 86 * Sends a Google Cloud Print search API request.
77 * @param {boolean} isRecent Whether to search for only recently used 87 * @param {boolean} isRecent Whether to search for only recently used
78 * printers. 88 * printers.
79 */ 89 */
80 search: function(isRecent) { 90 search: function(isRecent) {
81 var params = {}; 91 var params = {};
82 if (isRecent) { 92 if (isRecent) {
83 params['q'] = '^recent'; 93 params['q'] = '^recent';
84 } 94 }
85 params['connection_status'] = 'ALL'; 95 params['connection_status'] = 'ALL';
86 params['client'] = 'chrome'; 96 params['client'] = 'chrome';
87 this.sendRequest_('GET', 'search', params, null, 97 this.sendRequest_('GET', 'search', params,
88 this.onSearchDone_.bind(this, isRecent)); 98 this.onSearchDone_.bind(this, isRecent));
89 }, 99 },
90 100
91 /** 101 /**
92 * Sends a Google Cloud Print submit API request. 102 * Sends a Google Cloud Print submit API request.
93 * @param {string} body Body of the HTTP post request to send. 103 * @param {!print_preview.Destination} destination Cloud destination to
104 * print to.
105 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the
106 * print ticket to print.
107 * @param {string} data Base64 encoded data of the document.
94 */ 108 */
95 submit: function(body) { 109 submit: function(destination, printTicketStore, data) {
96 this.sendRequest_('POST', 'submit', null, body, 110 var params = {
111 'printerid': destination.id,
112 'contentType': 'dataUrl',
113 'title': printTicketStore.getDocumentTitle(),
114 'capabilities': this.createPrintTicket_(destination, printTicketStore),
115 'content': 'data:application/pdf;base64,' + data,
116 };
117 this.sendRequest_('POST', 'submit', params,
97 this.onSubmitDone_.bind(this)); 118 this.onSubmitDone_.bind(this));
98 }, 119 },
99 120
100 /** 121 /**
101 * Sends a Google Cloud Print printer API request. 122 * Sends a Google Cloud Print printer API request.
102 * @param {string} printerId ID of the printer to lookup. 123 * @param {string} printerId ID of the printer to lookup.
103 */ 124 */
104 printer: function(printerId) { 125 printer: function(printerId) {
105 var params = {'printerid': printerId}; 126 var params = {'printerid': printerId};
106 this.sendRequest_('GET', 'printer', params, null, 127 this.sendRequest_('GET', 'printer', params,
107 this.onPrinterDone_.bind(this, printerId)); 128 this.onPrinterDone_.bind(this, printerId));
108 }, 129 },
109 130
110 /** 131 /**
111 * Sends a Google Cloud Print update API request to accept (or reject) the 132 * Sends a Google Cloud Print update API request to accept (or reject) the
112 * terms-of-service of the given printer. 133 * terms-of-service of the given printer.
113 * @param {string} printerId ID of the printer to accept the 134 * @param {string} printerId ID of the printer to accept the
114 * terms-of-service for. 135 * terms-of-service for.
115 * @param {boolean} isAccepted Whether the user accepted the 136 * @param {boolean} isAccepted Whether the user accepted the
116 * terms-of-service. 137 * terms-of-service.
117 */ 138 */
118 updatePrinterTosAcceptance: function(printerId, isAccepted) { 139 updatePrinterTosAcceptance: function(printerId, isAccepted) {
119 var params = { 140 var params = {
120 'printerid': printerId, 141 'printerid': printerId,
121 'is_tos_accepted': isAccepted 142 'is_tos_accepted': isAccepted
122 }; 143 };
123 this.sendRequest_('POST', 'update', params, null, 144 this.sendRequest_('POST', 'update', params,
124 this.onUpdatePrinterTosAcceptanceDone_.bind(this)); 145 this.onUpdatePrinterTosAcceptanceDone_.bind(this));
125 }, 146 },
126 147
127 /** 148 /**
128 * Creates an object that represents a Google Cloud Print print ticket. 149 * Creates an object that represents a Google Cloud Print print ticket.
129 * @param {!print_preview.Destination} destination Destination to print to. 150 * @param {!print_preview.Destination} destination Destination to print to.
130 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create 151 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create
131 * the state of the print ticket. 152 * the state of the print ticket.
132 * @return {!Object} Google Cloud Print print ticket. 153 * @return {!Object} Google Cloud Print print ticket.
154 * @private
133 */ 155 */
134 createPrintTicket: function(destination, printTicketStore) { 156 createPrintTicket_: function(destination, printTicketStore) {
135 assert(!destination.isLocal, 157 assert(!destination.isLocal,
136 'Trying to create a Google Cloud Print print ticket for a local ' + 158 'Trying to create a Google Cloud Print print ticket for a local ' +
137 'destination'); 159 'destination');
138 assert(destination.capabilities, 160 assert(destination.capabilities,
139 'Trying to create a Google Cloud Print print ticket for a ' + 161 'Trying to create a Google Cloud Print print ticket for a ' +
140 'destination with no print capabilities'); 162 'destination with no print capabilities');
141 163
142 var ticketItems = []; 164 var ticketItems = [];
143 165
144 if (destination.capabilities.collateCapability) { 166 if (destination.capabilities.collateCapability) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 return { 209 return {
188 'capabilities': ticketItems 210 'capabilities': ticketItems
189 }; 211 };
190 }, 212 },
191 213
192 /** 214 /**
193 * Sends a request to the Google Cloud Print API. 215 * Sends a request to the Google Cloud Print API.
194 * @param {string} method HTTP method of the request. 216 * @param {string} method HTTP method of the request.
195 * @param {string} action Google Cloud Print action to perform. 217 * @param {string} action Google Cloud Print action to perform.
196 * @param {Object} params HTTP parameters to include in the request. 218 * @param {Object} params HTTP parameters to include in the request.
197 * @param {string} body HTTP multi-part encoded body.
198 * @param {function(number, Object)} callback Callback to invoke when 219 * @param {function(number, Object)} callback Callback to invoke when
199 * request completes. 220 * request completes.
200 */ 221 */
201 sendRequest_: function(method, action, params, body, callback) { 222 sendRequest_: function(method, action, params, callback) {
202 if (!this.xsrfToken_) { 223 if (!this.xsrfToken_) {
203 // TODO(rltoscano): Should throw an error if not a read-only action or 224 // TODO(rltoscano): Should throw an error if not a read-only action or
204 // issue an xsrf token request. 225 // issue an xsrf token request.
205 } 226 }
206 var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_; 227 var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_;
228 var body = null;
207 229
208 if (params) { 230 if (params) {
209 for (var paramName in params) { 231 if (method == 'GET') {
210 url += '&' + paramName + '=' + encodeURIComponent(params[paramName]); 232 for (var paramName in params) {
233 url += '&' + paramName + '=' +
234 encodeURIComponent(params[paramName]);
235 }
236 } else {
237 body = '--' + CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n';
238 for (var paramName in params) {
239 body += 'Content-Disposition: form-data; name=\"' + paramName +
240 '\"\r\n\r\n' + params[paramName] + '\r\n--' +
241 CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n';
242 }
211 } 243 }
212 } 244 }
213 245
214 var headers = {}; 246 var headers = {};
215 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; 247 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview';
216 if (method == 'GET') { 248 if (method == 'GET') {
217 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; 249 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_;
218 } else if (method == 'POST') { 250 } else if (method == 'POST') {
219 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; 251 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_;
220 } 252 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 this.dispatchEvent(errorEvent); 396 this.dispatchEvent(errorEvent);
365 } 397 }
366 } 398 }
367 }; 399 };
368 400
369 // Export 401 // Export
370 return { 402 return {
371 CloudPrintInterface: CloudPrintInterface 403 CloudPrintInterface: CloudPrintInterface
372 }; 404 };
373 }); 405 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/print_ticket_store.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698