OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 var localStrings = new LocalStrings(); | 5 var localStrings = new LocalStrings(); |
6 | 6 |
7 // Store the last selected printer index. | 7 // Store the last selected printer index. |
8 var lastSelectedPrinterIndex = 0; | 8 var lastSelectedPrinterIndex = 0; |
9 | 9 |
10 // Used to disable some printing options when the preview is not modifiable. | 10 // Used to disable some printing options when the preview is not modifiable. |
11 var previewModifiable = false; | 11 var previewModifiable = false; |
12 | 12 |
13 // Destination list special value constants. | 13 // Destination list special value constants. |
14 const PRINT_TO_PDF = 'Print To PDF'; | 14 const PRINT_TO_PDF = 'Print To PDF'; |
15 const MANAGE_PRINTERS = 'Manage Printers'; | 15 const MANAGE_PRINTERS = 'Manage Printers'; |
16 | 16 |
17 // State of the print preview settings. | 17 // State of the print preview settings. |
18 var printSettings = new PrintSettings(); | 18 var printSettings = new PrintSettings(); |
19 | 19 |
20 // The name of the default or last used printer. | 20 // The name of the default or last used printer. |
21 var defaultOrLastUsedPrinterName = ''; | 21 var defaultOrLastUsedPrinterName = ''; |
22 | 22 |
23 // True when a pending print preview request exists. | 23 // True when a pending print preview request exists. |
24 var hasPendingPreviewRequest = false; | 24 var hasPendingPreviewRequest = false; |
25 | 25 |
| 26 // The ID of the last preview request. |
| 27 var lastPreviewRequestID = -1; |
| 28 |
26 // True when a pending print file request exists. | 29 // True when a pending print file request exists. |
27 var hasPendingPrintFileRequest = false; | 30 var hasPendingPrintFileRequest = false; |
28 | 31 |
29 // True when preview tab is hidden. | 32 // True when preview tab is hidden. |
30 var isTabHidden = false; | 33 var isTabHidden = false; |
31 | 34 |
32 // Object holding all the pages related settings. | 35 // Object holding all the pages related settings. |
33 var pageSettings; | 36 var pageSettings; |
34 | 37 |
35 // Object holding all the copies related settings. | 38 // Object holding all the copies related settings. |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 * @return {number} duplex mode. | 253 * @return {number} duplex mode. |
251 */ | 254 */ |
252 function getDuplexMode() { | 255 function getDuplexMode() { |
253 // Constants values matches printing::DuplexMode enum. | 256 // Constants values matches printing::DuplexMode enum. |
254 const SIMPLEX = 0; | 257 const SIMPLEX = 0; |
255 const LONG_EDGE = 1; | 258 const LONG_EDGE = 1; |
256 return !copiesSettings.twoSidedCheckbox.checked ? SIMPLEX : LONG_EDGE; | 259 return !copiesSettings.twoSidedCheckbox.checked ? SIMPLEX : LONG_EDGE; |
257 } | 260 } |
258 | 261 |
259 /** | 262 /** |
260 * Creates a JSON string based on the values in the printer settings. | 263 * Creates an object based on the values in the printer settings. |
261 * | 264 * |
262 * @return {string} JSON string with print job settings. | 265 * @return {Object} Object containing print job settings. |
263 */ | 266 */ |
264 function getSettingsJSON() { | 267 function getSettings() { |
265 var deviceName = getSelectedPrinterName(); | 268 var deviceName = getSelectedPrinterName(); |
266 var printToPDF = (deviceName == PRINT_TO_PDF); | 269 var printToPDF = (deviceName == PRINT_TO_PDF); |
267 | 270 |
268 return JSON.stringify( | 271 var settings = |
269 {'deviceName': deviceName, | 272 {'deviceName': deviceName, |
270 'pageRange': pageSettings.selectedPageRanges, | 273 'pageRange': pageSettings.selectedPageRanges, |
271 'printAll': pageSettings.allPagesRadioButton.checked, | 274 'printAll': pageSettings.allPagesRadioButton.checked, |
272 'duplex': getDuplexMode(), | 275 'duplex': getDuplexMode(), |
273 'copies': copiesSettings.numberOfCopies, | 276 'copies': copiesSettings.numberOfCopies, |
274 'collate': isCollated(), | 277 'collate': isCollated(), |
275 'landscape': isLandscape(), | 278 'landscape': isLandscape(), |
276 'color': isColor(), | 279 'color': isColor(), |
277 'printToPDF': printToPDF}); | 280 'printToPDF': printToPDF, |
| 281 'requestID': 0}; |
| 282 return settings; |
278 } | 283 } |
279 | 284 |
280 /** | 285 /** |
| 286 * @return {number} The next unused preview request id. |
| 287 */ |
| 288 function generatePreviewRequestID() { |
| 289 return ++lastPreviewRequestID; |
| 290 } |
| 291 |
| 292 /** |
281 * Returns the name of the selected printer or the empty string if no | 293 * Returns the name of the selected printer or the empty string if no |
282 * printer is selected. | 294 * printer is selected. |
283 */ | 295 */ |
284 function getSelectedPrinterName() { | 296 function getSelectedPrinterName() { |
285 var printerList = $('printer-list') | 297 var printerList = $('printer-list') |
286 var selectedPrinter = printerList.selectedIndex; | 298 var selectedPrinter = printerList.selectedIndex; |
287 if (selectedPrinter < 0) | 299 if (selectedPrinter < 0) |
288 return ''; | 300 return ''; |
289 return printerList.options[selectedPrinter].value; | 301 return printerList.options[selectedPrinter].value; |
290 } | 302 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 * Sends a message to cancel the pending print request. | 350 * Sends a message to cancel the pending print request. |
339 */ | 351 */ |
340 function cancelPendingPrintRequest() { | 352 function cancelPendingPrintRequest() { |
341 chrome.send('cancelPendingPrintRequest'); | 353 chrome.send('cancelPendingPrintRequest'); |
342 } | 354 } |
343 | 355 |
344 /** | 356 /** |
345 * Sends a message to initiate print workflow. | 357 * Sends a message to initiate print workflow. |
346 */ | 358 */ |
347 function sendPrintFileRequest() { | 359 function sendPrintFileRequest() { |
348 chrome.send('print', [getSettingsJSON()]); | 360 chrome.send('print', [JSON.stringify(getSettings())]); |
349 } | 361 } |
350 | 362 |
351 /** | 363 /** |
352 * Asks the browser to generate a preview PDF based on current print settings. | 364 * Asks the browser to generate a preview PDF based on current print settings. |
353 */ | 365 */ |
354 function requestPrintPreview() { | 366 function requestPrintPreview() { |
355 hasPendingPreviewRequest = true; | 367 hasPendingPreviewRequest = true; |
356 removeEventListeners(); | 368 removeEventListeners(); |
357 printSettings.save(); | 369 printSettings.save(); |
358 if (!isTabHidden) | 370 if (!isTabHidden) |
359 showLoadingAnimation(); | 371 showLoadingAnimation(); |
360 | 372 |
361 chrome.send('getPreview', [getSettingsJSON()]); | 373 var settings = getSettings(); |
| 374 settings.requestID = generatePreviewRequestID(); |
| 375 chrome.send('getPreview', [JSON.stringify(settings)]); |
362 } | 376 } |
363 | 377 |
364 /** | 378 /** |
365 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print | 379 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
366 * preview tab regarding the file selection cancel event. | 380 * preview tab regarding the file selection cancel event. |
367 */ | 381 */ |
368 function fileSelectionCancelled() { | 382 function fileSelectionCancelled() { |
369 // TODO(thestig) re-enable controls here. | 383 // TODO(thestig) re-enable controls here. |
370 } | 384 } |
371 | 385 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 // TODO(thestig) Make use of |pageNumber| for pipelined preview generation. | 552 // TODO(thestig) Make use of |pageNumber| for pipelined preview generation. |
539 } | 553 } |
540 | 554 |
541 /** | 555 /** |
542 * Update the print preview when new preview data is available. | 556 * Update the print preview when new preview data is available. |
543 * Create the PDF plugin as needed. | 557 * Create the PDF plugin as needed. |
544 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 558 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
545 * @param {string} jobTitle The print job title. | 559 * @param {string} jobTitle The print job title. |
546 * @param {boolean} modifiable If the preview is modifiable. | 560 * @param {boolean} modifiable If the preview is modifiable. |
547 * @param {string} previewUid Preview unique identifier. | 561 * @param {string} previewUid Preview unique identifier. |
| 562 * @param {number} previewRequestId The preview request id that resulted in this |
| 563 * response. |
548 */ | 564 */ |
549 function updatePrintPreview(jobTitle, modifiable, previewUid) { | 565 function updatePrintPreview(jobTitle, |
| 566 modifiable, |
| 567 previewUid, |
| 568 previewRequestId) { |
| 569 if (lastPreviewRequestID != previewRequestId) |
| 570 return; |
550 hasPendingPreviewRequest = false; | 571 hasPendingPreviewRequest = false; |
551 | 572 |
552 if (checkIfSettingsChangedAndRegeneratePreview()) | 573 if (checkIfSettingsChangedAndRegeneratePreview()) |
553 return; | 574 return; |
554 | 575 |
555 previewModifiable = modifiable; | 576 previewModifiable = modifiable; |
556 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 577 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
557 | 578 |
558 createPDFPlugin(previewUid); | 579 createPDFPlugin(previewUid); |
559 updatePrintSummary(); | 580 updatePrintSummary(); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 this.isLandscape = ''; | 748 this.isLandscape = ''; |
728 } | 749 } |
729 | 750 |
730 /** | 751 /** |
731 * Takes a snapshot of the print settings. | 752 * Takes a snapshot of the print settings. |
732 */ | 753 */ |
733 PrintSettings.prototype.save = function() { | 754 PrintSettings.prototype.save = function() { |
734 this.deviceName = getSelectedPrinterName(); | 755 this.deviceName = getSelectedPrinterName(); |
735 this.isLandscape = isLandscape(); | 756 this.isLandscape = isLandscape(); |
736 } | 757 } |
OLD | NEW |