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

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

Issue 2862203002: Print Preview: Fix data/ errors (Closed)
Patch Set: Make tests pass Created 3 years, 7 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 destination.id);
94 }
95 };
96
97 /**
98 * A data store that stores destinations and dispatches events when the data
99 * store changes.
100 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print 11 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print
101 * destinations. 12 * destinations.
102 * @param {!print_preview.UserInfo} userInfo User information repository. 13 * @param {!print_preview.UserInfo} userInfo User information repository.
103 * @param {!print_preview.AppState} appState Application state. 14 * @param {!print_preview.AppState} appState Application state.
104 * @constructor 15 * @constructor
105 * @extends {cr.EventTarget} 16 * @extends {cr.EventTarget}
106 */ 17 */
107 function DestinationStore(nativeLayer, userInfo, appState) { 18 function DestinationStore(nativeLayer, userInfo, appState) {
108 cr.EventTarget.call(this); 19 cr.EventTarget.call(this);
109 20
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 /** 100 /**
190 * Used to fetch cloud-based print destinations. 101 * Used to fetch cloud-based print destinations.
191 * @type {cloudprint.CloudPrintInterface} 102 * @type {cloudprint.CloudPrintInterface}
192 * @private 103 * @private
193 */ 104 */
194 this.cloudPrintInterface_ = null; 105 this.cloudPrintInterface_ = null;
195 106
196 /** 107 /**
197 * Maps user account to the list of origins for which destinations are 108 * Maps user account to the list of origins for which destinations are
198 * already loaded. 109 * already loaded.
199 * @type {!Object<Array<print_preview.Destination.Origin>>} 110 * @type {!Object<Array<print_preview.DestinationOrigin>>}
200 * @private 111 * @private
201 */ 112 */
202 this.loadedCloudOrigins_ = {}; 113 this.loadedCloudOrigins_ = {};
203 114
204 /** 115 /**
205 * ID of a timeout after the initial destination ID is set. If no inserted 116 * ID of a timeout after the initial destination ID is set. If no inserted
206 * destination matches the initial destination ID after the specified 117 * destination matches the initial destination ID after the specified
207 * timeout, the first destination in the store will be automatically 118 * timeout, the first destination in the store will be automatically
208 * selected. 119 * selected.
209 * @type {?number} 120 * @type {?number}
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 /** 186 /**
276 * MDNS service name of destination that we are waiting to register. 187 * MDNS service name of destination that we are waiting to register.
277 * @type {?string} 188 * @type {?string}
278 * @private 189 * @private
279 */ 190 */
280 this.waitForRegisterDestination_ = null; 191 this.waitForRegisterDestination_ = null;
281 192
282 /** 193 /**
283 * Local destinations are CROS destinations on ChromeOS because they require 194 * Local destinations are CROS destinations on ChromeOS because they require
284 * extra setup. 195 * extra setup.
285 * @type {!print_preview.Destination.Origin} 196 * @type {!print_preview.DestinationOrigin}
286 * @private 197 * @private
287 */ 198 */
288 this.platformOrigin_ = cr.isChromeOS ? 199 this.platformOrigin_ = cr.isChromeOS ?
289 print_preview.Destination.Origin.CROS : 200 print_preview.DestinationOrigin.CROS :
290 print_preview.Destination.Origin.LOCAL; 201 print_preview.DestinationOrigin.LOCAL;
291 202
292 this.addEventListeners_(); 203 this.addEventListeners_();
293 this.reset_(); 204 this.reset_();
294 }; 205 }
295 206
296 /** 207 /**
297 * Event types dispatched by the data store. 208 * Event types dispatched by the data store.
298 * @enum {string} 209 * @enum {string}
299 */ 210 */
300 DestinationStore.EventType = { 211 DestinationStore.EventType = {
301 DESTINATION_SEARCH_DONE: 212 DESTINATION_SEARCH_DONE:
302 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE', 213 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE',
303 DESTINATION_SEARCH_STARTED: 214 DESTINATION_SEARCH_STARTED:
304 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED', 215 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED',
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 'PRC_5': 'prc5 Envelope', 417 'PRC_5': 'prc5 Envelope',
507 'PRC_6': 'prc6 Envelope', 418 'PRC_6': 'prc6 Envelope',
508 'PRC_7': 'prc7 Envelope', 419 'PRC_7': 'prc7 Envelope',
509 'PRC_8': 'prc8 Envelope', 420 'PRC_8': 'prc8 Envelope',
510 'ROC_16K': 'ROC 16K', 421 'ROC_16K': 'ROC 16K',
511 'ROC_8K': 'ROC 8k', 422 'ROC_8K': 'ROC 8k',
512 }; 423 };
513 424
514 /** 425 /**
515 * Localizes printer capabilities. 426 * Localizes printer capabilities.
516 * @param {!Object} capabilities Printer capabilities to localize. 427 * @param {!print_preview.Cdd} capabilities Printer capabilities to localize.
517 * @return {!Object} Localized capabilities. 428 * @return {!print_preview.Cdd} Localized capabilities.
518 * @private 429 * @private
519 */ 430 */
520 DestinationStore.localizeCapabilities_ = function(capabilities) { 431 DestinationStore.localizeCapabilities_ = function(capabilities) {
521 if (!capabilities.printer) 432 if (!capabilities.printer)
522 return capabilities; 433 return capabilities;
523 434
524 var mediaSize = capabilities.printer.media_size; 435 var mediaSize = capabilities.printer.media_size;
525 if (!mediaSize) 436 if (!mediaSize)
526 return capabilities; 437 return capabilities;
527 438
528 for (var i = 0, media; media = mediaSize.option[i]; i++) { 439 for (var i = 0, media; (media = mediaSize.option[i]); i++) {
529 // No need to patch capabilities with localized names provided. 440 // No need to patch capabilities with localized names provided.
530 if (!media.custom_display_name_localized) { 441 if (!media.custom_display_name_localized) {
531 media.custom_display_name = 442 media.custom_display_name =
532 media.custom_display_name || 443 media.custom_display_name ||
533 DestinationStore.MEDIA_DISPLAY_NAMES_[media.name] || 444 DestinationStore.MEDIA_DISPLAY_NAMES_[media.name] ||
534 media.name; 445 media.name;
535 } 446 }
536 } 447 }
537 return capabilities; 448 return capabilities;
538 }; 449 };
539 450
540 /** 451 /**
541 * Compare two media sizes by their names. 452 * Compare two media sizes by their names.
542 * @param {!Object} a Media to compare. 453 * @param {!Object} a Media to compare.
543 * @param {!Object} b Media to compare. 454 * @param {!Object} b Media to compare.
544 * @return {number} 1 if a > b, -1 if a < b, or 0 if a == b. 455 * @return {number} 1 if a > b, -1 if a < b, or 0 if a == b.
545 * @private 456 * @private
546 */ 457 */
547 DestinationStore.compareMediaNames_ = function(a, b) { 458 DestinationStore.compareMediaNames_ = function(a, b) {
548 var nameA = a.custom_display_name_localized || a.custom_display_name; 459 var nameA = a.custom_display_name_localized || a.custom_display_name;
549 var nameB = b.custom_display_name_localized || b.custom_display_name; 460 var nameB = b.custom_display_name_localized || b.custom_display_name;
550 return nameA == nameB ? 0 : (nameA > nameB ? 1 : -1); 461 return nameA == nameB ? 0 : (nameA > nameB ? 1 : -1);
551 }; 462 };
552 463
553 /** 464 /**
554 * Sort printer media sizes. 465 * Sort printer media sizes.
555 * @param {!Object} capabilities Printer capabilities to localize. 466 * @param {!print_preview.Cdd} capabilities Printer capabilities to localize.
556 * @return {!Object} Localized capabilities. 467 * @return {!print_preview.Cdd} Localized capabilities.
557 * @private 468 * @private
558 */ 469 */
559 DestinationStore.sortMediaSizes_ = function(capabilities) { 470 DestinationStore.sortMediaSizes_ = function(capabilities) {
560 if (!capabilities.printer) 471 if (!capabilities.printer)
561 return capabilities; 472 return capabilities;
562 473
563 var mediaSize = capabilities.printer.media_size; 474 var mediaSize = capabilities.printer.media_size;
564 if (!mediaSize) 475 if (!mediaSize)
565 return capabilities; 476 return capabilities;
566 477
567 // For the standard sizes, separate into categories, as seen in the Cloud 478 // For the standard sizes, separate into categories, as seen in the Cloud
568 // Print CDD guide: 479 // Print CDD guide:
569 // - North American 480 // - North American
570 // - Chinese 481 // - Chinese
571 // - ISO 482 // - ISO
572 // - Japanese 483 // - Japanese
573 // - Other metric 484 // - Other metric
574 // Otherwise, assume they are custom sizes. 485 // Otherwise, assume they are custom sizes.
575 var categoryStandardNA = []; 486 var categoryStandardNA = [];
576 var categoryStandardCN = []; 487 var categoryStandardCN = [];
577 var categoryStandardISO = []; 488 var categoryStandardISO = [];
578 var categoryStandardJP = []; 489 var categoryStandardJP = [];
579 var categoryStandardMisc = []; 490 var categoryStandardMisc = [];
580 var categoryCustom = []; 491 var categoryCustom = [];
581 for (var i = 0, media; media = mediaSize.option[i]; i++) { 492 for (var i = 0, media; (media = mediaSize.option[i]); i++) {
582 var name = media.name || 'CUSTOM'; 493 var name = media.name || 'CUSTOM';
583 var category; 494 var category;
584 if (name.startsWith('NA_')) { 495 if (name.startsWith('NA_')) {
585 category = categoryStandardNA; 496 category = categoryStandardNA;
586 } else if (name.startsWith('PRC_') || name.startsWith('ROC_') || 497 } else if (name.startsWith('PRC_') || name.startsWith('ROC_') ||
587 name == 'OM_DAI_PA_KAI' || name == 'OM_JUURO_KU_KAI' || 498 name == 'OM_DAI_PA_KAI' || name == 'OM_JUURO_KU_KAI' ||
588 name == 'OM_PA_KAI') { 499 name == 'OM_PA_KAI') {
589 category = categoryStandardCN; 500 category = categoryStandardCN;
590 } else if (name.startsWith('ISO_')) { 501 } else if (name.startsWith('ISO_')) {
591 category = categoryStandardISO; 502 category = categoryStandardISO;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 }, 568 },
658 569
659 /** 570 /**
660 * @return {boolean} Whether a search for cloud destinations is in progress. 571 * @return {boolean} Whether a search for cloud destinations is in progress.
661 */ 572 */
662 get isCloudDestinationSearchInProgress() { 573 get isCloudDestinationSearchInProgress() {
663 return !!this.cloudPrintInterface_ && 574 return !!this.cloudPrintInterface_ &&
664 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; 575 this.cloudPrintInterface_.isCloudDestinationSearchInProgress;
665 }, 576 },
666 577
667 /* 578 /**
668 * Initializes the destination store. Sets the initially selected 579 * Initializes the destination store. Sets the initially selected
669 * destination. If any inserted destinations match this ID, that destination 580 * destination. If any inserted destinations match this ID, that destination
670 * will be automatically selected. This method must be called after the 581 * will be automatically selected. This method must be called after the
671 * print_preview.AppState has been initialized. 582 * print_preview.AppState has been initialized.
672 * @param {boolean} isInAppKioskMode Whether the print preview is in App 583 * @param {boolean} isInAppKioskMode Whether the print preview is in App
673 * Kiosk mode. 584 * Kiosk mode.
674 * @param {?string} systemDefaultDestinationId ID of the system default 585 * @param {?string} systemDefaultDestinationId ID of the system default
675 * destination. 586 * destination.
676 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized 587 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized
677 * default destination selection rules. 588 * default destination selection rules.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 name, 650 name,
740 capabilities, 651 capabilities,
741 extensionId, 652 extensionId,
742 extensionName); 653 extensionName);
743 } 654 }
744 } 655 }
745 } 656 }
746 if (foundDestination) return; 657 if (foundDestination) return;
747 658
748 // Try the system default 659 // Try the system default
749 id = this.systemDefaultDestinationId_; 660 id = this.systemDefaultDestinationId_ || '';
750 origin = id == print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? 661 origin = id == print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ?
751 print_preview.Destination.Origin.LOCAL : 662 print_preview.DestinationOrigin.LOCAL :
752 this.platformOrigin_; 663 this.platformOrigin_;
753 account = ''; 664 account = '';
754 var candidate = 665 var candidate =
755 this.destinationMap_[this.getDestinationKey_(origin, id, account)]; 666 this.destinationMap_[this.getDestinationKey_(origin, id, account)];
756 if (candidate != null) { 667 if (candidate != null) {
757 this.selectDestination(candidate); 668 this.selectDestination(candidate);
758 return; 669 return;
759 } 670 }
760 671
761 if (this.fetchPreselectedDestination_( 672 if (this.fetchPreselectedDestination_(
762 origin, 673 origin,
763 id, 674 id,
764 account, 675 account,
765 name, 676 name,
766 capabilities, 677 capabilities,
767 extensionId, 678 extensionId,
768 extensionName)) { 679 extensionName)) {
769 return; 680 return;
770 } 681 }
771 682
772 this.selectPdfDestination_(); 683 this.selectPdfDestination_();
773 }, 684 },
774 685
775 /** 686 /**
776 * Attempts to fetch capabilities of the destination identified by the 687 * Attempts to fetch capabilities of the destination identified by the
777 * provided origin, id and account. 688 * provided origin, id and account.
778 * @param {!print_preview.Destination.Origin} origin Destination origin. 689 * @param {string | print_preview.DestinationOrigin} origin Destination
690 * origin.
779 * @param {string} id Destination id. 691 * @param {string} id Destination id.
780 * @param {string} account User account destination is registered for. 692 * @param {string} account User account destination is registered for.
781 * @param {string} name Destination display name. 693 * @param {string} name Destination display name.
782 * @param {?print_preview.Cdd} capabilities Destination capabilities. 694 * @param {?print_preview.Cdd} capabilities Destination capabilities.
783 * @param {string} extensionId Extension ID associated with this 695 * @param {string} extensionId Extension ID associated with this
784 * destination. 696 * destination.
785 * @param {string} extensionName Extension name associated with this 697 * @param {string} extensionName Extension name associated with this
786 * destination. 698 * destination.
787 * @private 699 * @private
788 */ 700 */
789 fetchPreselectedDestination_: function( 701 fetchPreselectedDestination_: function(
790 origin, id, account, name, capabilities, extensionId, extensionName) { 702 origin, id, account, name, capabilities, extensionId, extensionName) {
791 this.autoSelectMatchingDestination_ = 703 this.autoSelectMatchingDestination_ =
792 this.createExactDestinationMatch_(origin, id); 704 this.createExactDestinationMatch_(origin, id);
793 705
794 if (origin == print_preview.Destination.Origin.LOCAL || 706 if (origin == print_preview.DestinationOrigin.LOCAL ||
795 origin == print_preview.Destination.Origin.CROS) { 707 origin == print_preview.DestinationOrigin.CROS) {
796 this.nativeLayer_.startGetLocalDestinationCapabilities(id); 708 this.nativeLayer_.startGetLocalDestinationCapabilities(id);
797 return true; 709 return true;
798 } 710 }
799 711
800 if (this.cloudPrintInterface_ && 712 if (this.cloudPrintInterface_ &&
801 (origin == print_preview.Destination.Origin.COOKIES || 713 (origin == print_preview.DestinationOrigin.COOKIES ||
802 origin == print_preview.Destination.Origin.DEVICE)) { 714 origin == print_preview.DestinationOrigin.DEVICE)) {
803 this.cloudPrintInterface_.printer(id, origin, account); 715 this.cloudPrintInterface_.printer(
716 id,
717 /** @type {print_preview.DestinationOrigin} */(origin),
718 account);
804 return true; 719 return true;
805 } 720 }
806 721
807 if (origin == print_preview.Destination.Origin.PRIVET) { 722 if (origin == print_preview.DestinationOrigin.PRIVET) {
808 // TODO(noamsml): Resolve a specific printer instead of listing all 723 // TODO(noamsml): Resolve a specific printer instead of listing all
809 // privet printers in this case. 724 // privet printers in this case.
810 this.nativeLayer_.startGetPrivetDestinations(); 725 this.nativeLayer_.startGetPrivetDestinations();
811 726
812 // Create a fake selectedDestination_ that is not actually in the 727 // Create a fake selectedDestination_ that is not actually in the
813 // destination store. When the real destination is created, this 728 // destination store. When the real destination is created, this
814 // destination will be overwritten. 729 // destination will be overwritten.
815 this.selectedDestination_ = new print_preview.Destination( 730 this.selectedDestination_ = new print_preview.Destination(
816 id, 731 id,
817 print_preview.Destination.Type.LOCAL, 732 print_preview.DestinationType.LOCAL,
818 print_preview.Destination.Origin.PRIVET, 733 print_preview.DestinationOrigin.PRIVET,
819 name, 734 name,
820 false /*isRecent*/, 735 false /*isRecent*/,
821 print_preview.Destination.ConnectionStatus.ONLINE); 736 print_preview.DestinationConnectionStatus.ONLINE);
822 this.selectedDestination_.capabilities = capabilities; 737 this.selectedDestination_.capabilities = capabilities;
823 738
824 cr.dispatchSimpleEvent( 739 cr.dispatchSimpleEvent(
825 this, 740 this,
826 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY); 741 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
827 return true; 742 return true;
828 } 743 }
829 744
830 if (origin == print_preview.Destination.Origin.EXTENSION) { 745 if (origin == print_preview.DestinationOrigin.EXTENSION) {
831 // TODO(tbarzic): Add support for requesting a single extension's 746 // TODO(tbarzic): Add support for requesting a single extension's
832 // printer list. 747 // printer list.
833 this.startLoadExtensionDestinations(); 748 this.startLoadExtensionDestinations();
834 749
835 this.selectedDestination_ = 750 this.selectedDestination_ =
836 print_preview.ExtensionDestinationParser.parse({ 751 print_preview.ExtensionDestinationParser.parse({
837 extensionId: extensionId, 752 extensionId: extensionId,
838 extensionName: extensionName, 753 extensionName: extensionName,
839 id: id, 754 id: id,
840 name: name 755 name: name
(...skipping 15 matching lines...) Expand all
856 771
857 /** 772 /**
858 * Attempts to find a destination matching the provided rules. 773 * Attempts to find a destination matching the provided rules.
859 * @param {!print_preview.DestinationMatch} destinationMatch Rules to match. 774 * @param {!print_preview.DestinationMatch} destinationMatch Rules to match.
860 * @private 775 * @private
861 */ 776 */
862 fetchMatchingDestination_: function(destinationMatch) { 777 fetchMatchingDestination_: function(destinationMatch) {
863 this.autoSelectMatchingDestination_ = destinationMatch; 778 this.autoSelectMatchingDestination_ = destinationMatch;
864 779
865 if (destinationMatch.matchOrigin( 780 if (destinationMatch.matchOrigin(
866 print_preview.Destination.Origin.LOCAL) || 781 print_preview.DestinationOrigin.LOCAL) ||
867 destinationMatch.matchOrigin( 782 destinationMatch.matchOrigin(
868 print_preview.Destination.Origin.CROS)) { 783 print_preview.DestinationOrigin.CROS)) {
869 this.startLoadLocalDestinations(); 784 this.startLoadLocalDestinations();
870 } 785 }
871 if (destinationMatch.matchOrigin( 786 if (destinationMatch.matchOrigin(
872 print_preview.Destination.Origin.PRIVET)) { 787 print_preview.DestinationOrigin.PRIVET)) {
873 this.startLoadPrivetDestinations(); 788 this.startLoadPrivetDestinations();
874 } 789 }
875 if (destinationMatch.matchOrigin( 790 if (destinationMatch.matchOrigin(
876 print_preview.Destination.Origin.EXTENSION)) { 791 print_preview.DestinationOrigin.EXTENSION)) {
877 this.startLoadExtensionDestinations(); 792 this.startLoadExtensionDestinations();
878 } 793 }
879 if (destinationMatch.matchOrigin( 794 if (destinationMatch.matchOrigin(
880 print_preview.Destination.Origin.COOKIES) || 795 print_preview.DestinationOrigin.COOKIES) ||
881 destinationMatch.matchOrigin( 796 destinationMatch.matchOrigin(
882 print_preview.Destination.Origin.DEVICE) || 797 print_preview.DestinationOrigin.DEVICE) ||
883 destinationMatch.matchOrigin( 798 destinationMatch.matchOrigin(
884 print_preview.Destination.Origin.PROFILE)) { 799 print_preview.DestinationOrigin.PROFILE)) {
885 this.startLoadCloudDestinations(); 800 this.startLoadCloudDestinations();
886 } 801 }
887 }, 802 },
888 803
889 /** 804 /**
890 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized 805 * @param {?string} serializedDefaultDestinationSelectionRulesStr Serialized
891 * default destination selection rules. 806 * default destination selection rules.
892 * @return {!print_preview.DestinationMatch} Creates rules matching 807 * @return {?print_preview.DestinationMatch} Creates rules matching
893 * previously selected destination. 808 * previously selected destination.
894 * @private 809 * @private
895 */ 810 */
896 convertToDestinationMatch_: function( 811 convertToDestinationMatch_: function(
897 serializedDefaultDestinationSelectionRulesStr) { 812 serializedDefaultDestinationSelectionRulesStr) {
898 var matchRules = null; 813 var matchRules = null;
899 try { 814 try {
900 if (serializedDefaultDestinationSelectionRulesStr) { 815 if (serializedDefaultDestinationSelectionRulesStr) {
901 matchRules = 816 matchRules =
902 JSON.parse(serializedDefaultDestinationSelectionRulesStr); 817 JSON.parse(serializedDefaultDestinationSelectionRulesStr);
903 } 818 }
904 } catch(e) { 819 } catch(e) {
905 console.error( 820 console.error(
906 'Failed to parse defaultDestinationSelectionRules: ' + e); 821 'Failed to parse defaultDestinationSelectionRules: ' + e);
907 } 822 }
908 if (!matchRules) 823 if (!matchRules)
909 return; 824 return null;
910 825
911 var isLocal = !matchRules.kind || matchRules.kind == 'local'; 826 var isLocal = !matchRules.kind || matchRules.kind == 'local';
912 var isCloud = !matchRules.kind || matchRules.kind == 'cloud'; 827 var isCloud = !matchRules.kind || matchRules.kind == 'cloud';
913 if (!isLocal && !isCloud) { 828 if (!isLocal && !isCloud) {
914 console.error('Unsupported type: "' + matchRules.kind + '"'); 829 console.error('Unsupported type: "' + matchRules.kind + '"');
915 return null; 830 return null;
916 } 831 }
917 832
918 var origins = []; 833 var origins = [];
919 if (isLocal) { 834 if (isLocal) {
920 origins.push(print_preview.Destination.Origin.LOCAL); 835 origins.push(print_preview.DestinationOrigin.LOCAL);
921 origins.push(print_preview.Destination.Origin.PRIVET); 836 origins.push(print_preview.DestinationOrigin.PRIVET);
922 origins.push(print_preview.Destination.Origin.EXTENSION); 837 origins.push(print_preview.DestinationOrigin.EXTENSION);
923 origins.push(print_preview.Destination.Origin.CROS); 838 origins.push(print_preview.DestinationOrigin.CROS);
924 } 839 }
925 if (isCloud) { 840 if (isCloud) {
926 origins.push(print_preview.Destination.Origin.COOKIES); 841 origins.push(print_preview.DestinationOrigin.COOKIES);
927 origins.push(print_preview.Destination.Origin.DEVICE); 842 origins.push(print_preview.DestinationOrigin.DEVICE);
928 origins.push(print_preview.Destination.Origin.PROFILE); 843 origins.push(print_preview.DestinationOrigin.PROFILE);
929 } 844 }
930 845
931 var idRegExp = null; 846 var idRegExp = null;
932 try { 847 try {
933 if (matchRules.idPattern) { 848 if (matchRules.idPattern) {
934 idRegExp = new RegExp(matchRules.idPattern || '.*'); 849 idRegExp = new RegExp(matchRules.idPattern || '.*');
935 } 850 }
936 } catch (e) { 851 } catch (e) {
937 console.error('Failed to parse regexp for "id": ' + e); 852 console.error('Failed to parse regexp for "id": ' + e);
938 } 853 }
939 854
940 var displayNameRegExp = null; 855 var displayNameRegExp = null;
941 try { 856 try {
942 if (matchRules.namePattern) { 857 if (matchRules.namePattern) {
943 displayNameRegExp = new RegExp(matchRules.namePattern || '.*'); 858 displayNameRegExp = new RegExp(matchRules.namePattern || '.*');
944 } 859 }
945 } catch (e) { 860 } catch (e) {
946 console.error('Failed to parse regexp for "name": ' + e); 861 console.error('Failed to parse regexp for "name": ' + e);
947 } 862 }
948 863
949 return new DestinationMatch( 864 return new print_preview.DestinationMatch(
950 origins, 865 origins,
951 idRegExp, 866 idRegExp,
952 displayNameRegExp, 867 displayNameRegExp,
953 true /*skipVirtualDestinations*/); 868 true /*skipVirtualDestinations*/);
954 }, 869 },
955 870
956 /** 871 /**
957 * @return {print_preview.DestinationMatch} Creates rules matching 872 * @return {print_preview.DestinationMatch} Creates rules matching
958 * previously selected destination. 873 * previously selected destination.
959 * @private 874 * @private
960 */ 875 */
961 convertPreselectedToDestinationMatch_: function() { 876 convertPreselectedToDestinationMatch_: function() {
962 if (this.appState_.isSelectedDestinationValid()) { 877 if (this.appState_.isSelectedDestinationValid()) {
963 return this.createExactDestinationMatch_( 878 return this.createExactDestinationMatch_(
964 this.appState_.selectedDestination.origin, 879 this.appState_.selectedDestination.origin,
965 this.appState_.selectedDestination.id); 880 this.appState_.selectedDestination.id);
966 } 881 }
967 if (this.systemDefaultDestinationId_) { 882 if (this.systemDefaultDestinationId_) {
968 return this.createExactDestinationMatch_( 883 return this.createExactDestinationMatch_(
969 this.platformOrigin_, 884 this.platformOrigin_,
970 this.systemDefaultDestinationId_); 885 this.systemDefaultDestinationId_);
971 } 886 }
972 return null; 887 return null;
973 }, 888 },
974 889
975 /** 890 /**
976 * @param {!print_preview.Destination.Origin} origin Destination origin. 891 * @param {string | print_preview.DestinationOrigin} origin Destination
892 * origin.
977 * @param {string} id Destination id. 893 * @param {string} id Destination id.
978 * @return {!print_preview.DestinationMatch} Creates rules matching 894 * @return {!print_preview.DestinationMatch} Creates rules matching
979 * provided destination. 895 * provided destination.
980 * @private 896 * @private
981 */ 897 */
982 createExactDestinationMatch_: function(origin, id) { 898 createExactDestinationMatch_: function(origin, id) {
983 return new DestinationMatch( 899 return new print_preview.DestinationMatch(
984 [origin], 900 [origin],
985 new RegExp('^' + id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$'), 901 new RegExp('^' + id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$'),
986 null /*displayNameRegExp*/, 902 null /*displayNameRegExp*/,
987 false /*skipVirtualDestinations*/); 903 false /*skipVirtualDestinations*/);
988 }, 904 },
989 905
990 /** 906 /**
991 * Sets the destination store's Google Cloud Print interface. 907 * Sets the destination store's Google Cloud Print interface.
992 * @param {!cloudprint.CloudPrintInterface} cloudPrintInterface Interface 908 * @param {!cloudprint.CloudPrintInterface} cloudPrintInterface Interface
993 * to set. 909 * to set.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 } 1010 }
1095 }, 1011 },
1096 1012
1097 /** 1013 /**
1098 * Attempt to resolve the capabilities for a Chrome OS printer. 1014 * Attempt to resolve the capabilities for a Chrome OS printer.
1099 * @param {!print_preview.Destination} destination The destination which 1015 * @param {!print_preview.Destination} destination The destination which
1100 * requires resolution. 1016 * requires resolution.
1101 * @return {!Promise<!print_preview.PrinterSetupResponse>} 1017 * @return {!Promise<!print_preview.PrinterSetupResponse>}
1102 */ 1018 */
1103 resolveCrosDestination: function(destination) { 1019 resolveCrosDestination: function(destination) {
1104 assert(destination.origin == print_preview.Destination.Origin.CROS); 1020 assert(destination.origin == print_preview.DestinationOrigin.CROS);
1105 return this.nativeLayer_.setupPrinter(destination.id); 1021 return this.nativeLayer_.setupPrinter(destination.id);
1106 }, 1022 },
1107 1023
1108 /** 1024 /**
1109 * Attempts to resolve a provisional destination. 1025 * Attempts to resolve a provisional destination.
1110 * @param {!print_preview.Destination} destinaion Provisional destination 1026 * @param {!print_preview.Destination} destination Provisional destination
1111 * that should be resolved. 1027 * that should be resolved.
1112 */ 1028 */
1113 resolveProvisionalDestination: function(destination) { 1029 resolveProvisionalDestination: function(destination) {
1114 assert( 1030 assert(
1115 destination.provisionalType == 1031 destination.provisionalType ==
1116 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION, 1032 print_preview.DestinationProvisionalType.NEEDS_USB_PERMISSION,
1117 'Provisional type cannot be resolved.'); 1033 'Provisional type cannot be resolved.');
1118 this.nativeLayer_.grantExtensionPrinterAccess(destination.id); 1034 this.nativeLayer_.grantExtensionPrinterAccess(destination.id);
1119 }, 1035 },
1120 1036
1121 /** 1037 /**
1122 * Selects 'Save to PDF' destination (since it always exists). 1038 * Selects 'Save to PDF' destination (since it always exists).
1123 * @private 1039 * @private
1124 */ 1040 */
1125 selectPdfDestination_: function() { 1041 selectPdfDestination_: function() {
1126 var saveToPdfKey = this.getDestinationKey_( 1042 var saveToPdfKey = this.getDestinationKey_(
1127 print_preview.Destination.Origin.LOCAL, 1043 print_preview.DestinationOrigin.LOCAL,
1128 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, 1044 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
1129 ''); 1045 '');
1130 this.selectDestination( 1046 this.selectDestination(
1131 this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null); 1047 this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null);
1132 }, 1048 },
1133 1049
1134 /** 1050 /**
1135 * Attempts to select system default destination with a fallback to 1051 * Attempts to select system default destination with a fallback to
1136 * 'Save to PDF' destination. 1052 * 'Save to PDF' destination.
1137 * @private 1053 * @private
1138 */ 1054 */
1139 selectDefaultDestination_: function() { 1055 selectDefaultDestination_: function() {
1140 if (this.systemDefaultDestinationId_) { 1056 if (this.systemDefaultDestinationId_) {
1141 if (this.autoSelectMatchingDestination_ && 1057 if (this.autoSelectMatchingDestination_ &&
1142 !this.autoSelectMatchingDestination_.matchIdAndOrigin( 1058 !this.autoSelectMatchingDestination_.matchIdAndOrigin(
1143 this.systemDefaultDestinationId_, 1059 this.systemDefaultDestinationId_,
1144 this.plaformOrigin_)) { 1060 this.platformOrigin_)) {
1145 if (this.fetchPreselectedDestination_( 1061 if (this.fetchPreselectedDestination_(
1146 this.platformOrigin_, 1062 this.platformOrigin_,
1147 this.systemDefaultDestinationId_, 1063 this.systemDefaultDestinationId_,
1148 '' /*account*/, 1064 '' /*account*/,
1149 '' /*name*/, 1065 '' /*name*/,
1150 null /*capabilities*/, 1066 null /*capabilities*/,
1151 '' /*extensionId*/, 1067 '' /*extensionId*/,
1152 '' /*extensionName*/)) { 1068 '' /*extensionName*/)) {
1153 return; 1069 return;
1154 } 1070 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1195 this.nativeLayer_.startGetExtensionDestinations(); 1111 this.nativeLayer_.startGetExtensionDestinations();
1196 cr.dispatchSimpleEvent( 1112 cr.dispatchSimpleEvent(
1197 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); 1113 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
1198 this.extensionSearchTimeout_ = setTimeout( 1114 this.extensionSearchTimeout_ = setTimeout(
1199 this.endExtensionPrinterSearch_.bind(this), 1115 this.endExtensionPrinterSearch_.bind(this),
1200 DestinationStore.EXTENSION_SEARCH_DURATION_); 1116 DestinationStore.EXTENSION_SEARCH_DURATION_);
1201 }, 1117 },
1202 1118
1203 /** 1119 /**
1204 * Initiates loading of cloud destinations. 1120 * Initiates loading of cloud destinations.
1205 * @param {print_preview.Destination.Origin=} opt_origin Search destinations 1121 * @param {print_preview.DestinationOrigin=} opt_origin Search destinations
1206 * for the specified origin only. 1122 * for the specified origin only.
1207 */ 1123 */
1208 startLoadCloudDestinations: function(opt_origin) { 1124 startLoadCloudDestinations: function(opt_origin) {
1209 if (this.cloudPrintInterface_ != null) { 1125 if (this.cloudPrintInterface_ != null) {
1210 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; 1126 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
1211 if (origins.length == 0 || 1127 if (origins.length == 0 ||
1212 (opt_origin && origins.indexOf(opt_origin) < 0)) { 1128 (opt_origin && origins.indexOf(opt_origin) < 0)) {
1213 this.cloudPrintInterface_.search( 1129 this.cloudPrintInterface_.search(
1214 this.userInfo_.activeUser, opt_origin); 1130 this.userInfo_.activeUser || '', opt_origin);
1215 cr.dispatchSimpleEvent( 1131 cr.dispatchSimpleEvent(
1216 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); 1132 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
1217 } 1133 }
1218 } 1134 }
1219 }, 1135 },
1220 1136
1221 /** Requests load of COOKIE based cloud destinations. */ 1137 /** Requests load of COOKIE based cloud destinations. */
1222 reloadUserCookieBasedDestinations: function() { 1138 reloadUserCookieBasedDestinations: function() {
1223 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || []; 1139 var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
1224 if (origins.indexOf(print_preview.Destination.Origin.COOKIES) >= 0) { 1140 if (origins.indexOf(print_preview.DestinationOrigin.COOKIES) >= 0) {
1225 cr.dispatchSimpleEvent( 1141 cr.dispatchSimpleEvent(
1226 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); 1142 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
1227 } else { 1143 } else {
1228 this.startLoadCloudDestinations( 1144 this.startLoadCloudDestinations(
1229 print_preview.Destination.Origin.COOKIES); 1145 print_preview.DestinationOrigin.COOKIES);
1230 } 1146 }
1231 }, 1147 },
1232 1148
1233 /** Initiates loading of all known destination types. */ 1149 /** Initiates loading of all known destination types. */
1234 startLoadAllDestinations: function() { 1150 startLoadAllDestinations: function() {
1235 this.startLoadCloudDestinations(); 1151 this.startLoadCloudDestinations();
1236 this.startLoadLocalDestinations(); 1152 this.startLoadLocalDestinations();
1237 this.startLoadPrivetDestinations(); 1153 this.startLoadPrivetDestinations();
1238 this.startLoadExtensionDestinations(); 1154 this.startLoadExtensionDestinations();
1239 }, 1155 },
(...skipping 10 matching lines...) Expand all
1250 * Event handler for {@code 1166 * Event handler for {@code
1251 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}. 1167 * print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED}.
1252 * Currently assumes the provisional destination is an extension 1168 * Currently assumes the provisional destination is an extension
1253 * destination. 1169 * destination.
1254 * Called when a provisional destination resolvement attempt finishes. 1170 * Called when a provisional destination resolvement attempt finishes.
1255 * The provisional destination is removed from the store and replaced with 1171 * The provisional destination is removed from the store and replaced with
1256 * a destination created from the resolved destination properties, if any 1172 * a destination created from the resolved destination properties, if any
1257 * are reported. 1173 * are reported.
1258 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED} 1174 * Emits {@code DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED}
1259 * event. 1175 * event.
1260 * @param {!Event} The event containing the provisional destination ID and 1176 * @param {!Event} evt The event containing the provisional destination ID
1261 * resolved destination description. If the destination was not 1177 * and resolved destination description. If the destination was not
1262 * successfully resolved, the description will not be set. 1178 * successfully resolved, the description will not be set.
1263 * @private 1179 * @private
1264 */ 1180 */
1265 handleProvisionalDestinationResolved_: function(evt) { 1181 handleProvisionalDestinationResolved_: function(evt) {
1266 var provisionalDestinationIndex = -1; 1182 var provisionalDestinationIndex = -1;
1267 var provisionalDestination = null; 1183 var provisionalDestination = null;
1268 for (var i = 0; i < this.destinations_.length; ++i) { 1184 for (var i = 0; i < this.destinations_.length; ++i) {
1269 if (evt.provisionalId == this.destinations_[i].id) { 1185 if (evt.provisionalId == this.destinations_[i].id) {
1270 provisionalDestinationIndex = i; 1186 provisionalDestinationIndex = i;
1271 provisionalDestination = this.destinations_[i]; 1187 provisionalDestination = this.destinations_[i];
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1349 1265
1350 /** 1266 /**
1351 * Updates an existing print destination with capabilities and display name 1267 * Updates an existing print destination with capabilities and display name
1352 * information. If the destination doesn't already exist, it will be added. 1268 * information. If the destination doesn't already exist, it will be added.
1353 * @param {!print_preview.Destination} destination Destination to update. 1269 * @param {!print_preview.Destination} destination Destination to update.
1354 * @private 1270 * @private
1355 */ 1271 */
1356 updateDestination_: function(destination) { 1272 updateDestination_: function(destination) {
1357 assert(destination.constructor !== Array, 'Single printer expected'); 1273 assert(destination.constructor !== Array, 'Single printer expected');
1358 destination.capabilities_ = DestinationStore.localizeCapabilities_( 1274 destination.capabilities_ = DestinationStore.localizeCapabilities_(
1359 destination.capabilities_); 1275 assert(destination.capabilities_));
1360 destination.capabilities_ = DestinationStore.sortMediaSizes_( 1276 destination.capabilities_ = DestinationStore.sortMediaSizes_(
1361 destination.capabilities_); 1277 destination.capabilities_);
1362 var existingDestination = this.destinationMap_[this.getKey_(destination)]; 1278 var existingDestination = this.destinationMap_[this.getKey_(destination)];
1363 if (existingDestination != null) { 1279 if (existingDestination != null) {
1364 existingDestination.capabilities = destination.capabilities; 1280 existingDestination.capabilities = destination.capabilities;
1365 } else { 1281 } else {
1366 this.insertDestination_(destination); 1282 this.insertDestination_(destination);
1367 } 1283 }
1368 1284
1369 if (this.selectedDestination_ && 1285 if (this.selectedDestination_ &&
(...skipping 24 matching lines...) Expand all
1394 */ 1310 */
1395 endExtensionPrinterSearch_: function() { 1311 endExtensionPrinterSearch_: function() {
1396 this.isExtensionDestinationSearchInProgress_ = false; 1312 this.isExtensionDestinationSearchInProgress_ = false;
1397 this.hasLoadedAllExtensionDestinations_ = true; 1313 this.hasLoadedAllExtensionDestinations_ = true;
1398 cr.dispatchSimpleEvent( 1314 cr.dispatchSimpleEvent(
1399 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE); 1315 this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
1400 // Clear initially selected (cached) extension destination if it hasn't 1316 // Clear initially selected (cached) extension destination if it hasn't
1401 // been found among reported extension destinations. 1317 // been found among reported extension destinations.
1402 if (this.autoSelectMatchingDestination_ && 1318 if (this.autoSelectMatchingDestination_ &&
1403 this.autoSelectMatchingDestination_.matchOrigin( 1319 this.autoSelectMatchingDestination_.matchOrigin(
1404 print_preview.Destination.Origin.EXTENSION) && 1320 print_preview.DestinationOrigin.EXTENSION) &&
1405 this.selectedDestination_ && 1321 this.selectedDestination_ &&
1406 this.selectedDestination_.isExtension) { 1322 this.selectedDestination_.isExtension) {
1407 this.selectDefaultDestination_(); 1323 this.selectDefaultDestination_();
1408 } 1324 }
1409 }, 1325 },
1410 1326
1411 /** 1327 /**
1412 * Inserts a destination into the store without dispatching any events. 1328 * Inserts a destination into the store without dispatching any events.
1413 * @return {boolean} Whether the inserted destination was not already in the 1329 * @return {boolean} Whether the inserted destination was not already in the
1414 * store. 1330 * store.
1415 * @private 1331 * @private
1416 */ 1332 */
1417 insertIntoStore_: function(destination) { 1333 insertIntoStore_: function(destination) {
1418 var key = this.getKey_(destination); 1334 var key = this.getKey_(destination);
1419 var existingDestination = this.destinationMap_[key]; 1335 var existingDestination = this.destinationMap_[key];
1420 if (existingDestination == null) { 1336 if (existingDestination == null) {
1421 destination.isRecent |= this.appState_.recentDestinations.some( 1337 destination.isRecent |= this.appState_.recentDestinations.some(
1422 function(recent) { 1338 function(recent) {
1423 return (destination.id == recent.id && 1339 return (destination.id == recent.id &&
1424 destination.origin == recent.origin); 1340 destination.origin == recent.origin);
1425 }, this); 1341 }, this);
1426 this.destinations_.push(destination); 1342 this.destinations_.push(destination);
1427 this.destinationMap_[key] = destination; 1343 this.destinationMap_[key] = destination;
1428 return true; 1344 return true;
1429 } else if (existingDestination.connectionStatus == 1345 } else if (existingDestination.connectionStatus ==
1430 print_preview.Destination.ConnectionStatus.UNKNOWN && 1346 print_preview.DestinationConnectionStatus.UNKNOWN &&
1431 destination.connectionStatus != 1347 destination.connectionStatus !=
1432 print_preview.Destination.ConnectionStatus.UNKNOWN) { 1348 print_preview.DestinationConnectionStatus.UNKNOWN) {
1433 existingDestination.connectionStatus = destination.connectionStatus; 1349 existingDestination.connectionStatus = destination.connectionStatus;
1434 return true; 1350 return true;
1435 } else { 1351 } else {
1436 return false; 1352 return false;
1437 } 1353 }
1438 }, 1354 },
1439 1355
1440 /** 1356 /**
1441 * Binds handlers to events. 1357 * Binds handlers to events.
1442 * @private 1358 * @private
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1475 print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET, 1391 print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET,
1476 this.onExtensionCapabilitiesSet_.bind(this)); 1392 this.onExtensionCapabilitiesSet_.bind(this));
1477 this.tracker_.add( 1393 this.tracker_.add(
1478 this.nativeLayer_, 1394 this.nativeLayer_,
1479 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED, 1395 print_preview.NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED,
1480 this.handleProvisionalDestinationResolved_.bind(this)); 1396 this.handleProvisionalDestinationResolved_.bind(this));
1481 }, 1397 },
1482 1398
1483 /** 1399 /**
1484 * Creates a local PDF print destination. 1400 * Creates a local PDF print destination.
1485 * @return {!print_preview.Destination} Created print destination.
1486 * @private 1401 * @private
1487 */ 1402 */
1488 createLocalPdfPrintDestination_: function() { 1403 createLocalPdfPrintDestination_: function() {
1489 // TODO(alekseys): Create PDF printer in the native code and send its 1404 // TODO(alekseys): Create PDF printer in the native code and send its
1490 // capabilities back with other local printers. 1405 // capabilities back with other local printers.
1491 if (this.pdfPrinterEnabled_) { 1406 if (this.pdfPrinterEnabled_) {
1492 this.insertDestination_(new print_preview.Destination( 1407 this.insertDestination_(new print_preview.Destination(
1493 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, 1408 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
1494 print_preview.Destination.Type.LOCAL, 1409 print_preview.DestinationType.LOCAL,
1495 print_preview.Destination.Origin.LOCAL, 1410 print_preview.DestinationOrigin.LOCAL,
1496 loadTimeData.getString('printToPDF'), 1411 loadTimeData.getString('printToPDF'),
1497 false /*isRecent*/, 1412 false /*isRecent*/,
1498 print_preview.Destination.ConnectionStatus.ONLINE)); 1413 print_preview.DestinationConnectionStatus.ONLINE));
1499 } 1414 }
1500 }, 1415 },
1501 1416
1502 /** 1417 /**
1503 * Resets the state of the destination store to its initial state. 1418 * Resets the state of the destination store to its initial state.
1504 * @private 1419 * @private
1505 */ 1420 */
1506 reset_: function() { 1421 reset_: function() {
1507 this.destinations_ = []; 1422 this.destinations_ = [];
1508 this.destinationMap_ = {}; 1423 this.destinationMap_ = {};
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 * @private 1458 * @private
1544 */ 1459 */
1545 onLocalDestinationCapabilitiesSet_: function(event) { 1460 onLocalDestinationCapabilitiesSet_: function(event) {
1546 var destinationId = event.settingsInfo['printerId']; 1461 var destinationId = event.settingsInfo['printerId'];
1547 var printerName = event.settingsInfo['printerName']; 1462 var printerName = event.settingsInfo['printerName'];
1548 var printerDescription = event.settingsInfo['printerDescription']; 1463 var printerDescription = event.settingsInfo['printerDescription'];
1549 // PDF is special since we don't need to query the device for 1464 // PDF is special since we don't need to query the device for
1550 // capabilities. 1465 // capabilities.
1551 var origin = destinationId == 1466 var origin = destinationId ==
1552 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ? 1467 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ?
1553 print_preview.Destination.Origin.LOCAL : this.platformOrigin_; 1468 print_preview.DestinationOrigin.LOCAL : this.platformOrigin_;
1554 var key = this.getDestinationKey_( 1469 var key = this.getDestinationKey_(
1555 origin, 1470 origin,
1556 destinationId, 1471 destinationId,
1557 ''); 1472 '');
1558 var destination = this.destinationMap_[key]; 1473 var destination = this.destinationMap_[key];
1559 var capabilities = DestinationStore.localizeCapabilities_( 1474 var capabilities = DestinationStore.localizeCapabilities_(
1560 event.settingsInfo.capabilities); 1475 event.settingsInfo.capabilities);
1561 // Special case for PDF printer (until local printers capabilities are 1476 // Special case for PDF printer (until local printers capabilities are
1562 // reported in CDD format too). 1477 // reported in CDD format too).
1563 if (destinationId == 1478 if (destinationId ==
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 onCloudPrintProcessInviteDone_: function(event) { 1582 onCloudPrintProcessInviteDone_: function(event) {
1668 if (event.accept && event.printer) { 1583 if (event.accept && event.printer) {
1669 // Hint the destination list to promote this new destination. 1584 // Hint the destination list to promote this new destination.
1670 event.printer.isRecent = true; 1585 event.printer.isRecent = true;
1671 this.insertDestination_(event.printer); 1586 this.insertDestination_(event.printer);
1672 } 1587 }
1673 }, 1588 },
1674 1589
1675 /** 1590 /**
1676 * Called when a Privet printer is added to the local network. 1591 * Called when a Privet printer is added to the local network.
1677 * @param {Object} event Contains information about the added printer. 1592 * @param {{printer: {serviceName: string,
1593 * name: string,
1594 * hasLocalPrinting: boolean,
1595 * isUnregistered: boolean,
1596 * cloudID: string}}} event Contains information about
1597 * the added printer.
1678 * @private 1598 * @private
1679 */ 1599 */
1680 onPrivetPrinterAdded_: function(event) { 1600 onPrivetPrinterAdded_: function(event) {
1681 if (event.printer.serviceName == this.waitForRegisterDestination_ && 1601 if (event.printer.serviceName == this.waitForRegisterDestination_ &&
1682 !event.printer.isUnregistered) { 1602 !event.printer.isUnregistered) {
1683 this.waitForRegisterDestination_ = null; 1603 this.waitForRegisterDestination_ = null;
1684 this.onDestinationsReload_(); 1604 this.onDestinationsReload_();
1685 } else { 1605 } else {
1686 this.insertDestinations_( 1606 this.insertDestinations_(
1687 print_preview.PrivetDestinationParser.parse(event.printer)); 1607 print_preview.PrivetDestinationParser.parse(event.printer));
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1722 } 1642 }
1723 }, 1643 },
1724 1644
1725 /** 1645 /**
1726 * Called when capabilities for an extension managed printer are set. 1646 * Called when capabilities for an extension managed printer are set.
1727 * @param {Object} event Contains the printer's capabilities and ID. 1647 * @param {Object} event Contains the printer's capabilities and ID.
1728 * @private 1648 * @private
1729 */ 1649 */
1730 onExtensionCapabilitiesSet_: function(event) { 1650 onExtensionCapabilitiesSet_: function(event) {
1731 var destinationKey = this.getDestinationKey_( 1651 var destinationKey = this.getDestinationKey_(
1732 print_preview.Destination.Origin.EXTENSION, 1652 print_preview.DestinationOrigin.EXTENSION,
1733 event.printerId, 1653 event.printerId,
1734 '' /* account */); 1654 '' /* account */);
1735 var destination = this.destinationMap_[destinationKey]; 1655 var destination = this.destinationMap_[destinationKey];
1736 if (!destination) 1656 if (!destination)
1737 return; 1657 return;
1738 destination.capabilities = event.capabilities; 1658 destination.capabilities = event.capabilities;
1739 this.updateDestination_(destination); 1659 this.updateDestination_(destination);
1740 }, 1660 },
1741 1661
1742 /** 1662 /**
1743 * Called from native layer after the user was requested to sign in, and did 1663 * Called from native layer after the user was requested to sign in, and did
1744 * so successfully. 1664 * so successfully.
1745 * @private 1665 * @private
1746 */ 1666 */
1747 onDestinationsReload_: function() { 1667 onDestinationsReload_: function() {
1748 this.reset_(); 1668 this.reset_();
1749 this.autoSelectMatchingDestination_ = 1669 this.autoSelectMatchingDestination_ =
1750 this.convertPreselectedToDestinationMatch_(); 1670 this.convertPreselectedToDestinationMatch_();
1751 this.createLocalPdfPrintDestination_(); 1671 this.createLocalPdfPrintDestination_();
1752 this.startLoadAllDestinations(); 1672 this.startLoadAllDestinations();
1753 }, 1673 },
1754 1674
1755 // TODO(vitalybuka): Remove three next functions replacing Destination.id 1675 // TODO(vitalybuka): Remove three next functions replacing Destination.id
1756 // and Destination.origin by complex ID. 1676 // and Destination.origin by complex ID.
1757 /** 1677 /**
1758 * Returns key to be used with {@code destinationMap_}. 1678 * Returns key to be used with {@code destinationMap_}.
1759 * @param {!print_preview.Destination.Origin} origin Destination origin. 1679 * @param {print_preview.DestinationOrigin | string} origin Destination
1680 * origin.
1760 * @param {string} id Destination id. 1681 * @param {string} id Destination id.
1761 * @param {string} account User account destination is registered for. 1682 * @param {string} account User account destination is registered for.
1762 * @private 1683 * @private
1763 */ 1684 */
1764 getDestinationKey_: function(origin, id, account) { 1685 getDestinationKey_: function(origin, id, account) {
1765 return origin + '/' + id + '/' + account; 1686 return origin + '/' + id + '/' + account;
1766 }, 1687 },
1767 1688
1768 /** 1689 /**
1769 * Returns key to be used with {@code destinationMap_}. 1690 * Returns key to be used with {@code destinationMap_}.
1770 * @param {!print_preview.Destination} destination Destination. 1691 * @param {!print_preview.Destination} destination Destination.
1771 * @private 1692 * @private
1772 */ 1693 */
1773 getKey_: function(destination) { 1694 getKey_: function(destination) {
1774 return this.getDestinationKey_( 1695 return this.getDestinationKey_(
1775 destination.origin, destination.id, destination.account); 1696 destination.origin, destination.id, destination.account);
1776 } 1697 }
1777 }; 1698 };
1778 1699
1779 // Export 1700 // Export
1780 return { 1701 return {
1781 DestinationStore: DestinationStore 1702 DestinationStore: DestinationStore
1782 }; 1703 };
1783 }); 1704 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698