diff options
author | abodenha@google.com <abodenha@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-28 20:50:04 +0000 |
---|---|---|
committer | abodenha@google.com <abodenha@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-28 20:50:04 +0000 |
commit | f0be15bd7394ea2e056303b70fbb3360834e980a (patch) | |
tree | fc134a83a18912322273be7cf4142f8aadac8446 /chrome/browser/resources | |
parent | fb6a4a95b8156dfca24a27f0e8fb8e7211547462 (diff) | |
download | chromium_src-f0be15bd7394ea2e056303b70fbb3360834e980a.zip chromium_src-f0be15bd7394ea2e056303b70fbb3360834e980a.tar.gz chromium_src-f0be15bd7394ea2e056303b70fbb3360834e980a.tar.bz2 |
Refactored code to move cloudprint specific code into print_preview_cloud.js.
Removed "Local" and "Cloud" labels from dropdown
The "More printers" label is now more clear and shares behavior with the cloud print printer option in Chrome.
The system print dialog link is replaced with a similar link referencing cloud print in chrome os.
Choosing cloud print from the printers dropdown now requires the user to hit print before opening the cloud print dialog.
Fixed broken tests.
BUG=88098, 97175, http://code.google.com/p/chromium-os/issues/detail?id=16082, http://code.google.com/p/chromium-os/issues/detail?id=20121, http://code.google.com/p/chromium-os/issues/detail?id=20119
TEST=Enable print preview in about:flags. Printing should work with cloud print printers in much the same way as local printers in Chrome
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=103010
Review URL: http://codereview.chromium.org/7976017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103173 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/resources')
3 files changed, 120 insertions, 167 deletions
diff --git a/chrome/browser/resources/print_preview/print_preview.html b/chrome/browser/resources/print_preview/print_preview.html index ed69dba..ea168fc 100644 --- a/chrome/browser/resources/print_preview/print_preview.html +++ b/chrome/browser/resources/print_preview/print_preview.html @@ -46,8 +46,14 @@ <include src="header_footer_settings.html"></include> <hr> <div id="system-dialog-div"> - <button id="system-dialog-link" class="link-button" - i18n-content="systemDialogOption"></button> + <if expr="pp_ifdef('chromeos')"> + <button id="system-dialog-link" class="link-button" + i18n-content="cloudPrintDialogOption"></button> + </if> + <if expr="not pp_ifdef('chromeos')"> + <button id="system-dialog-link" class="link-button" + i18n-content="systemDialogOption"></button> + </if> <!-- TODO(dpapad): Investigate whey hidden attribute does not work. --> <div id="system-dialog-throbber" class="throbber hidden"></div> </div> diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js index 9232850..3d4be80 100644 --- a/chrome/browser/resources/print_preview/print_preview.js +++ b/chrome/browser/resources/print_preview/print_preview.js @@ -85,10 +85,6 @@ var currentPreviewUid = ''; // True if we need to generate draft preview data. var generateDraftData = true; -// TODO(abodenha@chromium.org) A lot of cloud print specific logic has -// made its way into this file. Refactor to create a cleaner boundary -// between print preview and GCP code. Reference bug 88098 when fixing. - // A dictionary of cloud printers that have been added to the printer // dropdown. var addedCloudPrinters = {}; @@ -223,12 +219,8 @@ function updateControlsWithSelectedPrinterCapabilities() { var skip_refresh = false; var selectedValue = printerList.options[selectedIndex].value; if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) { - updateWithCloudPrinterCapabilities(); - skip_refresh = true; - } else if (selectedValue == PRINT_WITH_CLOUD_PRINT) { - // If a preview is pending this will just disable controls. - // Once the preview completes we'll try again. - printWithCloudPrintDialog(); + cloudprint.updatePrinterCaps(printerList.options[selectedIndex], + doUpdateCloudPrinterCapabilities); skip_refresh = true; } else if (selectedValue == SIGN_IN || selectedValue == MANAGE_CLOUD_PRINTERS || @@ -236,7 +228,8 @@ function updateControlsWithSelectedPrinterCapabilities() { printerList.selectedIndex = lastSelectedPrinterIndex; chrome.send(selectedValue); skip_refresh = true; - } else if (selectedValue == PRINT_TO_PDF) { + } else if (selectedValue == PRINT_TO_PDF || + selectedValue == PRINT_WITH_CLOUD_PRINT) { updateWithPrinterCapabilities({ 'disableColorOption': true, 'setColorAsDefault': true, @@ -258,17 +251,6 @@ function updateControlsWithSelectedPrinterCapabilities() { } /** - * Updates the printer capabilities for the currently selected - * cloud print printer. - */ -function updateWithCloudPrinterCapabilities() { - var printerList = $('printer-list'); - var selectedIndex = printerList.selectedIndex; - cloudprint.updatePrinterCaps(printerList.options[selectedIndex], - doUpdateCloudPrinterCapabilities); -} - -/** * Helper function to do the actual work of updating cloud printer * capabilities. * @param {Object} printer The printer object to set capabilities for. @@ -329,9 +311,11 @@ function finishedCloudPrinting() { * @return {boolean} true if settings are valid, false if not. */ function areSettingsValid() { + var selectedPrinter = getSelectedPrinterName(); return pageSettings.isPageSelectionValid() && (copiesSettings.isValid() || - getSelectedPrinterName() == PRINT_TO_PDF); + selectedPrinter == PRINT_TO_PDF || + selectedPrinter == PRINT_WITH_CLOUD_PRINT); } /** @@ -341,7 +325,8 @@ function areSettingsValid() { */ function getSettings() { var deviceName = getSelectedPrinterName(); - var printToPDF = (deviceName == PRINT_TO_PDF); + var printToPDF = deviceName == PRINT_TO_PDF; + var printWithCloudPrint = deviceName == PRINT_WITH_CLOUD_PRINT; var settings = {'deviceName': deviceName, @@ -352,6 +337,7 @@ function getSettings() { 'landscape': layoutSettings.isLandscape(), 'color': colorSettings.colorMode, 'printToPDF': printToPDF, + 'printWithCloudPrint': printWithCloudPrint, 'isFirstRequest' : false, 'headerFooterEnabled': headerFooterSettings.hasHeaderFooter(), 'defaultMarginsSelected': marginSettings.isDefaultMarginsSelected(), @@ -435,11 +421,15 @@ function getSelectedPrinterName() { */ function requestToPrintDocument() { hasPendingPrintDocumentRequest = !isPrintReadyMetafileReady; - var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; - + var selectedPrinterName = getSelectedPrinterName(); + var printToPDF = selectedPrinterName == PRINT_TO_PDF; + var printWithCloudPrint = selectedPrinterName == PRINT_WITH_CLOUD_PRINT; if (hasPendingPrintDocumentRequest) { if (printToPDF) { sendPrintDocumentRequest(); + } else if (printWithCloudPrint) { + showCustomMessage(localStrings.getString('printWithCloudPrintWait')); + disableInputElementsInSidebar(); } else { isTabHidden = true; chrome.send('hidePreview'); @@ -552,7 +542,7 @@ function setDefaultPrinter(printer_name, cloudPrintData) { if (cloudPrintData) { cloudprint.setDefaultPrinter(printer_name, cloudPrintData, - addCloudPrinters, + addDestinationListOptionAtPosition, doUpdateCloudPrinterCapabilities); } else { $('printer-list')[0].value = defaultOrLastUsedPrinterName; @@ -598,14 +588,14 @@ function setPrinters(printers) { } // Add options to manage printers. if (!cr.isChromeOS) { - addDestinationListOption(localStrings.getString('manageLocalPrinters'), + addDestinationListOption(localStrings.getString('managePrinters'), MANAGE_LOCAL_PRINTERS, false, false, false); } else if (useCloudPrint) { // Fetch recent printers. - cloudprint.fetchPrinters(addCloudPrinters, false); + cloudprint.fetchPrinters(addDestinationListOptionAtPosition, false); // Fetch the full printer list. - cloudprint.fetchPrinters(addCloudPrinters, true); - addDestinationListOption(localStrings.getString('manageCloudPrinters'), + cloudprint.fetchPrinters(addDestinationListOptionAtPosition, true); + addDestinationListOption(localStrings.getString('managePrinters'), MANAGE_CLOUD_PRINTERS, false, false, false); } @@ -683,108 +673,6 @@ function addDestinationListOptionAtPosition(position, printerList.add(option, before); return option; } - -/** - * Test if a particular cloud printer has already been added to the - * printer dropdown. - * @param {string} id A unique value to track this printer. - * @return {boolean} True if this id has previously been passed to - * trackCloudPrinterAdded. - */ -function cloudPrinterAlreadyAdded(id) { - return (addedCloudPrinters[id]); -} - -/** - * Record that a cloud printer will added to the printer dropdown. - * @param {string} id A unique value to track this printer. - * @return {boolean} False if adding this printer would exceed - * |maxCloudPrinters|. - */ -function trackCloudPrinterAdded(id) { - if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) { - addedCloudPrinters[id] = true; - return true; - } else { - return false; - } -} - - -/** - * Add cloud printers to the list drop down. - * Called from the cloudprint object on receipt of printer information from the - * cloud print server. - * @param {Array} printers Array of printer info objects. - * @return {Object} The currently selected printer. - */ -function addCloudPrinters(printers) { - var isFirstPass = false; - var printerList = $('printer-list'); - - if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) { - isFirstPass = true; - // Remove empty entry added by setDefaultPrinter. - if (printerList[0] && printerList[0].textContent == '') - printerList.remove(0); - var option = addDestinationListOptionAtPosition( - lastCloudPrintOptionPos++, - localStrings.getString('cloudPrinters'), - 'Label', - false, - true, - false); - cloudprint.setCloudPrint(option, null, null); - } - if (printers != null) { - for (var i = 0; i < printers.length; i++) { - if (!cloudPrinterAlreadyAdded(printers[i]['id'])) { - if (!trackCloudPrinterAdded(printers[i]['id'])) { - break; - } - var option = addDestinationListOptionAtPosition( - lastCloudPrintOptionPos++, - printers[i]['name'], - printers[i]['id'], - printers[i]['name'] == defaultOrLastUsedPrinterName, - false, - false); - cloudprint.setCloudPrint(option, - printers[i]['name'], - printers[i]['id']); - } - } - } else { - if (!cloudPrinterAlreadyAdded(SIGN_IN)) { - addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, - localStrings.getString('signIn'), - SIGN_IN, - false, - false, - false); - trackCloudPrinterAdded(SIGN_IN); - } - } - if (isFirstPass) { - addDestinationListOptionAtPosition(lastCloudPrintOptionPos, - '', - '', - false, - true, - true); - addDestinationListOptionAtPosition(lastCloudPrintOptionPos + 1, - localStrings.getString('localPrinters'), - '', - false, - true, - false); - } - var selectedPrinter = printerList.selectedIndex; - if (selectedPrinter < 0) - return null; - return printerList.options[selectedPrinter]; -} - /** * Sets the color mode for the PDF plugin. * Called from PrintPreviewHandler::ProcessColorSetting(). @@ -909,9 +797,9 @@ function onDidGetDefaultPageLayout(pageLayout) { */ function checkAndHideOverlayLayerIfValid() { var selectedPrinter = getSelectedPrinterName(); - var printToPDF = selectedPrinter == PRINT_TO_PDF; - var printWithCloudPrint = selectedPrinter == PRINT_WITH_CLOUD_PRINT; - if ((printToPDF || printWithCloudPrint || !previewModifiable) && + var printToDialog = selectedPrinter == PRINT_TO_PDF || + selectedPrinter == PRINT_WITH_CLOUD_PRINT; + if ((printToDialog || !previewModifiable) && !isPrintReadyMetafileReady && hasPendingPrintDocumentRequest) { return; } @@ -1016,19 +904,14 @@ function sendPrintDocumentRequestIfNeeded() { // printing. If the preview source is modifiable, we need to wait till all // the requested pages are loaded in the plugin for printing. var selectedPrinter = getSelectedPrinterName(); - var printToPDF = selectedPrinter == PRINT_TO_PDF; - var printWithCloudPrint = selectedPrinter == PRINT_WITH_CLOUD_PRINT; - if (((printToPDF || !previewModifiable || printWithCloudPrint) && - !isPrintReadyMetafileReady) || + var printToDialog = selectedPrinter == PRINT_TO_PDF || + selectedPrinter == PRINT_WITH_CLOUD_PRINT; + if (((printToDialog || !previewModifiable) && !isPrintReadyMetafileReady) || (previewModifiable && hasPendingPreviewRequest)) { return; } hasPendingPrintDocumentRequest = false; - if (printWithCloudPrint) { - chrome.send('printWithCloudPrint'); - return; - } if (!areSettingsValid()) { if (isTabHidden) @@ -1141,21 +1024,6 @@ function setInitiatorTabTitle(initiatorTabTitle) { 'printPreviewTitleFormat', initiatorTabTitle); } -/** - * Attempt to hide the preview tab and display the Cloud Print - * dialog instead. Just disables controls if we're waiting on a new preview - * to be generated. - */ -function printWithCloudPrintDialog() { - if (isPrintReadyMetafileReady) { - chrome.send('printWithCloudPrint'); - } else { - showCustomMessage(localStrings.getString('printWithCloudPrintWait')); - disableInputElementsInSidebar(); - hasPendingPrintDocumentRequest = true; - } -} - /// Pull in all other scripts in a single shot. <include src="print_preview_animations.js"/> <include src="print_preview_cloud.js"/> diff --git a/chrome/browser/resources/print_preview/print_preview_cloud.js b/chrome/browser/resources/print_preview/print_preview_cloud.js index 943f667..dc2aa1a 100644 --- a/chrome/browser/resources/print_preview/print_preview_cloud.js +++ b/chrome/browser/resources/print_preview/print_preview_cloud.js @@ -119,13 +119,11 @@ cr.define('cloudprint', function() { var searchResult = JSON.parse(xhr.responseText); if (searchResult['success']) { var printerList = searchResult['printers']; - callback.call(this, printerList); - } else { - callback.call(this, null); + addCloudPrinters(printerList, callback); + return; } - } else { - callback.call(this, null); } + addCloudPrinters(null, callback); } /** @@ -306,7 +304,8 @@ cr.define('cloudprint', function() { cloud_print_data, add_callback, update_caps_callback) { - var printer = add_callback([JSON.parse(cloud_print_data)]); + var printer = addCloudPrinters([JSON.parse(cloud_print_data)], + add_callback); if (printer) update_caps_callback(printer); } @@ -350,7 +349,87 @@ cr.define('cloudprint', function() { printer.cloudPrintOptions.id = id; } + /** + * Test if a particular cloud printer has already been added to the + * printer dropdown. + * @param {string} id A unique value to track this printer. + * @return {boolean} True if |id| has previously been passed to + * trackCloudPrinterAdded. + */ + function cloudPrinterAlreadyAdded(id) { + return addedCloudPrinters[id]; + } + + /** + * Test if a particular printer has already been added to the printers + * dropdown. Records it if not. + * @param {string} id A unique value to track this printer. + * @return {boolean} False if adding this printer would exceed + * |maxCloudPrinters|. + */ + function trackCloudPrinterAdded(id) { + if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) { + addedCloudPrinters[id] = true; + return true; + } else { + return false; + } + } + + /** + * Add cloud printers to the list drop down. + * Called from the cloudprint object on receipt of printer information from + * the cloud print server. + * @param {Array} printers Array of printer info objects. + * @return {Object} The currently selected printer. + */ + function addCloudPrinters(printers, addDestinationListOptionAtPosition) { + var isFirstPass = false; + var printerList = $('printer-list'); + + if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) { + isFirstPass = true; + // Remove empty entry added by setDefaultPrinter. + if (printerList[0] && printerList[0].textContent == '') + printerList.remove(0); + } + if (printers != null) { + for (var i = 0; i < printers.length; i++) { + if (!cloudPrinterAlreadyAdded(printers[i]['id'])) { + if (!trackCloudPrinterAdded(printers[i]['id'])) { + break; + } + var option = addDestinationListOptionAtPosition( + lastCloudPrintOptionPos++, + printers[i]['name'], + printers[i]['id'], + printers[i]['name'] == defaultOrLastUsedPrinterName, + false, + false); + cloudprint.setCloudPrint(option, + printers[i]['name'], + printers[i]['id']); + } + } + } else { + if (!cloudPrinterAlreadyAdded(SIGN_IN)) { + addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, + localStrings.getString('signIn'), + SIGN_IN, + false, + false, + false); + trackCloudPrinterAdded(SIGN_IN); + } + } + var selectedPrinter = printerList.selectedIndex; + if (selectedPrinter < 0) + return null; + return printerList.options[selectedPrinter]; + } + return { + addCloudPrinters: addCloudPrinters, colorIsDefault: colorIsDefault, fetchPrinters: fetchPrinters, getBaseURL: getBaseURL, |