| OLD | NEW |
| (Empty) | |
| 1 |
| 2 cr.define('print_preview', function() { |
| 3 'use strict'; |
| 4 |
| 5 /** @enum {string} */ |
| 6 var ResolverState = { |
| 7 INITIAL: 'INITIAL', |
| 8 ACTIVE: 'ACTIVE', |
| 9 GRANTING_PERMISSION: 'GRANTING_PERMISSION', |
| 10 DONE: 'DONE' |
| 11 }; |
| 12 |
| 13 /** |
| 14 * @param {!Promise.<!print_preview.Destination>} promise |
| 15 * @param {function(!print_preview.Destination)} resolve |
| 16 * @param {function()} reject |
| 17 * @constructor @struct |
| 18 */ |
| 19 function PromiseResolver(promise, resolve, reject) { |
| 20 /** @type {!Promise.<!print_preview.Destination>} */ |
| 21 this.promise = promise; |
| 22 /** @type {function(!print_preview.Destination)} */ |
| 23 this.resolve = resolve; |
| 24 /** @type {function()} */ |
| 25 this.reject = reject; |
| 26 } |
| 27 |
| 28 /** @return {!PromiseResolver} */ |
| 29 PromiseResolver.create = function() { |
| 30 var reject = null; |
| 31 var resolve = null; |
| 32 /** @type {!Promise.<!print_preview.Destination>} */ |
| 33 var promise = new Promise(function(resolvePromise, rejectPromise) { |
| 34 resolve = /** @type {function(!print_preview.Destination)}*/( |
| 35 resolvePromise); |
| 36 reject = /** @type {function()} */(rejectPromise); |
| 37 }); |
| 38 |
| 39 return new PromiseResolver(promise, resolve, reject); |
| 40 }; |
| 41 |
| 42 /** |
| 43 * @param {!print_preview.DestinationStore} destinationStore |
| 44 * @param {!print_preview.Destination} destination |
| 45 * @constructor |
| 46 * @extends {print_preview.Overlay} |
| 47 */ |
| 48 function ProvisionalDestinationResolver(destinationStore, destination) { |
| 49 print_preview.Overlay.call(this); |
| 50 |
| 51 /** @private {!print_preview.DestinationStore} */ |
| 52 this.destinationStore_ = destinationStore; |
| 53 /** @private {!print_preview.Destination} */ |
| 54 this.destination_ = destination; |
| 55 |
| 56 /** @private {ResolverState} */ |
| 57 this.state_ = ResolverState.INITIAL; |
| 58 |
| 59 /** @private {?PromiseResolver} */ |
| 60 this.promiseResolver_ = null; |
| 61 } |
| 62 |
| 63 /** |
| 64 * @param {!print_preview.DestinationStore} store |
| 65 * @param {!print_preview.Destination} destination |
| 66 * @return {?ProvisionalDestinationResolver} |
| 67 */ |
| 68 ProvisionalDestinationResolver.create = function(store, destination) { |
| 69 if (destination.provisionalType != |
| 70 print_preview.Destination.ProvisionalType.NEEDS_USB_PERMISSION) { |
| 71 return null; |
| 72 } |
| 73 return new ProvisionalDestinationResolver(store, destination); |
| 74 }; |
| 75 |
| 76 ProvisionalDestinationResolver.prototype = { |
| 77 __proto__: print_preview.Overlay.prototype, |
| 78 |
| 79 /** @override */ |
| 80 enterDocument: function() { |
| 81 print_preview.Overlay.prototype.enterDocument.call(this); |
| 82 |
| 83 this.tracker.add( |
| 84 this.getChildElement('.usb-permission-ok-button'), |
| 85 'click', |
| 86 this.grantDevicePermission_.bind(this)); |
| 87 this.tracker.add( |
| 88 this.getChildElement('.usb-permission-cancel-button'), |
| 89 'click', |
| 90 this.cancel.bind(this)); |
| 91 |
| 92 this.tracker.add( |
| 93 this.destinationStore_, |
| 94 print_preview.DestinationStore.EventType |
| 95 .PROVISIONAL_DESTINATION_RESOLVED, |
| 96 this.onDestinationResolved_.bind(this)); |
| 97 }, |
| 98 |
| 99 /** @override */ |
| 100 onSetVisibleInternal: function(visible) { |
| 101 if (visible) { |
| 102 assert(this.state_ == ResolverState.INITIAL, |
| 103 'Showing overlay while not in initial state.'); |
| 104 assert(!this.promiseResolver_, 'Promise resolver already set.'); |
| 105 this.setState_(ResolverState.ACTIVE); |
| 106 this.promiseResolver_ = PromiseResolver.create(); |
| 107 |
| 108 this.getChildElement('.usb-permission-ok-button').disabled = false; |
| 109 this.getChildElement('.usb-permission-cancel-button').disabled = false; |
| 110 this.getChildElement('.throbber').hidden = true; |
| 111 } else if (this.state_ != ResolverState.DONE) { |
| 112 assert(this.state_ != ResolverState.INITIAL, 'Hiding in initial state'); |
| 113 this.setState_(ResolverState.DONE); |
| 114 this.promiseResolver_.reject(); |
| 115 this.promiseResolver_ = null; |
| 116 } |
| 117 }, |
| 118 |
| 119 /** @override */ |
| 120 createDom: function() { |
| 121 this.setElementInternal(this.cloneTemplateInternal( |
| 122 'extension-usb-resolver')); |
| 123 |
| 124 var extNameEl = this.getChildElement('.usb-permission-extension-name'); |
| 125 extNameEl.title = this.destination_.extensionName; |
| 126 extNameEl.textContent = this.destination_.extensionName; |
| 127 |
| 128 var extIconEl = this.getChildElement('.usb-permission-extension-icon'); |
| 129 extIconEl.style.backgroundImage = '-webkit-image-set(' + |
| 130 'url(chrome://extension-icon/' + |
| 131 this.destination_.extensionId + '/24/1) 1x,' + |
| 132 'url(chrome://extension-icon/' + |
| 133 this.destination_.extensionId + '/48/1) 2x)'; |
| 134 }, |
| 135 |
| 136 /** @private */ |
| 137 grantDevicePermission_: function() { |
| 138 assert(this.state_ == ResolverState.ACTIVE, |
| 139 'Invalid state in request grant permission'); |
| 140 |
| 141 this.setState_(ResolverState.GRANTING_PERMISSION); |
| 142 this.destinationStore_.resolveProvisionalDestination(this.destination_); |
| 143 }, |
| 144 |
| 145 /** |
| 146 * @param {Event} event |
| 147 * @private |
| 148 */ |
| 149 onDestinationResolved_: function(event) { |
| 150 if (this.state_ == ResolverState.DONE) |
| 151 return; |
| 152 |
| 153 if (event.provisionalId != this.destination_.id) |
| 154 return; |
| 155 |
| 156 this.setState_(ResolverState.DONE); |
| 157 if (event.destination) { |
| 158 this.promiseResolver_.resolve(event.destination); |
| 159 } else { |
| 160 this.promiseResolver_.reject(); |
| 161 } |
| 162 |
| 163 this.promiseResolver_ = null; |
| 164 this.setIsVisible(false); |
| 165 }, |
| 166 |
| 167 /** |
| 168 * @param {ResolverState} state |
| 169 * @private |
| 170 */ |
| 171 setState_: function(state) { |
| 172 if (this.state_ == state) |
| 173 return; |
| 174 |
| 175 this.state_ = state; |
| 176 this.updateControls_(); |
| 177 }, |
| 178 |
| 179 /** @private */ |
| 180 updateControls_: function() { |
| 181 var enabled = this.state_ == ResolverState.ACTIVE; |
| 182 this.getChildElement('.usb-permission-ok-button').disabled = !enabled; |
| 183 this.getChildElement('.usb-permission-cancel-button').disabled = !enabled; |
| 184 this.getChildElement('.throbber').hidden = |
| 185 this.state_ == ResolverState.GRANTiNG_PERMISSION; |
| 186 }, |
| 187 |
| 188 /** |
| 189 * @param {!HTMLElement} parent |
| 190 * @return {!Promise.<!print_preview.Destination>} |
| 191 */ |
| 192 run: function(parent) { |
| 193 this.render(parent); |
| 194 this.setIsVisible(true); |
| 195 |
| 196 assert(this.promiseResolver_, 'Promise resolver not created.'); |
| 197 return this.promiseResolver_.promise; |
| 198 } |
| 199 }; |
| 200 |
| 201 return { |
| 202 ProvisionalDestinationResolver: ProvisionalDestinationResolver |
| 203 }; |
| 204 }); |
| OLD | NEW |