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 13 matching lines...) Expand all Loading... |
24 this.nativeLayer_ = nativeLayer; | 24 this.nativeLayer_ = nativeLayer; |
25 | 25 |
26 /** | 26 /** |
27 * Internal backing store for the data store. | 27 * Internal backing store for the data store. |
28 * @type {!Array.<!print_preview.Destination>} | 28 * @type {!Array.<!print_preview.Destination>} |
29 * @private | 29 * @private |
30 */ | 30 */ |
31 this.destinations_ = []; | 31 this.destinations_ = []; |
32 | 32 |
33 /** | 33 /** |
34 * Cache used for constant lookup of printers. | 34 * Cache used for constant lookup of destinations. |
35 * @type {object.<string, !print_preview.Destination>} | 35 * @type {object.<string, !print_preview.Destination>} |
36 * @private | 36 * @private |
37 */ | 37 */ |
38 this.destinationMap_ = {}; | 38 this.destinationMap_ = {}; |
39 | 39 |
40 /** | 40 /** |
41 * Currently selected destination. | 41 * Currently selected destination. |
42 * @type {print_preview.Destination} | 42 * @type {print_preview.Destination} |
43 * @private | 43 * @private |
44 */ | 44 */ |
45 this.selectedDestination_ = null; | 45 this.selectedDestination_ = null; |
46 | 46 |
47 /** | 47 /** |
48 * Initial destination ID used to auto-select the first inserted destination | 48 * Initial destination ID used to auto-select the first inserted destination |
49 * that matches. If {@code null}, the first destination inserted into the | 49 * that matches. If {@code null}, the first destination inserted into the |
50 * store will be selected. | 50 * store will be selected. |
51 * @type {?string} | 51 * @type {?string} |
52 * @private | 52 * @private |
53 */ | 53 */ |
54 this.initialDestinationId_ = null; | 54 this.initialDestinationId_ = null; |
55 | 55 |
56 /** | 56 /** |
| 57 * Whether the initial destination is a local one or not. |
| 58 * @type {boolean} |
| 59 * @private |
| 60 */ |
| 61 this.isInitialDestinationLocal_ = true; |
| 62 |
| 63 /** |
57 * Whether the destination store will auto select the destination that | 64 * Whether the destination store will auto select the destination that |
58 * matches the initial destination. | 65 * matches the initial destination. |
59 * @type {boolean} | 66 * @type {boolean} |
60 * @private | 67 * @private |
61 */ | 68 */ |
62 this.isInAutoSelectMode_ = false; | 69 this.isInAutoSelectMode_ = false; |
63 | 70 |
64 /** | 71 /** |
65 * Event tracker used to track event listeners of the destination store. | 72 * Event tracker used to track event listeners of the destination store. |
66 * @type {!EventTracker} | 73 * @type {!EventTracker} |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 */ | 169 */ |
163 get selectedDestination() { | 170 get selectedDestination() { |
164 return this.selectedDestination_; | 171 return this.selectedDestination_; |
165 }, | 172 }, |
166 | 173 |
167 /** | 174 /** |
168 * Sets the initially selected destination. If any inserted destinations | 175 * Sets the initially selected destination. If any inserted destinations |
169 * match this ID, that destination will be automatically selected. This | 176 * match this ID, that destination will be automatically selected. This |
170 * occurs only once for every time this setter is called or if the store is | 177 * occurs only once for every time this setter is called or if the store is |
171 * cleared. | 178 * cleared. |
172 * @param {?string} ID of the destination that should be selected | 179 * @param {?string} initialDestinationId ID of the destination that should |
173 * automatically when added to the store or {@code null} if the first | 180 * be selected automatically when added to the store or {@code null} if |
174 * destination that is inserted should be selected. | 181 * the first destination that is inserted should be selected. |
| 182 * @param {boolean} isLocalDestination Whether the initial destination is |
| 183 * local. |
175 */ | 184 */ |
176 setInitialDestinationId: function(initialDestinationId) { | 185 setInitialDestinationId: function( |
| 186 initialDestinationId, isLocalDestination) { |
177 this.initialDestinationId_ = initialDestinationId; | 187 this.initialDestinationId_ = initialDestinationId; |
| 188 this.isInitialDestinationLocal_ = isLocalDestination; |
178 this.isInAutoSelectMode_ = true; | 189 this.isInAutoSelectMode_ = true; |
179 if (this.initialDestinationId_ == null) { | 190 if (this.initialDestinationId_ == null) { |
180 assert(this.destinations_.length > 0, | 191 assert(this.destinations_.length > 0, |
181 'No destinations available to select'); | 192 'No destinations available to select'); |
182 this.selectDestination(this.destinations_[0]); | 193 this.selectDestination(this.destinations_[0]); |
183 } else { | 194 } else { |
184 var candidate = this.destinationMap_[this.initialDestinationId_]; | 195 var candidate = this.destinationMap_[this.initialDestinationId_]; |
185 if (candidate != null) { | 196 if (candidate != null) { |
186 this.selectDestination(candidate); | 197 this.selectDestination(candidate); |
187 } else { | 198 } else if (!cr.isChromeOS && isLocalDestination) { |
188 // Try and fetch the destination | 199 this.nativeLayer_.startGetLocalDestinationCapabilities( |
189 // TODO(rltoscano): Since we don't know if the initialDestinationId is | 200 initialDestinationId); |
190 // a local printer or a cloud printer, we are going to assume based on | |
191 // platform. The C++ layer should be modified to return more | |
192 // information about the initially selected printer so this assumption | |
193 // does not have to be made. See http://crbug.com/132831. | |
194 if (!cr.isChromeOS) { | |
195 this.nativeLayer_.startGetLocalDestinationCapabilities( | |
196 initialDestinationId); | |
197 } | |
198 } | 201 } |
199 } | 202 } |
200 }, | 203 }, |
201 | 204 |
202 /** | 205 /** |
203 * Sets the destination store's Google Cloud Print interface. | 206 * Sets the destination store's Google Cloud Print interface. |
204 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface | 207 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface |
205 * to set. | 208 * to set. |
206 */ | 209 */ |
207 setCloudPrintInterface: function(cloudPrintInterface) { | 210 setCloudPrintInterface: function(cloudPrintInterface) { |
208 this.cloudPrintInterface_ = cloudPrintInterface; | 211 this.cloudPrintInterface_ = cloudPrintInterface; |
209 this.tracker_.add( | 212 this.tracker_.add( |
210 this.cloudPrintInterface_, | 213 this.cloudPrintInterface_, |
211 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, | 214 cloudprint.CloudPrintInterface.EventType.SEARCH_DONE, |
212 this.onCloudPrintSearchDone_.bind(this)); | 215 this.onCloudPrintSearchDone_.bind(this)); |
213 this.tracker_.add( | 216 this.tracker_.add( |
214 this.cloudPrintInterface_, | 217 this.cloudPrintInterface_, |
215 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, | 218 cloudprint.CloudPrintInterface.EventType.PRINTER_DONE, |
216 this.onCloudPrintPrinterDone_.bind(this)); | 219 this.onCloudPrintPrinterDone_.bind(this)); |
| 220 this.tracker_.add( |
| 221 this.cloudPrintInterface_, |
| 222 cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED, |
| 223 this.onPrinterFailed_.bind(this)); |
| 224 // Fetch initial destination if its a cloud destination. |
| 225 if (this.isInAutoSelectMode_ && !this.isInitialDestinationLocal_) { |
| 226 this.cloudPrintInterface_.printer(this.initialDestinationId_); |
| 227 } |
217 }, | 228 }, |
218 | 229 |
219 /** @param {!print_preview.Destination} Destination to select. */ | 230 /** @param {!print_preview.Destination} Destination to select. */ |
220 selectDestination: function(destination) { | 231 selectDestination: function(destination) { |
221 this.selectedDestination_ = destination; | 232 this.selectedDestination_ = destination; |
222 this.selectedDestination_.isRecent = true; | 233 this.selectedDestination_.isRecent = true; |
223 this.isInAutoSelectMode_ = false; | 234 this.isInAutoSelectMode_ = false; |
224 if (this.autoSelectTimeout_ != null) { | 235 if (this.autoSelectTimeout_ != null) { |
225 clearTimeout(this.autoSelectTimeout_); | 236 clearTimeout(this.autoSelectTimeout_); |
226 this.autoSelectTimeout_ = null; | 237 this.autoSelectTimeout_ = null; |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 this.tracker_.add( | 382 this.tracker_.add( |
372 this.nativeLayer_, | 383 this.nativeLayer_, |
373 print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET, | 384 print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET, |
374 this.onLocalDestinationsSet_.bind(this)); | 385 this.onLocalDestinationsSet_.bind(this)); |
375 this.tracker_.add( | 386 this.tracker_.add( |
376 this.nativeLayer_, | 387 this.nativeLayer_, |
377 print_preview.NativeLayer.EventType.CAPABILITIES_SET, | 388 print_preview.NativeLayer.EventType.CAPABILITIES_SET, |
378 this.onLocalDestinationCapabilitiesSet_.bind(this)); | 389 this.onLocalDestinationCapabilitiesSet_.bind(this)); |
379 this.tracker_.add( | 390 this.tracker_.add( |
380 this.nativeLayer_, | 391 this.nativeLayer_, |
| 392 print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL, |
| 393 this.onGetCapabilitiesFail_.bind(this)); |
| 394 this.tracker_.add( |
| 395 this.nativeLayer_, |
381 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, | 396 print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD, |
382 this.onDestinationsReload_.bind(this)); | 397 this.onDestinationsReload_.bind(this)); |
383 }, | 398 }, |
384 | 399 |
385 /** | 400 /** |
386 * Resets the state of the destination store to its initial state. | 401 * Resets the state of the destination store to its initial state. |
387 * @private | 402 * @private |
388 */ | 403 */ |
389 reset_: function() { | 404 reset_: function() { |
390 this.destinations_ = []; | 405 this.destinations_ = []; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 // the same as "printerName". We should include the "printerName" in the | 452 // the same as "printerName". We should include the "printerName" in the |
438 // response. See http://crbug.com/132831. | 453 // response. See http://crbug.com/132831. |
439 destination = print_preview.LocalDestinationParser.parse( | 454 destination = print_preview.LocalDestinationParser.parse( |
440 {deviceName: destinationId, printerName: destinationId}); | 455 {deviceName: destinationId, printerName: destinationId}); |
441 destination.capabilities = capabilities; | 456 destination.capabilities = capabilities; |
442 this.insertDestination(destination); | 457 this.insertDestination(destination); |
443 } | 458 } |
444 }, | 459 }, |
445 | 460 |
446 /** | 461 /** |
447 * Called when the /search call completes. Adds the fetched printers to the | 462 * Called when a request to get a local destination's print capabilities |
448 * destination store. | 463 * fails. If the destination is the initial destination, auto-select another |
449 * @param {cr.Event} event Contains the fetched printers. | 464 * destination instead. |
| 465 * @param {cr.Event} event Contains the destination ID that failed. |
| 466 * @private |
| 467 */ |
| 468 onGetCapabilitiesFail_: function(event) { |
| 469 console.error('Failed to get print capabilities for printer ' + |
| 470 event.destinationId); |
| 471 if (this.isInAutoSelectMode_ && |
| 472 this.initialDestinationId_ == event.destinationId) { |
| 473 assert(this.destinations_.length > 0, |
| 474 'No destinations were loaded when failed to get initial ' + |
| 475 'destination'); |
| 476 this.selectDestination(this.destinations_[0]); |
| 477 } |
| 478 }, |
| 479 |
| 480 /** |
| 481 * Called when the /search call completes. Adds the fetched destinations to |
| 482 * the destination store. |
| 483 * @param {cr.Event} event Contains the fetched destinations. |
450 * @private | 484 * @private |
451 */ | 485 */ |
452 onCloudPrintSearchDone_: function(event) { | 486 onCloudPrintSearchDone_: function(event) { |
453 this.insertDestinations(event.printers); | 487 this.insertDestinations(event.printers); |
454 }, | 488 }, |
455 | 489 |
456 /** | 490 /** |
457 * Called when /printer call completes. Updates the specified destination's | 491 * Called when /printer call completes. Updates the specified destination's |
458 * print capabilities. | 492 * print capabilities. |
459 * @param {cr.Event} event Contains detailed information about the | 493 * @param {cr.Event} event Contains detailed information about the |
(...skipping 25 matching lines...) Expand all Loading... |
485 /** | 519 /** |
486 * Called when no destination was auto-selected after some timeout. Selects | 520 * Called when no destination was auto-selected after some timeout. Selects |
487 * the first destination in store. | 521 * the first destination in store. |
488 * @private | 522 * @private |
489 */ | 523 */ |
490 onAutoSelectTimeoutExpired_: function() { | 524 onAutoSelectTimeoutExpired_: function() { |
491 this.autoSelectTimeout_ = null; | 525 this.autoSelectTimeout_ = null; |
492 assert(this.destinations_.length > 0, | 526 assert(this.destinations_.length > 0, |
493 'No destinations were loaded before auto-select timeout expired'); | 527 'No destinations were loaded before auto-select timeout expired'); |
494 this.selectDestination(this.destinations_[0]); | 528 this.selectDestination(this.destinations_[0]); |
| 529 }, |
| 530 |
| 531 /** |
| 532 * Called when the Google Cloud Print interface fails to lookup a |
| 533 * destination. Selects another destination if the failed destination was |
| 534 * the initial destination. |
| 535 * @param {object} event Contains the ID of the destination that was failed |
| 536 * to be looked up. |
| 537 * @private |
| 538 */ |
| 539 onPrinterFailed_: function(event) { |
| 540 if (event.errorCode == '111' && |
| 541 this.isInAutoSelectMode_ && |
| 542 this.initialDestinationId_ == event.destinationId) { |
| 543 console.error('Could not find initial printer: ' + event.destinationId); |
| 544 assert(this.destinations_.length > 0, |
| 545 'No destinations were loaded when failed to get initial ' + |
| 546 'destination'); |
| 547 this.selectDestination(this.destinations_[0]); |
| 548 } |
495 } | 549 } |
496 }; | 550 }; |
497 | 551 |
498 // Export | 552 // Export |
499 return { | 553 return { |
500 DestinationStore: DestinationStore | 554 DestinationStore: DestinationStore |
501 }; | 555 }; |
502 }); | 556 }); |
OLD | NEW |