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

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

Issue 2606043004: Perform printer setup on Chrome OS before selecting printer. (Closed)
Patch Set: fix unit test Created 3 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 cr.define('print_preview', function() {
6 'use strict';
7
8 /** @enum {string} */
9 var ResolverState = {
10 INITIAL: 'INITIAL',
11 CONFIGURING_PRINTER: 'CONFIGURING_PRINTER',
12 ERROR: 'ERROR',
13 DONE: 'DONE'
14 };
15
16 /**
17 * Overlay used to notify the user of printer setup steps for Chrome OS
18 * printers. Printer installation can occur before any print job is executed.
19 *
20 * @param {!print_preview.DestinationStore} destinationStore The destination
21 * store containing the destination. Used as a proxy to native layer for
22 * resolving the destination.
23 * @param {!print_preview.Destination} destination The destination that has
24 * to be resolved.
25 * @constructor
26 * @extends {print_preview.Overlay}
27 */
28 function CrosDestinationResolver(destinationStore, destination) {
29 print_preview.Overlay.call(this);
30
31 /** @private {!print_preview.DestinationStore} */
32 this.destinationStore_ = destinationStore;
33 /** @private {!print_preview.Destination} */
34 this.destination_ = destination;
35
36 /** @private {!ResolverState} */
37 this.state_ = ResolverState.INITIAL;
38
39 /**
40 * Promise resolver for promise returned by {@code this.run}.
41 * @private {?PromiseResolver<!print_preview.Destination>}
42 */
43 this.promiseResolver_ = null;
44 };
45
46 CrosDestinationResolver.prototype = {
47 __proto__: print_preview.Overlay.prototype,
48
49 /** @override */
50 enterDocument: function() {
51 print_preview.Overlay.prototype.enterDocument.call(this);
52
53 this.tracker.add(
54 this.destinationStore_,
55 print_preview.DestinationStore.EventType.PRINTER_CONFIGURED,
56 this.onDestinationResolved_.bind(this));
57 },
58
59 /** @override */
60 createDom: function() {
61 this.setElementInternal(this.cloneTemplateInternal(
62 'cros-printer-resolver'));
63 },
64
65 /**
66 * Dispatch the request to resolve the destination to the native layer.
67 * @private
68 */
69 startResolveDestination_: function() {
70 assert(this.state_ == ResolverState.INITIAL);
71 assert(!this.promiseResolver_, 'Promise resolver already set');
72 this.promiseResolver_ = new PromiseResolver();
73 this.destinationStore_.resolveCrosDestination(this.destination_);
74 this.setState_(ResolverState.CONFIGURING_PRINTER);
75 },
76
77 /**
78 * Handler for PRINTER_SETUP event. It finalizes the
79 * resolver state once the destination associated with the resolver gets
80 * resolved.
81 * @param {Event} event
82 * @private
83 */
84 onDestinationResolved_: function(event) {
85 assert(this.promiseResolver_, 'Promise resolver not found');
86 if (event.success) {
87 this.setState_(ResolverState.DONE);
88 assert(event.capabilities);
89 // event contains printerId and capabilities.
90 this.promiseResolver_.resolve(event);
91 this.setIsVisible(false);
92 } else {
93 this.setState_(ResolverState.ERROR);
94 this.promiseResolver_.reject();
95 }
96
97 this.promiseResolver_ = null;
98 },
99
100 /**
101 * Sets new resolver state and updates the UI accordingly.
102 * @param {ResolverState} state
103 * @private
104 */
105 setState_: function(state) {
106 if (this.state_ == state)
107 return;
108
109 this.state_ = state;
110 this.updateUI_();
111 },
112
113 /**
114 * Updates the resolver overlay UI to match the resolver state.
115 * @private
116 */
117 updateUI_: function() {
118 // TODO(crbug.com/677567): Replace with meaningful UI.
119 },
120
121 /**
122 * Initiates and shows the resolver overlay.
123 * @param {!HTMLElement} parent The element that should parent the resolver
124 * UI.
125 * @return {!Promise<!print_preview.Destination>} Promise that will be
126 * fulfilled when the destination resolving is finished.
127 */
128 run: function(parent) {
129 this.render(parent);
130 this.setIsVisible(true);
131 this.startResolveDestination_();
132
133 assert(this.promiseResolver_, 'Promise resolver not created.');
134 return this.promiseResolver_.promise;
135 }
136 };
137
138 return {
139 CrosDestinationResolver: CrosDestinationResolver
140 };
141 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698