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

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

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

Powered by Google App Engine
This is Rietveld 408576698