Update printing code to Chrome 37.

This commit is contained in:
Cheng Zhao 2014-09-01 15:35:39 +08:00
parent 55003716aa
commit 134f8236cc
6 changed files with 61 additions and 59 deletions

View file

@ -12,6 +12,7 @@
#include "base/timer/timer.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/printing/print_job_worker.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "printing/printed_document.h"
#include "printing/printed_page.h"
@ -69,7 +70,10 @@ void PrintJob::Initialize(PrintJobWorkerOwner* job,
settings_ = job->settings();
PrintedDocument* new_doc =
new PrintedDocument(settings_, source_, job->cookie());
new PrintedDocument(settings_,
source_,
job->cookie(),
content::BrowserThread::GetBlockingPool());
new_doc->set_page_count(page_count);
UpdatePrintedDocument(new_doc);

View file

@ -120,39 +120,19 @@ void PrintJobWorker::SetSettings(
DCHECK_EQ(message_loop(), base::MessageLoop::current());
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&HoldRefCallback, make_scoped_refptr(owner_),
BrowserThread::UI,
FROM_HERE,
base::Bind(&HoldRefCallback,
make_scoped_refptr(owner_),
base::Bind(&PrintJobWorker::UpdatePrintSettings,
base::Unretained(this), new_settings)));
base::Unretained(this),
base::Owned(new_settings))));
}
void PrintJobWorker::UpdatePrintSettings(
const base::DictionaryValue* const new_settings) {
// Create new PageRanges based on |new_settings|.
PageRanges new_ranges;
const base::ListValue* page_range_array;
if (new_settings->GetList(kSettingPageRange, &page_range_array)) {
for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
const base::DictionaryValue* dict;
if (!page_range_array->GetDictionary(index, &dict))
continue;
PageRange range;
if (!dict->GetInteger(kSettingPageRangeFrom, &range.from) ||
!dict->GetInteger(kSettingPageRangeTo, &range.to)) {
continue;
}
// Page numbers are 1-based in the dictionary.
// Page numbers are 0-based for the printing context.
range.from--;
range.to--;
new_ranges.push_back(range);
}
}
PrintingContext::Result result =
printing_context_->UpdatePrintSettings(*new_settings, new_ranges);
delete new_settings;
printing_context_->UpdatePrintSettings(*new_settings);
GetSettingsDone(result);
}
@ -218,8 +198,6 @@ void PrintJobWorker::StartPrinting(PrintedDocument* new_document) {
base::string16 document_name =
printing::SimplifyDocumentTitle(document_->name());
if (document_name.empty()) {
// document_name = printing::SimplifyDocumentTitle(
// l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE));
}
PrintingContext::Result result =
printing_context_->NewDocument(document_name);
@ -272,8 +250,8 @@ void PrintJobWorker::OnNewPage() {
while (true) {
// Is the page available?
scoped_refptr<PrintedPage> page;
if (!document_->GetPage(page_number_.ToInt(), &page)) {
scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt());
if (!page) {
// We need to wait for the page to be available.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,

View file

@ -4,8 +4,6 @@
#include "chrome/browser/printing/print_view_manager_base.h"
#include <map>
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
@ -34,12 +32,16 @@
using base::TimeDelta;
using content::BrowserThread;
#if defined(OS_WIN)
namespace printing {
namespace {
#if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)
// Limits memory usage by raster to 64 MiB.
const int kMaxRasterSizeInPixels = 16*1024*1024;
#endif
namespace printing {
} // namespace
PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
@ -49,7 +51,8 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
cookie_(0),
queue_(g_browser_process->print_job_manager()->queue()) {
DCHECK(queue_);
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \
defined(WIN_PDF_METAFILE_FOR_PRINTING)
expecting_first_page_ = true;
#endif
printing_enabled_ = true;
@ -103,7 +106,7 @@ void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) {
}
void PrintViewManagerBase::OnDidPrintPage(
const PrintHostMsg_DidPrintPage_Params& params) {
const PrintHostMsg_DidPrintPage_Params& params) {
if (!OpportunisticallyCreatePrintJob(params.document_cookie))
return;
@ -114,9 +117,10 @@ void PrintViewManagerBase::OnDidPrintPage(
return;
}
#if defined(OS_WIN) || defined(OS_MACOSX)
#if (defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)) || \
defined(OS_MACOSX)
const bool metafile_must_be_valid = true;
#elif defined(OS_POSIX)
#elif defined(OS_POSIX) || defined(WIN_PDF_METAFILE_FOR_PRINTING)
const bool metafile_must_be_valid = expecting_first_page_;
expecting_first_page_ = false;
#endif
@ -139,10 +143,10 @@ void PrintViewManagerBase::OnDidPrintPage(
}
}
#if defined(OS_WIN)
#if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)
bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize);
int raster_size = std::min(params.page_size.GetArea(),
kMaxRasterSizeInPixels);
int raster_size =
std::min(params.page_size.GetArea(), kMaxRasterSizeInPixels);
if (big_emf) {
scoped_ptr<NativeMetafile> raster_metafile(
metafile->RasterizeMetafile(raster_size));
@ -156,16 +160,39 @@ void PrintViewManagerBase::OnDidPrintPage(
return;
}
}
#endif
#endif // OS_WIN && !WIN_PDF_METAFILE_FOR_PRINTING
#if !defined(WIN_PDF_METAFILE_FOR_PRINTING)
// Update the rendered document. It will send notifications to the listener.
document->SetPage(params.page_number,
metafile.release(),
#if defined(OS_WIN)
params.actual_shrink,
#endif // OS_WIN
params.page_size,
params.content_area);
ShouldQuitFromInnerMessageLoop();
#else
if (metafile_must_be_valid) {
scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes(
reinterpret_cast<const unsigned char*>(shared_buf.memory()),
params.data_size);
document->DebugDumpData(bytes, FILE_PATH_LITERAL(".pdf"));
if (!pdf_to_emf_converter_)
pdf_to_emf_converter_ = PdfToEmfConverter::CreateDefault();
const int kPrinterDpi = print_job_->settings().dpi();
pdf_to_emf_converter_->Start(
bytes,
printing::PdfRenderSettings(params.content_area, kPrinterDpi, true),
base::Bind(&PrintViewManagerBase::OnPdfToEmfConverted,
base::Unretained(this),
params));
}
#endif // !WIN_PDF_METAFILE_FOR_PRINTING
}
void PrintViewManagerBase::OnPrintingFailed(int cookie) {
@ -186,10 +213,6 @@ void PrintViewManagerBase::OnShowInvalidPrinterSettingsError() {
LOG(ERROR) << "Invalid printer settings";
}
void PrintViewManagerBase::DidStartLoading(
content::RenderViewHost* render_view_host) {
}
bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
@ -363,7 +386,8 @@ void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
// DO NOT wait for the job to finish.
ReleasePrintJob();
}
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \
defined(WIN_PDF_METAFILE_FOR_PRINTING)
expecting_first_page_ = true;
#endif
}

View file

@ -23,6 +23,7 @@ class RenderViewHost;
namespace printing {
class JobEventDetails;
class PdfToEmfConverter;
class PrintJob;
class PrintJobWorkerOwner;
class PrintQueriesQueue;
@ -63,10 +64,6 @@ class PrintViewManagerBase : public content::NotificationObserver,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// content::WebContentsObserver implementation.
virtual void DidStartLoading(
content::RenderViewHost* render_view_host) OVERRIDE;
// Cancels the print job.
virtual void NavigationStopped() OVERRIDE;
@ -143,7 +140,8 @@ class PrintViewManagerBase : public content::NotificationObserver,
// print settings are being loaded.
bool inside_inner_message_loop_;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \
defined(WIN_PDF_METAFILE_FOR_PRINTING)
// Set to true when OnDidPrintPage() should be expecting the first page.
bool expecting_first_page_;
#endif

View file

@ -63,10 +63,9 @@ PrintingMessageFilter::PrintingMessageFilter(int render_process_id)
PrintingMessageFilter::~PrintingMessageFilter() {
}
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) {
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(PrintingMessageFilter, message, *message_was_ok)
IPC_BEGIN_MESSAGE_MAP(PrintingMessageFilter, message)
#if defined(OS_WIN)
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
#endif

View file

@ -38,8 +38,7 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
explicit PrintingMessageFilter(int render_process_id);
// content::BrowserMessageFilter methods.
virtual bool OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
private:
virtual ~PrintingMessageFilter();