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

Unified Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 900503002: List printers managed by extensions in print preview (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/data/destination_store.js
diff --git a/chrome/browser/resources/print_preview/data/destination_store.js b/chrome/browser/resources/print_preview/data/destination_store.js
index b3249f2de77fb6faf13cfdbbdcd790c522f949c0..03c839445cad9fde84aa4fc45d9600a8a87baee5 100644
--- a/chrome/browser/resources/print_preview/data/destination_store.js
+++ b/chrome/browser/resources/print_preview/data/destination_store.js
@@ -154,6 +154,29 @@ cr.define('print_preview', function() {
this.privetSearchTimeout_ = null;
/**
+ * Whether a search for extension destinations is in progress.
+ * @type {boolean}
+ * @private
+ */
+ this.isExtensionDestinationSearchInProgress_ = false;
+
+ /**
+ * Whether the destination store has already loaded all extension
+ * destinations.
+ * @type {boolean}
+ * @private
+ */
+ this.hasLoadedAllExtensionDestinations_ = false;
+
+ /**
+ * ID of a timeout set at the start of an extension destination search. The
+ * timeout ends the search.
+ * @type {?number}
+ * @private
+ */
+ this.extensionSearchTimeout_ = null;
+
+ /**
* MDNS service name of destination that we are waiting to register.
* @type {?string}
* @private
@@ -201,6 +224,15 @@ cr.define('print_preview', function() {
DestinationStore.PRIVET_SEARCH_DURATION_ = 2000;
/**
+ * Maximum amount of time spent searching for extension destinations, in
+ * milliseconds.
+ * @type {number}
+ * @const
+ * @private
+ */
+ DestinationStore.EXTENSION_SEARCH_DURATION_ = 2000;
Aleksey Shlyapnikov 2015/02/03 20:18:24 Just 2 seconds?
tbarzic 2015/02/04 02:21:23 I can increase the number a bit, but few seconds s
+
+ /**
* Localizes printer capabilities.
* @param {!Object} capabilities Printer capabilities to localize.
* @return {!Object} Localized capabilities.
@@ -267,7 +299,8 @@ cr.define('print_preview', function() {
*/
get isLocalDestinationSearchInProgress() {
return this.isLocalDestinationSearchInProgress_ ||
- this.isPrivetDestinationSearchInProgress_;
+ this.isPrivetDestinationSearchInProgress_ ||
+ this.isExtensionDestinationSearchInProgress_;
},
/**
@@ -339,6 +372,31 @@ cr.define('print_preview', function() {
cr.dispatchSimpleEvent(
this,
DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
+ } else if (this.appState_.selectedDestinationOrigin ==
+ print_preview.Destination.Origin.EXTENSION) {
+ // TODO(tbarzic): Add support for requesting a single extension's
+ // printer list.
+ this.startLoadExtensionDestinations();
+
+ var parsedDestinationID =
+ print_preview.ExtensionDestinationParser.parseDestinationID(
+ this.appState_.selectedDestinationId);
+ this.selectedDestination_ =
+ print_preview.ExtensionDestinationParser.parse({
+ extensionId: parsedDestinationID.extensionID,
+ id: parsedDestinationID.printerID,
+ name: this.appState_.selectedDestinationName_ || ''
+ });
+
+ if (this.appState_.selectedDestinationCapabilities) {
+ this.selectedDestination_.capabilities =
+ this.appState_.selectedDestinationCapabilities;
+
+ cr.dispatchSimpleEvent(
+ this,
+ DestinationStore.EventType
+ .CACHED_SELECTED_DESTINATION_INFO_READY);
+ }
} else {
this.selectDefaultDestination_();
}
@@ -437,8 +495,14 @@ cr.define('print_preview', function() {
if (destination.isPrivet) {
this.nativeLayer_.startGetPrivetDestinationCapabilities(
destination.id);
- }
- else if (destination.isLocal) {
+ } else if (destination.isExtension) {
+ var parsedDestinationID =
+ print_preview.ExtensionDestinationParser.parseDestinationID(
+ destination.id);
+ this.nativeLayer_.startGetExtensionDestinationCapabilities(
+ parsedDestinationID.extensionID,
+ parsedDestinationID.printerID);
+ } else if (destination.isLocal) {
this.nativeLayer_.startGetLocalDestinationCapabilities(
destination.id);
} else {
@@ -481,18 +545,35 @@ cr.define('print_preview', function() {
/** Initiates loading of privet print destinations. */
startLoadPrivetDestinations: function() {
if (!this.hasLoadedAllPrivetDestinations_) {
+ if (this.privetDestinationSearchInProgress_)
+ clearTimeout(this.privetSearchTimeout_);
this.isPrivetDestinationSearchInProgress_ = true;
this.nativeLayer_.startGetPrivetDestinations();
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
- if (this.privetDestinationSearchInProgress_)
- clearTimeout(this.privetSearchTimeout_);
this.privetSearchTimeout_ = setTimeout(
this.endPrivetPrinterSearch_.bind(this),
DestinationStore.PRIVET_SEARCH_DURATION_);
}
},
+ /** Initializes loading of extension managed print destinations. */
+ startLoadExtensionDestinations: function() {
+ if (this.hasLoadedAllExtensionDestinations_)
+ return;
+
+ if (this.isExtensionDestinationSearchInProgress_)
+ clearTimeout(this.extensionSearchTimeout_);
+
+ this.isExtensionDestinationSearchInProgress_ = true;
+ this.nativeLayer_.startGetExtensionDestinations();
+ cr.dispatchSimpleEvent(
+ this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
+ this.extensionSearchTimeout_ = setTimeout(
+ this.endExtensionPrinterSearch_.bind(this),
+ DestinationStore.EXTENSION_SEARCH_DURATION_);
+ },
+
/**
* Initiates loading of cloud destinations.
* @param {print_preview.Destination.Origin=} opt_origin Search destinations
@@ -528,6 +609,7 @@ cr.define('print_preview', function() {
this.startLoadCloudDestinations();
this.startLoadLocalDestinations();
this.startLoadPrivetDestinations();
+ this.startLoadExtensionDestinations();
},
/**
@@ -632,6 +714,21 @@ cr.define('print_preview', function() {
},
/**
+ * Called when loading of extension managed printers is done.
+ * @private
+ */
+ endExtensionPrinterSearch_: function() {
+ this.isExtensionDestinationSearchInProgress_ = false;
+ this.hasLoadedAllExtensionDestinations_ = true;
+ cr.dispatchSimpleEvent(
+ this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
+ // Clear initially selected (cached) extension destination if it hasn't
+ // been found among reported extension destinations.
+ if (this.isInAutoSelectMode_ && this.selectedDestination_.isExtension)
+ this.selectDefaultDestination_();
+ },
+
+ /**
* Inserts a destination into the store without dispatching any events.
* @return {boolean} Whether the inserted destination was not already in the
* store.
@@ -684,6 +781,14 @@ cr.define('print_preview', function() {
this.nativeLayer_,
print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET,
this.onPrivetCapabilitiesSet_.bind(this));
+ this.tracker_.add(
+ this.nativeLayer_,
+ print_preview.NativeLayer.EventType.EXTENSION_PRINTERS_ADDED,
+ this.onExtensionPrintersAdded_.bind(this));
+ this.tracker_.add(
+ this.nativeLayer_,
+ print_preview.NativeLayer.EventType.EXTENSION_CAPABILITIES_SET,
+ this.onExtensionCapabilitiesSet_.bind(this));
},
/**
@@ -716,6 +821,7 @@ cr.define('print_preview', function() {
this.loadedCloudOrigins_ = {};
this.hasLoadedAllLocalDestinations_ = false;
this.hasLoadedAllPrivetDestinations_ = false;
+ this.hasLoadedAllExtensionDestinations_ = false;
clearTimeout(this.autoSelectTimeout_);
this.autoSelectTimeout_ = setTimeout(
@@ -891,7 +997,6 @@ cr.define('print_preview', function() {
* @private
*/
onPrivetCapabilitiesSet_: function(event) {
- var destinationId = event.printerId;
var destinations =
print_preview.PrivetDestinationParser.parse(event.printer);
destinations.forEach(function(dest) {
@@ -901,6 +1006,47 @@ cr.define('print_preview', function() {
},
/**
+ * Called when an extension responds to a getExtensionDestinations
+ * request.
+ * @param {Object} event Contains information about list of printers
+ * reported by the extension.
+ * {@code done} parameter is set iff this is the final list of printers
+ * returned as part of getExtensionDestinations request.
+ * @private
+ */
+ onExtensionPrintersAdded_: function(event) {
+ this.insertDestinations_(event.printers.map(function(printer) {
+ return print_preview.ExtensionDestinationParser.parse(printer);
+ }));
+
+ if (event.done && this.isExtensionDestinationSearchInProgress_) {
+ clearTimeout(this.extensionSearchTimeout_);
+ this.endExtensionPrinterSearch_();
+ }
+ },
+
+ /**
+ * Called when capabilities for an extension managed printer are set.
+ * @param {Object} event Contains the printer capabilities and ID, and the
+ * ID of extension that manages the printer.
+ * @private
+ */
+ onExtensionCapabilitiesSet_: function(event) {
+ var destinationID =
+ print_preview.ExtensionDestinationParser.generateDestinationID(
+ event.extensionID, event.printerID);
+ var destinationKey = this.getDestinationKey_(
+ print_preview.Destination.Origin.EXTENSION,
+ destinationID,
+ '' /* account */);
+ var destination = this.destinationMap_[destinationKey];
+ if (!destination)
+ return;
+ destination.capabilities = event.capabilities;
+ this.updateDestination_(destination);
+ },
+
+ /**
* Called from native layer after the user was requested to sign in, and did
* so successfully.
* @private

Powered by Google App Engine
This is Rietveld 408576698