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. | |
| 13 * @constructor | 14 * @constructor |
| 14 * @extends {cr.EventTarget} | 15 * @extends {cr.EventTarget} |
| 15 */ | 16 */ |
| 16 function CloudPrintInterface(baseUrl) { | 17 function CloudPrintInterface(baseUrl, nativeLayer) { |
| 17 /** | 18 /** |
| 18 * The base URL of the Google Cloud Print API. | 19 * The base URL of the Google Cloud Print API. |
| 19 * @type {string} | 20 * @type {string} |
| 20 * @private | 21 * @private |
| 21 */ | 22 */ |
| 22 this.baseUrl_ = baseUrl; | 23 this.baseUrl_ = baseUrl; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * Last received XSRF token. Sent as a parameter in every request. | 26 * Used to get Auth2 tokens. |
| 26 * @type {string} | 27 * @type {!print_preview.NativeLayer} |
| 27 * @private | 28 * @private |
| 28 */ | 29 */ |
| 29 this.xsrfToken_ = ''; | 30 this.nativeLayer_ = nativeLayer; |
| 31 | |
| 32 /** | |
| 33 * Last received XSRF tokens for each origin type. Sent as a | |
| 34 * parameter in every request. | |
| 35 * @type {Dictionary} | |
| 36 * @private | |
| 37 */ | |
| 38 this.xsrfTokens_ = {}; | |
|
Toscano
2013/04/19 01:58:21
Get rid of this cache.
Vitaly Buka (NO REVIEWS)
2013/04/19 06:18:47
Done.
| |
| 39 | |
| 40 /** | |
| 41 * Pending requests we are when waiting for access tokens. | |
| 42 * @type {Dictionary} | |
| 43 * @private | |
| 44 */ | |
| 45 this.pendingRequests_ = {}; | |
|
Toscano
2013/04/19 01:58:21
this.deviceRequestQueue_
Vitaly Buka (NO REVIEWS)
2013/04/19 06:18:47
Done.
| |
| 30 | 46 |
| 31 /** | 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 | 124 |
| 100 CloudPrintInterface.prototype = { | 125 CloudPrintInterface.prototype = { |
| 101 __proto__: cr.EventTarget.prototype, | 126 __proto__: cr.EventTarget.prototype, |
| 102 | 127 |
| 103 /** @return {string} Base URL of the Google Cloud Print service. */ | 128 /** @return {string} Base URL of the Google Cloud Print service. */ |
| 104 get baseUrl() { | 129 get baseUrl() { |
| 105 return this.baseUrl_; | 130 return this.baseUrl_; |
| 106 }, | 131 }, |
| 107 | 132 |
| 108 /** | 133 /** |
| 134 * Adds event listeners to the relevant native layer events. | |
| 135 * @private | |
| 136 */ | |
| 137 addEventListeners_: function() { | |
|
Toscano
2013/04/19 01:58:21
Move after public methods.
Vitaly Buka (NO REVIEWS)
2013/04/19 06:18:47
Done.
| |
| 138 this.tracker_.add( | |
| 139 this.nativeLayer_, | |
| 140 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, | |
| 141 this.onAccessTokenReady_.bind(this)); | |
| 142 }, | |
| 143 | |
| 144 /** | |
| 145 * Called when a native layer received access token. | |
| 146 * @param {cr.Event} evt Contains the access token. | |
| 147 * @private | |
| 148 */ | |
| 149 onAccessTokenReady_: function(event) { | |
| 150 var requests = this.pendingRequests_[event.authType]; | |
| 151 this.pendingRequests_[event.authType] = []; | |
| 152 for (var i in requests) { | |
|
Toscano
2013/04/19 01:58:21
requests.forEach(function(request) {});
Vitaly Buka (NO REVIEWS)
2013/04/19 06:18:47
Done.
| |
| 153 requests[i](event.accessToken); | |
| 154 } | |
| 155 }, | |
| 156 | |
| 157 /** | |
| 109 * @return {boolean} Whether a search for cloud destinations is in progress. | 158 * @return {boolean} Whether a search for cloud destinations is in progress. |
| 110 */ | 159 */ |
| 111 get isCloudDestinationSearchInProgress() { | 160 get isCloudDestinationSearchInProgress() { |
| 112 return this.outstandingCloudSearchRequestCount_ > 0; | 161 return this.outstandingCloudSearchRequestCount_ > 0; |
| 113 }, | 162 }, |
| 114 | 163 |
| 115 /** | 164 /** |
| 116 * Sends a Google Cloud Print search API request. | 165 * Sends a Google Cloud Print search API request. |
| 117 * @param {boolean} isRecent Whether to search for only recently used | 166 * @param {boolean} isRecent Whether to search for only recently used |
| 118 * printers. | 167 * printers. |
| 119 */ | 168 */ |
| 120 search: function(isRecent) { | 169 search: function(isRecent) { |
| 121 var params = [ | 170 var params = [ |
| 122 new HttpParam('connection_status', 'ALL'), | 171 new HttpParam('connection_status', 'ALL'), |
| 123 new HttpParam('client', 'chrome'), | 172 new HttpParam('client', 'chrome'), |
| 124 new HttpParam('use_cdd', 'true') | 173 new HttpParam('use_cdd', 'true') |
| 125 ]; | 174 ]; |
| 126 if (isRecent) { | 175 if (isRecent) { |
| 127 params.push(new HttpParam('q', '^recent')); | 176 params.push(new HttpParam('q', '^recent')); |
| 128 } | 177 } |
| 129 ++this.outstandingCloudSearchRequestCount_; | 178 var cloud_origins = [ |
|
Toscano
2013/04/19 01:58:21
Move this into a static constant.
| |
| 130 this.sendRequest_('GET', 'search', params, | 179 print_preview.Destination.Origin.COOKIES, |
| 131 this.onSearchDone_.bind(this, isRecent)); | 180 // TODO(vitalybuka) Enable when implemented. |
| 181 // ready print_preview.Destination.Origin.PROFILE, | |
| 182 print_preview.Destination.Origin.DEVICE | |
| 183 ]; | |
| 184 for (var type in cloud_origins) { | |
|
Toscano
2013/04/19 01:58:21
cloud_origins.forEach(function(type) {
});
| |
| 185 var origin = cloud_origins[type]; | |
| 186 ++this.outstandingCloudSearchRequestCount_; | |
| 187 this.sendRequest_('GET', 'search', params, origin, | |
|
Toscano
2013/04/19 01:58:21
var cpRequest = this.buildRequest_('GET', 'search'
| |
| 188 this.onSearchDone_.bind(this, isRecent, origin)); | |
| 189 }; | |
| 132 }, | 190 }, |
| 133 | 191 |
| 134 /** | 192 /** |
| 135 * Sends a Google Cloud Print submit API request. | 193 * Sends a Google Cloud Print submit API request. |
| 136 * @param {!print_preview.Destination} destination Cloud destination to | 194 * @param {!print_preview.Destination} destination Cloud destination to |
| 137 * print to. | 195 * print to. |
| 138 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the | 196 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the |
| 139 * print ticket to print. | 197 * print ticket to print. |
| 140 * @param {string} data Base64 encoded data of the document. | 198 * @param {string} data Base64 encoded data of the document. |
| 141 */ | 199 */ |
| 142 submit: function(destination, printTicketStore, data) { | 200 submit: function(destination, printTicketStore, data) { |
| 143 var result = | 201 var result = |
| 144 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); | 202 CloudPrintInterface.VERSION_REGEXP_.exec(navigator.userAgent); |
| 145 var chromeVersion = 'unknown'; | 203 var chromeVersion = 'unknown'; |
| 146 if (result && result.length == 2) { | 204 if (result && result.length == 2) { |
| 147 chromeVersion = result[1]; | 205 chromeVersion = result[1]; |
| 148 } | 206 } |
| 149 var params = [ | 207 var params = [ |
| 150 new HttpParam('printerid', destination.id), | 208 new HttpParam('printerid', destination.id), |
| 151 new HttpParam('contentType', 'dataUrl'), | 209 new HttpParam('contentType', 'dataUrl'), |
| 152 new HttpParam('title', printTicketStore.getDocumentTitle()), | 210 new HttpParam('title', printTicketStore.getDocumentTitle()), |
| 153 new HttpParam('ticket', | 211 new HttpParam('ticket', |
| 154 this.createPrintTicket_(destination, printTicketStore)), | 212 this.createPrintTicket_(destination, printTicketStore)), |
| 155 new HttpParam('content', 'data:application/pdf;base64,' + data), | 213 new HttpParam('content', 'data:application/pdf;base64,' + data), |
| 156 new HttpParam('tag', | 214 new HttpParam('tag', |
| 157 '__google__chrome_version=' + chromeVersion), | 215 '__google__chrome_version=' + chromeVersion), |
| 158 new HttpParam('tag', '__google__os=' + navigator.platform) | 216 new HttpParam('tag', '__google__os=' + navigator.platform) |
| 159 ]; | 217 ]; |
| 160 this.sendRequest_('POST', 'submit', params, | 218 this.sendRequest_('POST', 'submit', params, destination.origin, |
| 161 this.onSubmitDone_.bind(this)); | 219 this.onSubmitDone_.bind(this)); |
| 162 }, | 220 }, |
| 163 | 221 |
| 164 /** | 222 /** |
| 165 * Sends a Google Cloud Print printer API request. | 223 * Sends a Google Cloud Print printer API request. |
| 166 * @param {string} printerId ID of the printer to lookup. | 224 * @param {string} printerId ID of the printer to lookup. |
| 167 */ | 225 */ |
| 168 printer: function(printerId) { | 226 printer: function(printerId, origin) { |
| 169 var params = [ | 227 var params = [ |
| 170 new HttpParam('printerid', printerId), | 228 new HttpParam('printerid', printerId), |
| 171 new HttpParam('use_cdd', 'true') | 229 new HttpParam('use_cdd', 'true') |
| 172 ]; | 230 ]; |
| 173 this.sendRequest_('GET', 'printer', params, | 231 this.sendRequest_('GET', 'printer', params, origin, |
| 174 this.onPrinterDone_.bind(this, printerId)); | 232 this.onPrinterDone_.bind(this, printerId, origin)); |
| 175 }, | 233 }, |
| 176 | 234 |
| 177 /** | 235 /** |
| 178 * Sends a Google Cloud Print update API request to accept (or reject) the | 236 * Sends a Google Cloud Print update API request to accept (or reject) the |
| 179 * terms-of-service of the given printer. | 237 * terms-of-service of the given printer. |
| 180 * @param {string} printerId ID of the printer to accept the | 238 * @param {string} printerId ID of the printer to accept the |
| 181 * terms-of-service for. | 239 * terms-of-service for. |
| 182 * @param {boolean} isAccepted Whether the user accepted the | 240 * @param {boolean} isAccepted Whether the user accepted the |
| 183 * terms-of-service. | 241 * terms-of-service. |
| 184 */ | 242 */ |
| 185 updatePrinterTosAcceptance: function(printerId, isAccepted) { | 243 updatePrinterTosAcceptance: function(printerId, origin, isAccepted) { |
|
Toscano
2013/04/19 01:58:21
1. Change order so that origin is at the end.
2. U
| |
| 186 var params = [ | 244 var params = [ |
| 187 new HttpParam('printerid', printerId), | 245 new HttpParam('printerid', printerId), |
| 188 new HttpParam('is_tos_accepted', isAccepted) | 246 new HttpParam('is_tos_accepted', isAccepted) |
| 189 ]; | 247 ]; |
| 190 this.sendRequest_('POST', 'update', params, | 248 this.sendRequest_('POST', 'update', params, origin, |
| 191 this.onUpdatePrinterTosAcceptanceDone_.bind(this)); | 249 this.onUpdatePrinterTosAcceptanceDone_.bind(this)); |
| 192 }, | 250 }, |
| 193 | 251 |
| 194 /** | 252 /** |
| 195 * Creates an object that represents a Google Cloud Print print ticket. | 253 * Creates an object that represents a Google Cloud Print print ticket. |
| 196 * @param {!print_preview.Destination} destination Destination to print to. | 254 * @param {!print_preview.Destination} destination Destination to print to. |
| 197 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create | 255 * @param {!print_preview.PrintTicketStore} printTicketStore Used to create |
| 198 * the state of the print ticket. | 256 * the state of the print ticket. |
| 199 * @return {!Object} Google Cloud Print print ticket. | 257 * @return {!Object} Google Cloud Print print ticket. |
| 200 * @private | 258 * @private |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 cjt.print.duplex = | 296 cjt.print.duplex = |
| 239 {type: pts.isDuplexEnabled() ? 'LONG_EDGE' : 'NO_DUPLEX'}; | 297 {type: pts.isDuplexEnabled() ? 'LONG_EDGE' : 'NO_DUPLEX'}; |
| 240 } | 298 } |
| 241 if (pts.hasOrientationCapability()) { | 299 if (pts.hasOrientationCapability()) { |
| 242 cjt.print.page_orientation = | 300 cjt.print.page_orientation = |
| 243 {type: pts.isLandscapeEnabled() ? 'LANDSCAPE' : 'PORTRAIT'}; | 301 {type: pts.isLandscapeEnabled() ? 'LANDSCAPE' : 'PORTRAIT'}; |
| 244 } | 302 } |
| 245 return JSON.stringify(cjt); | 303 return JSON.stringify(cjt); |
| 246 }, | 304 }, |
| 247 | 305 |
| 306 // TODO docs | |
| 307 sendRequestWithToken_: function(body, xhr, callback, origin, accessToken) { | |
| 308 if (origin != print_preview.Destination.Origin.COOKIES) { | |
| 309 if (!accessToken) { | |
| 310 // No valid token. | |
| 311 callback(401, { | |
| 312 'errorCode' : -1, | |
| 313 'message' : 'No access token.' | |
| 314 }); | |
| 315 retrun; | |
| 316 } | |
| 317 xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken); | |
| 318 xhr.withCredentials = false; | |
| 319 } | |
| 320 xhr.onreadystatechange = | |
| 321 this.onReadyStateChange_.bind(this, xhr, origin, callback); | |
| 322 xhr.send(body); | |
| 323 }, | |
| 324 | |
| 248 /** | 325 /** |
| 249 * Sends a request to the Google Cloud Print API. | 326 * Sends a request to the Google Cloud Print API. |
| 250 * @param {string} method HTTP method of the request. | 327 * @param {string} method HTTP method of the request. |
| 251 * @param {string} action Google Cloud Print action to perform. | 328 * @param {string} action Google Cloud Print action to perform. |
| 252 * @param {Array.<!HttpParam>} params HTTP parameters to include in the | 329 * @param {Array.<!HttpParam>} params HTTP parameters to include in the |
| 253 * request. | 330 * request. |
| 331 * TODO origin | |
| 254 * @param {function(number, Object)} callback Callback to invoke when | 332 * @param {function(number, Object)} callback Callback to invoke when |
| 255 * request completes. | 333 * request completes. |
| 256 */ | 334 */ |
| 257 sendRequest_: function(method, action, params, callback) { | 335 sendRequest_: function(method, action, params, origin, callback) { |
|
Toscano
2013/04/19 01:58:21
Change this method to buildRequest_(...); with out
| |
| 258 if (!this.xsrfToken_) { | 336 if (!this.xsrfTokens_[origin]) { |
| 259 // TODO(rltoscano): Should throw an error if not a read-only action or | 337 // TODO(rltoscano): Should throw an error if not a read-only action or |
| 260 // issue an xsrf token request. | 338 // issue an xsrf token request. |
| 261 } | 339 } |
| 262 var url = this.baseUrl_ + '/' + action + '?xsrf=' + this.xsrfToken_; | 340 |
| 341 var url = this.baseUrl_ + '/' + action + '?xsrf='; | |
| 342 if (this.xsrfTokens_[origin]) | |
| 343 url = url + this.xsrfTokens_[origin]; | |
| 263 var body = null; | 344 var body = null; |
| 264 | 345 |
| 265 if (params) { | 346 if (params) { |
| 266 if (method == 'GET') { | 347 if (method == 'GET') { |
| 267 url = params.reduce(function(partialUrl, param) { | 348 url = params.reduce(function(partialUrl, param) { |
| 268 return partialUrl + '&' + param.name + '=' + | 349 return partialUrl + '&' + param.name + '=' + |
| 269 encodeURIComponent(param.value); | 350 encodeURIComponent(param.value); |
| 270 }, url); | 351 }, url); |
| 271 } else if (method == 'POST') { | 352 } else if (method == 'POST') { |
| 272 body = params.reduce(function(partialBody, param) { | 353 body = params.reduce(function(partialBody, param) { |
| 273 return partialBody + 'Content-Disposition: form-data; name=\"' + | 354 return partialBody + 'Content-Disposition: form-data; name=\"' + |
| 274 param.name + '\"\r\n\r\n' + param.value + '\r\n--' + | 355 param.name + '\"\r\n\r\n' + param.value + '\r\n--' + |
| 275 CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'; | 356 CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'; |
| 276 }, '--' + CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'); | 357 }, '--' + CloudPrintInterface.MULTIPART_BOUNDARY_ + '\r\n'); |
| 277 } | 358 } |
| 278 } | 359 } |
| 279 | 360 |
| 280 var headers = {}; | 361 var headers = {}; |
| 281 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; | 362 headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview'; |
| 282 if (method == 'GET') { | 363 if (method == 'GET') { |
| 283 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; | 364 headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_; |
| 284 } else if (method == 'POST') { | 365 } else if (method == 'POST') { |
| 285 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; | 366 headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_; |
| 286 } | 367 } |
| 287 | 368 |
| 288 var xhr = new XMLHttpRequest(); | 369 var xhr = new XMLHttpRequest(); |
| 289 xhr.onreadystatechange = | |
| 290 this.onReadyStateChange_.bind(this, xhr, callback); | |
| 291 xhr.open(method, url, true); | 370 xhr.open(method, url, true); |
| 292 xhr.withCredentials = true; | 371 xhr.withCredentials = true; |
| 293 for (var header in headers) { | 372 for (var header in headers) { |
| 294 xhr.setRequestHeader(header, headers[header]); | 373 xhr.setRequestHeader(header, headers[header]); |
| 295 } | 374 } |
| 296 xhr.send(body); | 375 |
| 376 if (origin == print_preview.Destination.Origin.COOKIES) { | |
| 377 this.sendRequestWithToken_(body, xhr, callback, origin, ''); | |
| 378 } else { | |
| 379 if (!this.pendingRequests_[origin]) | |
| 380 this.pendingRequests_[origin] = []; | |
| 381 this.pendingRequests_[origin].push( | |
| 382 this.sendRequestWithToken_.bind(this, body, xhr, callback, origin)); | |
| 383 this.nativeLayer_.startGetAccessToken(origin); | |
| 384 } | |
| 297 }, | 385 }, |
| 298 | 386 |
| 299 /** | 387 /** |
| 300 * Creates a Google Cloud Print interface error that is ready to dispatch. | 388 * Creates a Google Cloud Print interface error that is ready to dispatch. |
| 301 * @param {!CloudPrintInterface.EventType} type Type of the error. | 389 * @param {!CloudPrintInterface.EventType} type Type of the error. |
| 302 * @param {number} status HTTP status code of the failed request. | 390 * @param {number} status HTTP status code of the failed request. |
| 303 * @param {Object} result JSON response of the request. {@code null} if | 391 * @param {Object} result JSON response of the request. {@code null} if |
| 304 * status was not 200. | 392 * status was not 200. |
| 305 * @return {!cr.Event} Google Cloud Print interface error event. | 393 * @return {!cr.Event} Google Cloud Print interface error event. |
| 306 * @private | 394 * @private |
| 307 */ | 395 */ |
| 308 createErrorEvent_: function(type, status, result) { | 396 createErrorEvent_: function(type, status, result) { |
| 309 var errorEvent = new cr.Event(type); | 397 var errorEvent = new cr.Event(type); |
| 310 errorEvent.status = status; | 398 errorEvent.status = status; |
| 311 errorEvent.errorCode = status == 200 ? result['errorCode'] : 0; | 399 errorEvent.errorCode = status == 200 ? result['errorCode'] : 0; |
| 312 errorEvent.message = status == 200 ? result['message'] : ''; | 400 errorEvent.message = status == 200 ? result['message'] : ''; |
| 313 return errorEvent; | 401 return errorEvent; |
| 314 }, | 402 }, |
| 315 | 403 |
| 316 /** | 404 /** |
| 317 * Called when the ready-state of a XML http request changes. | 405 * Called when the ready-state of a XML http request changes. |
| 318 * Calls the successCallback with the result or dispatches an ERROR event. | 406 * Calls the successCallback with the result or dispatches an ERROR event. |
| 319 * @param {XMLHttpRequest} xhr XML http request that changed. | 407 * @param {XMLHttpRequest} xhr XML http request that changed. |
| 320 * @param {function(number, Object)} callback Callback to invoke when | 408 * @param {function(number, Object)} callback Callback to invoke when |
| 321 * request completes. | 409 * request completes. |
| 322 * @private | 410 * @private |
| 323 */ | 411 */ |
| 324 onReadyStateChange_: function(xhr, callback) { | 412 onReadyStateChange_: function(xhr, origin, callback) { |
| 325 if (xhr.readyState == 4) { | 413 if (xhr.readyState == 4) { |
| 326 if (xhr.status == 200) { | 414 if (xhr.status == 200) { |
| 327 var result = JSON.parse(xhr.responseText); | 415 var result = JSON.parse(xhr.responseText); |
| 328 if (result['success']) { | 416 if (result['success']) { |
| 329 this.xsrfToken_ = result['xsrf_token']; | 417 this.xsrfTokens_[origin] = result['xsrf_token']; |
| 330 } | 418 } |
| 331 } | 419 } |
| 332 callback(xhr.status, result); | 420 callback(xhr.status, result); |
| 333 } | 421 } |
| 334 }, | 422 }, |
| 335 | 423 |
| 336 /** | 424 /** |
| 337 * Called when the search request completes. | 425 * Called when the search request completes. |
| 338 * @param {boolean} isRecent Whether the search request was for recent | 426 * @param {boolean} isRecent Whether the search request was for recent |
| 339 * destinations. | 427 * destinations. |
| 340 * @param {number} status Status of the HTTP request. | 428 * @param {number} status Status of the HTTP request. |
| 341 * @param {Object} result JSON response. | 429 * @param {Object} result JSON response. |
| 342 * @private | 430 * @private |
| 343 */ | 431 */ |
| 344 onSearchDone_: function(isRecent, status, result) { | 432 onSearchDone_: function(isRecent, origin, status, result) { |
| 345 --this.outstandingCloudSearchRequestCount_; | 433 --this.outstandingCloudSearchRequestCount_; |
| 346 if (status == 200 && result['success']) { | 434 if (status == 200 && result['success']) { |
| 347 var printerListJson = result['printers'] || []; | 435 var printerListJson = result['printers'] || []; |
| 348 var printerList = []; | 436 var printerList = []; |
| 349 printerListJson.forEach(function(printerJson) { | 437 printerListJson.forEach(function(printerJson) { |
| 350 try { | 438 try { |
| 351 printerList.push( | 439 printerList.push(cloudprint.CloudDestinationParser.parse(printerJson , origin)); |
| 352 cloudprint.CloudDestinationParser.parse( | |
| 353 printerJson, print_preview.Destination.Origin.COOKIES)); | |
| 354 } catch (err) { | 440 } catch (err) { |
| 355 console.error('Unable to parse cloud print destination: ' + err); | 441 console.error('Unable to parse cloud print destination: ' + err); |
| 356 } | 442 } |
| 357 }); | 443 }); |
| 358 var searchDoneEvent = | 444 var searchDoneEvent = |
| 359 new cr.Event(CloudPrintInterface.EventType.SEARCH_DONE); | 445 new cr.Event(CloudPrintInterface.EventType.SEARCH_DONE); |
| 360 searchDoneEvent.printers = printerList; | 446 searchDoneEvent.printers = printerList; |
| 361 searchDoneEvent.isRecent = isRecent; | 447 searchDoneEvent.isRecent = isRecent; |
| 362 searchDoneEvent.email = result['request']['user']; | 448 searchDoneEvent.email = result['request']['user']; |
| 363 this.dispatchEvent(searchDoneEvent); | 449 this.dispatchEvent(searchDoneEvent); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 387 } | 473 } |
| 388 }, | 474 }, |
| 389 | 475 |
| 390 /** | 476 /** |
| 391 * Called when the printer request completes. | 477 * Called when the printer request completes. |
| 392 * @param {string} destinationId ID of the destination that was looked up. | 478 * @param {string} destinationId ID of the destination that was looked up. |
| 393 * @param {number} status Status of the HTTP request. | 479 * @param {number} status Status of the HTTP request. |
| 394 * @param {Object} result JSON response. | 480 * @param {Object} result JSON response. |
| 395 * @private | 481 * @private |
| 396 */ | 482 */ |
| 397 onPrinterDone_: function(destinationId, status, result) { | 483 onPrinterDone_: function(destinationId, origin, status, result) { |
| 398 if (status == 200 && result['success']) { | 484 if (status == 200 && result['success']) { |
| 399 var printerJson = result['printers'][0]; | 485 var printerJson = result['printers'][0]; |
| 400 var printer; | 486 var printer; |
| 401 try { | 487 try { |
| 402 printer = cloudprint.CloudDestinationParser.parse( | 488 printer = cloudprint.CloudDestinationParser.parse(printerJson, origin) ; |
| 403 printerJson, print_preview.Destination.Origin.COOKIES); | |
| 404 } catch (err) { | 489 } catch (err) { |
| 405 console.error('Failed to parse cloud print destination: ' + | 490 console.error('Failed to parse cloud print destination: ' + |
| 406 JSON.stringify(printerJson)); | 491 JSON.stringify(printerJson)); |
| 407 return; | 492 return; |
| 408 } | 493 } |
| 409 var printerDoneEvent = | 494 var printerDoneEvent = |
| 410 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); | 495 new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE); |
| 411 printerDoneEvent.printer = printer; | 496 printerDoneEvent.printer = printer; |
| 412 this.dispatchEvent(printerDoneEvent); | 497 this.dispatchEvent(printerDoneEvent); |
| 413 } else { | 498 } else { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 * @type {string} | 539 * @type {string} |
| 455 */ | 540 */ |
| 456 this.value = value; | 541 this.value = value; |
| 457 }; | 542 }; |
| 458 | 543 |
| 459 // Export | 544 // Export |
| 460 return { | 545 return { |
| 461 CloudPrintInterface: CloudPrintInterface | 546 CloudPrintInterface: CloudPrintInterface |
| 462 }; | 547 }; |
| 463 }); | 548 }); |
| OLD | NEW |