summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-19 21:40:18 +0000
committerthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-19 21:40:18 +0000
commitabd0d5088a5f3fefc8bbb86ccf93c4467bd5820b (patch)
treefdfe63b8a67b7b7cd73a14e3417f4b328b301d44 /chrome
parent569c2bf357f799e5c8e6e24c6505227373bc5769 (diff)
downloadchromium_src-abd0d5088a5f3fefc8bbb86ccf93c4467bd5820b.zip
chromium_src-abd0d5088a5f3fefc8bbb86ccf93c4467bd5820b.tar.gz
chromium_src-abd0d5088a5f3fefc8bbb86ccf93c4467bd5820b.tar.bz2
Print Preview: Refactor and cleanup parts of PrintPreviewHandler and PrintWebViewHelper.
BUG=none TEST=none Review URL: https://chromiumcodereview.appspot.com/9192004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118364 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/ui/webui/print_preview/print_preview_handler.cc46
-rw-r--r--chrome/browser/ui/webui/print_preview/print_preview_handler.h5
-rw-r--r--chrome/renderer/print_web_view_helper.cc24
-rw-r--r--chrome/renderer/print_web_view_helper.h43
4 files changed, 62 insertions, 56 deletions
diff --git a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc
index aa9b265..eaf5985 100644
--- a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc
+++ b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc
@@ -15,7 +15,7 @@
#include "base/i18n/file_util_icu.h"
#include "base/i18n/number_formatting.h"
#include "base/json/json_reader.h"
-#include "base/memory/ref_counted.h"
+#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
#include "base/threading/thread.h"
@@ -393,27 +393,32 @@ void PrintPreviewHandler::HandlePrint(const ListValue* args) {
}
}
+ // Never try to add headers/footers here. It's already in the generated PDF.
+ settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false);
+
bool print_to_pdf = false;
- settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
+ bool is_cloud_printer = false;
+ bool is_cloud_dialog = false;
bool open_pdf_in_preview = false;
#if defined(OS_MACOSX)
open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview);
#endif
- settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false);
+ if (!open_pdf_in_preview) {
+ settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
+ settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog);
+ is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId);
+ }
- bool is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId);
- bool is_cloud_dialog = false;
- settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog);
- if (is_cloud_printer && !open_pdf_in_preview) {
+ if (is_cloud_printer) {
std::string print_ticket;
bool res = args->GetString(1, &print_ticket);
DCHECK(res);
SendCloudPrintJob(*settings, print_ticket);
- } else if (print_to_pdf && !open_pdf_in_preview) {
+ } else if (print_to_pdf) {
HandlePrintToPdf(*settings);
- } else if (is_cloud_dialog && !open_pdf_in_preview) {
+ } else if (is_cloud_dialog) {
HandlePrintWithCloudPrint();
} else {
ReportPrintSettingsStats(*settings);
@@ -442,9 +447,14 @@ void PrintPreviewHandler::HandlePrint(const ListValue* args) {
void PrintPreviewHandler::HandlePrintToPdf(
const base::DictionaryValue& settings) {
+ PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
+ web_ui()->GetController());
if (print_to_pdf_path_.get()) {
// User has already selected a path, no need to show the dialog again.
- PostPrintToPdfTask();
+ scoped_refptr<RefCountedBytes> data;
+ print_preview_ui->GetPrintPreviewDataForIndex(
+ printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data);
+ PostPrintToPdfTask(data);
} else if (!select_file_dialog_.get() || !select_file_dialog_->IsRunning(
platform_util::GetTopLevel(preview_tab()->GetNativeView()))) {
ReportUserActionHistogram(PRINT_TO_PDF);
@@ -452,8 +462,6 @@ void PrintPreviewHandler::HandlePrintToPdf(
GetPageCountFromSettingsDictionary(settings));
// Pre-populating select file dialog with print job title.
- PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
- web_ui()->GetController());
string16 print_job_title_utf16 = print_preview_ui->initiator_tab_title();
#if defined(OS_WIN)
@@ -730,7 +738,6 @@ void PrintPreviewHandler::SendCloudPrintJob(const DictionaryValue& settings,
web_ui()->GetController());
print_preview_ui->GetPrintPreviewDataForIndex(
printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data);
- CHECK(data.get());
DCHECK_GT(data->size(), 0U);
string16 print_job_title_utf16 =
@@ -844,7 +851,7 @@ void PrintPreviewHandler::ShowSystemDialog() {
void PrintPreviewHandler::FileSelected(const FilePath& path,
int index, void* params) {
- // Updating last_saved_path_ to the newly selected folder.
+ // Updating |last_saved_path_| to the newly selected folder.
*last_saved_path_ = path.DirName();
PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
@@ -855,16 +862,11 @@ void PrintPreviewHandler::FileSelected(const FilePath& path,
printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data);
print_to_pdf_path_.reset(new FilePath(path));
if (data.get())
- PostPrintToPdfTask();
+ PostPrintToPdfTask(data);
}
-void PrintPreviewHandler::PostPrintToPdfTask() {
- PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
- web_ui()->GetController());
- scoped_refptr<RefCountedBytes> data;
- print_preview_ui->GetPrintPreviewDataForIndex(
- printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data);
- DCHECK(data.get());
+void PrintPreviewHandler::PostPrintToPdfTask(
+ scoped_refptr<RefCountedBytes> data) {
printing::PreviewMetafile* metafile = new printing::PreviewMetafile;
metafile->InitFromData(static_cast<const void*>(data->front()), data->size());
// PrintToPdfCallback takes ownership of |metafile|.
diff --git a/chrome/browser/ui/webui/print_preview/print_preview_handler.h b/chrome/browser/ui/webui/print_preview/print_preview_handler.h
index f1c2317..8674d55 100644
--- a/chrome/browser/ui/webui/print_preview/print_preview_handler.h
+++ b/chrome/browser/ui/webui/print_preview/print_preview_handler.h
@@ -20,6 +20,7 @@
class FilePath;
class PrintSystemTaskProxy;
+class RefCountedBytes;
class TabContentsWrapper;
namespace base {
@@ -174,8 +175,8 @@ class PrintPreviewHandler : public content::WebUIMessageHandler,
// Clears initiator tab details for this preview tab.
void ClearInitiatorTabDetails();
- // Posts a task to save to pdf at |print_to_pdf_path_|.
- void PostPrintToPdfTask();
+ // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
+ void PostPrintToPdfTask(scoped_refptr<RefCountedBytes> data);
// Populates |settings| according to the current locale.
void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index ec12341..ac67016 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -679,12 +679,7 @@ void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
if (is_preview_enabled_) {
print_preview_context_.InitWithFrame(frame);
- old_print_pages_params_.reset(); // Same as in RequestPrintPreview().
- IPC::SyncMessage* msg = new PrintHostMsg_ScriptedPrintPreview(
- routing_id(),
- print_preview_context_.IsModifiable());
- msg->EnableMessagePumping();
- Send(msg);
+ RequestPrintPreview(true);
} else {
Print(frame, WebNode());
}
@@ -979,7 +974,7 @@ void PrintWebViewHelper::OnInitiatePrintPreview() {
WebFrame* frame;
if (GetPrintFrame(&frame)) {
print_preview_context_.InitWithFrame(frame);
- RequestPrintPreview();
+ RequestPrintPreview(false);
}
}
@@ -993,7 +988,7 @@ void PrintWebViewHelper::PrintNode(const WebNode& node) {
// its |context_menu_node_|.
if (is_preview_enabled_) {
print_preview_context_.InitWithNode(node);
- RequestPrintPreview();
+ RequestPrintPreview(false);
} else {
WebNode duplicate_node(node);
Print(duplicate_node.document().frame(), duplicate_node);
@@ -1471,10 +1466,17 @@ void PrintWebViewHelper::DisplayPrintJobError() {
l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT));
}
-void PrintWebViewHelper::RequestPrintPreview() {
+void PrintWebViewHelper::RequestPrintPreview(bool is_script_initiated) {
+ const bool is_modifiable = print_preview_context_.IsModifiable();
old_print_pages_params_.reset();
- Send(new PrintHostMsg_RequestPrintPreview(
- routing_id(), print_preview_context_.IsModifiable()));
+ if (is_script_initiated) {
+ IPC::SyncMessage* msg = new PrintHostMsg_ScriptedPrintPreview(
+ routing_id(), is_modifiable);
+ msg->EnableMessagePumping();
+ Send(msg);
+ } else {
+ Send(new PrintHostMsg_RequestPrintPreview(routing_id(), is_modifiable));
+ }
}
bool PrintWebViewHelper::CheckForCancel() {
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index 5c6941d..ec3a7cf 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -107,6 +107,25 @@ class PrintWebViewHelper
FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintWithIframe);
#endif // defined(OS_WIN) || defined(OS_MACOSX)
+ enum PrintingResult {
+ OK,
+ FAIL_PRINT,
+ FAIL_PREVIEW,
+ };
+
+ enum PrintPreviewErrorBuckets {
+ PREVIEW_ERROR_NONE, // Always first.
+ PREVIEW_ERROR_BAD_SETTING,
+ PREVIEW_ERROR_METAFILE_COPY_FAILED,
+ PREVIEW_ERROR_METAFILE_INIT_FAILED,
+ PREVIEW_ERROR_ZERO_PAGES,
+ PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED,
+ PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE,
+ PREVIEW_ERROR_UPDATING_PRINT_SETTINGS,
+ PREVIEW_ERROR_INVALID_PRINTER_SETTINGS,
+ PREVIEW_ERROR_LAST_ENUM // Always last.
+ };
+
// RenderViewObserver implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void PrintPage(WebKit::WebFrame* frame) OVERRIDE;
@@ -161,12 +180,6 @@ class PrintWebViewHelper
void Print(WebKit::WebFrame* frame, const WebKit::WebNode& node);
- enum PrintingResult {
- OK,
- FAIL_PRINT,
- FAIL_PREVIEW,
- };
-
// Notification when printing is done - signal tear-down/free resources.
void DidFinishPrinting(PrintingResult result);
@@ -308,7 +321,8 @@ class PrintWebViewHelper
// Displays the print job error message to the user.
void DisplayPrintJobError();
- void RequestPrintPreview();
+ // If triggered by a window.print() |is_script_initiated| is set to true.
+ void RequestPrintPreview(bool is_script_initiated);
// Checks whether print preview should continue or not.
// Returns true if cancelling, false if continuing.
@@ -346,19 +360,6 @@ class PrintWebViewHelper
// footers if requested by the user.
scoped_ptr<base::DictionaryValue> header_footer_info_;
- enum PrintPreviewErrorBuckets {
- PREVIEW_ERROR_NONE, // Always first.
- PREVIEW_ERROR_BAD_SETTING,
- PREVIEW_ERROR_METAFILE_COPY_FAILED,
- PREVIEW_ERROR_METAFILE_INIT_FAILED,
- PREVIEW_ERROR_ZERO_PAGES,
- PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED,
- PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE,
- PREVIEW_ERROR_UPDATING_PRINT_SETTINGS,
- PREVIEW_ERROR_INVALID_PRINTER_SETTINGS,
- PREVIEW_ERROR_LAST_ENUM // Always last.
- };
-
// Keeps track of the state of print preview between messages.
class PrintPreviewContext {
public: