Index: chrome/browser/resources/print_preview.js |
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js |
index 4a273fd83df0cc4802a50235c5592289be8d946d..f56e66168c31d64d681102a768b647c76cdbb5fb 100644 |
--- a/chrome/browser/resources/print_preview.js |
+++ b/chrome/browser/resources/print_preview.js |
@@ -2,8 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+// require: cr/ui/print_preview_cloud.js |
+ |
var localStrings = new LocalStrings(); |
+var useCloudPrint = false; |
+var maxCloudPrinters = 10; |
+ |
// The total page count of the previewed document regardless of which pages the |
// user has selected. |
var totalPageCount = -1; |
@@ -35,8 +40,12 @@ var printerCapabilities; |
var previewModifiable = false; |
// Destination list special value constants. |
+const ADD_PRINTER = 'addPrinter'; |
+const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; |
+const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; |
+const MORE_PRINTERS = 'morePrinters'; |
+const SIGN_IN = 'signIn'; |
const PRINT_TO_PDF = 'Print To PDF'; |
-const MANAGE_PRINTERS = 'Manage Printers'; |
/** |
* Window onload handler, sets up the page and starts print preview by getting |
@@ -117,13 +126,19 @@ function updateControlsWithSelectedPrinterCapabilities() { |
return; |
var selectedValue = printerList.options[selectedIndex].value; |
- if (selectedValue == PRINT_TO_PDF) { |
- updateWithPrinterCapabilities({'disableColorOption': true, |
- 'setColorAsDefault': true}); |
- } else if (selectedValue == MANAGE_PRINTERS) { |
+ if (printerList.options[selectedIndex].isCloudPrint) { |
+ // TODO(abodenha@chromium.org) Retrieve the correct printer caps. |
+ updateWithPrinterCapabilities({}); |
+ return; |
+ } else if (selectedValue == SIGN_IN || |
+ selectedValue == MANAGE_CLOUD_PRINTERS || |
+ selectedValue == MANAGE_LOCAL_PRINTERS) { |
printerList.selectedIndex = lastSelectedPrinterIndex; |
- chrome.send('managePrinters'); |
+ chrome.send(selectedValue); |
return; |
+ } else if (selectedValue == PRINT_TO_PDF) { |
+ updateWithPrinterCapabilities({'disableColorOption': true, |
+ 'setColorAsDefault': true}); |
} else { |
// This message will call back to 'updateWithPrinterCapabilities' |
// function. |
@@ -164,6 +179,31 @@ function updateWithPrinterCapabilities(settingInfo) { |
} |
/** |
+ * Turn on the integration of Cloud Print. |
+ * @param {enable} true if cloud print should be used. |
+ */ |
+function setUseCloudPrint(enable, cloudPrintURL) { |
+ useCloudPrint = enable; |
+ cloudprint.setBaseURL(cloudPrintURL); |
+} |
+ |
+/** |
+ * Take the PDF data handed to us and submit it to the cloud, closing the print |
+ * preview tab once the upload is successful. |
+ * @param {pdfData} data to send as the print job. |
+ */ |
+function printToCloud(data) { |
+ cloudprint.printToCloud(data, finishedCloudPrinting); |
+} |
+ |
+/** |
+ * Cloud print upload of the PDF file is finished, time to close the dialog. |
+ */ |
+function finishedCloudPrinting() { |
+ chrome.send('closePrintPreviewTab'); |
+} |
+ |
+/** |
* Disables or enables all controls in the options pane except for the cancel |
* button. |
*/ |
@@ -290,19 +330,27 @@ function getDuplexMode() { |
* @return {string} JSON string with print job settings. |
*/ |
function getSettingsJSON() { |
+ var printerList = $('printer-list'); |
+ var selectedPrinter = printerList.selectedIndex; |
var printAll = $('all-pages').checked; |
var deviceName = getSelectedPrinterName(); |
var printToPDF = (deviceName == PRINT_TO_PDF); |
- return JSON.stringify({'deviceName': deviceName, |
- 'pageRange': getSelectedPageRanges(), |
- 'printAll': printAll, |
- 'duplex': getDuplexMode(), |
- 'copies': getCopies(), |
- 'collate': isCollated(), |
- 'landscape': isLandscape(), |
- 'color': isColor(), |
- 'printToPDF': printToPDF}); |
+ var settings = {'deviceName': deviceName, |
+ 'pageRange': getSelectedPageRanges(), |
+ 'printAll': printAll, |
+ 'duplex': getDuplexMode(), |
+ 'copies': getCopies(), |
+ 'collate': isCollated(), |
+ 'landscape': isLandscape(), |
+ 'color': isColor(), |
+ 'printToPDF': printToPDF}; |
+ if (printerList.options[selectedPrinter].isCloudPrint) { |
+ settings['cloudPrintID'] = |
+ printerList.options[selectedPrinter].value; |
+ } |
+ |
+ return JSON.stringify(settings); |
} |
/** |
@@ -351,6 +399,17 @@ function requestPrintPreview() { |
*/ |
function setPrinters(printers, defaultPrinterIndex) { |
var printerList = $('printer-list'); |
+ if (useCloudPrint) { |
+ displayErrorMessage(localStrings.getString('loadingPrinters')); |
+ cloudprint.fetchPrinters(addCloudPrinters); |
+ if (printers.length > 0) { |
+ option = addDestinationListOption('', '', false, true); |
+ option = addDestinationListOption(localStrings.getString('localPrinters'), |
+ '', |
+ false, |
+ true); |
+ } |
+ } |
for (var i = 0; i < printers.length; ++i) { |
addDestinationListOption(printers[i].printerName, printers[i].deviceName, |
i == defaultPrinterIndex, false); |
@@ -364,9 +423,15 @@ function setPrinters(printers, defaultPrinterIndex) { |
PRINT_TO_PDF, false, false); |
addDestinationListOption('', '', false, true); |
- // Add an option to manage printers. |
- addDestinationListOption(localStrings.getString('managePrinters'), |
- MANAGE_PRINTERS, false, false); |
+ // Add options to manage printers. |
+ if (!cr.isChromeOS) { |
+ addDestinationListOption(localStrings.getString('manageLocalPrinters'), |
+ MANAGE_LOCAL_PRINTERS, false, false); |
+ } |
+ if (useCloudPrint) { |
+ addDestinationListOption(localStrings.getString('manageCloudPrinters'), |
+ MANAGE_CLOUD_PRINTERS, false, false); |
+ } |
printerList.disabled = false; |
updateControlsWithSelectedPrinterCapabilities(); |
@@ -387,6 +452,44 @@ function addDestinationListOption(optionText, optionValue, isDefault, |
$('printer-list').add(option); |
option.selected = isDefault; |
option.disabled = isDisabled; |
+ return option; |
+} |
+ |
+/** |
+ * Add cloud printers to the list drop down. |
+ * Called from the cloudprint object on receipt of printer information from the |
+ * cloud print server. |
+ */ |
+function addCloudPrinters(printers) { |
+ addDestinationListOption(localStrings.getString('cloudPrinters'), |
+ '', |
+ false, |
+ true); |
+ if (printers != null) { |
+ var l = printers.length; |
+ if (l > maxCloudPrinters) { |
+ l = maxCloudPrinters; |
+ } |
+ for (var i = 0; i < l; i++) { |
+ var option = addDestinationListOption(printers[i]['name'], |
+ printers[i]['id'], |
+ i == 0, |
+ false); |
+ option.isCloudPrint = true; |
+ } |
+ if (printers.length == 0) { |
+ addDestinationListOption(localStrings.getString('addCloudPrinter'), |
+ ADD_PRINTER, false, false); |
+ } |
+ if (l < printers.length) { |
+ var option = addDestinationListOption('', '', false, true); |
+ addDestinationListOption(localStrings.getString('morePrinters'), |
+ MORE_PRINTERS, false, false); |
+ } |
+ } else { |
+ addDestinationListOption(localStrings.getString('signIn'), |
+ SIGN_IN, false, false); |
+ } |
} |
/** |
@@ -926,4 +1029,3 @@ function onCopiesButtonsClicked(sign) { |
} |
copiesFieldChanged(); |
} |
- |