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

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

Issue 1144983002: Introduce concept of provisional destinations to print preview (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 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.
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 * @enum {string} 192 * @enum {string}
193 */ 193 */
194 DestinationStore.EventType = { 194 DestinationStore.EventType = {
195 DESTINATION_SEARCH_DONE: 195 DESTINATION_SEARCH_DONE:
196 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE', 196 'print_preview.DestinationStore.DESTINATION_SEARCH_DONE',
197 DESTINATION_SEARCH_STARTED: 197 DESTINATION_SEARCH_STARTED:
198 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED', 198 'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED',
199 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT', 199 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT',
200 DESTINATIONS_INSERTED: 200 DESTINATIONS_INSERTED:
201 'print_preview.DestinationStore.DESTINATIONS_INSERTED', 201 'print_preview.DestinationStore.DESTINATIONS_INSERTED',
202 PROVISIONAL_DESTINATION_RESOLVED:
203 'print_preview.DestinationStore.PROVISIONAL_DESTINATION_RESOLVED',
202 CACHED_SELECTED_DESTINATION_INFO_READY: 204 CACHED_SELECTED_DESTINATION_INFO_READY:
203 'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY', 205 'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY',
204 SELECTED_DESTINATION_CAPABILITIES_READY: 206 SELECTED_DESTINATION_CAPABILITIES_READY:
205 'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY' 207 'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY'
206 }; 208 };
207 209
208 /** 210 /**
209 * Delay in milliseconds before the destination store ignores the initial 211 * Delay in milliseconds before the destination store ignores the initial
210 * destination ID and just selects any printer (since the initial destination 212 * destination ID and just selects any printer (since the initial destination
211 * was not found). 213 * was not found).
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 this.autoSelectTimeout_ = null; 456 this.autoSelectTimeout_ = null;
455 } else if (destination == this.selectedDestination_) { 457 } else if (destination == this.selectedDestination_) {
456 return; 458 return;
457 } 459 }
458 if (destination == null) { 460 if (destination == null) {
459 this.selectedDestination_ = null; 461 this.selectedDestination_ = null;
460 cr.dispatchSimpleEvent( 462 cr.dispatchSimpleEvent(
461 this, DestinationStore.EventType.DESTINATION_SELECT); 463 this, DestinationStore.EventType.DESTINATION_SELECT);
462 return; 464 return;
463 } 465 }
466
467 assert(!destination.isProvisional, 'Selecting provisonal destination');
468
464 // Update and persist selected destination. 469 // Update and persist selected destination.
465 this.selectedDestination_ = destination; 470 this.selectedDestination_ = destination;
466 this.selectedDestination_.isRecent = true; 471 this.selectedDestination_.isRecent = true;
467 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && 472 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX &&
468 !destination.isTosAccepted) { 473 !destination.isTosAccepted) {
469 assert(this.cloudPrintInterface_ != null, 474 assert(this.cloudPrintInterface_ != null,
470 'Selected FedEx destination, but GCP API is not available'); 475 'Selected FedEx destination, but GCP API is not available');
471 destination.isTosAccepted = true; 476 destination.isTosAccepted = true;
472 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true); 477 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true);
473 } 478 }
(...skipping 30 matching lines...) Expand all
504 this.cloudPrintInterface_.printer( 509 this.cloudPrintInterface_.printer(
505 destination.id, destination.origin, destination.account); 510 destination.id, destination.origin, destination.account);
506 } 511 }
507 } else { 512 } else {
508 cr.dispatchSimpleEvent( 513 cr.dispatchSimpleEvent(
509 this, 514 this,
510 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); 515 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY);
511 } 516 }
512 }, 517 },
513 518
519 resolveProvisionalDestination: function(destination) {
520 assert(
521 destination.provisionalType ==
522 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION,
523 'Provisional type cannot be resolved.');
524 assert(destination.usbId != null,
525 'Resolving provisional destination without USB ID.');
526 this.nativeLayer_.grantUsbExtensionPermission(
527 destination.id, destination.extensionId, destination.usbId);
528 },
529
514 /** 530 /**
515 * Selects 'Save to PDF' destination (since it always exists). 531 * Selects 'Save to PDF' destination (since it always exists).
516 * @private 532 * @private
517 */ 533 */
518 selectDefaultDestination_: function() { 534 selectDefaultDestination_: function() {
519 var saveToPdfKey = this.getDestinationKey_( 535 var saveToPdfKey = this.getDestinationKey_(
520 print_preview.Destination.Origin.LOCAL, 536 print_preview.Destination.Origin.LOCAL,
521 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, 537 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
522 ''); 538 '');
523 this.selectDestination( 539 this.selectDestination(
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 }, 622 },
607 623
608 /** 624 /**
609 * Wait for a privet device to be registered. 625 * Wait for a privet device to be registered.
610 */ 626 */
611 waitForRegister: function(id) { 627 waitForRegister: function(id) {
612 this.nativeLayer_.startGetPrivetDestinations(); 628 this.nativeLayer_.startGetPrivetDestinations();
613 this.waitForRegisterDestination_ = id; 629 this.waitForRegisterDestination_ = id;
614 }, 630 },
615 631
632 resolveProvisionalDestination_: function(evt) {
633 var provisionalDestinationIndex = -1;
634 var provisionalDestination = null;
635 for (var i = 0; i < this.destinations_.length; ++i) {
636 if (evt.provisionalId == this.destinations_[i].id) {
637 provisionalDestinationIndex = i;
638 provisionalDestination = this.destinations_[i];
639 break;
640 }
641 }
642
643 if (!provisionalDestination)
644 return;
645
646 this.destinations_.splice(provisionalDestinationIndex, 1);
647 this.destinationMap_[this.getKey_(provisionalDestination)] = undefined;
648
649 var destination = evt.destination ?
650 print_preview.ExtensionDestinationParser.parse(evt.destination) :
651 null;
652 if (destination)
653 this.insertIntoStore_(destination);
654
655 var event = new Event(
656 DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED);
657 event.provisionalId = evt.provisionalId;
658 event.destination = destination;
659 this.dispatchEvent(event);
660 },
661
616 /** 662 /**
617 * Inserts {@code destination} to the data store and dispatches a 663 * Inserts {@code destination} to the data store and dispatches a
618 * DESTINATIONS_INSERTED event. 664 * DESTINATIONS_INSERTED event.
619 * @param {!print_preview.Destination} destination Print destination to 665 * @param {!print_preview.Destination} destination Print destination to
620 * insert. 666 * insert.
621 * @private 667 * @private
622 */ 668 */
623 insertDestination_: function(destination) { 669 insertDestination_: function(destination) {
624 if (this.insertIntoStore_(destination)) { 670 if (this.insertIntoStore_(destination)) {
625 this.destinationsInserted_(destination); 671 this.destinationsInserted_(destination);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, 821 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET,
776 this.onPrivetCapabilitiesSet_.bind(this)); 822 this.onPrivetCapabilitiesSet_.bind(this));
777 this.tracker_.add( 823 this.tracker_.add(
778 this.nativeLayer_, 824 this.nativeLayer_,
779 print_preview.NativeLayer.EventType.EXTENSION_PRINTERS_ADDED, 825 print_preview.NativeLayer.EventType.EXTENSION_PRINTERS_ADDED,
780 this.onExtensionPrintersAdded_.bind(this)); 826 this.onExtensionPrintersAdded_.bind(this));
781 this.tracker_.add( 827 this.tracker_.add(
782 this.nativeLayer_, 828 this.nativeLayer_,
783 print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET, 829 print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET,
784 this.onExtensionCapabilitiesSet_.bind(this)); 830 this.onExtensionCapabilitiesSet_.bind(this));
831 this.tracker_.add(
832 this.nativeLayer_,
833 print_preview.NativeLayer.EventType.EXTENSION_USB_PRINTER_RESOLVED,
834 this.resolveProvisionalDestination_.bind(this));
785 }, 835 },
786 836
787 /** 837 /**
788 * Creates a local PDF print destination. 838 * Creates a local PDF print destination.
789 * @return {!print_preview.Destination} Created print destination. 839 * @return {!print_preview.Destination} Created print destination.
790 * @private 840 * @private
791 */ 841 */
792 createLocalPdfPrintDestination_: function() { 842 createLocalPdfPrintDestination_: function() {
793 // TODO(alekseys): Create PDF printer in the native code and send its 843 // TODO(alekseys): Create PDF printer in the native code and send its
794 // capabilities back with other local printers. 844 // capabilities back with other local printers.
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 /** 1051 /**
1002 * Called when an extension responds to a getExtensionDestinations 1052 * Called when an extension responds to a getExtensionDestinations
1003 * request. 1053 * request.
1004 * @param {Object} event Contains information about list of printers 1054 * @param {Object} event Contains information about list of printers
1005 * reported by the extension. 1055 * reported by the extension.
1006 * {@code done} parameter is set iff this is the final list of printers 1056 * {@code done} parameter is set iff this is the final list of printers
1007 * returned as part of getExtensionDestinations request. 1057 * returned as part of getExtensionDestinations request.
1008 * @private 1058 * @private
1009 */ 1059 */
1010 onExtensionPrintersAdded_: function(event) { 1060 onExtensionPrintersAdded_: function(event) {
1011 // Filter out printers enumerated from providers' "usbPrinters" manifest 1061 event.printers.forEach(function(printer) {
1012 // key for now. 1062 if (!printer.needsUsbPermission) {
1013 var filteredPrinters = event.printers.filter(function(printer) { 1063 this.insertDestinations_([
1014 return 'extensionId' in printer; 1064 print_preview.ExtensionDestinationParser.parse(printer)
1015 }); 1065 ]);
1016 this.insertDestinations_(filteredPrinters.map(function(printer) { 1066 return;
1017 return print_preview.ExtensionDestinationParser.parse(printer); 1067 }
1018 })); 1068
1069 // Add provisional extension destinations that support a USB printer.
1070 printer.extensions.forEach(function(extension) {
1071 var provisionalDest = print_preview.ExtensionDestinationParser.parse({
1072 id: extension.printerId,
1073 extensionId: extension.extensionId,
1074 extensionName: extension.extensionName,
1075 name: printer.name,
1076 usbDevice: printer.usbDevice
1077 });
1078
1079 provisionalDest.provisionalType =
1080 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION;
1081 this.insertDestinations_([provisionalDest]);
1082 }.bind(this));
1083 }.bind(this));
1019 1084
1020 if (event.done && this.isExtensionDestinationSearchInProgress_) { 1085 if (event.done && this.isExtensionDestinationSearchInProgress_) {
1021 clearTimeout(this.extensionSearchTimeout_); 1086 clearTimeout(this.extensionSearchTimeout_);
1022 this.endExtensionPrinterSearch_(); 1087 this.endExtensionPrinterSearch_();
1023 } 1088 }
1024 }, 1089 },
1025 1090
1026 /** 1091 /**
1027 * Called when capabilities for an extension managed printer are set. 1092 * Called when capabilities for an extension managed printer are set.
1028 * @param {Object} event Contains the printer's capabilities and ID. 1093 * @param {Object} event Contains the printer's capabilities and ID.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 return id == this.appState_.selectedDestinationId && 1163 return id == this.appState_.selectedDestinationId &&
1099 origin == this.appState_.selectedDestinationOrigin; 1164 origin == this.appState_.selectedDestinationOrigin;
1100 } 1165 }
1101 }; 1166 };
1102 1167
1103 // Export 1168 // Export
1104 return { 1169 return {
1105 DestinationStore: DestinationStore 1170 DestinationStore: DestinationStore
1106 }; 1171 };
1107 }); 1172 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698