| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 8 /** |
| 9 * A data store that stores destinations and dispatches events when the data | 9 * A data store that stores destinations and dispatches events when the data |
| 10 * store changes. | 10 * store changes. |
| 11 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print | 11 * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print |
| 12 * destinations. | 12 * destinations. |
| 13 * @param {!print_preview.AppState} appState Application state. |
| 13 * @constructor | 14 * @constructor |
| 14 * @extends {cr.EventTarget} | 15 * @extends {cr.EventTarget} |
| 15 */ | 16 */ |
| 16 function DestinationStore(nativeLayer) { | 17 function DestinationStore(nativeLayer, appState) { |
| 17 cr.EventTarget.call(this); | 18 cr.EventTarget.call(this); |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * Used to fetch local print destinations. | 21 * Used to fetch local print destinations. |
| 21 * @type {!print_preview.NativeLayer} | 22 * @type {!print_preview.NativeLayer} |
| 22 * @private | 23 * @private |
| 23 */ | 24 */ |
| 24 this.nativeLayer_ = nativeLayer; | 25 this.nativeLayer_ = nativeLayer; |
| 25 | 26 |
| 26 /** | 27 /** |
| 28 * Used to load and persist the selected destination. |
| 29 * @type {!print_preview.AppState} |
| 30 * @private |
| 31 */ |
| 32 this.appState_ = appState; |
| 33 |
| 34 /** |
| 27 * Internal backing store for the data store. | 35 * Internal backing store for the data store. |
| 28 * @type {!Array.<!print_preview.Destination>} | 36 * @type {!Array.<!print_preview.Destination>} |
| 29 * @private | 37 * @private |
| 30 */ | 38 */ |
| 31 this.destinations_ = []; | 39 this.destinations_ = []; |
| 32 | 40 |
| 33 /** | 41 /** |
| 34 * Cache used for constant lookup of destinations. | 42 * Cache used for constant lookup of destinations by ID. |
| 35 * @type {object.<string, !print_preview.Destination>} | 43 * @type {object.<string, !print_preview.Destination>} |
| 36 * @private | 44 * @private |
| 37 */ | 45 */ |
| 38 this.destinationMap_ = {}; | 46 this.destinationMap_ = {}; |
| 39 | 47 |
| 40 /** | 48 /** |
| 41 * Currently selected destination. | 49 * Currently selected destination. |
| 42 * @type {print_preview.Destination} | 50 * @type {print_preview.Destination} |
| 43 * @private | 51 * @private |
| 44 */ | 52 */ |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 }, | 213 }, |
| 206 | 214 |
| 207 /** | 215 /** |
| 208 * @return {boolean} Whether a search for cloud destinations is in progress. | 216 * @return {boolean} Whether a search for cloud destinations is in progress. |
| 209 */ | 217 */ |
| 210 get isCloudDestinationSearchInProgress() { | 218 get isCloudDestinationSearchInProgress() { |
| 211 return this.outstandingCloudSearchRequestCount_ > 0; | 219 return this.outstandingCloudSearchRequestCount_ > 0; |
| 212 }, | 220 }, |
| 213 | 221 |
| 214 /** | 222 /** |
| 215 * Sets the initially selected destination. If any inserted destinations | 223 * Initializes the destination store. Sets the initially selected |
| 216 * match this ID, that destination will be automatically selected. This | 224 * destination. If any inserted destinations match this ID, that destination |
| 217 * occurs only once for every time this setter is called or if the store is | 225 * will be automatically selected. This method must be called after the |
| 218 * cleared. | 226 * print_preview.AppState has been initialized. |
| 219 * @param {?string} initialDestinationId ID of the destination that should | 227 * @param {?string} systemDefaultDestinationId ID of the system default |
| 220 * be selected automatically when added to the store or {@code null} if | 228 * destination. |
| 221 * the first destination that is inserted should be selected. | 229 * @private |
| 222 * @param {boolean} isLocalDestination Whether the initial destination is | |
| 223 * local. | |
| 224 */ | 230 */ |
| 225 setInitialDestinationId: function( | 231 init: function(systemDefaultDestinationId) { |
| 226 initialDestinationId, isLocalDestination) { | 232 if (this.appState_.selectedDestinationId) { |
| 227 this.initialDestinationId_ = initialDestinationId; | 233 this.initialDestinationId_ = this.appState_.selectedDestinationId; |
| 228 this.isInitialDestinationLocal_ = isLocalDestination; | 234 this.isInitialDestinationLocal_ = |
| 235 this.appState_.isSelectedDestinationLocal; |
| 236 } else { |
| 237 this.initialDestinationId_ = systemDefaultDestinationId; |
| 238 this.isInitialDestinationLocal_ = true; |
| 239 } |
| 240 |
| 229 this.isInAutoSelectMode_ = true; | 241 this.isInAutoSelectMode_ = true; |
| 230 if (this.initialDestinationId_ == null) { | 242 if (this.initialDestinationId_ == null) { |
| 231 assert(this.destinations_.length > 0, | 243 assert(this.destinations_.length > 0, |
| 232 'No destinations available to select'); | 244 'No destinations available to select'); |
| 233 this.selectDestination(this.destinations_[0]); | 245 this.selectDestination(this.destinations_[0]); |
| 234 } else { | 246 } else { |
| 235 var candidate = this.destinationMap_[this.initialDestinationId_]; | 247 var candidate = this.destinationMap_[this.initialDestinationId_]; |
| 236 if (candidate != null) { | 248 if (candidate != null) { |
| 237 this.selectDestination(candidate); | 249 this.selectDestination(candidate); |
| 238 } else if (!cr.isChromeOS && isLocalDestination) { | 250 } else if (!cr.isChromeOS && this.isInitialDestinationLocal_) { |
| 239 this.nativeLayer_.startGetLocalDestinationCapabilities( | 251 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 240 initialDestinationId); | 252 this.initialDestinationId_); |
| 241 } | 253 } |
| 242 } | 254 } |
| 243 }, | 255 }, |
| 244 | 256 |
| 245 /** | 257 /** |
| 246 * Sets the destination store's Google Cloud Print interface. | 258 * Sets the destination store's Google Cloud Print interface. |
| 247 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface | 259 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface |
| 248 * to set. | 260 * to set. |
| 249 */ | 261 */ |
| 250 setCloudPrintInterface: function(cloudPrintInterface) { | 262 setCloudPrintInterface: function(cloudPrintInterface) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } | 294 } |
| 283 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && | 295 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX && |
| 284 !destination.isTosAccepted) { | 296 !destination.isTosAccepted) { |
| 285 assert(this.cloudPrintInterface_ != null, | 297 assert(this.cloudPrintInterface_ != null, |
| 286 'Selected FedEx Office destination, but Google Cloud Print is ' + | 298 'Selected FedEx Office destination, but Google Cloud Print is ' + |
| 287 'not enabled'); | 299 'not enabled'); |
| 288 destination.isTosAccepted = true; | 300 destination.isTosAccepted = true; |
| 289 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination.id, | 301 this.cloudPrintInterface_.updatePrinterTosAcceptance(destination.id, |
| 290 true); | 302 true); |
| 291 } | 303 } |
| 304 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 292 cr.dispatchSimpleEvent( | 305 cr.dispatchSimpleEvent( |
| 293 this, DestinationStore.EventType.DESTINATION_SELECT); | 306 this, DestinationStore.EventType.DESTINATION_SELECT); |
| 294 if (destination.capabilities == null) { | 307 if (destination.capabilities == null) { |
| 295 if (destination.isLocal) { | 308 if (destination.isLocal) { |
| 296 this.nativeLayer_.startGetLocalDestinationCapabilities( | 309 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 297 destination.id); | 310 destination.id); |
| 298 } else { | 311 } else { |
| 299 assert(this.cloudPrintInterface_ != null, | 312 assert(this.cloudPrintInterface_ != null, |
| 300 'Selected destination is a cloud destination, but Google ' + | 313 'Selected destination is a cloud destination, but Google ' + |
| 301 'Cloud Print is not enabled'); | 314 'Cloud Print is not enabled'); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 * @param {cr.Event} event Contains the capabilities of the local print | 502 * @param {cr.Event} event Contains the capabilities of the local print |
| 490 * destination. | 503 * destination. |
| 491 * @private | 504 * @private |
| 492 */ | 505 */ |
| 493 onLocalDestinationCapabilitiesSet_: function(event) { | 506 onLocalDestinationCapabilitiesSet_: function(event) { |
| 494 var destinationId = event.settingsInfo['printerId']; | 507 var destinationId = event.settingsInfo['printerId']; |
| 495 var destination = this.destinationMap_[destinationId]; | 508 var destination = this.destinationMap_[destinationId]; |
| 496 var capabilities = print_preview.LocalCapabilitiesParser.parse( | 509 var capabilities = print_preview.LocalCapabilitiesParser.parse( |
| 497 event.settingsInfo); | 510 event.settingsInfo); |
| 498 if (destination) { | 511 if (destination) { |
| 512 // In case there were multiple capabilities request for this local |
| 513 // destination, just ignore the later ones. |
| 514 if (destination.capabilities != null) { |
| 515 return; |
| 516 } |
| 499 destination.capabilities = capabilities; | 517 destination.capabilities = capabilities; |
| 500 if (this.selectedDestination_ && | |
| 501 this.selectedDestination_.id == destinationId) { | |
| 502 cr.dispatchSimpleEvent(this, | |
| 503 DestinationStore.EventType. | |
| 504 SELECTED_DESTINATION_CAPABILITIES_READY); | |
| 505 } | |
| 506 } else { | 518 } else { |
| 507 // TODO(rltoscano): This makes the assumption that the "deviceName" is | 519 // TODO(rltoscano): This makes the assumption that the "deviceName" is |
| 508 // the same as "printerName". We should include the "printerName" in the | 520 // the same as "printerName". We should include the "printerName" in the |
| 509 // response. See http://crbug.com/132831. | 521 // response. See http://crbug.com/132831. |
| 510 destination = print_preview.LocalDestinationParser.parse( | 522 destination = print_preview.LocalDestinationParser.parse( |
| 511 {deviceName: destinationId, printerName: destinationId}); | 523 {deviceName: destinationId, printerName: destinationId}); |
| 512 destination.capabilities = capabilities; | 524 destination.capabilities = capabilities; |
| 513 this.insertDestination(destination); | 525 this.insertDestination(destination); |
| 514 } | 526 } |
| 527 if (this.selectedDestination_ && |
| 528 this.selectedDestination_.id == destinationId) { |
| 529 cr.dispatchSimpleEvent(this, |
| 530 DestinationStore.EventType. |
| 531 SELECTED_DESTINATION_CAPABILITIES_READY); |
| 532 } |
| 515 }, | 533 }, |
| 516 | 534 |
| 517 /** | 535 /** |
| 518 * Called when a request to get a local destination's print capabilities | 536 * Called when a request to get a local destination's print capabilities |
| 519 * fails. If the destination is the initial destination, auto-select another | 537 * fails. If the destination is the initial destination, auto-select another |
| 520 * destination instead. | 538 * destination instead. |
| 521 * @param {cr.Event} event Contains the destination ID that failed. | 539 * @param {cr.Event} event Contains the destination ID that failed. |
| 522 * @private | 540 * @private |
| 523 */ | 541 */ |
| 524 onGetCapabilitiesFail_: function(event) { | 542 onGetCapabilitiesFail_: function(event) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 'No destinations were loaded before auto-select timeout expired'); | 634 'No destinations were loaded before auto-select timeout expired'); |
| 617 this.selectDestination(this.destinations_[0]); | 635 this.selectDestination(this.destinations_[0]); |
| 618 } | 636 } |
| 619 }; | 637 }; |
| 620 | 638 |
| 621 // Export | 639 // Export |
| 622 return { | 640 return { |
| 623 DestinationStore: DestinationStore | 641 DestinationStore: DestinationStore |
| 624 }; | 642 }; |
| 625 }); | 643 }); |
| OLD | NEW |