| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** @enum {string} */ | 8 /** @enum {string} */ |
| 9 var ResolverState = { | 9 var ResolverState = { |
| 10 INITIAL: 'INITIAL', | 10 INITIAL: 'INITIAL', |
| 11 ACTIVE: 'ACTIVE', | 11 ACTIVE: 'ACTIVE', |
| 12 GRANTING_PERMISSION: 'GRANTING_PERMISSION', | 12 GRANTING_PERMISSION: 'GRANTING_PERMISSION', |
| 13 ERROR: 'ERROR', | 13 ERROR: 'ERROR', |
| 14 DONE: 'DONE' | 14 DONE: 'DONE' |
| 15 }; | 15 }; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Utility class for bundling a promise object with it's resolver methods. | |
| 19 * @param {!Promise<!print_preview.Destination>} promise A promise returning | |
| 20 * a destination. | |
| 21 * @param {function(!print_preview.Destination)} resolve Function resolving | |
| 22 * the promise. | |
| 23 * @param {function()} reject Function for rejecting the promise. | |
| 24 * @constructor @struct | |
| 25 */ | |
| 26 function PromiseResolver(promise, resolve, reject) { | |
| 27 /** @type {!Promise<!print_preview.Destination>} */ | |
| 28 this.promise = promise; | |
| 29 /** @type {function(!print_preview.Destination)} */ | |
| 30 this.resolve = resolve; | |
| 31 /** @type {function()} */ | |
| 32 this.reject = reject; | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Create a Promise and an associated PromiseResolver. | |
| 37 * @return {!PromiseResolver} | |
| 38 */ | |
| 39 PromiseResolver.create = function() { | |
| 40 var reject = null; | |
| 41 var resolve = null; | |
| 42 /** @type {!Promise<!print_preview.Destination>} */ | |
| 43 var promise = new Promise(function(resolvePromise, rejectPromise) { | |
| 44 resolve = /** @type {function(!print_preview.Destination)}*/( | |
| 45 resolvePromise); | |
| 46 reject = /** @type {function()} */(rejectPromise); | |
| 47 }); | |
| 48 | |
| 49 return new PromiseResolver(promise, resolve, reject); | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 53 * Overlay used to resolve a provisional extension destination. The user is | 18 * Overlay used to resolve a provisional extension destination. The user is |
| 54 * prompted to allow print preview to grant a USB device access to an | 19 * prompted to allow print preview to grant a USB device access to an |
| 55 * extension associated with the destination. If user agrees destination | 20 * extension associated with the destination. If user agrees destination |
| 56 * resolvement is attempted (which includes granting the extension USB access | 21 * resolvement is attempted (which includes granting the extension USB access |
| 57 * and requesting destination description from the extension). The overlay is | 22 * and requesting destination description from the extension). The overlay is |
| 58 * hidden when destination resolving is done. | 23 * hidden when destination resolving is done. |
| 59 * | 24 * |
| 60 * @param {!print_preview.DestinationStore} destinationStore The destination | 25 * @param {!print_preview.DestinationStore} destinationStore The destination |
| 61 * store containing the destination. Used as a proxy to native layer for | 26 * store containing the destination. Used as a proxy to native layer for |
| 62 * resolving the destination. | 27 * resolving the destination. |
| 63 * @param {!print_preview.Destination} destination The destination that has | 28 * @param {!print_preview.Destination} destination The destination that has |
| 64 * to be resolved. | 29 * to be resolved. |
| 65 * @constructor | 30 * @constructor |
| 66 * @extends {print_preview.Overlay} | 31 * @extends {print_preview.Overlay} |
| 67 */ | 32 */ |
| 68 function ProvisionalDestinationResolver(destinationStore, destination) { | 33 function ProvisionalDestinationResolver(destinationStore, destination) { |
| 69 print_preview.Overlay.call(this); | 34 print_preview.Overlay.call(this); |
| 70 | 35 |
| 71 /** @private {!print_preview.DestinationStore} */ | 36 /** @private {!print_preview.DestinationStore} */ |
| 72 this.destinationStore_ = destinationStore; | 37 this.destinationStore_ = destinationStore; |
| 73 /** @private {!print_preview.Destination} */ | 38 /** @private {!print_preview.Destination} */ |
| 74 this.destination_ = destination; | 39 this.destination_ = destination; |
| 75 | 40 |
| 76 /** @private {ResolverState} */ | 41 /** @private {ResolverState} */ |
| 77 this.state_ = ResolverState.INITIAL; | 42 this.state_ = ResolverState.INITIAL; |
| 78 | 43 |
| 79 /** | 44 /** |
| 80 * Promise resolver for promise returned by {@code this.run}. | 45 * Promise resolver for promise returned by {@code this.run}. |
| 81 * @private {?PromiseResolver} | 46 * @private {?PromiseResolver<!print_preview.Destination>} |
| 82 */ | 47 */ |
| 83 this.promiseResolver_ = null; | 48 this.promiseResolver_ = null; |
| 84 } | 49 } |
| 85 | 50 |
| 86 /** | 51 /** |
| 87 * @param {!print_preview.DestinationStore} store | 52 * @param {!print_preview.DestinationStore} store |
| 88 * @param {!print_preview.Destination} destination | 53 * @param {!print_preview.Destination} destination |
| 89 * @return {?ProvisionalDestinationResolver} | 54 * @return {?ProvisionalDestinationResolver} |
| 90 */ | 55 */ |
| 91 ProvisionalDestinationResolver.create = function(store, destination) { | 56 ProvisionalDestinationResolver.create = function(store, destination) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 119 this.onDestinationResolved_.bind(this)); | 84 this.onDestinationResolved_.bind(this)); |
| 120 }, | 85 }, |
| 121 | 86 |
| 122 /** @override */ | 87 /** @override */ |
| 123 onSetVisibleInternal: function(visible) { | 88 onSetVisibleInternal: function(visible) { |
| 124 if (visible) { | 89 if (visible) { |
| 125 assert(this.state_ == ResolverState.INITIAL, | 90 assert(this.state_ == ResolverState.INITIAL, |
| 126 'Showing overlay while not in initial state.'); | 91 'Showing overlay while not in initial state.'); |
| 127 assert(!this.promiseResolver_, 'Promise resolver already set.'); | 92 assert(!this.promiseResolver_, 'Promise resolver already set.'); |
| 128 this.setState_(ResolverState.ACTIVE); | 93 this.setState_(ResolverState.ACTIVE); |
| 129 this.promiseResolver_ = PromiseResolver.create(); | 94 this.promiseResolver_ = new PromiseResolver(); |
| 130 this.getChildElement('.default').focus(); | 95 this.getChildElement('.default').focus(); |
| 131 } else if (this.state_ != ResolverState.DONE) { | 96 } else if (this.state_ != ResolverState.DONE) { |
| 132 assert(this.state_ != ResolverState.INITIAL, 'Hiding in initial state'); | 97 assert(this.state_ != ResolverState.INITIAL, 'Hiding in initial state'); |
| 133 this.setState_(ResolverState.DONE); | 98 this.setState_(ResolverState.DONE); |
| 134 this.promiseResolver_.reject(); | 99 this.promiseResolver_.reject(); |
| 135 this.promiseResolver_ = null; | 100 this.promiseResolver_ = null; |
| 136 } | 101 } |
| 137 }, | 102 }, |
| 138 | 103 |
| 139 /** @override */ | 104 /** @override */ |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 208 |
| 244 assert(this.promiseResolver_, 'Promise resolver not created.'); | 209 assert(this.promiseResolver_, 'Promise resolver not created.'); |
| 245 return this.promiseResolver_.promise; | 210 return this.promiseResolver_.promise; |
| 246 } | 211 } |
| 247 }; | 212 }; |
| 248 | 213 |
| 249 return { | 214 return { |
| 250 ProvisionalDestinationResolver: ProvisionalDestinationResolver | 215 ProvisionalDestinationResolver: ProvisionalDestinationResolver |
| 251 }; | 216 }; |
| 252 }); | 217 }); |
| OLD | NEW |