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 18 matching lines...) Expand all Loading... |
29 this.xsrfToken_ = ''; | 29 this.xsrfToken_ = ''; |
30 }; | 30 }; |
31 | 31 |
32 /** | 32 /** |
33 * Event types dispatched by the interface. | 33 * Event types dispatched by the interface. |
34 * @enum {string} | 34 * @enum {string} |
35 */ | 35 */ |
36 CloudPrintInterface.EventType = { | 36 CloudPrintInterface.EventType = { |
37 ERROR: 'cloudprint.CloudPrintInterface.ERROR', | 37 ERROR: 'cloudprint.CloudPrintInterface.ERROR', |
38 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | 38 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', |
| 39 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', |
39 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | 40 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', |
40 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE' | 41 SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE' |
41 }; | 42 }; |
42 | 43 |
43 /** | 44 /** |
44 * Content type header value for a URL encoded HTTP request. | 45 * Content type header value for a URL encoded HTTP request. |
45 * @type {string} | 46 * @type {string} |
46 * @private | 47 * @private |
47 */ | 48 */ |
48 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = | 49 CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ = |
(...skipping 24 matching lines...) Expand all Loading... |
73 * @param {boolean} isRecent Whether to search for only recently used | 74 * @param {boolean} isRecent Whether to search for only recently used |
74 * printers. | 75 * printers. |
75 */ | 76 */ |
76 search: function(isRecent) { | 77 search: function(isRecent) { |
77 var params = {}; | 78 var params = {}; |
78 if (isRecent) { | 79 if (isRecent) { |
79 params['q'] = '^recent'; | 80 params['q'] = '^recent'; |
80 } | 81 } |
81 params['connection_status'] = 'ALL'; | 82 params['connection_status'] = 'ALL'; |
82 params['client'] = 'chrome'; | 83 params['client'] = 'chrome'; |
83 this.sendRequest_('GET', 'search', params, null, this.onSearchDone_); | 84 this.sendRequest_( |
| 85 'GET', 'search', params, null, this.onSearchDone_.bind(this)); |
84 }, | 86 }, |
85 | 87 |
86 /** | 88 /** |
87 * Sends a Google Cloud Print submit API request. | 89 * Sends a Google Cloud Print submit API request. |
88 * @param {string} body Body of the HTTP post request to send. | 90 * @param {string} body Body of the HTTP post request to send. |
89 */ | 91 */ |
90 submit: function(body) { | 92 submit: function(body) { |
91 this.sendRequest_('POST', 'submit', null, body, this.onSubmitDone_); | 93 this.sendRequest_( |
| 94 'POST', 'submit', null, body, this.onSubmitDone_.bind(this)); |
92 }, | 95 }, |
93 | 96 |
94 /** | 97 /** |
95 * Sends a Google Cloud Print printer API request. | 98 * Sends a Google Cloud Print printer API request. |
96 * @param {string} printerId ID of the printer to lookup. | 99 * @param {string} printerId ID of the printer to lookup. |
97 */ | 100 */ |
98 printer: function(printerId) { | 101 printer: function(printerId) { |
99 var params = {'printerid': printerId}; | 102 var params = {'printerid': printerId}; |
100 this.sendRequest_('GET', 'printer', params, null, this.onPrinterDone_); | 103 this.sendRequest_( |
| 104 'GET', 'printer', params, null, |
| 105 this.onPrinterDone_.bind(this), |
| 106 this.onPrinterFailed_.bind(this, printerId)); |
101 }, | 107 }, |
102 | 108 |
103 /** | 109 /** |
104 * Sends a Google Cloud Print update API request to accept (or reject) the | 110 * Sends a Google Cloud Print update API request to accept (or reject) the |
105 * terms-of-service of the given printer. | 111 * terms-of-service of the given printer. |
106 * @param {string} printerId ID of the printer to accept the | 112 * @param {string} printerId ID of the printer to accept the |
107 * terms-of-service for. | 113 * terms-of-service for. |
108 * @param {boolean} isAccepted Whether the user accepted the | 114 * @param {boolean} isAccepted Whether the user accepted the |
109 * terms-of-service. | 115 * terms-of-service. |
110 */ | 116 */ |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 }, | 189 }, |
184 | 190 |
185 /** | 191 /** |
186 * Sends a request to the Google Cloud Print API. | 192 * Sends a request to the Google Cloud Print API. |
187 * @param {string} method HTTP method of the request. | 193 * @param {string} method HTTP method of the request. |
188 * @param {string} action Google Cloud Print action to perform. | 194 * @param {string} action Google Cloud Print action to perform. |
189 * @param {Object} params HTTP parameters to include in the request. | 195 * @param {Object} params HTTP parameters to include in the request. |
190 * @param {string} body HTTP multi-part encoded body. | 196 * @param {string} body HTTP multi-part encoded body. |
191 * @param {function(Object)} successCallback Callback to invoke when request | 197 * @param {function(Object)} successCallback Callback to invoke when request |
192 * completes successfully. | 198 * completes successfully. |
| 199 * @param {function(Object)=} opt_failureCallback Callback to call if the |
| 200 * request failed. |
193 */ | 201 */ |
194 sendRequest_: function(method, action, params, body, successCallback) { | 202 sendRequest_: function(method, action, params, body, successCallback, |
| 203 opt_failureCallback) { |
195 if (!this.xsrfToken_) { | 204 if (!this.xsrfToken_) { |
196 // TODO(rltoscano): Should throw an error if not a read-only action or | 205 // TODO(rltoscano): Should throw an error if not a read-only action or |
197 // issue an xsrf token request. | 206 // issue an xsrf token request. |
198 } | 207 } |
199 var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_; | 208 var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_; |
200 | 209 |
201 if (params) { | 210 if (params) { |
202 for (var paramName in params) { | 211 for (var paramName in params) { |
203 url += '&' + paramName + '=' + encodeURIComponent(params[paramName]); | 212 url += '&' + paramName + '=' + encodeURIComponent(params[paramName]); |
204 } | 213 } |
205 } | 214 } |
206 | 215 |
207 var headers = {}; | 216 var headers = {}; |
208 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; | 217 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
209 if (method == 'GET') { | 218 if (method == 'GET') { |
210 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; | 219 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
211 } else if (method == 'POST') { | 220 } else if (method == 'POST') { |
212 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; | 221 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; |
213 } | 222 } |
214 | 223 |
215 var xhr = new XMLHttpRequest(); | 224 var xhr = new XMLHttpRequest(); |
216 xhr.onreadystatechange = this.onReadyStateChange_.bind( | 225 xhr.onreadystatechange = this.onReadyStateChange_.bind( |
217 this, xhr, successCallback.bind(this)); | 226 this, xhr, successCallback, opt_failureCallback); |
218 xhr.open(method, url, true); | 227 xhr.open(method, url, true); |
219 xhr.withCredentials = true; | 228 xhr.withCredentials = true; |
220 for (var header in headers) { | 229 for (var header in headers) { |
221 xhr.setRequestHeader(header, headers[header]); | 230 xhr.setRequestHeader(header, headers[header]); |
222 } | 231 } |
223 xhr.send(body); | 232 xhr.send(body); |
224 }, | 233 }, |
225 | 234 |
226 /** | 235 /** |
227 * Dispatches an ERROR event with the given error message. | 236 * Dispatches an ERROR event with the given error message. |
228 * @param {string} message Error message to include in the ERROR event. | 237 * @param {string} message Error message to include in the ERROR event. |
229 * @private | 238 * @private |
230 */ | 239 */ |
231 dispatchErrorEvent_: function(message) { | 240 dispatchErrorEvent_: function(message) { |
232 var errorEvent = new cr.Event(CloudPrintInterface.EventType.ERROR); | 241 var errorEvent = new cr.Event(CloudPrintInterface.EventType.ERROR); |
233 errorEvent.message = message; | 242 errorEvent.message = message; |
234 this.dispatchEvent(errorEvent); | 243 this.dispatchEvent(errorEvent); |
235 }, | 244 }, |
236 | 245 |
237 /** | 246 /** |
238 * Called when the ready-state of a XML http request changes. | 247 * Called when the ready-state of a XML http request changes. |
239 * Calls the successCallback with the result or dispatches an ERROR event. | 248 * Calls the successCallback with the result or dispatches an ERROR event. |
240 * @param {XMLHttpRequest} xhr XML http request that changed. | 249 * @param {XMLHttpRequest} xhr XML http request that changed. |
241 * @param {function(Object)} successCallback Callback to call if the request | 250 * @param {function(Object)} successCallback Callback to call if the request |
242 * was successful. | 251 * was successful. |
| 252 * @param {function(Object)=} opt_failureCallback Callback to call if the |
| 253 * request failed. |
243 * @private | 254 * @private |
244 */ | 255 */ |
245 onReadyStateChange_: function(xhr, successCallback) { | 256 onReadyStateChange_: function(xhr, successCallback, opt_failureCallback) { |
246 if (xhr.readyState == 4) { | 257 if (xhr.readyState == 4) { |
247 if (xhr.status == 200) { | 258 if (xhr.status == 200) { |
248 var result = JSON.parse(xhr.responseText); | 259 var result = JSON.parse(xhr.responseText); |
249 if (result['success']) { | 260 if (result['success']) { |
250 this.xsrfToken_ = result['xsrf_token']; | 261 this.xsrfToken_ = result['xsrf_token']; |
251 successCallback(result); | 262 successCallback(result); |
| 263 } else if (opt_failureCallback) { |
| 264 opt_failureCallback(result); |
252 } else { | 265 } else { |
253 this.dispatchErrorEvent_(result['message']); | 266 this.dispatchErrorEvent_(result['message']); |
254 } | 267 } |
255 } else { | 268 } else { |
256 this.dispatchErrorEvent_(xhr.status + ''); | 269 this.dispatchErrorEvent_(xhr.status + ''); |
257 } | 270 } |
258 } | 271 } |
259 }, | 272 }, |
260 | 273 |
261 /** | 274 /** |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 JSON.stringify(printerJson)); | 324 JSON.stringify(printerJson)); |
312 return; | 325 return; |
313 } | 326 } |
314 var printerDoneEvent = | 327 var printerDoneEvent = |
315 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); | 328 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); |
316 printerDoneEvent.printer = printer; | 329 printerDoneEvent.printer = printer; |
317 this.dispatchEvent(printerDoneEvent); | 330 this.dispatchEvent(printerDoneEvent); |
318 }, | 331 }, |
319 | 332 |
320 /** | 333 /** |
| 334 * Called when the printer request fails. |
| 335 * @param {string} ID of the destination that failed to be looked up. |
| 336 * @param {object} Contains the JSON response. |
| 337 * @private |
| 338 */ |
| 339 onPrinterFailed_: function(destinationId, result) { |
| 340 var printerFailedEvent = new cr.Event( |
| 341 CloudPrintInterface.EventType.PRINTER_FAILED); |
| 342 printerFailedEvent.destinationId = destinationId; |
| 343 printerFailedEvent.errorCode = result['errorCode']; |
| 344 this.dispatchEvent(printerFailedEvent); |
| 345 }, |
| 346 |
| 347 /** |
321 * Called when the update printer TOS acceptance request completes | 348 * Called when the update printer TOS acceptance request completes |
322 * successfully. | 349 * successfully. |
323 * @param {Object} result JSON response. | 350 * @param {Object} result JSON response. |
324 * @private | 351 * @private |
325 */ | 352 */ |
326 onUpdatePrinterTosAcceptanceDone_: function(result) { | 353 onUpdatePrinterTosAcceptanceDone_: function(result) { |
327 // Do nothing. | 354 // Do nothing. |
328 } | 355 } |
329 }; | 356 }; |
330 | 357 |
331 // Export | 358 // Export |
332 return { | 359 return { |
333 CloudPrintInterface: CloudPrintInterface | 360 CloudPrintInterface: CloudPrintInterface |
334 }; | 361 }; |
335 }); | 362 }); |
OLD | NEW |