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

Side by Side Diff: chrome/browser/resources/print_preview/search/provisional_destination_resolver.js

Issue 1755423003: Re-use PromiseResolver helper in print preview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@reject_promise
Patch Set: Created 4 years, 9 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
« no previous file with comments | « chrome/browser/resources/print_preview/print_preview.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) {
Dan Beam 2016/03/03 21:18:24 nit: perhaps we should templatize PromiseResolver
dpapad 2016/03/04 18:58:42 Done. I templatized it in a bit simpler way compar
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.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview/print_preview.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698