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

Unified Diff: chrome/browser/resources/print_preview/search/provisional_destination_resolver.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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/search/provisional_destination_resolver.js
diff --git a/chrome/browser/resources/print_preview/search/provisional_destination_resolver.js b/chrome/browser/resources/print_preview/search/provisional_destination_resolver.js
new file mode 100644
index 0000000000000000000000000000000000000000..05e1d65b9d7e725513c77c59713b1a56b443f707
--- /dev/null
+++ b/chrome/browser/resources/print_preview/search/provisional_destination_resolver.js
@@ -0,0 +1,204 @@
+
+cr.define('print_preview', function() {
+ 'use strict';
+
+ /** @enum {string} */
+ var ResolverState = {
+ INITIAL: 'INITIAL',
+ ACTIVE: 'ACTIVE',
+ GRANTING_PERMISSION: 'GRANTING_PERMISSION',
+ DONE: 'DONE'
+ };
+
+ /**
+ * @param {!Promise.<!print_preview.Destination>} promise
+ * @param {function(!print_preview.Destination)} resolve
+ * @param {function()} reject
+ * @constructor @struct
+ */
+ function PromiseResolver(promise, resolve, reject) {
+ /** @type {!Promise.<!print_preview.Destination>} */
+ this.promise = promise;
+ /** @type {function(!print_preview.Destination)} */
+ this.resolve = resolve;
+ /** @type {function()} */
+ this.reject = reject;
+ }
+
+ /** @return {!PromiseResolver} */
+ PromiseResolver.create = function() {
+ var reject = null;
+ var resolve = null;
+ /** @type {!Promise.<!print_preview.Destination>} */
+ var promise = new Promise(function(resolvePromise, rejectPromise) {
+ resolve = /** @type {function(!print_preview.Destination)}*/(
+ resolvePromise);
+ reject = /** @type {function()} */(rejectPromise);
+ });
+
+ return new PromiseResolver(promise, resolve, reject);
+ };
+
+ /**
+ * @param {!print_preview.DestinationStore} destinationStore
+ * @param {!print_preview.Destination} destination
+ * @constructor
+ * @extends {print_preview.Overlay}
+ */
+ function ProvisionalDestinationResolver(destinationStore, destination) {
+ print_preview.Overlay.call(this);
+
+ /** @private {!print_preview.DestinationStore} */
+ this.destinationStore_ = destinationStore;
+ /** @private {!print_preview.Destination} */
+ this.destination_ = destination;
+
+ /** @private {ResolverState} */
+ this.state_ = ResolverState.INITIAL;
+
+ /** @private {?PromiseResolver} */
+ this.promiseResolver_ = null;
+ }
+
+ /**
+ * @param {!print_preview.DestinationStore} store
+ * @param {!print_preview.Destination} destination
+ * @return {?ProvisionalDestinationResolver}
+ */
+ ProvisionalDestinationResolver.create = function(store, destination) {
+ if (destination.provisionalType !=
+ print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION) {
+ return null;
+ }
+ return new ProvisionalDestinationResolver(store, destination);
+ };
+
+ ProvisionalDestinationResolver.prototype = {
+ __proto__: print_preview.Overlay.prototype,
+
+ /** @override */
+ enterDocument: function() {
+ print_preview.Overlay.prototype.enterDocument.call(this);
+
+ this.tracker.add(
+ this.getChildElement('.usb-permission-ok-button'),
+ 'click',
+ this.grantDevicePermission_.bind(this));
+ this.tracker.add(
+ this.getChildElement('.usb-permission-cancel-button'),
+ 'click',
+ this.cancel.bind(this));
+
+ this.tracker.add(
+ this.destinationStore_,
+ print_preview.DestinationStore.EventType
+ .PROVISIONAL_DESTINATION_RESOLVED,
+ this.onDestinationResolved_.bind(this));
+ },
+
+ /** @override */
+ onSetVisibleInternal: function(visible) {
+ if (visible) {
+ assert(this.state_ == ResolverState.INITIAL,
+ 'Showing overlay while not in initial state.');
+ assert(!this.promiseResolver_, 'Promise resolver already set.');
+ this.setState_(ResolverState.ACTIVE);
+ this.promiseResolver_ = PromiseResolver.create();
+
+ this.getChildElement('.usb-permission-ok-button').disabled = false;
+ this.getChildElement('.usb-permission-cancel-button').disabled = false;
+ this.getChildElement('.throbber').hidden = true;
+ } else if (this.state_ != ResolverState.DONE) {
+ assert(this.state_ != ResolverState.INITIAL, 'Hiding in initial state');
+ this.setState_(ResolverState.DONE);
+ this.promiseResolver_.reject();
+ this.promiseResolver_ = null;
+ }
+ },
+
+ /** @override */
+ createDom: function() {
+ this.setElementInternal(this.cloneTemplateInternal(
+ 'extension-usb-resolver'));
+
+ var extNameEl = this.getChildElement('.usb-permission-extension-name');
+ extNameEl.title = this.destination_.extensionName;
+ extNameEl.textContent = this.destination_.extensionName;
+
+ var extIconEl = this.getChildElement('.usb-permission-extension-icon');
+ extIconEl.style.backgroundImage = '-webkit-image-set(' +
+ 'url(chrome://extension-icon/' +
+ this.destination_.extensionId + '/24/1) 1x,' +
+ 'url(chrome://extension-icon/' +
+ this.destination_.extensionId + '/48/1) 2x)';
+ },
+
+ /** @private */
+ grantDevicePermission_: function() {
+ assert(this.state_ == ResolverState.ACTIVE,
+ 'Invalid state in request grant permission');
+
+ this.setState_(ResolverState.GRANTING_PERMISSION);
+ this.destinationStore_.resolveProvisionalDestination(this.destination_);
+ },
+
+ /**
+ * @param {Event} event
+ * @private
+ */
+ onDestinationResolved_: function(event) {
+ if (this.state_ == ResolverState.DONE)
+ return;
+
+ if (event.provisionalId != this.destination_.id)
+ return;
+
+ this.setState_(ResolverState.DONE);
+ if (event.destination) {
+ this.promiseResolver_.resolve(event.destination);
+ } else {
+ this.promiseResolver_.reject();
+ }
+
+ this.promiseResolver_ = null;
+ this.setIsVisible(false);
+ },
+
+ /**
+ * @param {ResolverState} state
+ * @private
+ */
+ setState_: function(state) {
+ if (this.state_ == state)
+ return;
+
+ this.state_ = state;
+ this.updateControls_();
+ },
+
+ /** @private */
+ updateControls_: function() {
+ var enabled = this.state_ == ResolverState.ACTIVE;
+ this.getChildElement('.usb-permission-ok-button').disabled = !enabled;
+ this.getChildElement('.usb-permission-cancel-button').disabled = !enabled;
+ this.getChildElement('.throbber').hidden =
+ this.state_ == ResolverState.GRANTiNG_PERMISSION;
+ },
+
+ /**
+ * @param {!HTMLElement} parent
+ * @return {!Promise.<!print_preview.Destination>}
+ */
+ run: function(parent) {
+ this.render(parent);
+ this.setIsVisible(true);
+
+ assert(this.promiseResolver_, 'Promise resolver not created.');
+ return this.promiseResolver_.promise;
+ }
+ };
+
+ return {
+ ProvisionalDestinationResolver: ProvisionalDestinationResolver
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698