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

Side by Side Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 1368013004: Define Print Preview default printer selection policy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add tags to DefaultPrinterSelection policy. Created 5 years, 2 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
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('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.
11 * @param {!Array<!print_preview.Destination.Origin>} origins Match
12 * destinations from these origins.
13 * @param {RegExp} idRegExp Match destination's id.
14 * @param {RegExp} displayNameRegExp Match destination's displayName.
15 * @param {boolean} skipVirtualDestinations Whether to ignore virtual
16 * destinations, for example, Save as PDF.
17 * @constructor
18 */
19 function DestinationMatch(
20 origins, idRegExp, displayNameRegExp, skipVirtualDestinations) {
21
22 /** @private {!Array<!print_preview.Destination.Origin>} */
23 this.origins_ = origins;
24
25 /** @private {RegExp} */
26 this.idRegExp_ = idRegExp;
27
28 /** @private {RegExp} */
29 this.displayNameRegExp_ = displayNameRegExp;
30
31 /** @private {boolean} */
32 this.skipVirtualDestinations_ = skipVirtualDestinations;
33 };
34
35 DestinationMatch.prototype = {
36
37 /**
38 * @param {!print_preview.Destination.Origin} origin Origin to match.
39 * @return {boolean} Whether the origin is one of the {@code origins_}.
40 */
41 matchOrigin: function(origin) {
42 return arrayContains(this.origins_, origin);
43 },
44
45 /**
46 * @param {string} id Id of the destination.
47 * @param {string} origin Origin of the destination.
48 * @return {boolean} Whether destination is the same as initial.
49 */
50 matchIdAndOrigin: function(id, origin) {
51 return this.matchOrigin(origin) &&
52 this.idRegExp_ &&
53 this.idRegExp_.test(id);
54 },
55
56 /**
57 * @param {!print_preview.Destination} destination Destination to match.
58 * @return {boolean} Whether {@code destination} matches the last user
59 * selected one.
60 */
61 match: function(destination) {
62 if (!this.matchOrigin(destination.origin)) {
63 return false;
64 }
65 if (this.idRegExp_ && !this.idRegExp_.test(destination.id)) {
66 return false;
67 }
68 if (this.displayNameRegExp_ &&
69 !this.displayNameRegExp_.test(destination.displayName)) {
70 return false;
71 }
72 if (this.skipVirtualDestinations_ &&
73 this.isVirtualDestination_(destination)) {
74 return false;
75 }
76 return true;
77 },
78
79 /**
80 * @param {!print_preview.Destination} destination Destination to check.
81 * @return {boolean} Whether {@code destination} is virtual, in terms of
82 * destination selection.
83 * @private
84 */
85 isVirtualDestination_: function(destination) {
86 if (destination.origin == print_preview.Destination.Origin.LOCAL) {
87 return arrayContains(
88 [print_preview.Destination.GooglePromotedId.SAVE_AS_PDF],
89 destination.id);
90 }
91 return arrayContains(
92 [print_preview.Destination.GooglePromotedId.DOCS,
93 print_preview.Destination.GooglePromotedId.FEDEX],
94 destination.id);
95 }
96 };
97
98 /**
99 * A data store that stores destinations and dispatches events when the data
100 * store changes.
11 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print 101 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print
12 * destinations. 102 * destinations.
13 * @param {!print_preview.UserInfo} userInfo User information repository. 103 * @param {!print_preview.UserInfo} userInfo User information repository.
14 * @param {!print_preview.AppState} appState Application state. 104 * @param {!print_preview.AppState} appState Application state.
15 * @constructor 105 * @constructor
16 * @extends {cr.EventTarget} 106 * @extends {cr.EventTarget}
17 */ 107 */
18 function DestinationStore(nativeLayer, userInfo, appState) { 108 function DestinationStore(nativeLayer, userInfo, appState) {
19 cr.EventTarget.call(this); 109 cr.EventTarget.call(this);
20 110
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 152
63 /** 153 /**
64 * Currently selected destination. 154 * Currently selected destination.
65 * @type {print_preview.Destination} 155 * @type {print_preview.Destination}
66 * @private 156 * @private
67 */ 157 */
68 this.selectedDestination_ = null; 158 this.selectedDestination_ = null;
69 159
70 /** 160 /**
71 * Whether the destination store will auto select the destination that 161 * Whether the destination store will auto select the destination that
72 * matches the last used destination stored in appState_. 162 * matches this set of parameters.
73 * @type {boolean} 163 * @type {print_preview.DestinationMatch}
74 * @private 164 * @private
75 */ 165 */
76 this.isInAutoSelectMode_ = false; 166 this.autoSelectMatchingDestination_ = null;
77 167
78 /** 168 /**
79 * Event tracker used to track event listeners of the destination store. 169 * Event tracker used to track event listeners of the destination store.
80 * @type {!EventTracker} 170 * @type {!EventTracker}
81 * @private 171 * @private
82 */ 172 */
83 this.tracker_ = new EventTracker(); 173 this.tracker_ = new EventTracker();
84 174
85 /** 175 /**
86 * Whether PDF printer is enabled. It's disabled, for example, in App Kiosk 176 * Whether PDF printer is enabled. It's disabled, for example, in App Kiosk
87 * mode. 177 * mode.
88 * @type {boolean} 178 * @type {boolean}
89 * @private 179 * @private
90 */ 180 */
91 this.pdfPrinterEnabled_ = false; 181 this.pdfPrinterEnabled_ = false;
92 182
93 /** 183 /**
184 * ID of the system default destination.
185 * @type {?string}
186 * @private
187 */
188 this.systemDefaultDestinationId_ = null;
189
190 /**
94 * Used to fetch cloud-based print destinations. 191 * Used to fetch cloud-based print destinations.
95 * @type {cloudprint.CloudPrintInterface} 192 * @type {cloudprint.CloudPrintInterface}
96 * @private 193 * @private
97 */ 194 */
98 this.cloudPrintInterface_ = null; 195 this.cloudPrintInterface_ = null;
99 196
100 /** 197 /**
101 * Maps user account to the list of origins for which destinations are 198 * Maps user account to the list of origins for which destinations are
102 * already loaded. 199 * already loaded.
103 * @type {!Object<Array<print_preview.Destination.Origin>>} 200 * @type {!Object<Array<print_preview.Destination.Origin>>}
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; 410 this.cloudPrintInterface_.isCloudDestinationSearchInProgress;
314 }, 411 },
315 412
316 /** 413 /**
317 * Initializes the destination store. Sets the initially selected 414 * Initializes the destination store. Sets the initially selected
318 * destination. If any inserted destinations match this ID, that destination 415 * destination. If any inserted destinations match this ID, that destination
319 * will be automatically selected. This method must be called after the 416 * will be automatically selected. This method must be called after the
320 * print_preview.AppState has been initialized. 417 * print_preview.AppState has been initialized.
321 * @param {boolean} isInAppKioskMode Whether the print preview is in App 418 * @param {boolean} isInAppKioskMode Whether the print preview is in App
322 * Kiosk mode. 419 * Kiosk mode.
323 */ 420 * @param {?string} systemDefaultDestinationId ID of the system default
324 init: function(isInAppKioskMode) { 421 * destination.
422 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized
423 * default destination selection rules.
424 */
425 init: function(
426 isInAppKioskMode,
427 systemDefaultDestinationId,
428 serializedDefaultDestinationSelectionRulesStr) {
325 this.pdfPrinterEnabled_ = !isInAppKioskMode; 429 this.pdfPrinterEnabled_ = !isInAppKioskMode;
326 this.isInAutoSelectMode_ = true; 430 this.systemDefaultDestinationId_ = systemDefaultDestinationId;
327 this.createLocalPdfPrintDestination_(); 431 this.createLocalPdfPrintDestination_();
432
328 if (!this.appState_.selectedDestinationId || 433 if (!this.appState_.selectedDestinationId ||
329 !this.appState_.selectedDestinationOrigin) { 434 !this.appState_.selectedDestinationOrigin) {
330 this.selectDefaultDestination_(); 435 var destinationMatch = this.convertToDestinationMatch_(
331 } else { 436 serializedDefaultDestinationSelectionRulesStr);
332 var key = this.getDestinationKey_( 437 if (destinationMatch) {
438 this.fetchMatchingDestination_(destinationMatch);
439 return;
440 }
441 }
442
443 if (!this.systemDefaultDestinationId_ &&
444 !(this.appState_.selectedDestinationId &&
445 this.appState_.selectedDestinationOrigin)) {
446 this.selectPdfDestination_();
447 return;
448 }
449
450 var origin = print_preview.Destination.Origin.LOCAL;
451 var id = this.systemDefaultDestinationId_;
452 var account = '';
453 var name = '';
454 var capabilities = null;
455 var extensionId = '';
456 var extensionName = '';
457 if (this.appState_.selectedDestinationId &&
458 this.appState_.selectedDestinationOrigin) {
459 origin = this.appState_.selectedDestinationOrigin;
460 id = this.appState_.selectedDestinationId;
461 account = this.appState_.selectedDestinationAccount || '';
462 name = this.appState_.selectedDestinationName || '';
463 capabilities = this.appState_.selectedDestinationCapabilities;
464 extensionId = this.appState_.selectedDestinationExtensionId || '';
465 extensionName = this.appState_.selectedDestinationExtensionName || '';
466 }
467 var candidate =
468 this.destinationMap_[this.getDestinationKey_(origin, id, account)];
469 if (candidate != null) {
470 this.selectDestination(candidate);
471 return;
472 }
473
474 if (this.fetchPreselectedDestination_(
475 origin,
476 id,
477 account,
478 name,
479 capabilities,
480 extensionId,
481 extensionName)) {
482 return;
483 }
484
485 this.selectPdfDestination_();
486 },
487
488 /**
489 * Attempts to fetch capabilities of the destination identified by the
490 * provided origin, id and account.
491 * @param {!print_preview.Destination.Origin} origin Destination origin.
492 * @param {string} id Destination id.
493 * @param {string} account User account destination is registered for.
494 * @param {string} name Destination display name.
495 * @param {?print_preview.Cdd} capabilities Destination capabilities.
496 * @param {string} extensionId Extension ID associated with this
497 * destination.
498 * @param {string} extensionName Extension name associated with this
499 * destination.
500 * @private
501 */
502 fetchPreselectedDestination_: function(
503 origin, id, account, name, capabilities, extensionId, extensionName) {
504 this.autoSelectMatchingDestination_ =
505 this.createExactDestinationMatch_(origin, id);
506
507 if (origin == print_preview.Destination.Origin.LOCAL) {
508 this.nativeLayer_.startGetLocalDestinationCapabilities(id);
509 return true;
510 }
511
512 if (this.cloudPrintInterface_ &&
513 (origin == print_preview.Destination.Origin.COOKIES ||
514 origin == print_preview.Destination.Origin.DEVICE)) {
515 this.cloudPrintInterface_.printer(id, origin, account);
516 return true;
517 }
518
519 if (origin == print_preview.Destination.Origin.PRIVET) {
520 // TODO(noamsml): Resolve a specific printer instead of listing all
521 // privet printers in this case.
522 this.nativeLayer_.startGetPrivetDestinations();
523
524 // Create a fake selectedDestination_ that is not actually in the
525 // destination store. When the real destination is created, this
526 // destination will be overwritten.
527 this.selectedDestination_ = new print_preview.Destination(
528 id,
529 print_preview.Destination.Type.LOCAL,
530 print_preview.Destination.Origin.PRIVET,
531 name,
532 false /*isRecent*/,
533 print_preview.Destination.ConnectionStatus.ONLINE);
534 this.selectedDestination_.capabilities = capabilities;
535
536 cr.dispatchSimpleEvent(
537 this,
538 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
539 return true;
540 }
541
542 if (origin == print_preview.Destination.Origin.EXTENSION) {
543 // TODO(tbarzic): Add support for requesting a single extension's
544 // printer list.
545 this.startLoadExtensionDestinations();
546
547 this.selectedDestination_ =
548 print_preview.ExtensionDestinationParser.parse({
549 extensionId: extensionId,
550 extensionName: extensionName,
551 id: id,
552 name: name
553 });
554
555 if (capabilities) {
556 this.selectedDestination_.capabilities = capabilities;
557
558 cr.dispatchSimpleEvent(
559 this,
560 DestinationStore.EventType
561 .CACHED_SELECTED_DESTINATION_INFO_READY);
562 }
563 return true;
564 }
565
566 return false;
567 },
568
569 /**
570 * Attempts to find a destination matching the provided rules.
571 * @param {!print_preview.DestinationMatch} destinationMatch Rules to match.
572 * @private
573 */
574 fetchMatchingDestination_: function(destinationMatch) {
575 this.autoSelectMatchingDestination_ = destinationMatch;
576
577 if (destinationMatch.matchOrigin(
578 print_preview.Destination.Origin.LOCAL)) {
579 this.startLoadLocalDestinations();
580 }
581 if (destinationMatch.matchOrigin(
582 print_preview.Destination.Origin.PRIVET)) {
583 this.startLoadPrivetDestinations();
584 }
585 if (destinationMatch.matchOrigin(
586 print_preview.Destination.Origin.EXTENSION)) {
587 this.startLoadExtensionDestinations();
588 }
589 if (destinationMatch.matchOrigin(
590 print_preview.Destination.Origin.COOKIES) ||
591 destinationMatch.matchOrigin(
592 print_preview.Destination.Origin.DEVICE) ||
593 destinationMatch.matchOrigin(
594 print_preview.Destination.Origin.PROFILE)) {
595 this.startLoadCloudDestinations();
596 }
597 },
598
599 /**
600 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized
601 * default destination selection rules.
602 * @return {!print_preview.DestinationMatch} Creates rules matching
603 * previously selected destination.
604 * @private
605 */
606 convertToDestinationMatch_: function(
607 serializedDefaultDestinationSelectionRulesStr) {
608 var matchRules = null;
609 try {
610 if (serializedDefaultDestinationSelectionRulesStr) {
611 matchRules =
612 JSON.parse(serializedDefaultDestinationSelectionRulesStr);
613 }
614 } catch(e) {
615 console.error(
616 'Failed to parse defaultDestinationSelectionRules: ' + e);
617 }
618 if (!matchRules)
619 return;
620
621 var isLocal = !matchRules.kind || matchRules.kind == 'local';
622 var isCloud = !matchRules.kind || matchRules.kind == 'cloud';
623 if (!isLocal && !isCloud) {
624 console.error('Unsupported type: "' + matchRules.kind + '"');
625 return null;
626 }
627
628 var origins = [];
629 if (isLocal) {
630 origins.push(print_preview.Destination.Origin.LOCAL);
631 origins.push(print_preview.Destination.Origin.PRIVET);
632 origins.push(print_preview.Destination.Origin.EXTENSION);
633 }
634 if (isCloud) {
635 origins.push(print_preview.Destination.Origin.COOKIES);
636 origins.push(print_preview.Destination.Origin.DEVICE);
637 origins.push(print_preview.Destination.Origin.PROFILE);
638 }
639
640 var idRegExp = null;
641 try {
642 if (matchRules.idPattern) {
643 idRegExp = new RegExp(matchRules.idPattern || '.*');
644 }
645 } catch (e) {
646 console.error('Failed to parse regexp for "id": ' + e);
647 }
648
649 var displayNameRegExp = null;
650 try {
651 if (matchRules.namePattern) {
652 displayNameRegExp = new RegExp(matchRules.namePattern || '.*');
653 }
654 } catch (e) {
655 console.error('Failed to parse regexp for "name": ' + e);
656 }
657
658 return new DestinationMatch(
659 origins,
660 idRegExp,
661 displayNameRegExp,
662 true /*skipVirtualDestinations*/);
663 },
664
665 /**
666 * @return {print_preview.DestinationMatch} Creates rules matching
667 * previously selected destination.
668 * @private
669 */
670 convertPreselectedToDestinationMatch_: function() {
671 if (this.appState_.selectedDestinationId &&
672 this.appState_.selectedDestinationOrigin) {
673 return this.createExactDestinationMatch_(
333 this.appState_.selectedDestinationOrigin, 674 this.appState_.selectedDestinationOrigin,
334 this.appState_.selectedDestinationId, 675 this.appState_.selectedDestinationId);
335 this.appState_.selectedDestinationAccount || ''); 676 }
336 var candidate = this.destinationMap_[key]; 677 if (this.systemDefaultDestinationId_) {
337 if (candidate != null) { 678 return this.createExactDestinationMatch_(
338 this.selectDestination(candidate); 679 print_preview.Destination.Origin.LOCAL,
339 } else if (this.appState_.selectedDestinationOrigin == 680 this.systemDefaultDestinationId_);
340 print_preview.Destination.Origin.LOCAL) { 681 }
341 this.nativeLayer_.startGetLocalDestinationCapabilities( 682 return null;
342 this.appState_.selectedDestinationId); 683 },
343 } else if (this.cloudPrintInterface_ && 684
344 (this.appState_.selectedDestinationOrigin == 685 /**
345 print_preview.Destination.Origin.COOKIES || 686 * @param {!print_preview.Destination.Origin} origin Destination origin.
346 this.appState_.selectedDestinationOrigin == 687 * @param {string} id Destination id.
347 print_preview.Destination.Origin.DEVICE)) { 688 * @return {!print_preview.DestinationMatch} Creates rules matching
348 this.cloudPrintInterface_.printer( 689 * provided destination.
349 this.appState_.selectedDestinationId, 690 * @private
350 this.appState_.selectedDestinationOrigin, 691 */
351 this.appState_.selectedDestinationAccount || ''); 692 createExactDestinationMatch_: function(origin, id) {
352 } else if (this.appState_.selectedDestinationOrigin == 693 return new DestinationMatch(
353 print_preview.Destination.Origin.PRIVET) { 694 [origin],
354 // TODO(noamsml): Resolve a specific printer instead of listing all 695 new RegExp('^' + id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$'),
355 // privet printers in this case. 696 null /*displayNameRegExp*/,
356 this.nativeLayer_.startGetPrivetDestinations(); 697 false /*skipVirtualDestinations*/);
357
358 var destinationName = this.appState_.selectedDestinationName || '';
359
360 // Create a fake selectedDestination_ that is not actually in the
361 // destination store. When the real destination is created, this
362 // destination will be overwritten.
363 this.selectedDestination_ = new print_preview.Destination(
364 this.appState_.selectedDestinationId,
365 print_preview.Destination.Type.LOCAL,
366 print_preview.Destination.Origin.PRIVET,
367 destinationName,
368 false /*isRecent*/,
369 print_preview.Destination.ConnectionStatus.ONLINE);
370 this.selectedDestination_.capabilities =
371 this.appState_.selectedDestinationCapabilities;
372
373 cr.dispatchSimpleEvent(
374 this,
375 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
376 } else if (this.appState_.selectedDestinationOrigin ==
377 print_preview.Destination.Origin.EXTENSION) {
378 // TODO(tbarzic): Add support for requesting a single extension's
379 // printer list.
380 this.startLoadExtensionDestinations();
381
382 this.selectedDestination_ =
383 print_preview.ExtensionDestinationParser.parse({
384 extensionId: this.appState_.selectedDestinationExtensionId,
385 extensionName: this.appState_.selectedDestinationExtensionName,
386 id: this.appState_.selectedDestinationId,
387 name: this.appState_.selectedDestinationName || ''
388 });
389
390 if (this.appState_.selectedDestinationCapabilities) {
391 this.selectedDestination_.capabilities =
392 this.appState_.selectedDestinationCapabilities;
393
394 cr.dispatchSimpleEvent(
395 this,
396 DestinationStore.EventType
397 .CACHED_SELECTED_DESTINATION_INFO_READY);
398 }
399 } else {
400 this.selectDefaultDestination_();
401 }
402 }
403 }, 698 },
404 699
405 /** 700 /**
406 * Sets the destination store's Google Cloud Print interface. 701 * Sets the destination store's Google Cloud Print interface.
407 * @param {!cloudprint.CloudPrintInterface} cloudPrintInterface Interface 702 * @param {!cloudprint.CloudPrintInterface} cloudPrintInterface Interface
408 * to set. 703 * to set.
409 */ 704 */
410 setCloudPrintInterface: function(cloudPrintInterface) { 705 setCloudPrintInterface: function(cloudPrintInterface) {
411 this.cloudPrintInterface_ = cloudPrintInterface; 706 this.cloudPrintInterface_ = cloudPrintInterface;
412 this.tracker_.add( 707 this.tracker_.add(
(...skipping 28 matching lines...) Expand all
441 return dest.isLocal || 736 return dest.isLocal ||
442 dest.id == print_preview.Destination.GooglePromotedId.DOCS || 737 dest.id == print_preview.Destination.GooglePromotedId.DOCS ||
443 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; 738 dest.id == print_preview.Destination.GooglePromotedId.FEDEX;
444 }); 739 });
445 }, 740 },
446 741
447 /** 742 /**
448 * @param {print_preview.Destination} destination Destination to select. 743 * @param {print_preview.Destination} destination Destination to select.
449 */ 744 */
450 selectDestination: function(destination) { 745 selectDestination: function(destination) {
451 this.isInAutoSelectMode_ = false; 746 this.autoSelectMatchingDestination_ = null;
452 // When auto select expires, DESTINATION_SELECT event has to be dispatched 747 // When auto select expires, DESTINATION_SELECT event has to be dispatched
453 // anyway (see isAutoSelectDestinationInProgress() logic). 748 // anyway (see isAutoSelectDestinationInProgress() logic).
454 if (this.autoSelectTimeout_) { 749 if (this.autoSelectTimeout_) {
455 clearTimeout(this.autoSelectTimeout_); 750 clearTimeout(this.autoSelectTimeout_);
456 this.autoSelectTimeout_ = null; 751 this.autoSelectTimeout_ = null;
457 } else if (destination == this.selectedDestination_) { 752 } else if (destination == this.selectedDestination_) {
458 return; 753 return;
459 } 754 }
460 if (destination == null) { 755 if (destination == null) {
461 this.selectedDestination_ = null; 756 this.selectedDestination_ = null;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 destination.provisionalType == 822 destination.provisionalType ==
528 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION, 823 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION,
529 'Provisional type cannot be resolved.'); 824 'Provisional type cannot be resolved.');
530 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); 825 this.nativeLayer_.grantExtensionPrinterAccess(destination.id);
531 }, 826 },
532 827
533 /** 828 /**
534 * Selects 'Save to PDF' destination (since it always exists). 829 * Selects 'Save to PDF' destination (since it always exists).
535 * @private 830 * @private
536 */ 831 */
537 selectDefaultDestination_: function() { 832 selectPdfDestination_: function() {
538 var saveToPdfKey = this.getDestinationKey_( 833 var saveToPdfKey = this.getDestinationKey_(
539 print_preview.Destination.Origin.LOCAL, 834 print_preview.Destination.Origin.LOCAL,
540 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, 835 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
541 ''); 836 '');
542 this.selectDestination( 837 this.selectDestination(
543 this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null); 838 this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null);
544 }, 839 },
545 840
841 /**
842 * Attempts to select system default destination with a fallback to
843 * 'Save to PDF' destination.
844 * @private
845 */
846 selectDefaultDestination_: function() {
847 if (this.systemDefaultDestinationId_) {
848 if (this.autoSelectMatchingDestination_ &&
849 !this.autoSelectMatchingDestination_.matchIdAndOrigin(
850 this.systemDefaultDestinationId_,
851 print_preview.Destination.Origin.LOCAL)) {
852 if (this.fetchPreselectedDestination_(
853 print_preview.Destination.Origin.LOCAL,
854 this.systemDefaultDestinationId_,
855 '' /*account*/,
856 '' /*name*/,
857 null /*capabilities*/,
858 '' /*extensionId*/,
859 '' /*extensionName*/)) {
860 return;
861 }
862 }
863 }
864 this.selectPdfDestination_();
865 },
866
546 /** Initiates loading of local print destinations. */ 867 /** Initiates loading of local print destinations. */
547 startLoadLocalDestinations: function() { 868 startLoadLocalDestinations: function() {
548 if (!this.hasLoadedAllLocalDestinations_) { 869 if (!this.hasLoadedAllLocalDestinations_) {
549 this.hasLoadedAllLocalDestinations_ = true; 870 this.hasLoadedAllLocalDestinations_ = true;
550 this.nativeLayer_.startGetLocalDestinations(); 871 this.nativeLayer_.startGetLocalDestinations();
551 this.isLocalDestinationSearchInProgress_ = true; 872 this.isLocalDestinationSearchInProgress_ = true;
552 cr.dispatchSimpleEvent( 873 cr.dispatchSimpleEvent(
553 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); 874 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
554 } 875 }
555 }, 876 },
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 destinations.forEach(function(destination) { 1025 destinations.forEach(function(destination) {
705 inserted = this.insertIntoStore_(destination) || inserted; 1026 inserted = this.insertIntoStore_(destination) || inserted;
706 }, this); 1027 }, this);
707 if (inserted) { 1028 if (inserted) {
708 this.destinationsInserted_(); 1029 this.destinationsInserted_();
709 } 1030 }
710 }, 1031 },
711 1032
712 /** 1033 /**
713 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to 1034 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to
714 * update selected destination to match {@code appState_} settings. 1035 * update selected destination to match
1036 * {@code autoSelectMatchingDestination_}.
715 * @param {print_preview.Destination=} opt_destination The only destination 1037 * @param {print_preview.Destination=} opt_destination The only destination
716 * that was changed or skipped if possibly more than one destination was 1038 * that was changed or skipped if possibly more than one destination was
717 * changed. Used as a hint to limit destination search scope in 1039 * changed. Used as a hint to limit destination search scope against
718 * {@code isInAutoSelectMode_). 1040 * {@code autoSelectMatchingDestination_).
719 */ 1041 */
720 destinationsInserted_: function(opt_destination) { 1042 destinationsInserted_: function(opt_destination) {
721 cr.dispatchSimpleEvent( 1043 cr.dispatchSimpleEvent(
722 this, DestinationStore.EventType.DESTINATIONS_INSERTED); 1044 this, DestinationStore.EventType.DESTINATIONS_INSERTED);
723 if (this.isInAutoSelectMode_) { 1045 if (this.autoSelectMatchingDestination_) {
724 var destinationsToSearch = 1046 var destinationsToSearch =
725 opt_destination && [opt_destination] || this.destinations_; 1047 opt_destination && [opt_destination] || this.destinations_;
726 destinationsToSearch.some(function(destination) { 1048 destinationsToSearch.some(function(destination) {
727 if (this.matchPersistedDestination_(destination)) { 1049 if (this.autoSelectMatchingDestination_.match(destination)) {
728 this.selectDestination(destination); 1050 this.selectDestination(destination);
729 return true; 1051 return true;
730 } 1052 }
731 }, this); 1053 }, this);
732 } 1054 }
733 }, 1055 },
734 1056
735 /** 1057 /**
736 * Updates an existing print destination with capabilities and display name 1058 * Updates an existing print destination with capabilities and display name
737 * information. If the destination doesn't already exist, it will be added. 1059 * information. If the destination doesn't already exist, it will be added.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 * Called when loading of extension managed printers is done. 1098 * Called when loading of extension managed printers is done.
777 * @private 1099 * @private
778 */ 1100 */
779 endExtensionPrinterSearch_: function() { 1101 endExtensionPrinterSearch_: function() {
780 this.isExtensionDestinationSearchInProgress_ = false; 1102 this.isExtensionDestinationSearchInProgress_ = false;
781 this.hasLoadedAllExtensionDestinations_ = true; 1103 this.hasLoadedAllExtensionDestinations_ = true;
782 cr.dispatchSimpleEvent( 1104 cr.dispatchSimpleEvent(
783 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); 1105 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
784 // Clear initially selected (cached) extension destination if it hasn't 1106 // Clear initially selected (cached) extension destination if it hasn't
785 // been found among reported extension destinations. 1107 // been found among reported extension destinations.
786 if (this.isInAutoSelectMode_ && this.selectedDestination_.isExtension) 1108 if (this.autoSelectMatchingDestination_ &&
1109 this.autoSelectMatchingDestination_.matchOrigin(
1110 print_preview.Destination.Origin.EXTENSION) &&
1111 this.selectedDestination_ &&
1112 this.selectedDestination_.isExtension) {
787 this.selectDefaultDestination_(); 1113 this.selectDefaultDestination_();
1114 }
788 }, 1115 },
789 1116
790 /** 1117 /**
791 * Inserts a destination into the store without dispatching any events. 1118 * Inserts a destination into the store without dispatching any events.
792 * @return {boolean} Whether the inserted destination was not already in the 1119 * @return {boolean} Whether the inserted destination was not already in the
793 * store. 1120 * store.
794 * @private 1121 * @private
795 */ 1122 */
796 insertIntoStore_: function(destination) { 1123 insertIntoStore_: function(destination) {
797 var key = this.getKey_(destination); 1124 var key = this.getKey_(destination);
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 /** 1288 /**
962 * Called when a request to get a local destination's print capabilities 1289 * Called when a request to get a local destination's print capabilities
963 * fails. If the destination is the initial destination, auto-select another 1290 * fails. If the destination is the initial destination, auto-select another
964 * destination instead. 1291 * destination instead.
965 * @param {Event} event Contains the destination ID that failed. 1292 * @param {Event} event Contains the destination ID that failed.
966 * @private 1293 * @private
967 */ 1294 */
968 onGetCapabilitiesFail_: function(event) { 1295 onGetCapabilitiesFail_: function(event) {
969 console.error('Failed to get print capabilities for printer ' + 1296 console.error('Failed to get print capabilities for printer ' +
970 event.destinationId); 1297 event.destinationId);
971 if (this.isInAutoSelectMode_ && 1298 if (this.autoSelectMatchingDestination_ &&
972 this.sameAsPersistedDestination_(event.destinationId, 1299 this.autoSelectMatchingDestination_.matchIdAndOrigin(
973 event.destinationOrigin)) { 1300 event.destinationId, event.destinationOrigin)) {
974 this.selectDefaultDestination_(); 1301 this.selectDefaultDestination_();
975 } 1302 }
976 }, 1303 },
977 1304
978 /** 1305 /**
979 * Called when the /search call completes, either successfully or not. 1306 * Called when the /search call completes, either successfully or not.
980 * In case of success, stores fetched destinations. 1307 * In case of success, stores fetched destinations.
981 * @param {Event} event Contains the request result. 1308 * @param {Event} event Contains the request result.
982 * @private 1309 * @private
983 */ 1310 */
(...skipping 24 matching lines...) Expand all
1008 1335
1009 /** 1336 /**
1010 * Called when the Google Cloud Print interface fails to lookup a 1337 * Called when the Google Cloud Print interface fails to lookup a
1011 * destination. Selects another destination if the failed destination was 1338 * destination. Selects another destination if the failed destination was
1012 * the initial destination. 1339 * the initial destination.
1013 * @param {Object} event Contains the ID of the destination that was failed 1340 * @param {Object} event Contains the ID of the destination that was failed
1014 * to be looked up. 1341 * to be looked up.
1015 * @private 1342 * @private
1016 */ 1343 */
1017 onCloudPrintPrinterFailed_: function(event) { 1344 onCloudPrintPrinterFailed_: function(event) {
1018 if (this.isInAutoSelectMode_ && 1345 if (this.autoSelectMatchingDestination_ &&
1019 this.sameAsPersistedDestination_(event.destinationId, 1346 this.autoSelectMatchingDestination_.matchIdAndOrigin(
1020 event.destinationOrigin)) { 1347 event.destinationId, event.destinationOrigin)) {
1021 console.error( 1348 console.error(
1022 'Failed to fetch last used printer caps: ' + event.destinationId); 1349 'Failed to fetch last used printer caps: ' + event.destinationId);
1023 this.selectDefaultDestination_(); 1350 this.selectDefaultDestination_();
1024 } 1351 }
1025 }, 1352 },
1026 1353
1027 /** 1354 /**
1028 * Called when printer sharing invitation was processed successfully. 1355 * Called when printer sharing invitation was processed successfully.
1029 * @param {Event} event Contains detailed information about the invite and 1356 * @param {Event} event Contains detailed information about the invite and
1030 * newly accepted destination (if known). 1357 * newly accepted destination (if known).
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 this.updateDestination_(destination); 1432 this.updateDestination_(destination);
1106 }, 1433 },
1107 1434
1108 /** 1435 /**
1109 * Called from native layer after the user was requested to sign in, and did 1436 * Called from native layer after the user was requested to sign in, and did
1110 * so successfully. 1437 * so successfully.
1111 * @private 1438 * @private
1112 */ 1439 */
1113 onDestinationsReload_: function() { 1440 onDestinationsReload_: function() {
1114 this.reset_(); 1441 this.reset_();
1115 this.isInAutoSelectMode_ = true; 1442 this.autoSelectMatchingDestination_ =
1443 this.convertPreselectedToDestinationMatch_();
1116 this.createLocalPdfPrintDestination_(); 1444 this.createLocalPdfPrintDestination_();
1117 this.startLoadAllDestinations(); 1445 this.startLoadAllDestinations();
1118 }, 1446 },
1119 1447
1120 // TODO(vitalybuka): Remove three next functions replacing Destination.id 1448 // TODO(vitalybuka): Remove three next functions replacing Destination.id
1121 // and Destination.origin by complex ID. 1449 // and Destination.origin by complex ID.
1122 /** 1450 /**
1123 * Returns key to be used with {@code destinationMap_}. 1451 * Returns key to be used with {@code destinationMap_}.
1124 * @param {!print_preview.Destination.Origin} origin Destination origin. 1452 * @param {!print_preview.Destination.Origin} origin Destination origin.
1125 * @param {string} id Destination id. 1453 * @param {string} id Destination id.
1126 * @param {string} account User account destination is registered for. 1454 * @param {string} account User account destination is registered for.
1127 * @private 1455 * @private
1128 */ 1456 */
1129 getDestinationKey_: function(origin, id, account) { 1457 getDestinationKey_: function(origin, id, account) {
1130 return origin + '/' + id + '/' + account; 1458 return origin + '/' + id + '/' + account;
1131 }, 1459 },
1132 1460
1133 /** 1461 /**
1134 * Returns key to be used with {@code destinationMap_}. 1462 * Returns key to be used with {@code destinationMap_}.
1135 * @param {!print_preview.Destination} destination Destination. 1463 * @param {!print_preview.Destination} destination Destination.
1136 * @private 1464 * @private
1137 */ 1465 */
1138 getKey_: function(destination) { 1466 getKey_: function(destination) {
1139 return this.getDestinationKey_( 1467 return this.getDestinationKey_(
1140 destination.origin, destination.id, destination.account); 1468 destination.origin, destination.id, destination.account);
1141 },
1142
1143 /**
1144 * @param {!print_preview.Destination} destination Destination to match.
1145 * @return {boolean} Whether {@code destination} matches the last user
1146 * selected one.
1147 * @private
1148 */
1149 matchPersistedDestination_: function(destination) {
1150 return !this.appState_.selectedDestinationId ||
1151 !this.appState_.selectedDestinationOrigin ||
1152 this.sameAsPersistedDestination_(
1153 destination.id, destination.origin);
1154 },
1155
1156 /**
1157 * @param {?string} id Id of the destination.
1158 * @param {?string} origin Oring of the destination.
1159 * @return {boolean} Whether destination is the same as initial.
1160 * @private
1161 */
1162 sameAsPersistedDestination_: function(id, origin) {
1163 return id == this.appState_.selectedDestinationId &&
1164 origin == this.appState_.selectedDestinationOrigin;
1165 } 1469 }
1166 }; 1470 };
1167 1471
1168 // Export 1472 // Export
1169 return { 1473 return {
1170 DestinationStore: DestinationStore 1474 DestinationStore: DestinationStore
1171 }; 1475 };
1172 }); 1476 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview/data/app_state.js ('k') | chrome/browser/resources/print_preview/native_layer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698