Chromium Code Reviews| 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 |
| 11 * with no trailing slash. For example, | 11 * with no trailing slash. For example, |
| 12 * 'https://www.google.com/cloudprint'. | 12 * 'https://www.google.com/cloudprint'. |
| 13 * @param {!print_preview.NativeLayer} nativeLayer Native layer used to get | |
| 14 * Auth2 tokens. | |
| 13 * @constructor | 15 * @constructor |
| 14 * @extends {cr.EventTarget} | 16 * @extends {cr.EventTarget} |
| 15 */ | 17 */ |
| 16 function CloudPrintInterface(baseUrl) { | 18 function CloudPrintInterface(baseUrl, nativeLayer) { |
| 17 /** | 19 /** |
| 18 * The base URL of the Google Cloud Print API. | 20 * The base URL of the Google Cloud Print API. |
| 19 * @type {string} | 21 * @type {string} |
| 20 * @private | 22 * @private |
| 21 */ | 23 */ |
| 22 this.baseUrl_ = baseUrl; | 24 this.baseUrl_ = baseUrl; |
| 23 | 25 |
| 24 /** | 26 /** |
| 27 * Used to get Auth2 tokens. | |
| 28 * @type {!print_preview.NativeLayer} | |
| 29 * @private | |
| 30 */ | |
| 31 this.nativeLayer_ = nativeLayer; | |
| 32 | |
| 33 /** | |
| 25 * Last received XSRF token. Sent as a parameter in every request. | 34 * Last received XSRF token. Sent as a parameter in every request. |
| 26 * @type {string} | 35 * @type {string} |
| 27 * @private | 36 * @private |
| 28 */ | 37 */ |
| 29 this.xsrfToken_ = ''; | 38 this.xsrfToken_ = ''; |
| 30 | 39 |
| 31 /** | 40 /** |
| 41 * Pending requests delayed until we get access token. | |
| 42 * @type {!Array<objects>} | |
|
Toscano
2013/04/19 17:04:33
The type looks like: {!Array.<Object>}
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Actually i have type so * @type {!Array<!ClourPrin
| |
| 43 * @private | |
| 44 */ | |
| 45 this.requestQueue_ = []; | |
| 46 | |
| 47 /** | |
| 32 * Number of outstanding cloud destination search requests. | 48 * Number of outstanding cloud destination search requests. |
| 33 * @type {number} | 49 * @type {number} |
| 34 * @private | 50 * @private |
| 35 */ | 51 */ |
| 36 this.outstandingCloudSearchRequestCount_ = 0; | 52 this.outstandingCloudSearchRequestCount_ = 0; |
| 53 | |
| 54 /** | |
| 55 * Event tracker used to keep track of native layer events. | |
| 56 * @type {!EventTracker} | |
| 57 * @private | |
| 58 */ | |
| 59 this.tracker_ = new EventTracker(); | |
| 60 | |
| 61 this.addEventListeners_(); | |
| 37 }; | 62 }; |
| 38 | 63 |
| 39 /** | 64 /** |
| 40 * Event types dispatched by the interface. | 65 * Event types dispatched by the interface. |
| 41 * @enum {string} | 66 * @enum {string} |
| 42 */ | 67 */ |
| 43 CloudPrintInterface.EventType = { | 68 CloudPrintInterface.EventType = { |
| 44 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', | 69 PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE', |
| 45 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', | 70 PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED', |
| 46 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', | 71 SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE', |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 | 115 |
| 91 /** | 116 /** |
| 92 * Enumeration of JSON response fields from Google Cloud Print API. | 117 * Enumeration of JSON response fields from Google Cloud Print API. |
| 93 * @enum {string} | 118 * @enum {string} |
| 94 * @private | 119 * @private |
| 95 */ | 120 */ |
| 96 CloudPrintInterface.JsonFields_ = { | 121 CloudPrintInterface.JsonFields_ = { |
| 97 PRINTER: 'printer' | 122 PRINTER: 'printer' |
| 98 }; | 123 }; |
| 99 | 124 |
| 125 /** | |
| 126 * Could Print origins used to search printers. | |
| 127 * @type {!Array<!print_preview.Destination.Origin>} | |
|
Toscano
2013/04/19 17:04:33
Missing the "." between "Array" and "<!...":
{!Ar
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 128 * @const | |
| 129 * @private | |
| 130 */ | |
| 131 CloudPrintInterface.CLOUD_ORIGINS_ = [ | |
| 132 print_preview.Destination.Origin.COOKIES, | |
| 133 print_preview.Destination.Origin.DEVICE | |
| 134 // TODO(vitalybuka): Enable when implemented. | |
| 135 // ready print_preview.Destination.Origin.PROFILE | |
| 136 ]; | |
| 137 | |
| 100 CloudPrintInterface.prototype = { | 138 CloudPrintInterface.prototype = { |
| 101 __proto__: cr.EventTarget.prototype, | 139 __proto__: cr.EventTarget.prototype, |
| 102 | 140 |
| 103 /** @return {string} Base URL of the Google Cloud Print service. */ | 141 /** @return {string} Base URL of the Google Cloud Print service. */ |
| 104 get baseUrl() { | 142 get baseUrl() { |
| 105 return this.baseUrl_; | 143 return this.baseUrl_; |
| 106 }, | 144 }, |
| 107 | 145 |
| 108 /** | 146 /** |
| 109 * @return {boolean} Whether a search for cloud destinations is in progress. | 147 * @return {boolean} Whether a search for cloud destinations is in progress. |
| 110 */ | 148 */ |
| 111 get isCloudDestinationSearchInProgress() { | 149 get isCloudDestinationSearchInProgress() { |
| 112 return this.outstandingCloudSearchRequestCount_ > 0; | 150 return this.outstandingCloudSearchRequestCount_ > 0; |
| 113 }, | 151 }, |
| 114 | 152 |
| 115 /** | 153 /** |
| 116 * Sends a Google Cloud Print search API request. | 154 * Sends a Google Cloud Print search API request. |
| 117 * @param {boolean} isRecent Whether to search for only recently used | 155 * @param {boolean} isRecent Whether to search for only recently used |
| 118 * printers. | 156 * printers. |
| 119 */ | 157 */ |
| 120 search: function(isRecent) { | 158 search: function(isRecent) { |
| 121 var params = [ | 159 var params = [ |
| 122 new HttpParam('connection_status', 'ALL'), | 160 new HttpParam('connection_status', 'ALL'), |
| 123 new HttpParam('client', 'chrome'), | 161 new HttpParam('client', 'chrome'), |
| 124 new HttpParam('use_cdd', 'true') | 162 new HttpParam('use_cdd', 'true') |
| 125 ]; | 163 ]; |
| 126 if (isRecent) { | 164 if (isRecent) { |
| 127 params.push(new HttpParam('q', '^recent')); | 165 params.push(new HttpParam('q', '^recent')); |
| 128 } | 166 } |
| 129 ++this.outstandingCloudSearchRequestCount_; | 167 CloudPrintInterface.CLOUD_ORIGINS_.forEach(function(origin) { |
| 130 this.sendRequest_('GET', 'search', params, | 168 ++this.outstandingCloudSearchRequestCount_; |
| 131 this.onSearchDone_.bind(this, isRecent)); | 169 var cpRequest = this.buildRequest_('GET', 'search', params, origin); |
| 170 this.sendOrQueueRequest_(cpRequest, | |
| 171 this.onSearchDone_.bind(this, isRecent)); | |
| 172 }, this); | |
| 132 }, | 173 }, |
| 133 | 174 |
| 134 /** | 175 /** |
| 135 * Sends a Google Cloud Print submit API request. | 176 * Sends a Google Cloud Print submit API request. |
| 136 * @param {!print_preview.Destination} destination Cloud destination to | 177 * @param {!print_preview.Destination} destination Cloud destination to |
| 137 * print to. | 178 * print to. |
| 138 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the | 179 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the |
| 139 * print ticket to print. | 180 * print ticket to print. |
| 140 * @param {string} data Base64 encoded data of the document. | 181 * @param {string} data Base64 encoded data of the document. |
| 141 */ | 182 */ |
| 142 submit: function(destination, printTicketStore, data) { | 183 submit: function(destination, printTicketStore, data) { |
| 143 var result = | 184 var result = |
| 144 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); | 185 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); |
| 145 var chromeVersion = 'unknown'; | 186 var chromeVersion = 'unknown'; |
| 146 if (result && result.length == 2) { | 187 if (result && result.length == 2) { |
| 147 chromeVersion = result[1]; | 188 chromeVersion = result[1]; |
| 148 } | 189 } |
| 149 var params = [ | 190 var params = [ |
| 150 new HttpParam('printerid', destination.id), | 191 new HttpParam('printerid', destination.id), |
| 151 new HttpParam('contentType', 'dataUrl'), | 192 new HttpParam('contentType', 'dataUrl'), |
| 152 new HttpParam('title', printTicketStore.getDocumentTitle()), | 193 new HttpParam('title', printTicketStore.getDocumentTitle()), |
| 153 new HttpParam('ticket', | 194 new HttpParam('ticket', |
| 154 this.createPrintTicket_(destination, printTicketStore)), | 195 this.createPrintTicket_(destination, printTicketStore)), |
| 155 new HttpParam('content', 'data:application/pdf;base64,' + data), | 196 new HttpParam('content', 'data:application/pdf;base64,' + data), |
| 156 new HttpParam('tag', | 197 new HttpParam('tag', |
| 157 '__google__chrome_version=' + chromeVersion), | 198 '__google__chrome_version=' + chromeVersion), |
| 158 new HttpParam('tag', '__google__os=' + navigator.platform) | 199 new HttpParam('tag', '__google__os=' + navigator.platform) |
| 159 ]; | 200 ]; |
| 160 this.sendRequest_('POST', 'submit', params, | 201 var cpRequest = this.buildRequest_('POST', 'submit', params, |
| 161 this.onSubmitDone_.bind(this)); | 202 destination.origin); |
| 203 this.sendOrQueueRequest_(cpRequest, this.onSubmitDone_.bind(this)); | |
| 162 }, | 204 }, |
| 163 | 205 |
| 164 /** | 206 /** |
| 165 * Sends a Google Cloud Print printer API request. | 207 * Sends a Google Cloud Print printer API request. |
| 166 * @param {string} printerId ID of the printer to lookup. | 208 * @param {string} printerId ID of the printer to lookup. |
| 209 * @param {!print_preview.Destination.Origin} origin Origin of the printer. | |
| 167 */ | 210 */ |
| 168 printer: function(printerId) { | 211 printer: function(printerId, origin) { |
| 169 var params = [ | 212 var params = [ |
| 170 new HttpParam('printerid', printerId), | 213 new HttpParam('printerid', printerId), |
| 171 new HttpParam('use_cdd', 'true') | 214 new HttpParam('use_cdd', 'true') |
| 172 ]; | 215 ]; |
| 173 this.sendRequest_('GET', 'printer', params, | 216 var cpRequest = this.buildRequest_('GET', 'printer', params, origin); |
| 174 this.onPrinterDone_.bind(this, printerId)); | 217 this.sendOrQueueRequest_(cpRequest, |
| 218 this.onPrinterDone_.bind(this, printerId)); | |
| 175 }, | 219 }, |
| 176 | 220 |
| 177 /** | 221 /** |
| 178 * Sends a Google Cloud Print update API request to accept (or reject) the | 222 * Sends a Google Cloud Print update API request to accept (or reject) the |
| 179 * terms-of-service of the given printer. | 223 * terms-of-service of the given printer. |
| 180 * @param {string} printerId ID of the printer to accept the | 224 * @param {string} printerId ID of the printer to accept the |
| 181 * terms-of-service for. | 225 * terms-of-service for. |
| 226 * @param {!print_preview.Destination.Origin} origin Origin of the printer. | |
| 182 * @param {boolean} isAccepted Whether the user accepted the | 227 * @param {boolean} isAccepted Whether the user accepted the |
| 183 * terms-of-service. | 228 * terms-of-service. |
| 184 */ | 229 */ |
| 185 updatePrinterTosAcceptance: function(printerId, isAccepted) { | 230 updatePrinterTosAcceptance: function(printerId, origin, isAccepted) { |
| 186 var params = [ | 231 var params = [ |
| 187 new HttpParam('printerid', printerId), | 232 new HttpParam('printerid', printerId), |
| 188 new HttpParam('is_tos_accepted', isAccepted) | 233 new HttpParam('is_tos_accepted', isAccepted) |
| 189 ]; | 234 ]; |
| 190 this.sendRequest_('POST', 'update', params, | 235 var cpRequest = this.buildRequest_('POST', 'update', params, origin); |
| 191 this.onUpdatePrinterTosAcceptanceDone_.bind(this)); | 236 this.sendOrQueueRequest_(cpRequest, |
| 237 this.onUpdateTosAcceptanceDone_.bind(this)); | |
|
Toscano
2013/04/19 17:04:33
Why did you change the name of the callback? Now i
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 192 }, | 238 }, |
| 193 | 239 |
| 194 /** | 240 /** |
| 241 * Adds event listeners to the relevant native layer events. | |
| 242 * @private | |
| 243 */ | |
| 244 addEventListeners_: function() { | |
| 245 this.tracker_.add( | |
| 246 this.nativeLayer_, | |
| 247 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | |
| 248 this.onAccessTokenReady_.bind(this)); | |
| 249 }, | |
| 250 | |
| 251 /** | |
| 252 * Called when a native layer receives access token. | |
| 253 * @param {cr.Event} evt Contains the authetication type and access token. | |
| 254 * @private | |
| 255 */ | |
| 256 onAccessTokenReady_: function(event) { | |
|
Toscano
2013/04/19 17:04:33
private handlers should come after all other priva
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 257 assert(event.authType == print_preview.Destination.Origin.DEVICE); | |
| 258 this.requestQueue_ = this.requestQueue_.filter(function(request) { | |
| 259 assert(request.origin == print_preview.Destination.Origin.DEVICE); | |
|
Toscano
2013/04/19 17:04:33
Can you add a TODO to remove this assert once we s
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 260 if (request.origin != event.authType) | |
|
Toscano
2013/04/19 17:04:33
Please enclose "if" body with {}
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 261 return true; | |
| 262 if (event.accessToken) { | |
| 263 request.xhr.setRequestHeader('Authorization', | |
| 264 'Bearer ' + event.accessToken); | |
| 265 this.sendRequest_(request); | |
| 266 } else { | |
| 267 // No valid token. | |
| 268 request.callback(401, { | |
|
Toscano
2013/04/19 17:04:33
Should be 403. With a string as the second paramet
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
403 cause sign in promo
| |
| 269 'errorCode' : -1, | |
| 270 'message' : 'No access token.' | |
| 271 }, request.origin); | |
| 272 } | |
| 273 return false; | |
| 274 }, this); | |
| 275 }, | |
| 276 | |
| 277 /** | |
| 195 * Creates an object that represents a Google Cloud Print print ticket. | 278 * Creates an object that represents a Google Cloud Print print ticket. |
| 196 * @param {!print_preview.Destination} destination Destination to print to. | 279 * @param {!print_preview.Destination} destination Destination to print to. |
| 197 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create | 280 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create |
| 198 * the state of the print ticket. | 281 * the state of the print ticket. |
| 199 * @return {!Object} Google Cloud Print print ticket. | 282 * @return {!Object} Google Cloud Print print ticket. |
| 200 * @private | 283 * @private |
| 201 */ | 284 */ |
| 202 createPrintTicket_: function(destination, printTicketStore) { | 285 createPrintTicket_: function(destination, printTicketStore) { |
| 203 assert(!destination.isLocal, | 286 assert(!destination.isLocal, |
| 204 'Trying to create a Google Cloud Print print ticket for a local ' + | 287 'Trying to create a Google Cloud Print print ticket for a local ' + |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 {type: pts.isDuplexEnabled() ? 'LONG_EDGE' : 'NO_DUPLEX'}; | 322 {type: pts.isDuplexEnabled() ? 'LONG_EDGE' : 'NO_DUPLEX'}; |
| 240 } | 323 } |
| 241 if (pts.hasOrientationCapability()) { | 324 if (pts.hasOrientationCapability()) { |
| 242 cjt.print.page_orientation = | 325 cjt.print.page_orientation = |
| 243 {type: pts.isLandscapeEnabled() ? 'LANDSCAPE' : 'PORTRAIT'}; | 326 {type: pts.isLandscapeEnabled() ? 'LANDSCAPE' : 'PORTRAIT'}; |
| 244 } | 327 } |
| 245 return JSON.stringify(cjt); | 328 return JSON.stringify(cjt); |
| 246 }, | 329 }, |
| 247 | 330 |
| 248 /** | 331 /** |
| 249 * Sends a request to the Google Cloud Print API. | 332 * Builds request to the Google Cloud Print API. |
| 250 * @param {string} method HTTP method of the request. | 333 * @param {string} method HTTP method of the request. |
| 251 * @param {string} action Google Cloud Print action to perform. | 334 * @param {string} action Google Cloud Print action to perform. |
| 252 * @param {Array.<!HttpParam>} params HTTP parameters to include in the | 335 * @param {Array.<!HttpParam>} params HTTP parameters to include in the |
| 253 * request. | 336 * request. |
| 254 * @param {function(number, Object)} callback Callback to invoke when | 337 * @param {!print_preview.Destination.Origin} origin Origin for destination. |
| 255 * request completes. | 338 * @private |
|
Toscano
2013/04/19 17:04:33
Where is the @return statement?
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 256 */ | 339 */ |
| 257 sendRequest_: function(method, action, params, callback) { | 340 buildRequest_: function(method, action, params, origin) { |
| 258 if (!this.xsrfToken_) { | 341 var url = this.baseUrl_ + '/' + action + '?xsrf='; |
|
Toscano
2013/04/19 17:04:33
You add "xsrf=" to the url, but you only provide a
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
that's how search request worked before, the first
Toscano
2013/04/19 22:32:33
Ok, not a big deal. As long as it works.
On 2013/
| |
| 259 // TODO(rltoscano): Should throw an error if not a read-only action or | 342 if (origin == print_preview.Destination.Origin.COOKIES) { |
| 260 // issue an xsrf token request. | 343 if (!this.xsrfToken_) { |
| 344 // TODO(rltoscano): Should throw an error if not a read-only action or | |
| 345 // issue an xsrf token request. | |
| 346 } else { | |
| 347 url = url + this.xsrfToken_; | |
| 348 } | |
| 261 } | 349 } |
| 262 var url = this.baseUrl_ + '/' + action + '?xsrf=' + this.xsrfToken_; | |
| 263 var body = null; | 350 var body = null; |
| 264 | |
| 265 if (params) { | 351 if (params) { |
| 266 if (method == 'GET') { | 352 if (method == 'GET') { |
| 267 url = params.reduce(function(partialUrl, param) { | 353 url = params.reduce(function(partialUrl, param) { |
| 268 return partialUrl + '&' + param.name + '=' + | 354 return partialUrl + '&' + param.name + '=' + |
| 269 encodeURIComponent(param.value); | 355 encodeURIComponent(param.value); |
| 270 }, url); | 356 }, url); |
| 271 } else if (method == 'POST') { | 357 } else if (method == 'POST') { |
| 272 body = params.reduce(function(partialBody, param) { | 358 body = params.reduce(function(partialBody, param) { |
| 273 return partialBody + 'Content-Disposition: form-data; name=\"' + | 359 return partialBody + 'Content-Disposition: form-data; name=\"' + |
| 274 param.name + '\"\r\n\r\n' + param.value + '\r\n--' + | 360 param.name + '\"\r\n\r\n' + param.value + '\r\n--' + |
| 275 CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'; | 361 CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'; |
| 276 }, '--' + CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'); | 362 }, '--' + CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'); |
| 277 } | 363 } |
| 278 } | 364 } |
| 279 | 365 |
| 280 var headers = {}; | 366 var headers = {}; |
| 281 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; | 367 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
| 282 if (method == 'GET') { | 368 if (method == 'GET') { |
| 283 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; | 369 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
| 284 } else if (method == 'POST') { | 370 } else if (method == 'POST') { |
| 285 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; | 371 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; |
| 286 } | 372 } |
| 287 | 373 |
| 288 var xhr = new XMLHttpRequest(); | 374 var xhr = new XMLHttpRequest(); |
| 289 xhr.onreadystatechange = | |
| 290 this.onReadyStateChange_.bind(this, xhr, callback); | |
| 291 xhr.open(method, url, true); | 375 xhr.open(method, url, true); |
| 292 xhr.withCredentials = true; | 376 xhr.withCredentials = |
| 377 (origin == print_preview.Destination.Origin.COOKIES); | |
| 293 for (var header in headers) { | 378 for (var header in headers) { |
| 294 xhr.setRequestHeader(header, headers[header]); | 379 xhr.setRequestHeader(header, headers[header]); |
| 295 } | 380 } |
| 296 xhr.send(body); | 381 |
| 382 return new ClourPrintRequest(xhr, body, origin); | |
|
Toscano
2013/04/19 17:04:33
Cloud*PrintRequest.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 297 }, | 383 }, |
| 298 | 384 |
| 299 /** | 385 /** |
| 386 * Sends a request to the Google Cloud Print API. Or queue if it needs to | |
|
Toscano
2013/04/19 17:04:33
This is one sentence. No need for a period before
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 387 * wait OAuth2 access token. | |
| 388 * @param {!CloudPrintRequest} request Request to send or queue. | |
| 389 * @param {function(number, Object, !print_preview.Destination.Origin)} | |
| 390 * callback Callback to invoke when request completes. | |
| 391 * @private | |
| 392 */ | |
| 393 sendOrQueueRequest_: function(request, callback) { | |
|
Toscano
2013/04/19 17:04:33
It actually looks like it makes more sense to incl
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 394 request.callback = callback; | |
|
Toscano
2013/04/19 17:04:33
The CloudPrintRequest does not have a callback fie
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 395 if (request.origin == print_preview.Destination.Origin.COOKIES) { | |
| 396 return this.sendRequest_(request); | |
| 397 } else { | |
| 398 this.requestQueue_.push(request); | |
| 399 this.nativeLayer_.startGetAccessToken(request.origin); | |
| 400 } | |
| 401 }, | |
| 402 | |
| 403 sendRequest_: function(request) { | |
|
Toscano
2013/04/19 17:04:33
Please add jsdoc for this method.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 404 request.xhr.onreadystatechange = | |
| 405 this.onReadyStateChange_.bind(this, request); | |
| 406 request.xhr.send(request.body); | |
| 407 }, | |
| 408 | |
| 409 /** | |
| 300 * Creates a Google Cloud Print interface error that is ready to dispatch. | 410 * Creates a Google Cloud Print interface error that is ready to dispatch. |
| 301 * @param {!CloudPrintInterface.EventType} type Type of the error. | 411 * @param {!CloudPrintInterface.EventType} type Type of the error. |
| 302 * @param {number} status HTTP status code of the failed request. | 412 * @param {number} status HTTP status code of the failed request. |
| 303 * @param {Object} result JSON response of the request. {@code null} if | 413 * @param {Object} result JSON response of the request. {@code null} if |
| 304 * status was not 200. | 414 * status was not 200. |
| 305 * @return {!cr.Event} Google Cloud Print interface error event. | 415 * @return {!cr.Event} Google Cloud Print interface error event. |
| 306 * @private | 416 * @private |
| 307 */ | 417 */ |
| 308 createErrorEvent_: function(type, status, result) { | 418 createErrorEvent_: function(type, status, result) { |
| 309 var errorEvent = new cr.Event(type); | 419 var errorEvent = new cr.Event(type); |
| 310 errorEvent.status = status; | 420 errorEvent.status = status; |
| 311 errorEvent.errorCode = status == 200 ? result['errorCode'] : 0; | 421 errorEvent.errorCode = status == 200 ? result['errorCode'] : 0; |
| 312 errorEvent.message = status == 200 ? result['message'] : ''; | 422 errorEvent.message = status == 200 ? result['message'] : ''; |
| 313 return errorEvent; | 423 return errorEvent; |
| 314 }, | 424 }, |
| 315 | 425 |
| 316 /** | 426 /** |
| 317 * Called when the ready-state of a XML http request changes. | 427 * Called when the ready-state of a XML http request changes. |
| 318 * Calls the successCallback with the result or dispatches an ERROR event. | 428 * Calls the successCallback with the result or dispatches an ERROR event. |
| 319 * @param {XMLHttpRequest} xhr XML http request that changed. | 429 * @param {!CloudPrintRequest} request Request that changed. |
| 320 * @param {function(number, Object)} callback Callback to invoke when | |
| 321 * request completes. | |
| 322 * @private | 430 * @private |
| 323 */ | 431 */ |
| 324 onReadyStateChange_: function(xhr, callback) { | 432 onReadyStateChange_: function(request) { |
| 325 if (xhr.readyState == 4) { | 433 if (request.xhr.readyState == 4) { |
| 326 if (xhr.status == 200) { | 434 if (request.xhr.status == 200) { |
| 327 var result = JSON.parse(xhr.responseText); | 435 var result = JSON.parse(request.xhr.responseText); |
| 328 if (result['success']) { | 436 if (request.origin == print_preview.Destination.Origin.COOKIES && |
| 437 result['success']) { | |
| 329 this.xsrfToken_ = result['xsrf_token']; | 438 this.xsrfToken_ = result['xsrf_token']; |
| 330 } | 439 } |
| 331 } | 440 } |
| 332 callback(xhr.status, result); | 441 request.callback(request.xhr.status, result, request.origin); |
|
Toscano
2013/04/19 17:04:33
Let's just add a result field to CloudPrintRequest
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 333 } | 442 } |
| 334 }, | 443 }, |
| 335 | 444 |
| 336 /** | 445 /** |
| 337 * Called when the search request completes. | 446 * Called when the search request completes. |
| 338 * @param {boolean} isRecent Whether the search request was for recent | 447 * @param {boolean} isRecent Whether the search request was for recent |
| 339 * destinations. | 448 * destinations. |
| 340 * @param {number} status Status of the HTTP request. | 449 * @param {number} status Status of the HTTP request. |
| 341 * @param {Object} result JSON response. | 450 * @param {Object} result JSON response. |
| 451 * @param {!print_preview.Destination.Origin} origin Origin for destination. | |
| 342 * @private | 452 * @private |
| 343 */ | 453 */ |
| 344 onSearchDone_: function(isRecent, status, result) { | 454 onSearchDone_: function(isRecent, status, result, origin) { |
| 345 --this.outstandingCloudSearchRequestCount_; | 455 --this.outstandingCloudSearchRequestCount_; |
| 346 if (status == 200 && result['success']) { | 456 if (status == 200 && result['success']) { |
| 347 var printerListJson = result['printers'] || []; | 457 var printerListJson = result['printers'] || []; |
| 348 var printerList = []; | 458 var printerList = []; |
| 349 printerListJson.forEach(function(printerJson) { | 459 printerListJson.forEach(function(printerJson) { |
| 350 try { | 460 try { |
| 351 printerList.push( | 461 printerList.push( |
| 352 cloudprint.CloudDestinationParser.parse( | 462 cloudprint.CloudDestinationParser.parse(printerJson, origin)); |
| 353 printerJson, print_preview.Destination.Origin.COOKIES)); | |
| 354 } catch (err) { | 463 } catch (err) { |
| 355 console.error('Unable to parse cloud print destination: ' + err); | 464 console.error('Unable to parse cloud print destination: ' + err); |
| 356 } | 465 } |
| 357 }); | 466 }); |
| 358 var searchDoneEvent = | 467 var searchDoneEvent = |
| 359 new cr.Event(CloudPrintInterface.EventType.SEARCH_DONE); | 468 new cr.Event(CloudPrintInterface.EventType.SEARCH_DONE); |
| 360 searchDoneEvent.printers = printerList; | 469 searchDoneEvent.printers = printerList; |
| 470 searchDoneEvent.origin = origin; | |
| 361 searchDoneEvent.isRecent = isRecent; | 471 searchDoneEvent.isRecent = isRecent; |
| 362 searchDoneEvent.email = result['request']['user']; | 472 searchDoneEvent.email = result['request']['user']; |
| 363 this.dispatchEvent(searchDoneEvent); | 473 this.dispatchEvent(searchDoneEvent); |
| 364 } else { | 474 } else { |
| 365 var errorEvent = this.createErrorEvent_( | 475 var errorEvent = this.createErrorEvent_( |
| 366 CloudPrintInterface.EventType.SEARCH_FAILED, status, result); | 476 CloudPrintInterface.EventType.SEARCH_FAILED, status, result); |
| 367 this.dispatchEvent(errorEvent); | 477 this.dispatchEvent(errorEvent); |
| 368 } | 478 } |
| 369 }, | 479 }, |
| 370 | 480 |
| 371 /** | 481 /** |
| 372 * Called when the submit request completes. | 482 * Called when the submit request completes. |
| 373 * @param {number} status Status of the HTTP request. | 483 * @param {number} status Status of the HTTP request. |
| 374 * @param {Object} result JSON response. | 484 * @param {Object} result JSON response. |
| 485 * @param {!print_preview.Destination.Origin} origin Origin for destination. | |
| 375 * @private | 486 * @private |
| 376 */ | 487 */ |
| 377 onSubmitDone_: function(status, result) { | 488 onSubmitDone_: function(status, result, origin) { |
| 378 if (status == 200 && result['success']) { | 489 if (status == 200 && result['success']) { |
| 379 var submitDoneEvent = new cr.Event( | 490 var submitDoneEvent = new cr.Event( |
| 380 CloudPrintInterface.EventType.SUBMIT_DONE); | 491 CloudPrintInterface.EventType.SUBMIT_DONE); |
| 381 submitDoneEvent.jobId = result['job']['id']; | 492 submitDoneEvent.jobId = result['job']['id']; |
| 382 this.dispatchEvent(submitDoneEvent); | 493 this.dispatchEvent(submitDoneEvent); |
| 383 } else { | 494 } else { |
| 384 var errorEvent = this.createErrorEvent_( | 495 var errorEvent = this.createErrorEvent_( |
| 385 CloudPrintInterface.EventType.SUBMIT_FAILED, status, result); | 496 CloudPrintInterface.EventType.SUBMIT_FAILED, status, result); |
| 386 this.dispatchEvent(errorEvent); | 497 this.dispatchEvent(errorEvent); |
| 387 } | 498 } |
| 388 }, | 499 }, |
| 389 | 500 |
| 390 /** | 501 /** |
| 391 * Called when the printer request completes. | 502 * Called when the printer request completes. |
| 392 * @param {string} destinationId ID of the destination that was looked up. | 503 * @param {string} destinationId ID of the destination that was looked up. |
| 393 * @param {number} status Status of the HTTP request. | 504 * @param {number} status Status of the HTTP request. |
| 394 * @param {Object} result JSON response. | 505 * @param {Object} result JSON response. |
| 506 * @param {!print_preview.Destination.Origin} origin Origin for destination. | |
| 395 * @private | 507 * @private |
| 396 */ | 508 */ |
| 397 onPrinterDone_: function(destinationId, status, result) { | 509 onPrinterDone_: function(destinationId, status, result, origin) { |
| 398 if (status == 200 && result['success']) { | 510 if (status == 200 && result['success']) { |
| 399 var printerJson = result['printers'][0]; | 511 var printerJson = result['printers'][0]; |
| 400 var printer; | 512 var printer; |
| 401 try { | 513 try { |
| 402 printer = cloudprint.CloudDestinationParser.parse( | 514 printer = cloudprint.CloudDestinationParser.parse(printerJson, |
| 403 printerJson, print_preview.Destination.Origin.COOKIES); | 515 origin); |
| 404 } catch (err) { | 516 } catch (err) { |
| 405 console.error('Failed to parse cloud print destination: ' + | 517 console.error('Failed to parse cloud print destination: ' + |
| 406 JSON.stringify(printerJson)); | 518 JSON.stringify(printerJson)); |
| 407 return; | 519 return; |
| 408 } | 520 } |
| 409 var printerDoneEvent = | 521 var printerDoneEvent = |
| 410 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); | 522 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); |
| 411 printerDoneEvent.printer = printer; | 523 printerDoneEvent.printer = printer; |
| 412 this.dispatchEvent(printerDoneEvent); | 524 this.dispatchEvent(printerDoneEvent); |
| 413 } else { | 525 } else { |
| 414 var errorEvent = this.createErrorEvent_( | 526 var errorEvent = this.createErrorEvent_( |
| 415 CloudPrintInterface.EventType.PRINTER_FAILED, status, result); | 527 CloudPrintInterface.EventType.PRINTER_FAILED, status, result); |
| 416 errorEvent.destinationId = destinationId; | 528 errorEvent.destinationId = destinationId; |
| 417 errorEvent.destinationOrigin = print_preview.Destination.Origin.COOKIES; | 529 errorEvent.destinationOrigin = origin; |
| 418 this.dispatchEvent(errorEvent); | 530 this.dispatchEvent(errorEvent, origin); |
| 419 } | 531 } |
| 420 }, | 532 }, |
| 421 | 533 |
| 422 /** | 534 /** |
| 423 * Called when the update printer TOS acceptance request completes. | 535 * Called when the update printer TOS acceptance request completes. |
| 424 * @param {number} status Status of the HTTP request. | 536 * @param {number} status Status of the HTTP request. |
| 425 * @param {Object} result JSON response. | 537 * @param {Object} result JSON response. |
| 538 * @param {!print_preview.Destination.Origin} origin Origin for destination. | |
| 426 * @private | 539 * @private |
| 427 */ | 540 */ |
| 428 onUpdatePrinterTosAcceptanceDone_: function(status, result) { | 541 onUpdateTosAcceptanceDone_: function(status, result, origin) { |
| 429 if (status == 200 && result['success']) { | 542 if (status == 200 && result['success']) { |
| 430 // Do nothing. | 543 // Do nothing. |
| 431 } else { | 544 } else { |
| 432 var errorEvent = this.createErrorEvent_( | 545 var errorEvent = this.createErrorEvent_( |
| 433 CloudPrintInterface.EventType.SUBMIT_FAILED, status, result); | 546 CloudPrintInterface.EventType.SUBMIT_FAILED, status, result); |
| 434 this.dispatchEvent(errorEvent); | 547 this.dispatchEvent(errorEvent); |
| 435 } | 548 } |
| 436 } | 549 } |
| 437 }; | 550 }; |
| 438 | 551 |
| 439 /** | 552 /** |
| 553 * Data structure that holds data for delayed Cloud Print requests. | |
|
Toscano
2013/04/19 17:04:33
delayed: not all of them are delayed.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 554 * @param {!XMLHttpRequest} xhr Partially prepared http request. | |
| 555 * @param {string} body Data to send with POST requests. | |
| 556 * @param {!print_preview.Destination.Origin} origin Origin for destination. | |
| 557 * @constructor | |
| 558 */ | |
| 559 function ClourPrintRequest(xhr, body, origin) { | |
|
Toscano
2013/04/19 17:04:33
Cloud*PrintRequest
Toscano
2013/04/19 17:04:33
Please add callback parameter.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 560 /** | |
| 561 * Partially prepared http request. | |
| 562 * @type {!XMLHttpRequest} | |
| 563 */ | |
| 564 this.xhr = xhr; | |
| 565 | |
| 566 /** | |
| 567 * Data to send with POST requests. | |
| 568 * @type {string} | |
| 569 */ | |
| 570 this.body = body; | |
| 571 | |
| 572 /** | |
| 573 * Origin for destination. | |
| 574 * @type {!print_preview.Destination.Origin} | |
| 575 */ | |
| 576 this.origin = origin; | |
| 577 }; | |
|
Toscano
2013/04/19 17:04:33
Please add "callback" and "result" fields.
Vitaly Buka (NO REVIEWS)
2013/04/19 22:14:29
Done.
| |
| 578 | |
| 579 /** | |
| 440 * Data structure that represents an HTTP parameter. | 580 * Data structure that represents an HTTP parameter. |
| 441 * @param {string} name Name of the parameter. | 581 * @param {string} name Name of the parameter. |
| 442 * @param {string} value Value of the parameter. | 582 * @param {string} value Value of the parameter. |
| 443 * @constructor | 583 * @constructor |
| 444 */ | 584 */ |
| 445 function HttpParam(name, value) { | 585 function HttpParam(name, value) { |
| 446 /** | 586 /** |
| 447 * Name of the parameter. | 587 * Name of the parameter. |
| 448 * @type {string} | 588 * @type {string} |
| 449 */ | 589 */ |
| 450 this.name = name; | 590 this.name = name; |
| 451 | 591 |
| 452 /** | 592 /** |
| 453 * Name of the value. | 593 * Name of the value. |
| 454 * @type {string} | 594 * @type {string} |
| 455 */ | 595 */ |
| 456 this.value = value; | 596 this.value = value; |
| 457 }; | 597 }; |
| 458 | 598 |
| 459 // Export | 599 // Export |
| 460 return { | 600 return { |
| 461 CloudPrintInterface: CloudPrintInterface | 601 CloudPrintInterface: CloudPrintInterface |
| 462 }; | 602 }; |
| 463 }); | 603 }); |
| OLD | NEW |