| OLD | NEW |
| (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 {CustomEvent} event |
| 82 * @private |
| 83 */ |
| 84 onDestinationResolved_: function(event) { |
| 85 assert(this.promiseResolver_, 'Promise resolver not found'); |
| 86 var detail = event.detail; |
| 87 if (detail.success) { |
| 88 this.setState_(ResolverState.DONE); |
| 89 assert(detail.capabilities); |
| 90 // |detail| contains printerId and capabilities. |
| 91 this.promiseResolver_.resolve(detail); |
| 92 this.setIsVisible(false); |
| 93 } else { |
| 94 this.setState_(ResolverState.ERROR); |
| 95 this.promiseResolver_.reject(); |
| 96 } |
| 97 |
| 98 this.promiseResolver_ = null; |
| 99 }, |
| 100 |
| 101 /** |
| 102 * Sets new resolver state and updates the UI accordingly. |
| 103 * @param {!ResolverState} state |
| 104 * @private |
| 105 */ |
| 106 setState_: function(state) { |
| 107 if (this.state_ == state) |
| 108 return; |
| 109 |
| 110 this.state_ = state; |
| 111 this.updateUI_(); |
| 112 }, |
| 113 |
| 114 /** |
| 115 * Updates the resolver overlay UI to match the resolver state. |
| 116 * @private |
| 117 */ |
| 118 updateUI_: function() { |
| 119 // TODO(crbug.com/677567): Replace with meaningful UI. |
| 120 }, |
| 121 |
| 122 /** |
| 123 * Initiates and shows the resolver overlay. |
| 124 * @param {!HTMLElement} parent The element that should parent the resolver |
| 125 * UI. |
| 126 * @return {!Promise<!print_preview.Destination>} Promise that will be |
| 127 * fulfilled when the destination resolving is finished. |
| 128 */ |
| 129 run: function(parent) { |
| 130 this.render(parent); |
| 131 this.setIsVisible(true); |
| 132 this.startResolveDestination_(); |
| 133 |
| 134 assert(this.promiseResolver_, 'Promise resolver not created.'); |
| 135 return this.promiseResolver_.promise; |
| 136 } |
| 137 }; |
| 138 |
| 139 return { |
| 140 CrosDestinationResolver: CrosDestinationResolver |
| 141 }; |
| 142 }); |
| OLD | NEW |