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

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

Issue 233623003: Remember and restore the account last used destination is registered for. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adjust PrintPreview unit test. Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/app_state.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 29 matching lines...) Expand all
40 40
41 /** 41 /**
42 * Currently logged in users (identified by email) mapped to the Google 42 * Currently logged in users (identified by email) mapped to the Google
43 * session index. 43 * session index.
44 * @type {!Object.<string, number>} 44 * @type {!Object.<string, number>}
45 * @private 45 * @private
46 */ 46 */
47 this.userSessionIndex_ = {}; 47 this.userSessionIndex_ = {};
48 48
49 /** 49 /**
50 * Last received XSRF token. Sent as a parameter in every request. 50 * Stores last received XSRF tokens for each user account. Sent as
51 * @type {string} 51 * a parameter with every request.
52 * @type {!Object.<string, string>}
52 * @private 53 * @private
53 */ 54 */
54 this.xsrfToken_ = ''; 55 this.xsrfTokens_ = {};
55 56
56 /** 57 /**
57 * Pending requests delayed until we get access token. 58 * Pending requests delayed until we get access token.
58 * @type {!Array.<!CloudPrintRequest>} 59 * @type {!Array.<!CloudPrintRequest>}
59 * @private 60 * @private
60 */ 61 */
61 this.requestQueue_ = []; 62 this.requestQueue_ = [];
62 63
63 /** 64 /**
64 * Outstanding cloud destination search requests. 65 * Outstanding cloud destination search requests.
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 162
162 /** 163 /**
163 * @return {boolean} Whether a search for cloud destinations is in progress. 164 * @return {boolean} Whether a search for cloud destinations is in progress.
164 */ 165 */
165 get isCloudDestinationSearchInProgress() { 166 get isCloudDestinationSearchInProgress() {
166 return this.outstandingCloudSearchRequests_.length > 0; 167 return this.outstandingCloudSearchRequests_.length > 0;
167 }, 168 },
168 169
169 /** 170 /**
170 * Sends Google Cloud Print search API request. 171 * Sends Google Cloud Print search API request.
172 * @param {string=} opt_account Account the search is sent for. When
173 * omitted, the search is done on behalf of the primary user.
171 * @param {print_preview.Destination.Origin=} opt_origin When specified, 174 * @param {print_preview.Destination.Origin=} opt_origin When specified,
172 * searches destinations for {@code opt_origin} only, otherwise starts 175 * searches destinations for {@code opt_origin} only, otherwise starts
173 * searches for all origins. 176 * searches for all origins.
174 */ 177 */
175 search: function(opt_origin) { 178 search: function(opt_account, opt_origin) {
179 var account = opt_account || '';
176 var origins = 180 var origins =
177 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_; 181 opt_origin && [opt_origin] || CloudPrintInterface.CLOUD_ORIGINS_;
178 // Terminate outstanding search requests for all requested origins. 182 this.abortSearchRequests_(origins);
179 this.outstandingCloudSearchRequests_ = 183 this.search_(true, account, origins);
180 this.outstandingCloudSearchRequests_.filter(function(request) { 184 this.search_(false, account, origins);
181 if (origins.indexOf(request.origin) >= 0) {
182 request.xhr.abort();
183 return false;
184 }
185 return true;
186 });
187 this.search_(true, origins);
188 this.search_(false, origins);
189 }, 185 },
190 186
191 /** 187 /**
192 * Sends Google Cloud Print search API requests. 188 * Sends Google Cloud Print search API requests.
193 * @param {boolean} isRecent Whether to search for only recently used 189 * @param {boolean} isRecent Whether to search for only recently used
194 * printers. 190 * printers.
191 * @param {string} account Account the search is sent for. It matters for
192 * COOKIES origin only, and can be empty (sent on behalf of the primary
193 * user in this case).
195 * @param {!Array.<!print_preview.Destination.Origin>} origins Origins to 194 * @param {!Array.<!print_preview.Destination.Origin>} origins Origins to
196 * search printers for. 195 * search printers for.
197 * @private 196 * @private
198 */ 197 */
199 search_: function(isRecent, origins) { 198 search_: function(isRecent, account, origins) {
200 var params = [ 199 var params = [
201 new HttpParam('connection_status', 'ALL'), 200 new HttpParam('connection_status', 'ALL'),
202 new HttpParam('client', 'chrome'), 201 new HttpParam('client', 'chrome'),
203 new HttpParam('use_cdd', 'true') 202 new HttpParam('use_cdd', 'true')
204 ]; 203 ];
205 if (isRecent) { 204 if (isRecent) {
206 params.push(new HttpParam('q', '^recent')); 205 params.push(new HttpParam('q', '^recent'));
207 } 206 }
208 origins.forEach(function(origin) { 207 origins.forEach(function(origin) {
209 var cpRequest = 208 var cpRequest = this.buildRequest_(
210 this.buildRequest_('GET', 'search', params, origin, 209 'GET',
211 this.onSearchDone_.bind(this, isRecent)); 210 'search',
211 params,
212 origin,
213 account,
214 this.onSearchDone_.bind(this, isRecent));
212 this.outstandingCloudSearchRequests_.push(cpRequest); 215 this.outstandingCloudSearchRequests_.push(cpRequest);
213 this.sendOrQueueRequest_(cpRequest); 216 this.sendOrQueueRequest_(cpRequest);
214 }, this); 217 }, this);
215 }, 218 },
216 219
217 /** 220 /**
218 * Sends a Google Cloud Print submit API request. 221 * Sends a Google Cloud Print submit API request.
219 * @param {!print_preview.Destination} destination Cloud destination to 222 * @param {!print_preview.Destination} destination Cloud destination to
220 * print to. 223 * print to.
221 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the 224 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the
(...skipping 12 matching lines...) Expand all
234 new HttpParam('printerid', destination.id), 237 new HttpParam('printerid', destination.id),
235 new HttpParam('contentType', 'dataUrl'), 238 new HttpParam('contentType', 'dataUrl'),
236 new HttpParam('title', documentInfo.title), 239 new HttpParam('title', documentInfo.title),
237 new HttpParam('ticket', 240 new HttpParam('ticket',
238 printTicketStore.createPrintTicket(destination)), 241 printTicketStore.createPrintTicket(destination)),
239 new HttpParam('content', 'data:application/pdf;base64,' + data), 242 new HttpParam('content', 'data:application/pdf;base64,' + data),
240 new HttpParam('tag', 243 new HttpParam('tag',
241 '__google__chrome_version=' + chromeVersion), 244 '__google__chrome_version=' + chromeVersion),
242 new HttpParam('tag', '__google__os=' + navigator.platform) 245 new HttpParam('tag', '__google__os=' + navigator.platform)
243 ]; 246 ];
244 var cpRequest = this.buildRequest_('POST', 'submit', params, 247 var cpRequest = this.buildRequest_(
245 destination.origin, 248 'POST',
246 this.onSubmitDone_.bind(this)); 249 'submit',
250 params,
251 destination.origin,
252 destination.account,
253 this.onSubmitDone_.bind(this));
247 this.sendOrQueueRequest_(cpRequest); 254 this.sendOrQueueRequest_(cpRequest);
248 }, 255 },
249 256
250 /** 257 /**
251 * Sends a Google Cloud Print printer API request. 258 * Sends a Google Cloud Print printer API request.
252 * @param {string} printerId ID of the printer to lookup. 259 * @param {string} printerId ID of the printer to lookup.
253 * @param {!print_preview.Destination.Origin} origin Origin of the printer. 260 * @param {!print_preview.Destination.Origin} origin Origin of the printer.
261 * @param {string=} account Account this printer is registered for. When
262 * provided for COOKIES {@code origin}, and users sessions are still not
263 * known, will be checked against the response (both success and failure
264 * to get printer) and, if the active user account is not the one
265 * requested, {@code account} is activated and printer request reissued.
254 */ 266 */
255 printer: function(printerId, origin) { 267 printer: function(printerId, origin, account) {
256 var params = [ 268 var params = [
257 new HttpParam('printerid', printerId), 269 new HttpParam('printerid', printerId),
258 new HttpParam('use_cdd', 'true'), 270 new HttpParam('use_cdd', 'true'),
259 new HttpParam('printer_connection_status', 'true') 271 new HttpParam('printer_connection_status', 'true')
260 ]; 272 ];
261 var cpRequest = 273 this.sendOrQueueRequest_(this.buildRequest_(
262 this.buildRequest_('GET', 'printer', params, origin, 274 'GET',
263 this.onPrinterDone_.bind(this, printerId)); 275 'printer',
264 this.sendOrQueueRequest_(cpRequest); 276 params,
277 origin,
278 account,
279 this.onPrinterDone_.bind(this, printerId)));
265 }, 280 },
266 281
267 /** 282 /**
268 * Sends a Google Cloud Print update API request to accept (or reject) the 283 * Sends a Google Cloud Print update API request to accept (or reject) the
269 * terms-of-service of the given printer. 284 * terms-of-service of the given printer.
270 * @param {string} printerId ID of the printer to accept the 285 * @param {!print_preview.Destination} destination Destination to accept ToS
271 * terms-of-service for. 286 * for.
272 * @param {!print_preview.Destination.Origin} origin Origin of the printer. 287 * @param {boolean} isAccepted Whether the user accepted ToS or not.
273 * @param {boolean} isAccepted Whether the user accepted the
274 * terms-of-service.
275 */ 288 */
276 updatePrinterTosAcceptance: function(printerId, origin, isAccepted) { 289 updatePrinterTosAcceptance: function(destination, isAccepted) {
277 var params = [ 290 var params = [
278 new HttpParam('printerid', printerId), 291 new HttpParam('printerid', destination.id),
279 new HttpParam('is_tos_accepted', isAccepted) 292 new HttpParam('is_tos_accepted', isAccepted)
280 ]; 293 ];
281 var cpRequest = 294 this.sendOrQueueRequest_(this.buildRequest_(
282 this.buildRequest_('POST', 'update', params, origin, 295 'POST',
283 this.onUpdatePrinterTosAcceptanceDone_.bind(this)); 296 'update',
284 this.sendOrQueueRequest_(cpRequest); 297 params,
298 destination.origin,
299 destination.account,
300 this.onUpdatePrinterTosAcceptanceDone_.bind(this)));
285 }, 301 },
286 302
287 /** 303 /**
288 * Adds event listeners to relevant events. 304 * Adds event listeners to relevant events.
289 * @private 305 * @private
290 */ 306 */
291 addEventListeners_: function() { 307 addEventListeners_: function() {
292 this.tracker_.add( 308 this.tracker_.add(
293 this.nativeLayer_, 309 this.nativeLayer_,
294 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY, 310 print_preview.NativeLayer.EventType.ACCESS_TOKEN_READY,
295 this.onAccessTokenReady_.bind(this)); 311 this.onAccessTokenReady_.bind(this));
296 }, 312 },
297 313
298 /** 314 /**
299 * Builds request to the Google Cloud Print API. 315 * Builds request to the Google Cloud Print API.
300 * @param {string} method HTTP method of the request. 316 * @param {string} method HTTP method of the request.
301 * @param {string} action Google Cloud Print action to perform. 317 * @param {string} action Google Cloud Print action to perform.
302 * @param {Array.<!HttpParam>} params HTTP parameters to include in the 318 * @param {Array.<!HttpParam>} params HTTP parameters to include in the
303 * request. 319 * request.
304 * @param {!print_preview.Destination.Origin} origin Origin for destination. 320 * @param {!print_preview.Destination.Origin} origin Origin for destination.
321 * @param {?string} account Account the request is sent for. Can be
322 * {@code null} or empty string if the request is not cookie bound or
323 * is sent on behalf of the primary user.
305 * @param {function(number, Object, !print_preview.Destination.Origin)} 324 * @param {function(number, Object, !print_preview.Destination.Origin)}
306 * callback Callback to invoke when request completes. 325 * callback Callback to invoke when request completes.
307 * @return {!CloudPrintRequest} Partially prepared request. 326 * @return {!CloudPrintRequest} Partially prepared request.
308 * @private 327 * @private
309 */ 328 */
310 buildRequest_: function(method, action, params, origin, callback) { 329 buildRequest_: function(method, action, params, origin, account, callback) {
311 var url = this.baseUrl_ + '/' + action + '?xsrf='; 330 var url = this.baseUrl_ + '/' + action + '?xsrf=';
312 if (origin == print_preview.Destination.Origin.COOKIES) { 331 if (origin == print_preview.Destination.Origin.COOKIES) {
313 if (!this.xsrfToken_) { 332 var xsrfToken = this.xsrfTokens_[account];
333 if (!xsrfToken) {
314 // TODO(rltoscano): Should throw an error if not a read-only action or 334 // TODO(rltoscano): Should throw an error if not a read-only action or
315 // issue an xsrf token request. 335 // issue an xsrf token request.
316 } else { 336 } else {
317 url = url + this.xsrfToken_; 337 url = url + xsrfToken;
318 } 338 }
319 params = params || []; 339 if (account) {
320 if (this.userInfo_.activeUser) { 340 var index = this.userSessionIndex_[account] || 0;
321 var index = this.userSessionIndex_[this.userInfo_.activeUser] || 0;
322 if (index > 0) { 341 if (index > 0) {
323 params.push(new HttpParam('user', index)); 342 url += '&user=' + index;
324 } 343 }
325 } 344 }
326 } 345 }
327 var body = null; 346 var body = null;
328 if (params) { 347 if (params) {
329 if (method == 'GET') { 348 if (method == 'GET') {
330 url = params.reduce(function(partialUrl, param) { 349 url = params.reduce(function(partialUrl, param) {
331 return partialUrl + '&' + param.name + '=' + 350 return partialUrl + '&' + param.name + '=' +
332 encodeURIComponent(param.value); 351 encodeURIComponent(param.value);
333 }, url); 352 }, url);
(...skipping 15 matching lines...) Expand all
349 } 368 }
350 369
351 var xhr = new XMLHttpRequest(); 370 var xhr = new XMLHttpRequest();
352 xhr.open(method, url, true); 371 xhr.open(method, url, true);
353 xhr.withCredentials = 372 xhr.withCredentials =
354 (origin == print_preview.Destination.Origin.COOKIES); 373 (origin == print_preview.Destination.Origin.COOKIES);
355 for (var header in headers) { 374 for (var header in headers) {
356 xhr.setRequestHeader(header, headers[header]); 375 xhr.setRequestHeader(header, headers[header]);
357 } 376 }
358 377
359 return new CloudPrintRequest(xhr, body, origin, callback); 378 return new CloudPrintRequest(xhr, body, origin, account, callback);
360 }, 379 },
361 380
362 /** 381 /**
363 * Sends a request to the Google Cloud Print API or queues if it needs to 382 * Sends a request to the Google Cloud Print API or queues if it needs to
364 * wait OAuth2 access token. 383 * wait OAuth2 access token.
365 * @param {!CloudPrintRequest} request Request to send or queue. 384 * @param {!CloudPrintRequest} request Request to send or queue.
366 * @private 385 * @private
367 */ 386 */
368 sendOrQueueRequest_: function(request) { 387 sendOrQueueRequest_: function(request) {
369 if (request.origin == print_preview.Destination.Origin.COOKIES) { 388 if (request.origin == print_preview.Destination.Origin.COOKIES) {
(...skipping 30 matching lines...) Expand all
400 errorEvent.message = request.result['message']; 419 errorEvent.message = request.result['message'];
401 } else { 420 } else {
402 errorEvent.errorCode = 0; 421 errorEvent.errorCode = 0;
403 errorEvent.message = ''; 422 errorEvent.message = '';
404 } 423 }
405 errorEvent.origin = request.origin; 424 errorEvent.origin = request.origin;
406 return errorEvent; 425 return errorEvent;
407 }, 426 },
408 427
409 /** 428 /**
429 * Updates user info and session index from the {@code request} response.
430 * @param {!CloudPrintRequest} request Request to extract user info from.
431 * @private
432 */
433 setUsers_: function(request) {
434 if (request.origin == print_preview.Destination.Origin.COOKIES) {
435 var users = request.result['request']['users'] || [];
436 this.userSessionIndex_ = {};
437 for (var i = 0; i < users.length; i++) {
438 this.userSessionIndex_[users[i]] = i;
439 }
440 this.userInfo_.setUsers(request.result['request']['user'], users);
441 }
442 },
443
444 /**
445 * Terminates search requests for requested {@code origins}.
446 * @param {!Array.<print_preview.Destination.Origin>} origins Origins
447 * to terminate search requests for.
448 * @private
449 */
450 abortSearchRequests_: function(origins) {
451 this.outstandingCloudSearchRequests_ =
452 this.outstandingCloudSearchRequests_.filter(function(request) {
453 if (origins.indexOf(request.origin) >= 0) {
454 request.xhr.abort();
455 return false;
456 }
457 return true;
458 });
459 },
460
461 /**
410 * Called when a native layer receives access token. 462 * Called when a native layer receives access token.
411 * @param {Event} evt Contains the authentication type and access token. 463 * @param {Event} evt Contains the authentication type and access token.
412 * @private 464 * @private
413 */ 465 */
414 onAccessTokenReady_: function(event) { 466 onAccessTokenReady_: function(event) {
415 // TODO(vitalybuka): remove when other Origins implemented. 467 // TODO(vitalybuka): remove when other Origins implemented.
416 assert(event.authType == print_preview.Destination.Origin.DEVICE); 468 assert(event.authType == print_preview.Destination.Origin.DEVICE);
417 this.requestQueue_ = this.requestQueue_.filter(function(request) { 469 this.requestQueue_ = this.requestQueue_.filter(function(request) {
418 assert(request.origin == print_preview.Destination.Origin.DEVICE); 470 assert(request.origin == print_preview.Destination.Origin.DEVICE);
419 if (request.origin != event.authType) { 471 if (request.origin != event.authType) {
420 return true; 472 return true;
421 } 473 }
422 if (event.accessToken) { 474 if (event.accessToken) {
423 request.xhr.setRequestHeader('Authorization', 475 request.xhr.setRequestHeader('Authorization',
424 'Bearer ' + event.accessToken); 476 'Bearer ' + event.accessToken);
425 this.sendRequest_(request); 477 this.sendRequest_(request);
426 } else { // No valid token. 478 } else { // No valid token.
427 // Without abort status does not exists. 479 // Without abort status does not exist.
428 request.xhr.abort(); 480 request.xhr.abort();
429 request.callback(request); 481 request.callback(request);
430 } 482 }
431 return false; 483 return false;
432 }, this); 484 }, this);
433 }, 485 },
434 486
435 /** 487 /**
436 * Called when the ready-state of a XML http request changes. 488 * Called when the ready-state of a XML http request changes.
437 * Calls the successCallback with the result or dispatches an ERROR event. 489 * Calls the successCallback with the result or dispatches an ERROR event.
438 * @param {!CloudPrintRequest} request Request that was changed. 490 * @param {!CloudPrintRequest} request Request that was changed.
439 * @private 491 * @private
440 */ 492 */
441 onReadyStateChange_: function(request) { 493 onReadyStateChange_: function(request) {
442 if (request.xhr.readyState == 4) { 494 if (request.xhr.readyState == 4) {
443 if (request.xhr.status == 200) { 495 if (request.xhr.status == 200) {
444 request.result = JSON.parse(request.xhr.responseText); 496 request.result = JSON.parse(request.xhr.responseText);
445 if (request.origin == print_preview.Destination.Origin.COOKIES && 497 if (request.origin == print_preview.Destination.Origin.COOKIES &&
446 request.result['success']) { 498 request.result['success']) {
447 this.xsrfToken_ = request.result['xsrf_token']; 499 this.xsrfTokens_[request.result['request']['user']] =
500 request.result['xsrf_token'];
448 } 501 }
449 } 502 }
450 request.status = request.xhr.status; 503 request.status = request.xhr.status;
451 request.callback(request); 504 request.callback(request);
452 } 505 }
453 }, 506 },
454 507
455 /** 508 /**
456 * Called when the search request completes. 509 * Called when the search request completes.
457 * @param {boolean} isRecent Whether the search request was for recent 510 * @param {boolean} isRecent Whether the search request was for recent
458 * destinations. 511 * destinations.
459 * @param {!CloudPrintRequest} request Request that has been completed. 512 * @param {!CloudPrintRequest} request Request that has been completed.
460 * @private 513 * @private
461 */ 514 */
462 onSearchDone_: function(isRecent, request) { 515 onSearchDone_: function(isRecent, request) {
516 var lastRequestForThisOrigin = true;
463 this.outstandingCloudSearchRequests_ = 517 this.outstandingCloudSearchRequests_ =
464 this.outstandingCloudSearchRequests_.filter(function(item) { 518 this.outstandingCloudSearchRequests_.filter(function(item) {
519 if (item != request && item.origin == request.origin) {
520 lastRequestForThisOrigin = false;
521 }
465 return item != request; 522 return item != request;
466 }); 523 });
524 var activeUser = '';
525 if (request.origin == print_preview.Destination.Origin.COOKIES) {
526 activeUser =
527 request.result &&
528 request.result['request'] &&
529 request.result['request']['user'];
530 }
531 var event = null;
467 if (request.xhr.status == 200 && request.result['success']) { 532 if (request.xhr.status == 200 && request.result['success']) {
468 var activeUser = ''; 533 // Extract printers.
469 if (request.origin == print_preview.Destination.Origin.COOKIES) {
470 activeUser = request.result['request']['user'];
471 }
472 var printerListJson = request.result['printers'] || []; 534 var printerListJson = request.result['printers'] || [];
473 var printerList = []; 535 var printerList = [];
474 printerListJson.forEach(function(printerJson) { 536 printerListJson.forEach(function(printerJson) {
475 try { 537 try {
476 printerList.push(cloudprint.CloudDestinationParser.parse( 538 printerList.push(cloudprint.CloudDestinationParser.parse(
477 printerJson, request.origin, activeUser)); 539 printerJson, request.origin, activeUser));
478 } catch (err) { 540 } catch (err) {
479 console.error('Unable to parse cloud print destination: ' + err); 541 console.error('Unable to parse cloud print destination: ' + err);
480 } 542 }
481 }); 543 });
482 var searchDoneEvent = 544 // Extract and store users.
483 new Event(CloudPrintInterface.EventType.SEARCH_DONE); 545 this.setUsers_(request);
484 searchDoneEvent.printers = printerList; 546 // Dispatch SEARCH_DONE event.
485 searchDoneEvent.origin = request.origin; 547 event = new Event(CloudPrintInterface.EventType.SEARCH_DONE);
486 searchDoneEvent.isRecent = isRecent; 548 event.origin = request.origin;
487 if (request.origin == print_preview.Destination.Origin.COOKIES) { 549 event.printers = printerList;
488 var users = request.result['request']['users'] || []; 550 event.isRecent = isRecent;
489 this.userSessionIndex_ = {};
490 for (var i = 0; i < users.length; i++) {
491 this.userSessionIndex_[users[i]] = i;
492 }
493 this.userInfo_.setUsers(activeUser, users);
494 }
495 this.dispatchEvent(searchDoneEvent);
496 } else { 551 } else {
497 var errorEvent = this.createErrorEvent_( 552 event = this.createErrorEvent_(
498 CloudPrintInterface.EventType.SEARCH_FAILED, request); 553 CloudPrintInterface.EventType.SEARCH_FAILED,
499 this.dispatchEvent(errorEvent); 554 request);
500 } 555 }
556 event.user = activeUser;
557 event.searchDone = lastRequestForThisOrigin;
558 this.dispatchEvent(event);
501 }, 559 },
502 560
503 /** 561 /**
504 * Called when the submit request completes. 562 * Called when the submit request completes.
505 * @param {!CloudPrintRequest} request Request that has been completed. 563 * @param {!CloudPrintRequest} request Request that has been completed.
506 * @private 564 * @private
507 */ 565 */
508 onSubmitDone_: function(request) { 566 onSubmitDone_: function(request) {
509 if (request.xhr.status == 200 && request.result['success']) { 567 if (request.xhr.status == 200 && request.result['success']) {
510 var submitDoneEvent = new Event( 568 var submitDoneEvent = new Event(
511 CloudPrintInterface.EventType.SUBMIT_DONE); 569 CloudPrintInterface.EventType.SUBMIT_DONE);
512 submitDoneEvent.jobId = request.result['job']['id']; 570 submitDoneEvent.jobId = request.result['job']['id'];
513 this.dispatchEvent(submitDoneEvent); 571 this.dispatchEvent(submitDoneEvent);
514 } else { 572 } else {
515 var errorEvent = this.createErrorEvent_( 573 var errorEvent = this.createErrorEvent_(
516 CloudPrintInterface.EventType.SUBMIT_FAILED, request); 574 CloudPrintInterface.EventType.SUBMIT_FAILED, request);
517 this.dispatchEvent(errorEvent); 575 this.dispatchEvent(errorEvent);
518 } 576 }
519 }, 577 },
520 578
521 /** 579 /**
522 * Called when the printer request completes. 580 * Called when the printer request completes.
523 * @param {string} destinationId ID of the destination that was looked up. 581 * @param {string} destinationId ID of the destination that was looked up.
524 * @param {!CloudPrintRequest} request Request that has been completed. 582 * @param {!CloudPrintRequest} request Request that has been completed.
525 * @private 583 * @private
526 */ 584 */
527 onPrinterDone_: function(destinationId, request) { 585 onPrinterDone_: function(destinationId, request) {
586 // Special handling of the first printer request. It does not matter at
587 // this point, whether printer was found or not.
588 if (request.origin == print_preview.Destination.Origin.COOKIES &&
589 request.result &&
590 request.account &&
591 request.result['request']['user'] &&
592 request.result['request']['users'] &&
593 request.account != request.result['request']['user']) {
594 this.setUsers_(request);
595 // In case the user account is known, but not the primary one,
596 // activate it.
597 if (this.userSessionIndex_[request.account] > 0) {
598 this.userInfo_.activeUser = request.account;
599 // Repeat the request for the newly activated account.
600 this.printer(
601 request.result['request']['params']['printerid'],
602 request.origin,
603 request.account);
604 // Stop processing this request, wait for the new response.
605 return;
606 }
607 }
608 // Process response.
528 if (request.xhr.status == 200 && request.result['success']) { 609 if (request.xhr.status == 200 && request.result['success']) {
529 var activeUser = ''; 610 var activeUser = '';
530 if (request.origin == print_preview.Destination.Origin.COOKIES) { 611 if (request.origin == print_preview.Destination.Origin.COOKIES) {
531 activeUser = request.result['request']['user']; 612 activeUser = request.result['request']['user'];
532 } 613 }
533 var printerJson = request.result['printers'][0]; 614 var printerJson = request.result['printers'][0];
534 var printer; 615 var printer;
535 try { 616 try {
536 printer = cloudprint.CloudDestinationParser.parse( 617 printer = cloudprint.CloudDestinationParser.parse(
537 printerJson, request.origin, activeUser); 618 printerJson, request.origin, activeUser);
(...skipping 29 matching lines...) Expand all
567 this.dispatchEvent(errorEvent); 648 this.dispatchEvent(errorEvent);
568 } 649 }
569 } 650 }
570 }; 651 };
571 652
572 /** 653 /**
573 * Data structure that holds data for Cloud Print requests. 654 * Data structure that holds data for Cloud Print requests.
574 * @param {!XMLHttpRequest} xhr Partially prepared http request. 655 * @param {!XMLHttpRequest} xhr Partially prepared http request.
575 * @param {string} body Data to send with POST requests. 656 * @param {string} body Data to send with POST requests.
576 * @param {!print_preview.Destination.Origin} origin Origin for destination. 657 * @param {!print_preview.Destination.Origin} origin Origin for destination.
658 * @param {?string} account Account the request is sent for. Can be
659 * {@code null} or empty string if the request is not cookie bound or
660 * is sent on behalf of the primary user.
577 * @param {function(!CloudPrintRequest)} callback Callback to invoke when 661 * @param {function(!CloudPrintRequest)} callback Callback to invoke when
578 * request completes. 662 * request completes.
579 * @constructor 663 * @constructor
580 */ 664 */
581 function CloudPrintRequest(xhr, body, origin, callback) { 665 function CloudPrintRequest(xhr, body, origin, account, callback) {
582 /** 666 /**
583 * Partially prepared http request. 667 * Partially prepared http request.
584 * @type {!XMLHttpRequest} 668 * @type {!XMLHttpRequest}
585 */ 669 */
586 this.xhr = xhr; 670 this.xhr = xhr;
587 671
588 /** 672 /**
589 * Data to send with POST requests. 673 * Data to send with POST requests.
590 * @type {string} 674 * @type {string}
591 */ 675 */
592 this.body = body; 676 this.body = body;
593 677
594 /** 678 /**
595 * Origin for destination. 679 * Origin for destination.
596 * @type {!print_preview.Destination.Origin} 680 * @type {!print_preview.Destination.Origin}
597 */ 681 */
598 this.origin = origin; 682 this.origin = origin;
599 683
600 /** 684 /**
685 * User account this request is expected to be executed for.
686 * @type {?string}
687 */
688 this.account = account;
689
690 /**
601 * Callback to invoke when request completes. 691 * Callback to invoke when request completes.
602 * @type {function(!CloudPrintRequest)} 692 * @type {function(!CloudPrintRequest)}
603 */ 693 */
604 this.callback = callback; 694 this.callback = callback;
605 695
606 /** 696 /**
607 * Result for requests. 697 * Result for requests.
608 * @type {Object} JSON response. 698 * @type {Object} JSON response.
609 */ 699 */
610 this.result = null; 700 this.result = null;
(...skipping 17 matching lines...) Expand all
628 * @type {string} 718 * @type {string}
629 */ 719 */
630 this.value = value; 720 this.value = value;
631 }; 721 };
632 722
633 // Export 723 // Export
634 return { 724 return {
635 CloudPrintInterface: CloudPrintInterface 725 CloudPrintInterface: CloudPrintInterface
636 }; 726 };
637 }); 727 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/app_state.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698