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('print_preview', function() { | 5 cr.define('print_preview', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * A data store that stores destinations and dispatches events when the data | 9 * A data store that stores destinations and dispatches events when the data |
10 * store changes. | 10 * store changes. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 this.destinationMap_ = {}; | 62 this.destinationMap_ = {}; |
63 | 63 |
64 /** | 64 /** |
65 * Currently selected destination. | 65 * Currently selected destination. |
66 * @type {print_preview.Destination} | 66 * @type {print_preview.Destination} |
67 * @private | 67 * @private |
68 */ | 68 */ |
69 this.selectedDestination_ = null; | 69 this.selectedDestination_ = null; |
70 | 70 |
71 /** | 71 /** |
72 * Initial destination ID used to auto-select the first inserted destination | |
73 * that matches. If {@code null}, the first destination inserted into the | |
74 * store will be selected. | |
75 * @type {?string} | |
76 * @private | |
77 */ | |
78 this.initialDestinationId_ = null; | |
79 | |
80 /** | |
81 * Initial origin used to auto-select destination. | |
82 * @type {print_preview.Destination.Origin} | |
83 * @private | |
84 */ | |
85 this.initialDestinationOrigin_ = print_preview.Destination.Origin.LOCAL; | |
86 | |
87 /** | |
88 * Whether the destination store will auto select the destination that | 72 * Whether the destination store will auto select the destination that |
89 * matches the initial destination. | 73 * matches the last used destination stored in appState_. |
90 * @type {boolean} | 74 * @type {boolean} |
91 * @private | 75 * @private |
92 */ | 76 */ |
93 this.isInAutoSelectMode_ = false; | 77 this.isInAutoSelectMode_ = false; |
94 | 78 |
95 /** | 79 /** |
96 * Event tracker used to track event listeners of the destination store. | 80 * Event tracker used to track event listeners of the destination store. |
97 * @type {!EventTracker} | 81 * @type {!EventTracker} |
98 * @private | 82 * @private |
99 */ | 83 */ |
100 this.tracker_ = new EventTracker(); | 84 this.tracker_ = new EventTracker(); |
101 | 85 |
102 /** | 86 /** |
103 * Used to fetch cloud-based print destinations. | 87 * Used to fetch cloud-based print destinations. |
104 * @type {print_preview.CloudPrintInterface} | 88 * @type {print_preview.CloudPrintInterface} |
105 * @private | 89 * @private |
106 */ | 90 */ |
107 this.cloudPrintInterface_ = null; | 91 this.cloudPrintInterface_ = null; |
108 | 92 |
109 /** | 93 /** |
110 * Whether the destination store has already loaded or is loading all cloud | 94 * Maps user account to the list of origins for which destinations are |
111 * destinations. | 95 * already loaded. |
112 * @type {boolean} | 96 * @type {!Object.<string, Array.<print_preview.Destination.Origin>>} |
113 * @private | 97 * @private |
114 */ | 98 */ |
115 this.hasLoadedAllCloudDestinations_ = false; | 99 this.loadedCloudOrigins_ = {}; |
116 | 100 |
117 /** | 101 /** |
118 * ID of a timeout after the initial destination ID is set. If no inserted | 102 * ID of a timeout after the initial destination ID is set. If no inserted |
119 * destination matches the initial destination ID after the specified | 103 * destination matches the initial destination ID after the specified |
120 * timeout, the first destination in the store will be automatically | 104 * timeout, the first destination in the store will be automatically |
121 * selected. | 105 * selected. |
122 * @type {?number} | 106 * @type {?number} |
123 * @private | 107 * @private |
124 */ | 108 */ |
125 this.autoSelectTimeout_ = null; | 109 this.autoSelectTimeout_ = null; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 color: { option: [{type: 'STANDARD_COLOR', is_default: true}] } | 219 color: { option: [{type: 'STANDARD_COLOR', is_default: true}] } |
236 } | 220 } |
237 }; | 221 }; |
238 return dest; | 222 return dest; |
239 }; | 223 }; |
240 | 224 |
241 DestinationStore.prototype = { | 225 DestinationStore.prototype = { |
242 __proto__: cr.EventTarget.prototype, | 226 __proto__: cr.EventTarget.prototype, |
243 | 227 |
244 /** | 228 /** |
245 * @return {!Array.<!print_preview.Destination>} List of destinations in | 229 * @param {string=} opt_account Account to filter destinations by. When |
246 * the store. | 230 * omitted, all destinations are returned. |
231 * @return {!Array.<!print_preview.Destination>} List of destinations | |
232 * accessible by the {@code account}. | |
247 */ | 233 */ |
248 get destinations() { | 234 destinations: function(opt_account) { |
249 return this.destinations_.slice(0); | 235 if (opt_account) { |
236 return this.destinations_.filter(function(destination) { | |
237 return !destination.account || destination.account == opt_account; | |
238 }); | |
239 } else { | |
240 return this.destinations_.slice(0); | |
241 } | |
250 }, | 242 }, |
251 | 243 |
252 /** | 244 /** |
253 * @return {print_preview.Destination} The currently selected destination or | 245 * @return {print_preview.Destination} The currently selected destination or |
254 * {@code null} if none is selected. | 246 * {@code null} if none is selected. |
255 */ | 247 */ |
256 get selectedDestination() { | 248 get selectedDestination() { |
257 return this.selectedDestination_; | 249 return this.selectedDestination_; |
258 }, | 250 }, |
259 | 251 |
(...skipping 11 matching lines...) Expand all Loading... | |
271 get isCloudDestinationSearchInProgress() { | 263 get isCloudDestinationSearchInProgress() { |
272 return this.cloudPrintInterface_ && | 264 return this.cloudPrintInterface_ && |
273 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; | 265 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; |
274 }, | 266 }, |
275 | 267 |
276 /** | 268 /** |
277 * Initializes the destination store. Sets the initially selected | 269 * Initializes the destination store. Sets the initially selected |
278 * destination. If any inserted destinations match this ID, that destination | 270 * destination. If any inserted destinations match this ID, that destination |
279 * will be automatically selected. This method must be called after the | 271 * will be automatically selected. This method must be called after the |
280 * print_preview.AppState has been initialized. | 272 * print_preview.AppState has been initialized. |
281 * @param {?string} systemDefaultDestinationId ID of the system default | |
282 * destination. | |
283 * @private | 273 * @private |
284 */ | 274 */ |
285 init: function(systemDefaultDestinationId) { | 275 init: function() { |
286 if (this.appState_.selectedDestinationId && | |
287 this.appState_.selectedDestinationOrigin) { | |
288 this.initialDestinationId_ = this.appState_.selectedDestinationId; | |
289 this.initialDestinationOrigin_ = | |
290 this.appState_.selectedDestinationOrigin; | |
291 } else if (systemDefaultDestinationId) { | |
Vitaly Buka (NO REVIEWS)
2014/04/11 02:32:57
separate change?
Aleksey Shlyapnikov
2014/04/11 19:09:29
Done.
| |
292 this.initialDestinationId_ = systemDefaultDestinationId; | |
293 this.initialDestinationOrigin_ = print_preview.Destination.Origin.LOCAL; | |
294 } | |
295 this.isInAutoSelectMode_ = true; | 276 this.isInAutoSelectMode_ = true; |
296 if (!this.initialDestinationId_ || !this.initialDestinationOrigin_) { | 277 if (!this.appState_.selectedDestinationId || |
278 !this.appState_.selectedDestinationOrigin) { | |
297 this.onAutoSelectFailed_(); | 279 this.onAutoSelectFailed_(); |
298 } else { | 280 } else { |
299 var key = this.getDestinationKey_(this.initialDestinationOrigin_, | 281 var key = this.getDestinationKey_( |
300 this.initialDestinationId_); | 282 this.appState_.selectedDestinationOrigin, |
283 this.appState_.selectedDestinationId, | |
284 this.appState_.selectedDestinationAccount); | |
301 var candidate = this.destinationMap_[key]; | 285 var candidate = this.destinationMap_[key]; |
302 if (candidate != null) { | 286 if (candidate != null) { |
303 this.selectDestination(candidate); | 287 this.selectDestination(candidate); |
304 } else if (this.initialDestinationOrigin_ == | 288 } else if (this.appState_.selectedDestinationOrigin == |
305 print_preview.Destination.Origin.LOCAL) { | 289 print_preview.Destination.Origin.LOCAL) { |
306 this.nativeLayer_.startGetLocalDestinationCapabilities( | 290 this.nativeLayer_.startGetLocalDestinationCapabilities( |
307 this.initialDestinationId_); | 291 this.appState_.selectedDestinationId); |
308 } else if (this.cloudPrintInterface_ && | 292 } else if (this.cloudPrintInterface_ && |
309 (this.initialDestinationOrigin_ == | 293 (this.appState_.selectedDestinationOrigin == |
310 print_preview.Destination.Origin.COOKIES || | 294 print_preview.Destination.Origin.COOKIES || |
311 this.initialDestinationOrigin_ == | 295 this.appState_.selectedDestinationOrigin == |
312 print_preview.Destination.Origin.DEVICE)) { | 296 print_preview.Destination.Origin.DEVICE)) { |
313 this.cloudPrintInterface_.printer(this.initialDestinationId_, | 297 this.cloudPrintInterface_.printer( |
314 this.initialDestinationOrigin_); | 298 this.appState_.selectedDestinationId, |
315 } else if (this.initialDestinationOrigin_ == | 299 this.appState_.selectedDestinationOrigin, |
300 this.appState_.selectedDestinationAccount); | |
301 } else if (this.appState_.selectedDestinationOrigin == | |
316 print_preview.Destination.Origin.PRIVET) { | 302 print_preview.Destination.Origin.PRIVET) { |
317 // TODO(noamsml): Resolve a specific printer instead of listing all | 303 // TODO(noamsml): Resolve a specific printer instead of listing all |
318 // privet printers in this case. | 304 // privet printers in this case. |
319 this.nativeLayer_.startGetPrivetDestinations(); | 305 this.nativeLayer_.startGetPrivetDestinations(); |
320 | 306 |
321 var destinationName = this.appState_.selectedDestinationName || ''; | 307 var destinationName = this.appState_.selectedDestinationName || ''; |
322 | 308 |
323 // Create a fake selectedDestination_ that is not actually in the | 309 // Create a fake selectedDestination_ that is not actually in the |
324 // destination store. When the real destination is created, this | 310 // destination store. When the real destination is created, this |
325 // destination will be overwritten. | 311 // destination will be overwritten. |
326 this.selectedDestination_ = new print_preview.Destination( | 312 this.selectedDestination_ = new print_preview.Destination( |
327 this.initialDestinationId_, | 313 this.appState_.selectedDestinationId, |
328 print_preview.Destination.Type.LOCAL, | 314 print_preview.Destination.Type.LOCAL, |
329 print_preview.Destination.Origin.PRIVET, | 315 print_preview.Destination.Origin.PRIVET, |
330 destinationName, | 316 destinationName, |
331 false /*isRecent*/, | 317 false /*isRecent*/, |
332 print_preview.Destination.ConnectionStatus.ONLINE); | 318 print_preview.Destination.ConnectionStatus.ONLINE); |
333 this.selectedDestination_.capabilities = | 319 this.selectedDestination_.capabilities = |
334 this.appState_.selectedDestinationCapabilities; | 320 this.appState_.selectedDestinationCapabilities; |
335 | 321 |
336 cr.dispatchSimpleEvent( | 322 cr.dispatchSimpleEvent( |
337 this, | 323 this, |
(...skipping 12 matching lines...) Expand all Loading... | |
350 */ | 336 */ |
351 setCloudPrintInterface: function(cloudPrintInterface) { | 337 setCloudPrintInterface: function(cloudPrintInterface) { |
352 this.cloudPrintInterface_ = cloudPrintInterface; | 338 this.cloudPrintInterface_ = cloudPrintInterface; |
353 this.tracker_.add( | 339 this.tracker_.add( |
354 this.cloudPrintInterface_, | 340 this.cloudPrintInterface_, |
355 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, | 341 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, |
356 this.onCloudPrintSearchDone_.bind(this)); | 342 this.onCloudPrintSearchDone_.bind(this)); |
357 this.tracker_.add( | 343 this.tracker_.add( |
358 this.cloudPrintInterface_, | 344 this.cloudPrintInterface_, |
359 cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED, | 345 cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED, |
360 this.onCloudPrintSearchFailed_.bind(this)); | 346 this.onCloudPrintSearchDone_.bind(this)); |
361 this.tracker_.add( | 347 this.tracker_.add( |
362 this.cloudPrintInterface_, | 348 this.cloudPrintInterface_, |
363 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, | 349 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, |
364 this.onCloudPrintPrinterDone_.bind(this)); | 350 this.onCloudPrintPrinterDone_.bind(this)); |
365 this.tracker_.add( | 351 this.tracker_.add( |
366 this.cloudPrintInterface_, | 352 this.cloudPrintInterface_, |
367 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, | 353 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, |
368 this.onCloudPrintPrinterFailed_.bind(this)); | 354 this.onCloudPrintPrinterFailed_.bind(this)); |
369 }, | 355 }, |
370 | 356 |
371 /** | 357 /** |
372 * @return {boolean} Whether only default cloud destinations have been | 358 * @return {boolean} Whether only default cloud destinations have been |
373 * loaded. | 359 * loaded. |
374 */ | 360 */ |
375 hasOnlyDefaultCloudDestinations: function() { | 361 hasOnlyDefaultCloudDestinations: function() { |
362 // TODO: Move the logic to print_preview. | |
376 return this.destinations_.every(function(dest) { | 363 return this.destinations_.every(function(dest) { |
377 return dest.isLocal || | 364 return dest.isLocal || |
378 dest.id == print_preview.Destination.GooglePromotedId.DOCS || | 365 dest.id == print_preview.Destination.GooglePromotedId.DOCS || |
379 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; | 366 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; |
380 }); | 367 }); |
381 }, | 368 }, |
382 | 369 |
383 /** @param {!print_preview.Destination} Destination to select. */ | 370 /** @param {!print_preview.Destination} Destination to select. */ |
384 selectDestination: function(destination) { | 371 selectDestination: function(destination) { |
372 this.isInAutoSelectMode_ = false; | |
373 this.cancelAutoSelectTimeout_(); | |
374 if (destination == this.selectedDestination_) { | |
375 return; | |
376 } | |
377 if (destination == null) { | |
378 this.selectedDestination_ = null; | |
379 cr.dispatchSimpleEvent( | |
380 this, DestinationStore.EventType.DESTINATION_SELECT); | |
381 return; | |
382 } | |
383 // Update and persist selected destination. | |
385 this.selectedDestination_ = destination; | 384 this.selectedDestination_ = destination; |
386 this.selectedDestination_.isRecent = true; | 385 this.selectedDestination_.isRecent = true; |
387 this.isInAutoSelectMode_ = false; | |
388 if (this.autoSelectTimeout_ != null) { | |
389 clearTimeout(this.autoSelectTimeout_); | |
390 this.autoSelectTimeout_ = null; | |
391 } | |
392 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && | 386 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && |
393 !destination.isTosAccepted) { | 387 !destination.isTosAccepted) { |
394 assert(this.cloudPrintInterface_ != null, | 388 assert(this.cloudPrintInterface_ != null, |
395 'Selected FedEx Office destination, but Google Cloud Print is ' + | 389 'Selected FedEx destination, but GCP API is not available'); |
396 'not enabled'); | |
397 destination.isTosAccepted = true; | 390 destination.isTosAccepted = true; |
398 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination.id, | 391 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true); |
399 destination.origin, | |
400 true); | |
401 } | 392 } |
402 this.appState_.persistSelectedDestination(this.selectedDestination_); | 393 this.appState_.persistSelectedDestination(this.selectedDestination_); |
403 | 394 // Adjust metrics. |
404 if (destination.cloudID && | 395 if (destination.cloudID && |
405 this.destinations.some(function(otherDestination) { | 396 this.destinations_.some(function(otherDestination) { |
406 return otherDestination.cloudID == destination.cloudID && | 397 return otherDestination.cloudID == destination.cloudID && |
407 otherDestination != destination; | 398 otherDestination != destination; |
408 })) { | 399 })) { |
409 if (destination.isPrivet) { | 400 if (destination.isPrivet) { |
410 this.metrics_.incrementDestinationSearchBucket( | 401 this.metrics_.incrementDestinationSearchBucket( |
411 print_preview.Metrics.DestinationSearchBucket. | 402 print_preview.Metrics.DestinationSearchBucket. |
412 PRIVET_DUPLICATE_SELECTED); | 403 PRIVET_DUPLICATE_SELECTED); |
413 } else { | 404 } else { |
414 this.metrics_.incrementDestinationSearchBucket( | 405 this.metrics_.incrementDestinationSearchBucket( |
415 print_preview.Metrics.DestinationSearchBucket. | 406 print_preview.Metrics.DestinationSearchBucket. |
416 CLOUD_DUPLICATE_SELECTED); | 407 CLOUD_DUPLICATE_SELECTED); |
417 } | 408 } |
418 } | 409 } |
419 | 410 // Notify about selected destination change. |
420 cr.dispatchSimpleEvent( | 411 cr.dispatchSimpleEvent( |
421 this, DestinationStore.EventType.DESTINATION_SELECT); | 412 this, DestinationStore.EventType.DESTINATION_SELECT); |
413 // Request destination capabilities, of not known yet. | |
422 if (destination.capabilities == null) { | 414 if (destination.capabilities == null) { |
423 if (destination.isPrivet) { | 415 if (destination.isPrivet) { |
424 this.nativeLayer_.startGetPrivetDestinationCapabilities( | 416 this.nativeLayer_.startGetPrivetDestinationCapabilities( |
425 destination.id); | 417 destination.id); |
426 } | 418 } |
427 else if (destination.isLocal) { | 419 else if (destination.isLocal) { |
428 this.nativeLayer_.startGetLocalDestinationCapabilities( | 420 this.nativeLayer_.startGetLocalDestinationCapabilities( |
429 destination.id); | 421 destination.id); |
430 } else { | 422 } else { |
431 assert(this.cloudPrintInterface_ != null, | 423 assert(this.cloudPrintInterface_ != null, |
432 'Selected destination is a cloud destination, but Google ' + | 424 'Cloud destination selected, but GCP is not enabled'); |
433 'Cloud Print is not enabled'); | 425 this.cloudPrintInterface_.printer( |
434 this.cloudPrintInterface_.printer(destination.id, | 426 destination.id, destination.origin, destination.account); |
435 destination.origin); | |
436 } | 427 } |
437 } else { | 428 } else { |
438 cr.dispatchSimpleEvent( | 429 cr.dispatchSimpleEvent( |
439 this, | 430 this, |
440 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 431 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
441 } | 432 } |
442 }, | 433 }, |
443 | 434 |
444 /** | 435 /** |
445 * Inserts a print destination to the data store and dispatches a | 436 * Selects 'Save to PDF' destination (since it always exists). |
446 * DESTINATIONS_INSERTED event. If the destination matches the initial | 437 * @private |
447 * destination ID, then the destination will be automatically selected. | |
448 * @param {!print_preview.Destination} destination Print destination to | |
449 * insert. | |
450 */ | 438 */ |
451 insertDestination: function(destination) { | 439 selectDefaultDestination_: function() { |
452 if (this.insertDestination_(destination)) { | 440 var destination = this.destinationMap_[this.getDestinationKey_( |
453 cr.dispatchSimpleEvent( | 441 print_preview.Destination.Origin.LOCAL, |
454 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | 442 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
455 if (this.isInAutoSelectMode_ && | 443 '')] || null; |
456 this.matchInitialDestination_(destination.id, destination.origin)) { | 444 assert(destination != null, 'Save to PDF printer not found'); |
457 this.selectDestination(destination); | 445 this.selectDestination(destination); |
458 } | |
459 } | |
460 }, | |
461 | |
462 /** | |
463 * Inserts multiple print destinations to the data store and dispatches one | |
464 * DESTINATIONS_INSERTED event. If any of the destinations match the initial | |
465 * destination ID, then that destination will be automatically selected. | |
466 * @param {!Array.<print_preview.Destination>} destinations Print | |
467 * destinations to insert. | |
468 */ | |
469 insertDestinations: function(destinations) { | |
470 var insertedDestination = false; | |
471 var destinationToAutoSelect = null; | |
472 destinations.forEach(function(dest) { | |
473 if (this.insertDestination_(dest)) { | |
474 insertedDestination = true; | |
475 if (this.isInAutoSelectMode_ && | |
476 destinationToAutoSelect == null && | |
477 this.matchInitialDestination_(dest.id, dest.origin)) { | |
478 destinationToAutoSelect = dest; | |
479 } | |
480 } | |
481 }, this); | |
482 if (insertedDestination) { | |
483 cr.dispatchSimpleEvent( | |
484 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | |
485 } | |
486 if (destinationToAutoSelect != null) { | |
487 this.selectDestination(destinationToAutoSelect); | |
488 } | |
489 }, | |
490 | |
491 /** | |
492 * Updates an existing print destination with capabilities and display name | |
493 * information. If the destination doesn't already exist, it will be added. | |
494 * @param {!print_preview.Destination} destination Destination to update. | |
495 * @return {!print_preview.Destination} The existing destination that was | |
496 * updated or {@code null} if it was the new destination. | |
497 */ | |
498 updateDestination: function(destination) { | |
499 assert(destination.constructor !== Array, 'Single printer expected'); | |
500 var key = this.getDestinationKey_(destination.origin, destination.id); | |
501 var existingDestination = this.destinationMap_[key]; | |
502 if (existingDestination != null) { | |
503 existingDestination.capabilities = destination.capabilities; | |
504 } else { | |
505 this.insertDestination(destination); | |
506 } | |
507 | |
508 if (existingDestination == this.selectedDestination_ || | |
509 destination == this.selectedDestination_) { | |
510 this.appState_.persistSelectedDestination(this.selectedDestination_); | |
511 cr.dispatchSimpleEvent( | |
512 this, | |
513 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | |
514 } | |
515 | |
516 return existingDestination; | |
517 }, | 446 }, |
518 | 447 |
519 /** Initiates loading of local print destinations. */ | 448 /** Initiates loading of local print destinations. */ |
520 startLoadLocalDestinations: function() { | 449 startLoadLocalDestinations: function() { |
521 if (!this.hasLoadedAllLocalDestinations_) { | 450 if (!this.hasLoadedAllLocalDestinations_) { |
522 this.hasLoadedAllLocalDestinations_ = true; | 451 this.hasLoadedAllLocalDestinations_ = true; |
523 this.nativeLayer_.startGetLocalDestinations(); | 452 this.nativeLayer_.startGetLocalDestinations(); |
524 this.isLocalDestinationSearchInProgress_ = true; | 453 this.isLocalDestinationSearchInProgress_ = true; |
525 cr.dispatchSimpleEvent( | 454 cr.dispatchSimpleEvent( |
526 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 455 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); |
(...skipping 12 matching lines...) Expand all Loading... | |
539 DestinationStore.PRIVET_SEARCH_DURATION_); | 468 DestinationStore.PRIVET_SEARCH_DURATION_); |
540 } | 469 } |
541 }, | 470 }, |
542 | 471 |
543 /** | 472 /** |
544 * Initiates loading of cloud destinations. | 473 * Initiates loading of cloud destinations. |
545 * @param {print_preview.Destination.Origin=} opt_origin Search destinations | 474 * @param {print_preview.Destination.Origin=} opt_origin Search destinations |
546 * for the specified origin only. | 475 * for the specified origin only. |
547 */ | 476 */ |
548 startLoadCloudDestinations: function(opt_origin) { | 477 startLoadCloudDestinations: function(opt_origin) { |
549 if (this.cloudPrintInterface_ != null && | 478 if (this.cloudPrintInterface_ != null) { |
550 !this.hasLoadedAllCloudDestinations_) { | 479 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; |
551 this.hasLoadedAllCloudDestinations_ = true; | 480 if (origins.length == 0 || |
552 this.cloudPrintInterface_.search(opt_origin); | 481 (opt_origin && origins.indexOf(opt_origin) < 0)) { |
553 cr.dispatchSimpleEvent( | 482 this.cloudPrintInterface_.search(opt_origin); |
554 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 483 cr.dispatchSimpleEvent( |
484 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | |
485 } | |
555 } | 486 } |
556 }, | 487 }, |
557 | 488 |
489 /** Requests load of COOKIE based cloud destinations. */ | |
490 reloadUserCookieBasedDestinations: function() { | |
491 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; | |
492 if (origins.indexOf(print_preview.Destination.Origin.COOKIES) >= 0) { | |
493 cr.dispatchSimpleEvent( | |
494 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | |
495 } else { | |
496 this.startLoadCloudDestinations( | |
497 print_preview.Destination.Origin.COOKIES); | |
498 } | |
499 }, | |
500 | |
558 /** | 501 /** |
559 * Wait for a privet device to be registered. | 502 * Wait for a privet device to be registered. |
560 */ | 503 */ |
561 waitForRegister: function(id) { | 504 waitForRegister: function(id) { |
562 this.nativeLayer_.startGetPrivetDestinations(); | 505 this.nativeLayer_.startGetPrivetDestinations(); |
563 this.waitForRegisterDestination_ = id; | 506 this.waitForRegisterDestination_ = id; |
564 }, | 507 }, |
565 | 508 |
566 /** | 509 /** |
510 * Inserts {@code destination} to the data store and dispatches a | |
511 * DESTINATIONS_INSERTED event. | |
512 * @param {!print_preview.Destination} destination Print destination to | |
513 * insert. | |
514 * @private | |
515 */ | |
516 insertDestination_: function(destination) { | |
517 if (this.insertIntoStore_(destination)) { | |
518 this.destinationsInserted_(destination); | |
519 } | |
520 }, | |
521 | |
522 /** | |
523 * Inserts multiple {@code destinations} to the data store and dispatches | |
524 * single DESTINATIONS_INSERTED event. | |
525 * @param {!Array.<print_preview.Destination>} destinations Print | |
526 * destinations to insert. | |
527 * @private | |
528 */ | |
529 insertDestinations_: function(destinations) { | |
530 var inserted = false; | |
531 destinations.forEach(function(destination) { | |
532 inserted = this.insertIntoStore_(destination) || inserted; | |
533 }, this); | |
534 if (inserted) { | |
535 this.destinationsInserted_(); | |
536 } | |
537 }, | |
538 | |
539 /** | |
540 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to | |
541 * update selected destination to match {@code appState_} settings. | |
542 * @param {print_preview.Destination=} opt_destination The only destination | |
543 * that was changed or skipped if possibly more than one destination was | |
544 * changed. Used as a hint to limit destination search scope in | |
545 * {@code isInAutoSelectMode_). | |
546 */ | |
547 destinationsInserted_: function(opt_destination) { | |
548 cr.dispatchSimpleEvent( | |
549 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | |
550 if (this.isInAutoSelectMode_) { | |
551 var destinationsToSearch = | |
552 opt_destination && [opt_destination] || this.destinations_; | |
553 destinationsToSearch.some(function(destination) { | |
554 if (this.matchPersistedDestination_(destination)) { | |
555 this.selectDestination(destination); | |
556 return true; | |
557 } | |
558 }, this); | |
559 } | |
560 }, | |
561 | |
562 /** | |
563 * Updates an existing print destination with capabilities and display name | |
564 * information. If the destination doesn't already exist, it will be added. | |
565 * @param {!print_preview.Destination} destination Destination to update. | |
566 * @return {!print_preview.Destination} The existing destination that was | |
567 * updated or {@code null} if it was the new destination. | |
568 * @private | |
569 */ | |
570 updateDestination_: function(destination) { | |
571 assert(destination.constructor !== Array, 'Single printer expected'); | |
572 var existingDestination = this.destinationMap_[this.getKey_(destination)]; | |
573 if (existingDestination != null) { | |
574 existingDestination.capabilities = destination.capabilities; | |
575 } else { | |
576 this.insertDestination_(destination); | |
577 } | |
578 | |
579 if (existingDestination == this.selectedDestination_ || | |
580 destination == this.selectedDestination_) { | |
581 this.appState_.persistSelectedDestination(this.selectedDestination_); | |
582 cr.dispatchSimpleEvent( | |
583 this, | |
584 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | |
585 } | |
586 | |
587 return existingDestination; | |
588 }, | |
589 | |
590 /** | |
567 * Called when the search for Privet printers is done. | 591 * Called when the search for Privet printers is done. |
568 * @private | 592 * @private |
569 */ | 593 */ |
570 endPrivetPrinterSearch_: function() { | 594 endPrivetPrinterSearch_: function() { |
571 this.nativeLayer_.stopGetPrivetDestinations(); | 595 this.nativeLayer_.stopGetPrivetDestinations(); |
572 this.isPrivetDestinationSearchInProgress_ = false; | 596 this.isPrivetDestinationSearchInProgress_ = false; |
573 this.hasLoadedAllPrivetDestinations_ = true; | 597 this.hasLoadedAllPrivetDestinations_ = true; |
574 cr.dispatchSimpleEvent( | 598 cr.dispatchSimpleEvent( |
575 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 599 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
576 }, | 600 }, |
577 | 601 |
578 /** | 602 /** |
579 * Inserts a destination into the store without dispatching any events. | 603 * Inserts a destination into the store without dispatching any events. |
580 * @return {boolean} Whether the inserted destination was not already in the | 604 * @return {boolean} Whether the inserted destination was not already in the |
581 * store. | 605 * store. |
582 * @private | 606 * @private |
583 */ | 607 */ |
584 insertDestination_: function(destination) { | 608 insertIntoStore_: function(destination) { |
585 var key = this.getDestinationKey_(destination.origin, destination.id); | 609 var key = this.getKey_(destination); |
586 var existingDestination = this.destinationMap_[key]; | 610 var existingDestination = this.destinationMap_[key]; |
587 if (existingDestination == null) { | 611 if (existingDestination == null) { |
588 this.destinations_.push(destination); | 612 this.destinations_.push(destination); |
589 this.destinationMap_[key] = destination; | 613 this.destinationMap_[key] = destination; |
590 return true; | 614 return true; |
591 } else if (existingDestination.connectionStatus == | 615 } else if (existingDestination.connectionStatus == |
592 print_preview.Destination.ConnectionStatus.UNKNOWN && | 616 print_preview.Destination.ConnectionStatus.UNKNOWN && |
593 destination.connectionStatus != | 617 destination.connectionStatus != |
594 print_preview.Destination.ConnectionStatus.UNKNOWN) { | 618 print_preview.Destination.ConnectionStatus.UNKNOWN) { |
595 existingDestination.connectionStatus = destination.connectionStatus; | 619 existingDestination.connectionStatus = destination.connectionStatus; |
(...skipping 25 matching lines...) Expand all Loading... | |
621 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 645 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
622 this.onDestinationsReload_.bind(this)); | 646 this.onDestinationsReload_.bind(this)); |
623 this.tracker_.add( | 647 this.tracker_.add( |
624 this.nativeLayer_, | 648 this.nativeLayer_, |
625 print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED, | 649 print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED, |
626 this.onPrivetPrinterAdded_.bind(this)); | 650 this.onPrivetPrinterAdded_.bind(this)); |
627 this.tracker_.add( | 651 this.tracker_.add( |
628 this.nativeLayer_, | 652 this.nativeLayer_, |
629 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, | 653 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, |
630 this.onPrivetCapabilitiesSet_.bind(this)); | 654 this.onPrivetCapabilitiesSet_.bind(this)); |
631 this.tracker_.add( | |
632 this.userInfo_, | |
633 print_preview.UserInfo.EventType.ACTIVE_USER_CHANGED, | |
634 this.onActiveUserChanged_.bind(this)); | |
635 }, | 655 }, |
636 | 656 |
637 /** | 657 /** |
638 * Resets the state of the destination store to its initial state. | 658 * Resets the state of the destination store to its initial state. |
639 * @private | 659 * @private |
640 */ | 660 */ |
641 reset_: function() { | 661 reset_: function() { |
642 this.destinations_ = []; | 662 this.destinations_ = []; |
643 this.destinationMap_ = {}; | 663 this.destinationMap_ = {}; |
644 this.selectedDestination_ = null; | 664 this.selectDestination(null); |
645 this.hasLoadedAllCloudDestinations_ = false; | 665 this.loadedCloudOrigins_ = {}; |
646 this.hasLoadedAllLocalDestinations_ = false; | 666 this.hasLoadedAllLocalDestinations_ = false; |
647 this.insertDestination( | 667 this.insertDestination_( |
648 DestinationStore.createLocalPdfPrintDestination_()); | 668 DestinationStore.createLocalPdfPrintDestination_()); |
649 this.resetAutoSelectTimeout_(); | 669 this.resetAutoSelectTimeout_(); |
650 }, | 670 }, |
651 | 671 |
652 /** | 672 /** |
653 * Resets the state of the destination store to its initial state. | |
654 * @private | |
655 */ | |
656 resetCookiesDestinations_: function() { | |
657 // Forget all cookies based destinations. | |
658 this.destinations_ = this.destinations_.filter(function(destination) { | |
659 if (destination.origin == print_preview.Destination.Origin.COOKIES) { | |
660 delete this.destinationMap_[ | |
661 this.getDestinationKey_(destination.origin, destination.id)]; | |
662 return false; | |
663 } | |
664 return true; | |
665 }, this); | |
666 // Reset selected destination, if necessary. | |
667 if (this.selectedDestination_ && | |
668 this.selectedDestination_.origin == | |
669 print_preview.Destination.Origin.COOKIES) { | |
670 this.selectedDestination_ = null; | |
671 } | |
672 this.hasLoadedAllCloudDestinations_ = false; | |
673 this.resetAutoSelectTimeout_(); | |
674 }, | |
675 | |
676 /** | |
677 * Resets destination auto selection timeout. | 673 * Resets destination auto selection timeout. |
678 * @private | 674 * @private |
679 */ | 675 */ |
680 resetAutoSelectTimeout_: function() { | 676 resetAutoSelectTimeout_: function() { |
677 this.cancelAutoSelectTimeout_(); | |
681 this.autoSelectTimeout_ = | 678 this.autoSelectTimeout_ = |
682 setTimeout(this.onAutoSelectFailed_.bind(this), | 679 setTimeout(this.onAutoSelectFailed_.bind(this), |
683 DestinationStore.AUTO_SELECT_TIMEOUT_); | 680 DestinationStore.AUTO_SELECT_TIMEOUT_); |
684 }, | 681 }, |
685 | 682 |
686 /** | 683 /** |
687 * Called when active user changes. Resets cookie based destinations | 684 * Cancels destination auto selection timeout. |
688 * and starts loading cloud destinations for the active user. | |
689 * @private | 685 * @private |
690 */ | 686 */ |
691 onActiveUserChanged_: function() { | 687 cancelAutoSelectTimeout_: function() { |
692 this.resetCookiesDestinations_(); | 688 if (this.autoSelectTimeout_ != null) { |
693 this.isInAutoSelectMode_ = true; | 689 clearTimeout(this.autoSelectTimeout_); |
694 this.startLoadCloudDestinations(print_preview.Destination.Origin.COOKIES); | 690 this.autoSelectTimeout_ = null; |
691 } | |
695 }, | 692 }, |
696 | 693 |
697 /** | 694 /** |
698 * Called when the local destinations have been got from the native layer. | 695 * Called when the local destinations have been got from the native layer. |
699 * @param {Event} Contains the local destinations. | 696 * @param {Event} Contains the local destinations. |
700 * @private | 697 * @private |
701 */ | 698 */ |
702 onLocalDestinationsSet_: function(event) { | 699 onLocalDestinationsSet_: function(event) { |
703 var localDestinations = event.destinationInfos.map(function(destInfo) { | 700 var localDestinations = event.destinationInfos.map(function(destInfo) { |
704 return print_preview.LocalDestinationParser.parse(destInfo); | 701 return print_preview.LocalDestinationParser.parse(destInfo); |
705 }); | 702 }); |
706 this.insertDestinations(localDestinations); | 703 this.insertDestinations_(localDestinations); |
707 this.isLocalDestinationSearchInProgress_ = false; | 704 this.isLocalDestinationSearchInProgress_ = false; |
708 cr.dispatchSimpleEvent( | 705 cr.dispatchSimpleEvent( |
709 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 706 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
710 }, | 707 }, |
711 | 708 |
712 /** | 709 /** |
713 * Called when the native layer retrieves the capabilities for the selected | 710 * Called when the native layer retrieves the capabilities for the selected |
714 * local destination. Updates the destination with new capabilities if the | 711 * local destination. Updates the destination with new capabilities if the |
715 * destination already exists, otherwise it creates a new destination and | 712 * destination already exists, otherwise it creates a new destination and |
716 * then updates its capabilities. | 713 * then updates its capabilities. |
717 * @param {Event} event Contains the capabilities of the local print | 714 * @param {Event} event Contains the capabilities of the local print |
718 * destination. | 715 * destination. |
719 * @private | 716 * @private |
720 */ | 717 */ |
721 onLocalDestinationCapabilitiesSet_: function(event) { | 718 onLocalDestinationCapabilitiesSet_: function(event) { |
722 var destinationId = event.settingsInfo['printerId']; | 719 var destinationId = event.settingsInfo['printerId']; |
723 var key = | 720 var key = this.getDestinationKey_( |
724 this.getDestinationKey_(print_preview.Destination.Origin.LOCAL, | 721 print_preview.Destination.Origin.LOCAL, |
725 destinationId); | 722 destinationId, |
723 ''); | |
726 var destination = this.destinationMap_[key]; | 724 var destination = this.destinationMap_[key]; |
727 var capabilities = print_preview.LocalCapabilitiesParser.parse( | 725 var capabilities = print_preview.LocalCapabilitiesParser.parse( |
728 event.settingsInfo); | 726 event.settingsInfo); |
729 if (destination) { | 727 if (destination) { |
730 // In case there were multiple capabilities request for this local | 728 // In case there were multiple capabilities request for this local |
731 // destination, just ignore the later ones. | 729 // destination, just ignore the later ones. |
732 if (destination.capabilities != null) { | 730 if (destination.capabilities != null) { |
733 return; | 731 return; |
734 } | 732 } |
735 destination.capabilities = capabilities; | 733 destination.capabilities = capabilities; |
736 } else { | 734 } else { |
737 // TODO(rltoscano): This makes the assumption that the "deviceName" is | 735 // TODO(rltoscano): This makes the assumption that the "deviceName" is |
738 // the same as "printerName". We should include the "printerName" in the | 736 // the same as "printerName". We should include the "printerName" in the |
739 // response. See http://crbug.com/132831. | 737 // response. See http://crbug.com/132831. |
740 destination = print_preview.LocalDestinationParser.parse( | 738 destination = print_preview.LocalDestinationParser.parse( |
741 {deviceName: destinationId, printerName: destinationId}); | 739 {deviceName: destinationId, printerName: destinationId}); |
742 destination.capabilities = capabilities; | 740 destination.capabilities = capabilities; |
743 this.insertDestination(destination); | 741 this.insertDestination_(destination); |
744 } | 742 } |
745 if (this.selectedDestination_ && | 743 if (this.selectedDestination_ && |
746 this.selectedDestination_.id == destinationId) { | 744 this.selectedDestination_.id == destinationId) { |
747 cr.dispatchSimpleEvent(this, | 745 cr.dispatchSimpleEvent(this, |
748 DestinationStore.EventType. | 746 DestinationStore.EventType. |
749 SELECTED_DESTINATION_CAPABILITIES_READY); | 747 SELECTED_DESTINATION_CAPABILITIES_READY); |
750 } | 748 } |
751 }, | 749 }, |
752 | 750 |
753 /** | 751 /** |
754 * Called when a request to get a local destination's print capabilities | 752 * Called when a request to get a local destination's print capabilities |
755 * fails. If the destination is the initial destination, auto-select another | 753 * fails. If the destination is the initial destination, auto-select another |
756 * destination instead. | 754 * destination instead. |
757 * @param {Event} event Contains the destination ID that failed. | 755 * @param {Event} event Contains the destination ID that failed. |
758 * @private | 756 * @private |
759 */ | 757 */ |
760 onGetCapabilitiesFail_: function(event) { | 758 onGetCapabilitiesFail_: function(event) { |
761 console.error('Failed to get print capabilities for printer ' + | 759 console.error('Failed to get print capabilities for printer ' + |
762 event.destinationId); | 760 event.destinationId); |
763 if (this.isInAutoSelectMode_ && | 761 if (this.isInAutoSelectMode_ && |
764 this.matchInitialDestinationStrict_(event.destinationId, | 762 this.sameAsPersistedDestination_(event.destinationId, |
765 event.destinationOrigin)) { | 763 event.destinationOrigin)) { |
766 assert(this.destinations_.length > 0, | 764 this.selectDefaultDestination_(); |
767 'No destinations were loaded when failed to get initial ' + | |
768 'destination'); | |
769 this.selectDestination(this.destinations_[0]); | |
770 } | 765 } |
771 }, | 766 }, |
772 | 767 |
773 /** | 768 /** |
774 * Called when the /search call completes. Adds the fetched destinations to | 769 * Called when the /search call completes, either successfully or not. |
775 * the destination store. | 770 * In case of success, stores fetched destinations. |
776 * @param {Event} event Contains the fetched destinations. | 771 * @param {Event} event Contains the request result. |
777 * @private | 772 * @private |
778 */ | 773 */ |
779 onCloudPrintSearchDone_: function(event) { | 774 onCloudPrintSearchDone_: function(event) { |
780 this.insertDestinations(event.printers); | 775 if (event.printers) { |
776 this.insertDestinations_(event.printers); | |
777 } | |
778 if (event.searchDone) { | |
779 var origins = this.loadedCloudOrigins_[event.user] || []; | |
780 if (origins.indexOf(event.origin) < 0) { | |
781 this.loadedCloudOrigins_[event.user] = origins.concat([event.origin]); | |
782 } | |
783 } | |
781 cr.dispatchSimpleEvent( | 784 cr.dispatchSimpleEvent( |
782 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | 785 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); |
783 }, | 786 }, |
784 | |
785 /** | |
786 * Called when the /search call fails. Updates outstanding request count and | |
787 * dispatches CLOUD_DESTINATIONS_LOADED event. | |
788 * @private | |
789 */ | |
790 onCloudPrintSearchFailed_: function() { | |
791 cr.dispatchSimpleEvent( | |
792 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); | |
793 }, | |
794 | 787 |
795 /** | 788 /** |
796 * Called when /printer call completes. Updates the specified destination's | 789 * Called when /printer call completes. Updates the specified destination's |
797 * print capabilities. | 790 * print capabilities. |
798 * @param {Event} event Contains detailed information about the | 791 * @param {Event} event Contains detailed information about the |
799 * destination. | 792 * destination. |
800 * @private | 793 * @private |
801 */ | 794 */ |
802 onCloudPrintPrinterDone_: function(event) { | 795 onCloudPrintPrinterDone_: function(event) { |
803 this.updateDestination(event.printer); | 796 this.updateDestination_(event.printer); |
804 }, | 797 }, |
805 | 798 |
806 /** | 799 /** |
807 * Called when the Google Cloud Print interface fails to lookup a | 800 * Called when the Google Cloud Print interface fails to lookup a |
808 * destination. Selects another destination if the failed destination was | 801 * destination. Selects another destination if the failed destination was |
809 * the initial destination. | 802 * the initial destination. |
810 * @param {object} event Contains the ID of the destination that was failed | 803 * @param {object} event Contains the ID of the destination that was failed |
811 * to be looked up. | 804 * to be looked up. |
812 * @private | 805 * @private |
813 */ | 806 */ |
814 onCloudPrintPrinterFailed_: function(event) { | 807 onCloudPrintPrinterFailed_: function(event) { |
815 if (this.isInAutoSelectMode_ && | 808 if (this.isInAutoSelectMode_ && |
816 this.matchInitialDestinationStrict_(event.destinationId, | 809 this.sameAsPersistedDestination_(event.destinationId, |
817 event.destinationOrigin)) { | 810 event.destinationOrigin)) { |
818 console.error('Could not find initial printer: ' + event.destinationId); | 811 console.error( |
819 assert(this.destinations_.length > 0, | 812 'Failed to fetch last used printer caps: ' + event.destinationId); |
820 'No destinations were loaded when failed to get initial ' + | 813 this.selectDefaultDestination_(); |
821 'destination'); | |
822 this.selectDestination(this.destinations_[0]); | |
823 } | 814 } |
824 }, | 815 }, |
825 | 816 |
826 /** | 817 /** |
827 * Called when a Privet printer is added to the local network. | 818 * Called when a Privet printer is added to the local network. |
828 * @param {object} event Contains information about the added printer. | 819 * @param {object} event Contains information about the added printer. |
829 * @private | 820 * @private |
830 */ | 821 */ |
831 onPrivetPrinterAdded_: function(event) { | 822 onPrivetPrinterAdded_: function(event) { |
832 if (event.printer.serviceName == this.waitForRegisterDestination_ && | 823 if (event.printer.serviceName == this.waitForRegisterDestination_ && |
833 !event.printer.isUnregistered) { | 824 !event.printer.isUnregistered) { |
834 this.waitForRegisterDestination_ = null; | 825 this.waitForRegisterDestination_ = null; |
835 this.onDestinationsReload_(); | 826 this.onDestinationsReload_(); |
836 } else { | 827 } else { |
837 this.insertDestinations( | 828 this.insertDestinations_( |
838 print_preview.PrivetDestinationParser.parse(event.printer)); | 829 print_preview.PrivetDestinationParser.parse(event.printer)); |
839 } | 830 } |
840 }, | 831 }, |
841 | 832 |
842 /** | 833 /** |
843 * Called when capabilities for a privet printer are set. | 834 * Called when capabilities for a privet printer are set. |
844 * @param {object} event Contains the capabilities and printer ID. | 835 * @param {object} event Contains the capabilities and printer ID. |
845 * @private | 836 * @private |
846 */ | 837 */ |
847 onPrivetCapabilitiesSet_: function(event) { | 838 onPrivetCapabilitiesSet_: function(event) { |
848 var destinationId = event.printerId; | 839 var destinationId = event.printerId; |
849 var destinations = | 840 var destinations = |
850 print_preview.PrivetDestinationParser.parse(event.printer); | 841 print_preview.PrivetDestinationParser.parse(event.printer); |
851 destinations.forEach(function(dest) { | 842 destinations.forEach(function(dest) { |
852 dest.capabilities = event.capabilities; | 843 dest.capabilities = event.capabilities; |
853 this.updateDestination(dest); | 844 this.updateDestination_(dest); |
854 }, this); | 845 }, this); |
855 }, | 846 }, |
856 | 847 |
857 /** | 848 /** |
858 * Called from native layer after the user was requested to sign in, and did | 849 * Called from native layer after the user was requested to sign in, and did |
859 * so successfully. | 850 * so successfully. |
860 * @private | 851 * @private |
861 */ | 852 */ |
862 onDestinationsReload_: function() { | 853 onDestinationsReload_: function() { |
863 this.reset_(); | 854 this.reset_(); |
864 this.isInAutoSelectMode_ = true; | 855 this.isInAutoSelectMode_ = true; |
865 this.startLoadLocalDestinations(); | 856 this.startLoadLocalDestinations(); |
866 this.startLoadCloudDestinations(); | 857 this.startLoadCloudDestinations(); |
867 this.startLoadPrivetDestinations(); | 858 this.startLoadPrivetDestinations(); |
868 }, | 859 }, |
869 | 860 |
870 /** | 861 /** |
871 * Called when auto-selection fails. Selects the first destination in store. | 862 * Called when auto-selection fails. Selects the first destination in store. |
872 * @private | 863 * @private |
873 */ | 864 */ |
874 onAutoSelectFailed_: function() { | 865 onAutoSelectFailed_: function() { |
875 this.autoSelectTimeout_ = null; | 866 this.cancelAutoSelectTimeout_(); |
876 assert(this.destinations_.length > 0, | 867 this.selectDefaultDestination_(); |
877 'No destinations were loaded before auto-select timeout expired'); | |
878 this.selectDestination(this.destinations_[0]); | |
879 }, | 868 }, |
880 | 869 |
881 // TODO(vitalybuka): Remove three next functions replacing Destination.id | 870 // TODO(vitalybuka): Remove three next functions replacing Destination.id |
882 // and Destination.origin by complex ID. | 871 // and Destination.origin by complex ID. |
883 /** | 872 /** |
884 * Returns key to be used with {@code destinationMap_}. | 873 * Returns key to be used with {@code destinationMap_}. |
885 * @param {!print_preview.Destination.Origin} origin Destination origin. | 874 * @param {!print_preview.Destination.Origin} origin Destination origin. |
886 * @return {!string} id Destination id. | 875 * @return {string} id Destination id. |
876 * @return {string} account User account destination is registered for. | |
887 * @private | 877 * @private |
888 */ | 878 */ |
889 getDestinationKey_: function(origin, id) { | 879 getDestinationKey_: function(origin, id, account) { |
890 return origin + '/' + id; | 880 return origin + '/' + id + '/' + account; |
891 }, | 881 }, |
892 | 882 |
893 /** | 883 /** |
894 * @param {?string} id Id of the destination. | 884 * Returns key to be used with {@code destinationMap_}. |
895 * @param {?string} origin Oring of the destination. | 885 * @param {!print_preview.Destination} destination Destination. |
896 * @return {boolean} Whether a initial destination matches provided. | |
897 * @private | 886 * @private |
898 */ | 887 */ |
899 matchInitialDestination_: function(id, origin) { | 888 getKey_: function(destination) { |
900 return this.initialDestinationId_ == null || | 889 return this.getDestinationKey_( |
901 this.initialDestinationOrigin_ == null || | 890 destination.origin, destination.id, destination.account); |
902 this.matchInitialDestinationStrict_(id, origin); | |
903 }, | 891 }, |
904 | 892 |
905 /** | 893 /** |
894 * @param {!print_preview.Destination} destination Destination to match. | |
895 * @return {boolean} Whether {@code destination} matches the last user | |
896 * selected one. | |
897 * @private | |
898 */ | |
899 matchPersistedDestination_: function(destination) { | |
900 return !this.appState_.selectedDestinationId || | |
901 !this.appState_.selectedDestinationOrigin || | |
902 this.sameAsPersistedDestination_( | |
903 destination.id, destination.origin); | |
904 }, | |
905 | |
906 /** | |
906 * @param {?string} id Id of the destination. | 907 * @param {?string} id Id of the destination. |
907 * @param {?string} origin Oring of the destination. | 908 * @param {?string} origin Oring of the destination. |
908 * @return {boolean} Whether destination is the same as initial. | 909 * @return {boolean} Whether destination is the same as initial. |
909 * @private | 910 * @private |
910 */ | 911 */ |
911 matchInitialDestinationStrict_: function(id, origin) { | 912 sameAsPersistedDestination_: function(id, origin) { |
912 return id == this.initialDestinationId_ && | 913 return id == this.appState_.selectedDestinationId && |
913 origin == this.initialDestinationOrigin_; | 914 origin == this.appState_.selectedDestinationOrigin; |
914 } | 915 } |
915 }; | 916 }; |
916 | 917 |
917 // Export | 918 // Export |
918 return { | 919 return { |
919 DestinationStore: DestinationStore | 920 DestinationStore: DestinationStore |
920 }; | 921 }; |
921 }); | 922 }); |
OLD | NEW |