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

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: Address CL comments. 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));
312 this.tracker_.add(
313 this.userInfo_,
314 print_preview.UserInfo.EventType.ACTIVE_USER_CHANGED,
315 this.onActiveUserChanged_.bind(this));
296 }, 316 },
297 317
298 /** 318 /**
299 * Builds request to the Google Cloud Print API. 319 * Builds request to the Google Cloud Print API.
300 * @param {string} method HTTP method of the request. 320 * @param {string} method HTTP method of the request.
301 * @param {string} action Google Cloud Print action to perform. 321 * @param {string} action Google Cloud Print action to perform.
302 * @param {Array.<!HttpParam>} params HTTP parameters to include in the 322 * @param {Array.<!HttpParam>} params HTTP parameters to include in the
303 * request. 323 * request.
304 * @param {!print_preview.Destination.Origin} origin Origin for destination. 324 * @param {!print_preview.Destination.Origin} origin Origin for destination.
325 * @param {?string} account Account the request is sent for. Can be
326 * {@code null} or empty string if the request is not cookie bound or
327 * is sent on behalf of the primary user.
305 * @param {function(number, Object, !print_preview.Destination.Origin)} 328 * @param {function(number, Object, !print_preview.Destination.Origin)}
306 * callback Callback to invoke when request completes. 329 * callback Callback to invoke when request completes.
307 * @return {!CloudPrintRequest} Partially prepared request. 330 * @return {!CloudPrintRequest} Partially prepared request.
308 * @private 331 * @private
309 */ 332 */
310 buildRequest_: function(method, action, params, origin, callback) { 333 buildRequest_: function(method, action, params, origin, account, callback) {
311 var url = this.baseUrl_ + '/' + action + '?xsrf='; 334 var url = this.baseUrl_ + '/' + action + '?xsrf=';
312 if (origin == print_preview.Destination.Origin.COOKIES) { 335 if (origin == print_preview.Destination.Origin.COOKIES) {
313 if (!this.xsrfToken_) { 336 var xsrfToken = this.xsrfTokens_[account];
337 if (!xsrfToken) {
314 // TODO(rltoscano): Should throw an error if not a read-only action or 338 // TODO(rltoscano): Should throw an error if not a read-only action or
315 // issue an xsrf token request. 339 // issue an xsrf token request.
316 } else { 340 } else {
317 url = url + this.xsrfToken_; 341 url = url + xsrfToken;
318 } 342 }
319 params = params || []; 343 if (account) {
320 if (this.userInfo_.activeUser) { 344 var index = this.userSessionIndex_[account] || 0;
321 var index = this.userSessionIndex_[this.userInfo_.activeUser] || 0;
322 if (index > 0) { 345 if (index > 0) {
323 params.push(new HttpParam('user', index)); 346 url += '&user=' + index;
324 } 347 }
325 } 348 }
326 } 349 }
327 var body = null; 350 var body = null;
328 if (params) { 351 if (params) {
329 if (method == 'GET') { 352 if (method == 'GET') {
330 url = params.reduce(function(partialUrl, param) { 353 url = params.reduce(function(partialUrl, param) {
331 return partialUrl + '&' + param.name + '=' + 354 return partialUrl + '&' + param.name + '=' +
332 encodeURIComponent(param.value); 355 encodeURIComponent(param.value);
333 }, url); 356 }, url);
(...skipping 15 matching lines...) Expand all
349 } 372 }
350 373
351 var xhr = new XMLHttpRequest(); 374 var xhr = new XMLHttpRequest();
352 xhr.open(method, url, true); 375 xhr.open(method, url, true);
353 xhr.withCredentials = 376 xhr.withCredentials =
354 (origin == print_preview.Destination.Origin.COOKIES); 377 (origin == print_preview.Destination.Origin.COOKIES);
355 for (var header in headers) { 378 for (var header in headers) {
356 xhr.setRequestHeader(header, headers[header]); 379 xhr.setRequestHeader(header, headers[header]);
357 } 380 }
358 381
359 return new CloudPrintRequest(xhr, body, origin, callback); 382 return new CloudPrintRequest(xhr, body, origin, account, callback);
360 }, 383 },
361 384
362 /** 385 /**
363 * Sends a request to the Google Cloud Print API or queues if it needs to 386 * Sends a request to the Google Cloud Print API or queues if it needs to
364 * wait OAuth2 access token. 387 * wait OAuth2 access token.
365 * @param {!CloudPrintRequest} request Request to send or queue. 388 * @param {!CloudPrintRequest} request Request to send or queue.
366 * @private 389 * @private
367 */ 390 */
368 sendOrQueueRequest_: function(request) { 391 sendOrQueueRequest_: function(request) {
369 if (request.origin == print_preview.Destination.Origin.COOKIES) { 392 if (request.origin == print_preview.Destination.Origin.COOKIES) {
(...skipping 30 matching lines...) Expand all
400 errorEvent.message = request.result['message']; 423 errorEvent.message = request.result['message'];
401 } else { 424 } else {
402 errorEvent.errorCode = 0; 425 errorEvent.errorCode = 0;
403 errorEvent.message = ''; 426 errorEvent.message = '';
404 } 427 }
405 errorEvent.origin = request.origin; 428 errorEvent.origin = request.origin;
406 return errorEvent; 429 return errorEvent;
407 }, 430 },
408 431
409 /** 432 /**
433 * Updates user info and session index from the {@code request} response.
434 * @param {!CloudPrintRequest} request Request to extract user info from.
435 * @private
436 */
437 setUsers_: function(request) {
438 if (request.origin == print_preview.Destination.Origin.COOKIES) {
439 var users = request.result['request']['users'] || [];
440 this.userSessionIndex_ = {};
441 for (var i = 0; i < users.length; i++) {
442 this.userSessionIndex_[users[i]] = i;
443 }
444 this.userInfo_.setUsers(request.result['request']['user'], users);
445 }
446 },
447
448 /**
449 * Terminates search requests for requested {@code origins}.
450 * @param {!Array.<print_preview.Destination.Origin>} origins Origins
451 * to terminate search requests for.
452 * @private
453 */
454 abortSearchRequests_: function(origins) {
455 this.outstandingCloudSearchRequests_ =
456 this.outstandingCloudSearchRequests_.filter(function(request) {
457 if (origins.indexOf(request.origin) >= 0) {
458 request.xhr.abort();
459 return false;
460 }
461 return true;
462 });
463 },
464
465 /**
466 * Called when active user account changes. Terminates all outstanding
467 * cookie based search requests.
468 * @private
469 */
470 onActiveUserChanged_: function() {
471 this.abortSearchRequests_([print_preview.Destination.Origin.COOKIES]);
472 },
473
474 /**
410 * Called when a native layer receives access token. 475 * Called when a native layer receives access token.
411 * @param {Event} evt Contains the authentication type and access token. 476 * @param {Event} evt Contains the authentication type and access token.
412 * @private 477 * @private
413 */ 478 */
414 onAccessTokenReady_: function(event) { 479 onAccessTokenReady_: function(event) {
415 // TODO(vitalybuka): remove when other Origins implemented. 480 // TODO(vitalybuka): remove when other Origins implemented.
416 assert(event.authType == print_preview.Destination.Origin.DEVICE); 481 assert(event.authType == print_preview.Destination.Origin.DEVICE);
417 this.requestQueue_ = this.requestQueue_.filter(function(request) { 482 this.requestQueue_ = this.requestQueue_.filter(function(request) {
418 assert(request.origin == print_preview.Destination.Origin.DEVICE); 483 assert(request.origin == print_preview.Destination.Origin.DEVICE);
419 if (request.origin != event.authType) { 484 if (request.origin != event.authType) {
420 return true; 485 return true;
421 } 486 }
422 if (event.accessToken) { 487 if (event.accessToken) {
423 request.xhr.setRequestHeader('Authorization', 488 request.xhr.setRequestHeader('Authorization',
424 'Bearer ' + event.accessToken); 489 'Bearer ' + event.accessToken);
425 this.sendRequest_(request); 490 this.sendRequest_(request);
426 } else { // No valid token. 491 } else { // No valid token.
427 // Without abort status does not exists. 492 // Without abort status does not exist.
428 request.xhr.abort(); 493 request.xhr.abort();
429 request.callback(request); 494 request.callback(request);
430 } 495 }
431 return false; 496 return false;
432 }, this); 497 }, this);
433 }, 498 },
434 499
435 /** 500 /**
436 * Called when the ready-state of a XML http request changes. 501 * Called when the ready-state of a XML http request changes.
437 * Calls the successCallback with the result or dispatches an ERROR event. 502 * Calls the successCallback with the result or dispatches an ERROR event.
438 * @param {!CloudPrintRequest} request Request that was changed. 503 * @param {!CloudPrintRequest} request Request that was changed.
439 * @private 504 * @private
440 */ 505 */
441 onReadyStateChange_: function(request) { 506 onReadyStateChange_: function(request) {
442 if (request.xhr.readyState == 4) { 507 if (request.xhr.readyState == 4) {
443 if (request.xhr.status == 200) { 508 if (request.xhr.status == 200) {
444 request.result = JSON.parse(request.xhr.responseText); 509 request.result = JSON.parse(request.xhr.responseText);
445 if (request.origin == print_preview.Destination.Origin.COOKIES && 510 if (request.origin == print_preview.Destination.Origin.COOKIES &&
446 request.result['success']) { 511 request.result['success'] &&
447 this.xsrfToken_ = request.result['xsrf_token']; 512 request.account) {
513 this.xsrfTokens_[request.account] = request.result['xsrf_token'];
448 } 514 }
449 } 515 }
450 request.status = request.xhr.status; 516 request.status = request.xhr.status;
451 request.callback(request); 517 request.callback(request);
452 } 518 }
453 }, 519 },
454 520
455 /** 521 /**
456 * Called when the search request completes. 522 * Called when the search request completes.
457 * @param {boolean} isRecent Whether the search request was for recent 523 * @param {boolean} isRecent Whether the search request was for recent
458 * destinations. 524 * destinations.
459 * @param {!CloudPrintRequest} request Request that has been completed. 525 * @param {!CloudPrintRequest} request Request that has been completed.
460 * @private 526 * @private
461 */ 527 */
462 onSearchDone_: function(isRecent, request) { 528 onSearchDone_: function(isRecent, request) {
529 var lastRequestForThisOrigin = true;
463 this.outstandingCloudSearchRequests_ = 530 this.outstandingCloudSearchRequests_ =
464 this.outstandingCloudSearchRequests_.filter(function(item) { 531 this.outstandingCloudSearchRequests_.filter(function(item) {
532 if (item != request && item.origin == request.origin) {
533 lastRequestForThisOrigin = false;
534 }
465 return item != request; 535 return item != request;
466 }); 536 });
537 var activeUser = '';
538 if (request.origin == print_preview.Destination.Origin.COOKIES) {
539 activeUser = request.result['request']['user'];
540 }
541 var event = null;
467 if (request.xhr.status == 200 && request.result['success']) { 542 if (request.xhr.status == 200 && request.result['success']) {
468 var activeUser = ''; 543 // Extract printers.
469 if (request.origin == print_preview.Destination.Origin.COOKIES) {
470 activeUser = request.result['request']['user'];
471 }
472 var printerListJson = request.result['printers'] || []; 544 var printerListJson = request.result['printers'] || [];
473 var printerList = []; 545 var printerList = [];
474 printerListJson.forEach(function(printerJson) { 546 printerListJson.forEach(function(printerJson) {
475 try { 547 try {
476 printerList.push(cloudprint.CloudDestinationParser.parse( 548 printerList.push(cloudprint.CloudDestinationParser.parse(
477 printerJson, request.origin, activeUser)); 549 printerJson, request.origin, activeUser));
478 } catch (err) { 550 } catch (err) {
479 console.error('Unable to parse cloud print destination: ' + err); 551 console.error('Unable to parse cloud print destination: ' + err);
480 } 552 }
481 }); 553 });
482 var searchDoneEvent = 554 // Extract and store users.
483 new Event(CloudPrintInterface.EventType.SEARCH_DONE); 555 this.setUsers_(request);
484 searchDoneEvent.printers = printerList; 556 // Dispatch SEARCH_DONE event.
485 searchDoneEvent.origin = request.origin; 557 event = new Event(CloudPrintInterface.EventType.SEARCH_DONE);
486 searchDoneEvent.isRecent = isRecent; 558 event.origin = request.origin;
487 if (request.origin == print_preview.Destination.Origin.COOKIES) { 559 event.printers = printerList;
488 var users = request.result['request']['users'] || []; 560 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 { 561 } else {
497 var errorEvent = this.createErrorEvent_( 562 event = this.createErrorEvent_(
498 CloudPrintInterface.EventType.SEARCH_FAILED, request); 563 CloudPrintInterface.EventType.SEARCH_FAILED,
499 this.dispatchEvent(errorEvent); 564 request);
500 } 565 }
566 event.user = activeUser;
567 event.searchDone = lastRequestForThisOrigin;
568 this.dispatchEvent(event);
501 }, 569 },
502 570
503 /** 571 /**
504 * Called when the submit request completes. 572 * Called when the submit request completes.
505 * @param {!CloudPrintRequest} request Request that has been completed. 573 * @param {!CloudPrintRequest} request Request that has been completed.
506 * @private 574 * @private
507 */ 575 */
508 onSubmitDone_: function(request) { 576 onSubmitDone_: function(request) {
509 if (request.xhr.status == 200 && request.result['success']) { 577 if (request.xhr.status == 200 && request.result['success']) {
510 var submitDoneEvent = new Event( 578 var submitDoneEvent = new Event(
511 CloudPrintInterface.EventType.SUBMIT_DONE); 579 CloudPrintInterface.EventType.SUBMIT_DONE);
512 submitDoneEvent.jobId = request.result['job']['id']; 580 submitDoneEvent.jobId = request.result['job']['id'];
513 this.dispatchEvent(submitDoneEvent); 581 this.dispatchEvent(submitDoneEvent);
514 } else { 582 } else {
515 var errorEvent = this.createErrorEvent_( 583 var errorEvent = this.createErrorEvent_(
516 CloudPrintInterface.EventType.SUBMIT_FAILED, request); 584 CloudPrintInterface.EventType.SUBMIT_FAILED, request);
517 this.dispatchEvent(errorEvent); 585 this.dispatchEvent(errorEvent);
518 } 586 }
519 }, 587 },
520 588
521 /** 589 /**
522 * Called when the printer request completes. 590 * Called when the printer request completes.
523 * @param {string} destinationId ID of the destination that was looked up. 591 * @param {string} destinationId ID of the destination that was looked up.
524 * @param {!CloudPrintRequest} request Request that has been completed. 592 * @param {!CloudPrintRequest} request Request that has been completed.
525 * @private 593 * @private
526 */ 594 */
527 onPrinterDone_: function(destinationId, request) { 595 onPrinterDone_: function(destinationId, request) {
596 // Special handling of the first printer request. It does not matter at
597 // this point, whether printer was found or not.
598 if (request.origin == print_preview.Destination.Origin.COOKIES &&
599 request.account &&
600 request.result['request']['user'] &&
601 request.result['request']['users'] &&
602 request.account != request.result['request']['user']) {
Toscano 2014/04/12 02:09:09 Hmm, but you left all of this parsing logic above.
Aleksey Shlyapnikov 2014/04/12 03:44:03 When the response was not created by GCP server. O
603 this.setUsers_(request);
604 // In case the user account is known, but not the primary one,
605 // activate it.
606 if (this.userSessionIndex_[request.account] > 0) {
607 this.userInfo_.activeUser = request.account;
608 // Repeat the request for the newly activated account.
609 this.printer(
610 request.result['request']['params']['printerid'],
611 request.origin,
612 request.account);
613 // Stop processing this request, wait for the new response.
614 return;
615 }
616 }
617 // Process response.
528 if (request.xhr.status == 200 && request.result['success']) { 618 if (request.xhr.status == 200 && request.result['success']) {
529 var activeUser = ''; 619 var activeUser = '';
530 if (request.origin == print_preview.Destination.Origin.COOKIES) { 620 if (request.origin == print_preview.Destination.Origin.COOKIES) {
531 activeUser = request.result['request']['user']; 621 activeUser = request.result['request']['user'];
532 } 622 }
533 var printerJson = request.result['printers'][0]; 623 var printerJson = request.result['printers'][0];
534 var printer; 624 var printer;
535 try { 625 try {
536 printer = cloudprint.CloudDestinationParser.parse( 626 printer = cloudprint.CloudDestinationParser.parse(
537 printerJson, request.origin, activeUser); 627 printerJson, request.origin, activeUser);
(...skipping 29 matching lines...) Expand all
567 this.dispatchEvent(errorEvent); 657 this.dispatchEvent(errorEvent);
568 } 658 }
569 } 659 }
570 }; 660 };
571 661
572 /** 662 /**
573 * Data structure that holds data for Cloud Print requests. 663 * Data structure that holds data for Cloud Print requests.
574 * @param {!XMLHttpRequest} xhr Partially prepared http request. 664 * @param {!XMLHttpRequest} xhr Partially prepared http request.
575 * @param {string} body Data to send with POST requests. 665 * @param {string} body Data to send with POST requests.
576 * @param {!print_preview.Destination.Origin} origin Origin for destination. 666 * @param {!print_preview.Destination.Origin} origin Origin for destination.
667 * @param {?string} account Account the request is sent for. Can be
668 * {@code null} or empty string if the request is not cookie bound or
669 * is sent on behalf of the primary user.
577 * @param {function(!CloudPrintRequest)} callback Callback to invoke when 670 * @param {function(!CloudPrintRequest)} callback Callback to invoke when
578 * request completes. 671 * request completes.
579 * @constructor 672 * @constructor
580 */ 673 */
581 function CloudPrintRequest(xhr, body, origin, callback) { 674 function CloudPrintRequest(xhr, body, origin, account, callback) {
582 /** 675 /**
583 * Partially prepared http request. 676 * Partially prepared http request.
584 * @type {!XMLHttpRequest} 677 * @type {!XMLHttpRequest}
585 */ 678 */
586 this.xhr = xhr; 679 this.xhr = xhr;
587 680
588 /** 681 /**
589 * Data to send with POST requests. 682 * Data to send with POST requests.
590 * @type {string} 683 * @type {string}
591 */ 684 */
592 this.body = body; 685 this.body = body;
593 686
594 /** 687 /**
595 * Origin for destination. 688 * Origin for destination.
596 * @type {!print_preview.Destination.Origin} 689 * @type {!print_preview.Destination.Origin}
597 */ 690 */
598 this.origin = origin; 691 this.origin = origin;
599 692
600 /** 693 /**
694 * User account this request is expected to be executed for.
695 * @type {?string}
696 */
697 this.account = account;
698
699 /**
601 * Callback to invoke when request completes. 700 * Callback to invoke when request completes.
602 * @type {function(!CloudPrintRequest)} 701 * @type {function(!CloudPrintRequest)}
603 */ 702 */
604 this.callback = callback; 703 this.callback = callback;
605 704
606 /** 705 /**
607 * Result for requests. 706 * Result for requests.
608 * @type {Object} JSON response. 707 * @type {Object} JSON response.
609 */ 708 */
610 this.result = null; 709 this.result = null;
(...skipping 17 matching lines...) Expand all
628 * @type {string} 727 * @type {string}
629 */ 728 */
630 this.value = value; 729 this.value = value;
631 }; 730 };
632 731
633 // Export 732 // Export
634 return { 733 return {
635 CloudPrintInterface: CloudPrintInterface 734 CloudPrintInterface: CloudPrintInterface
636 }; 735 };
637 }); 736 });
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