Chromium Code Reviews| 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. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 this.appState_ = appState; | 32 this.appState_ = appState; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Internal backing store for the data store. | 35 * Internal backing store for the data store. |
| 36 * @type {!Array.<!print_preview.Destination>} | 36 * @type {!Array.<!print_preview.Destination>} |
| 37 * @private | 37 * @private |
| 38 */ | 38 */ |
| 39 this.destinations_ = []; | 39 this.destinations_ = []; |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Cache used for constant lookup of destinations by ID. | 42 * Cache used for constant lookup of destinations by origin and id. |
| 43 * @type {object.<string, !print_preview.Destination>} | 43 * @type {object.<string, !print_preview.Destination>} |
| 44 * @private | 44 * @private |
| 45 */ | 45 */ |
| 46 this.destinationMap_ = {}; | 46 this.destinationMap_ = {}; |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * Currently selected destination. | 49 * Currently selected destination. |
| 50 * @type {print_preview.Destination} | 50 * @type {print_preview.Destination} |
| 51 * @private | 51 * @private |
| 52 */ | 52 */ |
| 53 this.selectedDestination_ = null; | 53 this.selectedDestination_ = null; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Initial destination ID used to auto-select the first inserted destination | 56 * Initial destination ID used to auto-select the first inserted destination |
| 57 * that matches. If {@code null}, the first destination inserted into the | 57 * that matches. If {@code null}, the first destination inserted into the |
| 58 * store will be selected. | 58 * store will be selected. |
| 59 * @type {?string} | 59 * @type {?string} |
| 60 * @private | 60 * @private |
| 61 */ | 61 */ |
| 62 this.initialDestinationId_ = null; | 62 this.initialDestinationId_ = null; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Whether the initial destination is a local one or not. | 65 * Initial origin used to auto-select destination. |
| 66 * @type {boolean} | 66 * @type {print_preview.Destination.Origin} |
| 67 * @private | 67 * @private |
| 68 */ | 68 */ |
| 69 this.isInitialDestinationLocal_ = true; | 69 this.initialDestinationOrigin_ = print_preview.Destination.Origin.LOCAL; |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Whether the destination store will auto select the destination that | 72 * Whether the destination store will auto select the destination that |
| 73 * matches the initial destination. | 73 * matches the initial destination. |
| 74 * @type {boolean} | 74 * @type {boolean} |
| 75 * @private | 75 * @private |
| 76 */ | 76 */ |
| 77 this.isInAutoSelectMode_ = false; | 77 this.isInAutoSelectMode_ = false; |
| 78 | 78 |
| 79 /** | 79 /** |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 147 |
| 148 /** | 148 /** |
| 149 * Creates a local PDF print destination. | 149 * Creates a local PDF print destination. |
| 150 * @return {!print_preview.Destination} Created print destination. | 150 * @return {!print_preview.Destination} Created print destination. |
| 151 * @private | 151 * @private |
| 152 */ | 152 */ |
| 153 DestinationStore.createLocalPdfPrintDestination_ = function() { | 153 DestinationStore.createLocalPdfPrintDestination_ = function() { |
| 154 var dest = new print_preview.Destination( | 154 var dest = new print_preview.Destination( |
| 155 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, | 155 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
| 156 print_preview.Destination.Type.LOCAL, | 156 print_preview.Destination.Type.LOCAL, |
| 157 print_preview.Destination.AuthType.LOCAL, | 157 print_preview.Destination.Origin.LOCAL, |
| 158 localStrings.getString('printToPDF'), | 158 localStrings.getString('printToPDF'), |
| 159 false /*isRecent*/, | 159 false /*isRecent*/, |
| 160 print_preview.Destination.ConnectionStatus.ONLINE); | 160 print_preview.Destination.ConnectionStatus.ONLINE); |
| 161 dest.capabilities = new print_preview.ChromiumCapabilities( | 161 dest.capabilities = new print_preview.ChromiumCapabilities( |
| 162 false /*hasCopiesCapability*/, | 162 false /*hasCopiesCapability*/, |
| 163 '1' /*defaultCopiesStr*/, | 163 '1' /*defaultCopiesStr*/, |
| 164 false /*hasCollateCapability*/, | 164 false /*hasCollateCapability*/, |
| 165 false /*defaultIsCollateEnabled*/, | 165 false /*defaultIsCollateEnabled*/, |
| 166 false /*hasDuplexCapability*/, | 166 false /*hasDuplexCapability*/, |
| 167 false /*defaultIsDuplexEnabled*/, | 167 false /*defaultIsDuplexEnabled*/, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 /** | 209 /** |
| 210 * Initializes the destination store. Sets the initially selected | 210 * Initializes the destination store. Sets the initially selected |
| 211 * destination. If any inserted destinations match this ID, that destination | 211 * destination. If any inserted destinations match this ID, that destination |
| 212 * will be automatically selected. This method must be called after the | 212 * will be automatically selected. This method must be called after the |
| 213 * print_preview.AppState has been initialized. | 213 * print_preview.AppState has been initialized. |
| 214 * @param {?string} systemDefaultDestinationId ID of the system default | 214 * @param {?string} systemDefaultDestinationId ID of the system default |
| 215 * destination. | 215 * destination. |
| 216 * @private | 216 * @private |
| 217 */ | 217 */ |
| 218 init: function(systemDefaultDestinationId) { | 218 init: function(systemDefaultDestinationId) { |
| 219 if (this.appState_.selectedDestinationId) { | 219 if (this.appState_.selectedDestinationId && |
| 220 this.appState_.selectedDestinationOrigin) { | |
| 220 this.initialDestinationId_ = this.appState_.selectedDestinationId; | 221 this.initialDestinationId_ = this.appState_.selectedDestinationId; |
| 221 this.isInitialDestinationLocal_ = | 222 this.initialDestinationOrigin_ = |
| 222 this.appState_.isSelectedDestinationLocal; | 223 this.appState_.selectedDestinationOrigin_; |
| 223 } else { | 224 } else { |
| 224 this.initialDestinationId_ = systemDefaultDestinationId; | 225 this.initialDestinationId_ = systemDefaultDestinationId; |
| 225 this.isInitialDestinationLocal_ = true; | 226 this.initialDestinationOrigin_ = |
| 227 print_preview.Destination.Origin.LOCAL; | |
| 226 } | 228 } |
| 227 | |
| 228 this.isInAutoSelectMode_ = true; | 229 this.isInAutoSelectMode_ = true; |
| 229 if (this.initialDestinationId_ == null) { | 230 if (this.initialDestinationId_ == null || |
| 231 this.initialDestinationOrigin_ == null) { | |
| 230 assert(this.destinations_.length > 0, | 232 assert(this.destinations_.length > 0, |
| 231 'No destinations available to select'); | 233 'No destinations available to select'); |
| 232 this.selectDestination(this.destinations_[0]); | 234 this.selectDestination(this.destinations_[0]); |
| 233 } else { | 235 } else { |
| 234 var candidate = this.destinationMap_[this.initialDestinationId_]; | 236 var key = this.getDestinationKey_(this.initialDestinationOrigin_, |
| 237 this.initialDestinationId_); | |
| 238 var candidate = this.destinationMap_[key]; | |
| 235 if (candidate != null) { | 239 if (candidate != null) { |
| 236 this.selectDestination(candidate); | 240 this.selectDestination(candidate); |
| 237 } else if (!cr.isChromeOS && this.isInitialDestinationLocal_) { | 241 } else if (!cr.isChromeOS && |
| 242 this.initialDestinationOrigin_ == | |
| 243 print_preview.Destination.Origin.LOCAL) { | |
| 238 this.nativeLayer_.startGetLocalDestinationCapabilities( | 244 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 239 this.initialDestinationId_); | 245 this.initialDestinationId_); |
| 240 } | 246 } |
| 241 } | 247 } |
| 242 }, | 248 }, |
| 243 | 249 |
| 244 /** | 250 /** |
| 245 * Sets the destination store's Google Cloud Print interface. | 251 * Sets the destination store's Google Cloud Print interface. |
| 246 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface | 252 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface |
| 247 * to set. | 253 * to set. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 258 this.onCloudPrintSearchFailed_.bind(this)); | 264 this.onCloudPrintSearchFailed_.bind(this)); |
| 259 this.tracker_.add( | 265 this.tracker_.add( |
| 260 this.cloudPrintInterface_, | 266 this.cloudPrintInterface_, |
| 261 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, | 267 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, |
| 262 this.onCloudPrintPrinterDone_.bind(this)); | 268 this.onCloudPrintPrinterDone_.bind(this)); |
| 263 this.tracker_.add( | 269 this.tracker_.add( |
| 264 this.cloudPrintInterface_, | 270 this.cloudPrintInterface_, |
| 265 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, | 271 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, |
| 266 this.onCloudPrintPrinterFailed_.bind(this)); | 272 this.onCloudPrintPrinterFailed_.bind(this)); |
| 267 // Fetch initial destination if its a cloud destination. | 273 // Fetch initial destination if its a cloud destination. |
| 268 if (this.isInAutoSelectMode_ && !this.isInitialDestinationLocal_) { | 274 var origin = this.initialDestinationOrigin_; |
| 269 this.cloudPrintInterface_.printer(this.initialDestinationId_); | 275 if (this.isInAutoSelectMode_ && |
| 276 origin != print_preview.Destination.Origin.LOCAL) { | |
| 277 this.cloudPrintInterface_.printer(this.initialDestinationId_, origin); | |
| 270 } | 278 } |
| 271 }, | 279 }, |
| 272 | 280 |
| 273 /** | 281 /** |
| 274 * @return {boolean} Whether only default cloud destinations have been | 282 * @return {boolean} Whether only default cloud destinations have been |
| 275 * loaded. | 283 * loaded. |
| 276 */ | 284 */ |
| 277 hasOnlyDefaultCloudDestinations: function() { | 285 hasOnlyDefaultCloudDestinations: function() { |
| 278 return this.destinations_.every(function(dest) { | 286 return this.destinations_.every(function(dest) { |
| 279 return dest.isLocal || | 287 return dest.isLocal || |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 304 cr.dispatchSimpleEvent( | 312 cr.dispatchSimpleEvent( |
| 305 this, DestinationStore.EventType.DESTINATION_SELECT); | 313 this, DestinationStore.EventType.DESTINATION_SELECT); |
| 306 if (destination.capabilities == null) { | 314 if (destination.capabilities == null) { |
| 307 if (destination.isLocal) { | 315 if (destination.isLocal) { |
| 308 this.nativeLayer_.startGetLocalDestinationCapabilities( | 316 this.nativeLayer_.startGetLocalDestinationCapabilities( |
| 309 destination.id); | 317 destination.id); |
| 310 } else { | 318 } else { |
| 311 assert(this.cloudPrintInterface_ != null, | 319 assert(this.cloudPrintInterface_ != null, |
| 312 'Selected destination is a cloud destination, but Google ' + | 320 'Selected destination is a cloud destination, but Google ' + |
| 313 'Cloud Print is not enabled'); | 321 'Cloud Print is not enabled'); |
| 314 this.cloudPrintInterface_.printer(destination.id); | 322 this.cloudPrintInterface_.printer(destination.id, |
| 323 destination.origin); | |
| 315 } | 324 } |
| 316 } else { | 325 } else { |
| 317 cr.dispatchSimpleEvent( | 326 cr.dispatchSimpleEvent( |
| 318 this, | 327 this, |
| 319 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 328 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 320 } | 329 } |
| 321 }, | 330 }, |
| 322 | 331 |
| 323 /** | 332 /** |
| 324 * Inserts a print destination to the data store and dispatches a | 333 * Inserts a print destination to the data store and dispatches a |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 }, | 379 }, |
| 371 | 380 |
| 372 /** | 381 /** |
| 373 * Updates an existing print destination with capabilities information. If | 382 * Updates an existing print destination with capabilities information. If |
| 374 * the destination doesn't already exist, it will be added. | 383 * the destination doesn't already exist, it will be added. |
| 375 * @param {!print_preview.Destination} destination Destination to update. | 384 * @param {!print_preview.Destination} destination Destination to update. |
| 376 * @return {!print_preview.Destination} The existing destination that was | 385 * @return {!print_preview.Destination} The existing destination that was |
| 377 * updated. | 386 * updated. |
| 378 */ | 387 */ |
| 379 updateDestination: function(destination) { | 388 updateDestination: function(destination) { |
| 380 var existingDestination = this.destinationMap_[destination.id]; | 389 var key = this.getDestinationKey_(destination.origin, destination.id); |
| 390 var existingDestination = this.destinationMap_[key]; | |
| 381 if (existingDestination != null) { | 391 if (existingDestination != null) { |
| 382 existingDestination.capabilities = destination.capabilities; | 392 existingDestination.capabilities = destination.capabilities; |
| 383 return existingDestination; | 393 return existingDestination; |
| 384 } else { | 394 } else { |
| 385 this.insertDestination(destination); | 395 this.insertDestination(destination); |
|
Toscano
2013/04/18 19:26:41
Can you put a "return null;" statement after this
Vitaly Buka (NO REVIEWS)
2013/04/18 20:48:23
Probably "return destination;"?
On 2013/04/18 19:2
Vitaly Buka (NO REVIEWS)
2013/04/18 23:44:59
Done.
| |
| 386 } | 396 } |
| 387 }, | 397 }, |
| 388 | 398 |
| 389 /** Initiates loading of local print destinations. */ | 399 /** Initiates loading of local print destinations. */ |
| 390 startLoadLocalDestinations: function() { | 400 startLoadLocalDestinations: function() { |
| 391 this.nativeLayer_.startGetLocalDestinations(); | 401 this.nativeLayer_.startGetLocalDestinations(); |
| 392 this.isLocalDestinationSearchInProgress_ = true; | 402 this.isLocalDestinationSearchInProgress_ = true; |
| 393 cr.dispatchSimpleEvent( | 403 cr.dispatchSimpleEvent( |
| 394 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 404 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); |
| 395 }, | 405 }, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 409 } | 419 } |
| 410 }, | 420 }, |
| 411 | 421 |
| 412 /** | 422 /** |
| 413 * Inserts a destination into the store without dispatching any events. | 423 * Inserts a destination into the store without dispatching any events. |
| 414 * @return {boolean} Whether the inserted destination was not already in the | 424 * @return {boolean} Whether the inserted destination was not already in the |
| 415 * store. | 425 * store. |
| 416 * @private | 426 * @private |
| 417 */ | 427 */ |
| 418 insertDestination_: function(destination) { | 428 insertDestination_: function(destination) { |
| 419 var existingDestination = this.destinationMap_[destination.id]; | 429 var key = this.getDestinationKey_(destination.origin, destination.id); |
| 430 var existingDestination = this.destinationMap_[key]; | |
| 420 if (existingDestination == null) { | 431 if (existingDestination == null) { |
| 421 this.destinations_.push(destination); | 432 this.destinations_.push(destination); |
| 422 this.destinationMap_[destination.id] = destination; | 433 this.destinationMap_[key] = destination; |
| 423 return true; | 434 return true; |
| 424 } else if (existingDestination.connectionStatus == | 435 } else if (existingDestination.connectionStatus == |
| 425 print_preview.Destination.ConnectionStatus.UNKNOWN && | 436 print_preview.Destination.ConnectionStatus.UNKNOWN && |
| 426 destination.connectionStatus != | 437 destination.connectionStatus != |
| 427 print_preview.Destination.ConnectionStatus.UNKNOWN) { | 438 print_preview.Destination.ConnectionStatus.UNKNOWN) { |
| 428 existingDestination.connectionStatus = destination.connectionStatus; | 439 existingDestination.connectionStatus = destination.connectionStatus; |
| 429 return true; | 440 return true; |
| 430 } else { | 441 } else { |
| 431 return false; | 442 return false; |
| 432 } | 443 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 * Called when the native layer retrieves the capabilities for the selected | 501 * Called when the native layer retrieves the capabilities for the selected |
| 491 * local destination. Updates the destination with new capabilities if the | 502 * local destination. Updates the destination with new capabilities if the |
| 492 * destination already exists, otherwise it creates a new destination and | 503 * destination already exists, otherwise it creates a new destination and |
| 493 * then updates its capabilities. | 504 * then updates its capabilities. |
| 494 * @param {cr.Event} event Contains the capabilities of the local print | 505 * @param {cr.Event} event Contains the capabilities of the local print |
| 495 * destination. | 506 * destination. |
| 496 * @private | 507 * @private |
| 497 */ | 508 */ |
| 498 onLocalDestinationCapabilitiesSet_: function(event) { | 509 onLocalDestinationCapabilitiesSet_: function(event) { |
| 499 var destinationId = event.settingsInfo['printerId']; | 510 var destinationId = event.settingsInfo['printerId']; |
| 500 var destination = this.destinationMap_[destinationId]; | 511 var key = |
| 512 this.getDestinationKey_(print_preview.Destination.Origin.LOCAL, | |
| 513 destinationId); | |
| 514 var destination = this.destinationMap_[key]; | |
| 501 var capabilities = print_preview.LocalCapabilitiesParser.parse( | 515 var capabilities = print_preview.LocalCapabilitiesParser.parse( |
| 502 event.settingsInfo); | 516 event.settingsInfo); |
| 503 if (destination) { | 517 if (destination) { |
| 504 // In case there were multiple capabilities request for this local | 518 // In case there were multiple capabilities request for this local |
| 505 // destination, just ignore the later ones. | 519 // destination, just ignore the later ones. |
| 506 if (destination.capabilities != null) { | 520 if (destination.capabilities != null) { |
| 507 return; | 521 return; |
| 508 } | 522 } |
| 509 destination.capabilities = capabilities; | 523 destination.capabilities = capabilities; |
| 510 } else { | 524 } else { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 /** | 630 /** |
| 617 * Called when no destination was auto-selected after some timeout. Selects | 631 * Called when no destination was auto-selected after some timeout. Selects |
| 618 * the first destination in store. | 632 * the first destination in store. |
| 619 * @private | 633 * @private |
| 620 */ | 634 */ |
| 621 onAutoSelectTimeoutExpired_: function() { | 635 onAutoSelectTimeoutExpired_: function() { |
| 622 this.autoSelectTimeout_ = null; | 636 this.autoSelectTimeout_ = null; |
| 623 assert(this.destinations_.length > 0, | 637 assert(this.destinations_.length > 0, |
| 624 'No destinations were loaded before auto-select timeout expired'); | 638 'No destinations were loaded before auto-select timeout expired'); |
| 625 this.selectDestination(this.destinations_[0]); | 639 this.selectDestination(this.destinations_[0]); |
| 640 }, | |
| 641 | |
| 642 /** | |
| 643 * Returns key to be used with {@code destinationMap_}. | |
| 644 * @param {!print_preview.Destination.Origin} origin Destination origin. | |
| 645 * @return {!string} id Destination id. | |
| 646 * @private | |
| 647 */ | |
| 648 getDestinationKey_: function(origin, id) { | |
| 649 return origin + '/' + id; | |
|
Toscano
2013/04/18 19:26:41
Wouldn't it be better to move this into destinatio
Vitaly Buka (NO REVIEWS)
2013/04/18 20:48:23
We need to be able create key without instance of
Toscano
2013/04/18 22:45:40
SGTM
| |
| 626 } | 650 } |
| 627 }; | 651 }; |
| 628 | 652 |
| 629 // Export | 653 // Export |
| 630 return { | 654 return { |
| 631 DestinationStore: DestinationStore | 655 DestinationStore: DestinationStore |
| 632 }; | 656 }; |
| 633 }); | 657 }); |
| OLD | NEW |