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.exportPath('print_preview'); |
| 6 |
| 7 /** |
| 8 * States that the provisional destination resolver can be in. |
| 9 * @enum {string} |
| 10 */ |
| 11 print_preview.ResolverState = { |
| 12 INITIAL: 'INITIAL', |
| 13 ACTIVE: 'ACTIVE', |
| 14 GRANTING_PERMISSION: 'GRANTING_PERMISSION', |
| 15 ERROR: 'ERROR', |
| 16 DONE: 'DONE' |
| 17 }; |
| 18 |
5 cr.define('print_preview', function() { | 19 cr.define('print_preview', function() { |
6 'use strict'; | 20 'use strict'; |
7 | 21 |
8 /** @enum {string} */ | |
9 var ResolverState = { | |
10 INITIAL: 'INITIAL', | |
11 ACTIVE: 'ACTIVE', | |
12 GRANTING_PERMISSION: 'GRANTING_PERMISSION', | |
13 ERROR: 'ERROR', | |
14 DONE: 'DONE' | |
15 }; | |
16 | |
17 /** | 22 /** |
18 * Overlay used to resolve a provisional extension destination. The user is | 23 * Overlay used to resolve a provisional extension destination. The user is |
19 * prompted to allow print preview to grant a USB device access to an | 24 * prompted to allow print preview to grant a USB device access to an |
20 * extension associated with the destination. If user agrees destination | 25 * extension associated with the destination. If user agrees destination |
21 * resolvement is attempted (which includes granting the extension USB access | 26 * resolvement is attempted (which includes granting the extension USB access |
22 * and requesting destination description from the extension). The overlay is | 27 * and requesting destination description from the extension). The overlay is |
23 * hidden when destination resolving is done. | 28 * hidden when destination resolving is done. |
24 * | 29 * |
25 * @param {!print_preview.DestinationStore} destinationStore The destination | 30 * @param {!print_preview.DestinationStore} destinationStore The destination |
26 * store containing the destination. Used as a proxy to native layer for | 31 * store containing the destination. Used as a proxy to native layer for |
27 * resolving the destination. | 32 * resolving the destination. |
28 * @param {!print_preview.Destination} destination The destination that has | 33 * @param {!print_preview.Destination} destination The destination that has |
29 * to be resolved. | 34 * to be resolved. |
30 * @constructor | 35 * @constructor |
31 * @extends {print_preview.Overlay} | 36 * @extends {print_preview.Overlay} |
32 */ | 37 */ |
33 function ProvisionalDestinationResolver(destinationStore, destination) { | 38 function ProvisionalDestinationResolver(destinationStore, destination) { |
34 print_preview.Overlay.call(this); | 39 print_preview.Overlay.call(this); |
35 | 40 |
36 /** @private {!print_preview.DestinationStore} */ | 41 /** @private {!print_preview.DestinationStore} */ |
37 this.destinationStore_ = destinationStore; | 42 this.destinationStore_ = destinationStore; |
38 /** @private {!print_preview.Destination} */ | 43 /** @private {!print_preview.Destination} */ |
39 this.destination_ = destination; | 44 this.destination_ = destination; |
40 | 45 |
41 /** @private {ResolverState} */ | 46 /** @private {print_preview.ResolverState} */ |
42 this.state_ = ResolverState.INITIAL; | 47 this.state_ = print_preview.ResolverState.INITIAL; |
43 | 48 |
44 /** | 49 /** |
45 * Promise resolver for promise returned by {@code this.run}. | 50 * Promise resolver for promise returned by {@code this.run}. |
46 * @private {?PromiseResolver<!print_preview.Destination>} | 51 * @private {?PromiseResolver<!print_preview.Destination>} |
47 */ | 52 */ |
48 this.promiseResolver_ = null; | 53 this.promiseResolver_ = null; |
49 } | 54 } |
50 | 55 |
51 /** | 56 /** |
52 * @param {!print_preview.DestinationStore} store | 57 * @param {!print_preview.DestinationStore} store |
(...skipping 27 matching lines...) Expand all Loading... |
80 this.tracker.add( | 85 this.tracker.add( |
81 this.destinationStore_, | 86 this.destinationStore_, |
82 print_preview.DestinationStore.EventType | 87 print_preview.DestinationStore.EventType |
83 .PROVISIONAL_DESTINATION_RESOLVED, | 88 .PROVISIONAL_DESTINATION_RESOLVED, |
84 this.onDestinationResolved_.bind(this)); | 89 this.onDestinationResolved_.bind(this)); |
85 }, | 90 }, |
86 | 91 |
87 /** @override */ | 92 /** @override */ |
88 onSetVisibleInternal: function(visible) { | 93 onSetVisibleInternal: function(visible) { |
89 if (visible) { | 94 if (visible) { |
90 assert(this.state_ == ResolverState.INITIAL, | 95 assert(this.state_ == print_preview.ResolverState.INITIAL, |
91 'Showing overlay while not in initial state.'); | 96 'Showing overlay while not in initial state.'); |
92 assert(!this.promiseResolver_, 'Promise resolver already set.'); | 97 assert(!this.promiseResolver_, 'Promise resolver already set.'); |
93 this.setState_(ResolverState.ACTIVE); | 98 this.setState_(print_preview.ResolverState.ACTIVE); |
94 this.promiseResolver_ = new PromiseResolver(); | 99 this.promiseResolver_ = new PromiseResolver(); |
95 this.getChildElement('.default').focus(); | 100 this.getChildElement('.default').focus(); |
96 } else if (this.state_ != ResolverState.DONE) { | 101 } else if (this.state_ != print_preview.ResolverState.DONE) { |
97 assert(this.state_ != ResolverState.INITIAL, 'Hiding in initial state'); | 102 assert(this.state_ != print_preview.ResolverState.INITIAL, |
98 this.setState_(ResolverState.DONE); | 103 'Hiding in initial state'); |
| 104 this.setState_(print_preview.ResolverState.DONE); |
99 this.promiseResolver_.reject(); | 105 this.promiseResolver_.reject(); |
100 this.promiseResolver_ = null; | 106 this.promiseResolver_ = null; |
101 } | 107 } |
102 }, | 108 }, |
103 | 109 |
104 /** @override */ | 110 /** @override */ |
105 createDom: function() { | 111 createDom: function() { |
106 this.setElementInternal(this.cloneTemplateInternal( | 112 this.setElementInternal(this.cloneTemplateInternal( |
107 'extension-usb-resolver')); | 113 'extension-usb-resolver')); |
108 | 114 |
109 var extNameEl = this.getChildElement('.usb-permission-extension-name'); | 115 var extNameEl = this.getChildElement('.usb-permission-extension-name'); |
110 extNameEl.title = this.destination_.extensionName; | 116 extNameEl.title = this.destination_.extensionName; |
111 extNameEl.textContent = this.destination_.extensionName; | 117 extNameEl.textContent = this.destination_.extensionName; |
112 | 118 |
113 var extIconEl = this.getChildElement('.usb-permission-extension-icon'); | 119 var extIconEl = this.getChildElement('.usb-permission-extension-icon'); |
114 extIconEl.style.backgroundImage = '-webkit-image-set(' + | 120 extIconEl.style.backgroundImage = '-webkit-image-set(' + |
115 'url(chrome://extension-icon/' + | 121 'url(chrome://extension-icon/' + |
116 this.destination_.extensionId + '/24/1) 1x,' + | 122 this.destination_.extensionId + '/24/1) 1x,' + |
117 'url(chrome://extension-icon/' + | 123 'url(chrome://extension-icon/' + |
118 this.destination_.extensionId + '/48/1) 2x)'; | 124 this.destination_.extensionId + '/48/1) 2x)'; |
119 }, | 125 }, |
120 | 126 |
121 /** | 127 /** |
122 * Handler for click on OK button. It initiates destination resolving. | 128 * Handler for click on OK button. It initiates destination resolving. |
123 * @private | 129 * @private |
124 */ | 130 */ |
125 startResolveDestination_: function() { | 131 startResolveDestination_: function() { |
126 assert(this.state_ == ResolverState.ACTIVE, | 132 assert(this.state_ == print_preview.ResolverState.ACTIVE, |
127 'Invalid state in request grant permission'); | 133 'Invalid state in request grant permission'); |
128 | 134 |
129 this.setState_(ResolverState.GRANTING_PERMISSION); | 135 this.setState_(print_preview.ResolverState.GRANTING_PERMISSION); |
130 this.destinationStore_.resolveProvisionalDestination(this.destination_); | 136 this.destinationStore_.resolveProvisionalDestination(this.destination_); |
131 }, | 137 }, |
132 | 138 |
133 /** | 139 /** |
134 * Handler for PROVISIONAL_DESTINATION_RESOLVED event. It finalizes the | 140 * Handler for PROVISIONAL_DESTINATION_RESOLVED event. It finalizes the |
135 * resolver state once the destination associated with the resolver gets | 141 * resolver state once the destination associated with the resolver gets |
136 * resolved. | 142 * resolved. |
137 * @param {Event} event | 143 * @param {Event} event |
138 * @private | 144 * @private |
139 */ | 145 */ |
140 onDestinationResolved_: function(event) { | 146 onDestinationResolved_: function(event) { |
141 if (this.state_ == ResolverState.DONE) | 147 if (this.state_ == print_preview.ResolverState.DONE) |
142 return; | 148 return; |
143 | 149 |
144 if (event.provisionalId != this.destination_.id) | 150 if (event.provisionalId != this.destination_.id) |
145 return; | 151 return; |
146 | 152 |
147 if (event.destination) { | 153 if (event.destination) { |
148 this.setState_(ResolverState.DONE); | 154 this.setState_(print_preview.ResolverState.DONE); |
149 this.promiseResolver_.resolve(event.destination); | 155 this.promiseResolver_.resolve(event.destination); |
150 this.promiseResolver_ = null; | 156 this.promiseResolver_ = null; |
151 this.setIsVisible(false); | 157 this.setIsVisible(false); |
152 } else { | 158 } else { |
153 this.setState_(ResolverState.ERROR); | 159 this.setState_(print_preview.ResolverState.ERROR); |
154 } | 160 } |
155 }, | 161 }, |
156 | 162 |
157 /** | 163 /** |
158 * Sets new resolver state and updates the UI accordingly. | 164 * Sets new resolver state and updates the UI accordingly. |
159 * @param {ResolverState} state | 165 * @param {print_preview.ResolverState} state |
160 * @private | 166 * @private |
161 */ | 167 */ |
162 setState_: function(state) { | 168 setState_: function(state) { |
163 if (this.state_ == state) | 169 if (this.state_ == state) |
164 return; | 170 return; |
165 | 171 |
166 this.state_ = state; | 172 this.state_ = state; |
167 this.updateUI_(); | 173 this.updateUI_(); |
168 }, | 174 }, |
169 | 175 |
170 /** | 176 /** |
171 * Updates the resolver overlay UI to match the resolver state. | 177 * Updates the resolver overlay UI to match the resolver state. |
172 * @private | 178 * @private |
173 */ | 179 */ |
174 updateUI_: function() { | 180 updateUI_: function() { |
175 this.getChildElement('.usb-permission-ok-button').hidden = | 181 this.getChildElement('.usb-permission-ok-button').hidden = |
176 this.state_ == ResolverState.ERROR; | 182 this.state_ == print_preview.ResolverState.ERROR; |
177 this.getChildElement('.usb-permission-ok-button').disabled = | 183 this.getChildElement('.usb-permission-ok-button').disabled = |
178 this.state_ != ResolverState.ACTIVE; | 184 this.state_ != print_preview.ResolverState.ACTIVE; |
179 | 185 |
180 // If OK button is disabled, make sure Cancel button gets focus. | 186 // If OK button is disabled, make sure Cancel button gets focus. |
181 if (this.state_ != ResolverState.ACTIVE) | 187 if (this.state_ != print_preview.ResolverState.ACTIVE) |
182 this.getChildElement('.cancel').focus(); | 188 this.getChildElement('.cancel').focus(); |
183 | 189 |
184 this.getChildElement('.throbber-placeholder').classList.toggle( | 190 this.getChildElement('.throbber-placeholder').classList.toggle( |
185 'throbber', | 191 'throbber', |
186 this.state_ == ResolverState.GRANTING_PERMISSION); | 192 this.state_ == print_preview.ResolverState.GRANTING_PERMISSION); |
187 | 193 |
188 this.getChildElement('.usb-permission-extension-desc').hidden = | 194 this.getChildElement('.usb-permission-extension-desc').hidden = |
189 this.state_ == ResolverState.ERROR; | 195 this.state_ == print_preview.ResolverState.ERROR; |
190 | 196 |
191 this.getChildElement('.usb-permission-message').textContent = | 197 this.getChildElement('.usb-permission-message').textContent = |
192 this.state_ == ResolverState.ERROR ? | 198 this.state_ == print_preview.ResolverState.ERROR ? |
193 loadTimeData.getStringF('resolveExtensionUSBErrorMessage', | 199 loadTimeData.getStringF('resolveExtensionUSBErrorMessage', |
194 this.destination_.extensionName) : | 200 this.destination_.extensionName) : |
195 loadTimeData.getString('resolveExtensionUSBPermissionMessage'); | 201 loadTimeData.getString('resolveExtensionUSBPermissionMessage'); |
196 }, | 202 }, |
197 | 203 |
198 /** | 204 /** |
199 * Initiates and shows the resolver overlay. | 205 * Initiates and shows the resolver overlay. |
200 * @param {!HTMLElement} parent The element that should parent the resolver | 206 * @param {!HTMLElement} parent The element that should parent the resolver |
201 * UI. | 207 * UI. |
202 * @return {!Promise<!print_preview.Destination>} Promise that will be | 208 * @return {!Promise<!print_preview.Destination>} Promise that will be |
203 * fulfilled when the destination resolving is finished. | 209 * fulfilled when the destination resolving is finished. |
204 */ | 210 */ |
205 run: function(parent) { | 211 run: function(parent) { |
206 this.render(parent); | 212 this.render(parent); |
207 this.setIsVisible(true); | 213 this.setIsVisible(true); |
208 | 214 |
209 assert(this.promiseResolver_, 'Promise resolver not created.'); | 215 assert(this.promiseResolver_, 'Promise resolver not created.'); |
210 return this.promiseResolver_.promise; | 216 return this.promiseResolver_.promise; |
211 } | 217 } |
212 }; | 218 }; |
213 | 219 |
214 return { | 220 return { |
215 ProvisionalDestinationResolver: ProvisionalDestinationResolver | 221 ProvisionalDestinationResolver: ProvisionalDestinationResolver |
216 }; | 222 }; |
217 }); | 223 }); |
OLD | NEW |