summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorarthurhsu@chromium.org <arthurhsu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-01 04:25:19 +0000
committerarthurhsu@chromium.org <arthurhsu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-01 04:25:19 +0000
commitbfbcc6c91900a3055cc34cdb606543b4685102dd (patch)
tree5010659372a54fcb6bea97e305aedd463022a694
parent12cac7b56eeec397ce4ecbd74c0b14f6ad17eae7 (diff)
downloadchromium_src-bfbcc6c91900a3055cc34cdb606543b4685102dd.zip
chromium_src-bfbcc6c91900a3055cc34cdb606543b4685102dd.tar.gz
chromium_src-bfbcc6c91900a3055cc34cdb606543b4685102dd.tar.bz2
Print preview not showing if default print is invalid.
BUG=94082 TEST=none Review URL: http://codereview.chromium.org/7740005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99123 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/app/generated_resources.grd3
-rw-r--r--chrome/browser/printing/print_preview_message_handler.cc22
-rw-r--r--chrome/browser/printing/print_preview_message_handler.h1
-rw-r--r--chrome/browser/resources/print_preview/print_preview.js8
-rw-r--r--chrome/browser/ui/webui/print_preview_data_source.cc2
-rw-r--r--chrome/browser/ui/webui/print_preview_ui.cc4
-rw-r--r--chrome/browser/ui/webui/print_preview_ui.h4
-rw-r--r--chrome/common/print_messages.h6
-rw-r--r--chrome/renderer/mock_printer.cc11
-rw-r--r--chrome/renderer/mock_printer.h4
-rw-r--r--chrome/renderer/print_web_view_helper.cc34
-rw-r--r--chrome/renderer/print_web_view_helper.h8
-rw-r--r--chrome/renderer/print_web_view_helper_browsertest.cc48
13 files changed, 140 insertions, 15 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
index 79f6071..4fae468 100644
--- a/chrome/app/generated_resources.grd
+++ b/chrome/app/generated_resources.grd
@@ -5963,6 +5963,9 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_PRINT_PREVIEW_FAILED" desc="Message to display when print preview fails.">
Print preview failed.
</message>
+ <message name="IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS" desc="Message to display when selected printer is not reachable or its settings are invalid.">
+ Selected printer is not available or not installed correctly. Check your printer or select another printer.
+ </message>
<message name="IDS_PRINT_PREVIEW_INITIATOR_TAB_CRASHED" desc="Error message displayed when print preview fails because the tab that initiated the print preview crashed.">
Print is unavailable because the page you were trying to print has crashed.
</message>
diff --git a/chrome/browser/printing/print_preview_message_handler.cc b/chrome/browser/printing/print_preview_message_handler.cc
index b5b4744..d1687d5 100644
--- a/chrome/browser/printing/print_preview_message_handler.cc
+++ b/chrome/browser/printing/print_preview_message_handler.cc
@@ -209,6 +209,26 @@ void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) {
StopWorker(document_cookie);
}
+void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) {
+ // Always need to stop the worker.
+ if (document_cookie) {
+ StopWorker(document_cookie);
+ }
+
+ TabContentsWrapper* print_preview_tab = GetPrintPreviewTab();
+ if (!print_preview_tab || !print_preview_tab->web_ui())
+ return;
+
+ if (g_browser_process->background_printing_manager()->
+ HasPrintPreviewTab(print_preview_tab)) {
+ delete print_preview_tab;
+ } else {
+ PrintPreviewUI* print_preview_ui =
+ static_cast<PrintPreviewUI*>(print_preview_tab->web_ui());
+ print_preview_ui->OnInvalidPrinterSettings();
+ }
+}
+
bool PrintPreviewMessageHandler::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
@@ -227,6 +247,8 @@ bool PrintPreviewMessageHandler::OnMessageReceived(
OnDidGetDefaultPageLayout)
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled,
OnPrintPreviewCancelled)
+ IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings,
+ OnInvalidPrinterSettings)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
diff --git a/chrome/browser/printing/print_preview_message_handler.h b/chrome/browser/printing/print_preview_message_handler.h
index 3a43610..2cd380f 100644
--- a/chrome/browser/printing/print_preview_message_handler.h
+++ b/chrome/browser/printing/print_preview_message_handler.h
@@ -46,6 +46,7 @@ class PrintPreviewMessageHandler : public TabContentsObserver {
const PrintHostMsg_DidPreviewDocument_Params& params);
void OnPrintPreviewFailed(int document_cookie);
void OnPrintPreviewCancelled(int document_cookie);
+ void OnInvalidPrinterSettings(int document_cookie);
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
};
diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js
index 86459f3..c8fdcd6 100644
--- a/chrome/browser/resources/print_preview/print_preview.js
+++ b/chrome/browser/resources/print_preview/print_preview.js
@@ -863,6 +863,14 @@ function printPreviewFailed() {
}
/**
+ * Display an error message when encountered invalid printer settings.
+ * Called from PrintPreviewMessageHandler::OnInvalidDefaultPrinter().
+ */
+function invalidPrinterSettings() {
+ displayErrorMessage(localStrings.getString('invalidPrinterSettings'));
+}
+
+/**
* Called when the PDF plugin loads its document.
*/
function onPDFLoad() {
diff --git a/chrome/browser/ui/webui/print_preview_data_source.cc b/chrome/browser/ui/webui/print_preview_data_source.cc
index b63dfcd..b04078b 100644
--- a/chrome/browser/ui/webui/print_preview_data_source.cc
+++ b/chrome/browser/ui/webui/print_preview_data_source.cc
@@ -49,6 +49,8 @@ PrintPreviewDataSource::PrintPreviewDataSource()
#endif
AddLocalizedString("launchNativeDialog", IDS_PRINT_PREVIEW_NATIVE_DIALOG);
AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED);
+ AddLocalizedString("invalidPrinterSettings",
+ IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS);
AddLocalizedString("initiatorTabCrashed",
IDS_PRINT_PREVIEW_INITIATOR_TAB_CRASHED);
AddLocalizedString("initiatorTabClosed",
diff --git a/chrome/browser/ui/webui/print_preview_ui.cc b/chrome/browser/ui/webui/print_preview_ui.cc
index d4f53e5..7182070 100644
--- a/chrome/browser/ui/webui/print_preview_ui.cc
+++ b/chrome/browser/ui/webui/print_preview_ui.cc
@@ -237,6 +237,10 @@ void PrintPreviewUI::OnPrintPreviewFailed() {
CallJavascriptFunction("printPreviewFailed");
}
+void PrintPreviewUI::OnInvalidPrinterSettings() {
+ CallJavascriptFunction("invalidPrinterSettings");
+}
+
PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
return PrintPreviewDataService::GetInstance();
}
diff --git a/chrome/browser/ui/webui/print_preview_ui.h b/chrome/browser/ui/webui/print_preview_ui.h
index a219539..52a52f3 100644
--- a/chrome/browser/ui/webui/print_preview_ui.h
+++ b/chrome/browser/ui/webui/print_preview_ui.h
@@ -104,6 +104,10 @@ class PrintPreviewUI : public ChromeWebUI {
// Notifies the Web UI renderer that file selection has been cancelled.
void OnFileSelectionCancelled();
+ // Notifies the Web UI that the printer is unavailable or its settings are
+ // invalid.
+ void OnInvalidPrinterSettings();
+
// Notifies the Web UI to cancel the pending preview request.
void OnCancelPendingPreviewRequest();
diff --git a/chrome/common/print_messages.h b/chrome/common/print_messages.h
index 89d377e..67fa13c 100644
--- a/chrome/common/print_messages.h
+++ b/chrome/common/print_messages.h
@@ -382,3 +382,9 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintPreviewFailed,
// Tell the browser print preview was cancelled.
IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintPreviewCancelled,
int /* document cookie */)
+
+// Tell the browser print preview found the selected printer has invalid
+// settings (which typically caused by disconnected network printer or printer
+// driver is bogus).
+IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintPreviewInvalidPrinterSettings,
+ int /* document cookie */)
diff --git a/chrome/renderer/mock_printer.cc b/chrome/renderer/mock_printer.cc
index f8266c8..9a5d2f4 100644
--- a/chrome/renderer/mock_printer.cc
+++ b/chrome/renderer/mock_printer.cc
@@ -44,7 +44,8 @@ MockPrinter::MockPrinter()
display_header_footer_(false),
date_(ASCIIToUTF16("date")),
title_(ASCIIToUTF16("title")),
- url_(ASCIIToUTF16("url")) {
+ url_(ASCIIToUTF16("url")),
+ use_invalid_settings_(false) {
page_size_.SetSize(static_cast<int>(8.5 * dpi_),
static_cast<int>(11.0 * dpi_));
printable_size_.SetSize(static_cast<int>((7.5 * dpi_)),
@@ -87,6 +88,12 @@ void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) {
url_ = params.url;
}
+void MockPrinter::UseInvalidSettings() {
+ use_invalid_settings_ = true;
+ PrintMsg_Print_Params empty_param;
+ SetDefaultPrintSettings(empty_param);
+}
+
void MockPrinter::ScriptedPrint(int cookie,
int expected_pages_count,
bool has_selection,
@@ -238,7 +245,7 @@ bool MockPrinter::SaveBitmap(
}
int MockPrinter::CreateDocumentCookie() {
- return ++current_document_cookie_;
+ return use_invalid_settings_ ? 0 : ++current_document_cookie_;
}
void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) {
diff --git a/chrome/renderer/mock_printer.h b/chrome/renderer/mock_printer.h
index bbde217..b9d6846 100644
--- a/chrome/renderer/mock_printer.h
+++ b/chrome/renderer/mock_printer.h
@@ -69,6 +69,7 @@ class MockPrinter {
// Functions that changes settings of a pseudo printer.
void ResetPrinter();
void SetDefaultPrintSettings(const PrintMsg_Print_Params& params);
+ void UseInvalidSettings();
// Functions that handles IPC events.
void GetDefaultPrintSettings(PrintMsg_Print_Params* params);
@@ -142,6 +143,9 @@ class MockPrinter {
string16 title_;
string16 url_;
+ // Used for generating invalid settings.
+ bool use_invalid_settings_;
+
std::vector<scoped_refptr<MockPrinterPage> > pages_;
DISALLOW_COPY_AND_ASSIGN(MockPrinter);
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index 0d0ec73..36726ee 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -571,7 +571,7 @@ void PrintWebViewHelper::OnPrintForPrintPreview(
WebFrame* pdf_frame = pdf_element.document().frame();
scoped_ptr<PrepareFrameAndViewForPrint> prepare;
if (!InitPrintSettingsAndPrepareFrame(pdf_frame, &pdf_element, &prepare)) {
- NOTREACHED() << "Failed to initialize print page settings";
+ LOG(ERROR) << "Failed to initialize print page settings";
return;
}
@@ -631,8 +631,11 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
print_preview_context_.OnPrintPreview();
if (!InitPrintSettings(print_preview_context_.frame(),
- print_preview_context_.node())) {
- NOTREACHED();
+ print_preview_context_.node(),
+ true)) {
+ Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
+ routing_id(),
+ print_pages_params_->params.document_cookie));
return;
}
@@ -1037,7 +1040,8 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters(
}
bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
- WebKit::WebNode* node) {
+ WebKit::WebNode* node,
+ bool is_preview) {
DCHECK(frame);
PrintMsg_PrintPages_Params settings;
@@ -1046,27 +1050,33 @@ bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
// Check if the printer returned any settings, if the settings is empty, we
// can safely assume there are no printer drivers configured. So we safely
// terminate.
+ bool result = true;
if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
- render_view()->runModalAlertDialog(
- frame,
- l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING));
- return false;
+ if (!is_preview) {
+ render_view()->runModalAlertDialog(
+ frame,
+ l10n_util::GetStringUTF16(
+ IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
+ }
+ result = false;
}
- if (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0) {
+
+ if (result &&
+ (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
// Invalid print page settings.
NOTREACHED();
- return false;
+ result = false;
}
settings.pages.clear();
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
- return true;
+ return result;
}
bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
WebKit::WebFrame* frame, WebKit::WebNode* node,
scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
- if (!InitPrintSettings(frame, node))
+ if (!InitPrintSettings(frame, node, false))
return false;
DCHECK(!prepare->get());
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index dc38462..b47cda0 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -110,6 +110,10 @@ class PrintWebViewHelper : public RenderViewObserver,
OnPrintForPrintPreviewFail);
FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest,
OnPrintPreviewForSelectedPages);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest,
+ OnPrintPreviewUsingInvalidPrinterSettings);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest,
+ OnPrintForPrintPreviewUsingInvalidPrinterSettings);
#if defined(OS_WIN) || defined(OS_MACOSX)
FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintLayoutTest);
@@ -168,7 +172,9 @@ class PrintWebViewHelper : public RenderViewObserver,
// Print Settings -----------------------------------------------------------
// Initialize print page settings with default settings.
- bool InitPrintSettings(WebKit::WebFrame* frame, WebKit::WebNode* node);
+ bool InitPrintSettings(WebKit::WebFrame* frame,
+ WebKit::WebNode* node,
+ bool is_preview);
// Initialize print page settings with default settings and prepare the frame
// for print. A new PrepareFrameAndViewForPrint is created to fulfill the
diff --git a/chrome/renderer/print_web_view_helper_browsertest.cc b/chrome/renderer/print_web_view_helper_browsertest.cc
index abc34d5..2566e16 100644
--- a/chrome/renderer/print_web_view_helper_browsertest.cc
+++ b/chrome/renderer/print_web_view_helper_browsertest.cc
@@ -346,6 +346,13 @@ class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase {
EXPECT_EQ(did_fail, print_failed);
}
+ void VerifyPrintPreviewInvalidPrinterSettings(bool settings_invalid) {
+ bool print_preview_invalid_printer_settings =
+ (render_thread_.sink().GetUniqueMessageMatching(
+ PrintHostMsg_PrintPreviewInvalidPrinterSettings::ID) != NULL);
+ EXPECT_EQ(settings_invalid, print_preview_invalid_printer_settings);
+ }
+
// |page_number| is 0-based.
void VerifyDidPreviewPage(bool generate_draft_pages, int page_number) {
bool msg_found = false;
@@ -485,4 +492,45 @@ TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) {
VerifyPrintFailed(true);
VerifyPagesPrinted(false);
}
+
+// Tests that when default printer has invalid printer settings, print preview
+// receives error message.
+TEST_F(PrintWebViewHelperPreviewTest,
+ OnPrintPreviewUsingInvalidPrinterSettings) {
+ LoadHTML(kPrintPreviewHTML);
+
+ PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview();
+
+ // Set mock printer to provide invalid settings.
+ render_thread_.printer()->UseInvalidSettings();
+
+ // Fill in some dummy values.
+ DictionaryValue dict;
+ CreatePrintSettingsDictionary(&dict);
+ PrintWebViewHelper::Get(view_)->OnPrintPreview(dict);
+
+ // We should have received invalid printer settings from |printer_|.
+ VerifyPrintPreviewInvalidPrinterSettings(true);
+ EXPECT_EQ(0, render_thread_.print_preview_pages_remaining());
+
+ // It should receive the invalid printer settings message only.
+ VerifyPrintPreviewFailed(false);
+ VerifyPrintPreviewGenerated(false);
+}
+
+TEST_F(PrintWebViewHelperPreviewTest,
+ OnPrintForPrintPreviewUsingInvalidPrinterSettings) {
+ LoadHTML(kPrintPreviewHTML);
+
+ // Set mock printer to provide invalid settings.
+ render_thread_.printer()->UseInvalidSettings();
+
+ // Fill in some dummy values.
+ DictionaryValue dict;
+ CreatePrintSettingsDictionary(&dict);
+ PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict);
+
+ VerifyPagesPrinted(false);
+}
+
#endif // !defined(OS_CHROMEOS)