Update printing code to Chrome 37.
This commit is contained in:
parent
55003716aa
commit
134f8236cc
6 changed files with 61 additions and 59 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include "base/timer/timer.h"
|
#include "base/timer/timer.h"
|
||||||
#include "chrome/browser/chrome_notification_types.h"
|
#include "chrome/browser/chrome_notification_types.h"
|
||||||
#include "chrome/browser/printing/print_job_worker.h"
|
#include "chrome/browser/printing/print_job_worker.h"
|
||||||
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/notification_service.h"
|
#include "content/public/browser/notification_service.h"
|
||||||
#include "printing/printed_document.h"
|
#include "printing/printed_document.h"
|
||||||
#include "printing/printed_page.h"
|
#include "printing/printed_page.h"
|
||||||
|
@ -69,7 +70,10 @@ void PrintJob::Initialize(PrintJobWorkerOwner* job,
|
||||||
settings_ = job->settings();
|
settings_ = job->settings();
|
||||||
|
|
||||||
PrintedDocument* new_doc =
|
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);
|
new_doc->set_page_count(page_count);
|
||||||
UpdatePrintedDocument(new_doc);
|
UpdatePrintedDocument(new_doc);
|
||||||
|
|
||||||
|
|
|
@ -120,39 +120,19 @@ void PrintJobWorker::SetSettings(
|
||||||
DCHECK_EQ(message_loop(), base::MessageLoop::current());
|
DCHECK_EQ(message_loop(), base::MessageLoop::current());
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
BrowserThread::PostTask(
|
||||||
BrowserThread::UI, FROM_HERE,
|
BrowserThread::UI,
|
||||||
base::Bind(&HoldRefCallback, make_scoped_refptr(owner_),
|
FROM_HERE,
|
||||||
|
base::Bind(&HoldRefCallback,
|
||||||
|
make_scoped_refptr(owner_),
|
||||||
base::Bind(&PrintJobWorker::UpdatePrintSettings,
|
base::Bind(&PrintJobWorker::UpdatePrintSettings,
|
||||||
base::Unretained(this), new_settings)));
|
base::Unretained(this),
|
||||||
|
base::Owned(new_settings))));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintJobWorker::UpdatePrintSettings(
|
void PrintJobWorker::UpdatePrintSettings(
|
||||||
const base::DictionaryValue* const new_settings) {
|
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 =
|
PrintingContext::Result result =
|
||||||
printing_context_->UpdatePrintSettings(*new_settings, new_ranges);
|
printing_context_->UpdatePrintSettings(*new_settings);
|
||||||
delete new_settings;
|
|
||||||
GetSettingsDone(result);
|
GetSettingsDone(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,8 +198,6 @@ void PrintJobWorker::StartPrinting(PrintedDocument* new_document) {
|
||||||
base::string16 document_name =
|
base::string16 document_name =
|
||||||
printing::SimplifyDocumentTitle(document_->name());
|
printing::SimplifyDocumentTitle(document_->name());
|
||||||
if (document_name.empty()) {
|
if (document_name.empty()) {
|
||||||
// document_name = printing::SimplifyDocumentTitle(
|
|
||||||
// l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE));
|
|
||||||
}
|
}
|
||||||
PrintingContext::Result result =
|
PrintingContext::Result result =
|
||||||
printing_context_->NewDocument(document_name);
|
printing_context_->NewDocument(document_name);
|
||||||
|
@ -272,8 +250,8 @@ void PrintJobWorker::OnNewPage() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Is the page available?
|
// Is the page available?
|
||||||
scoped_refptr<PrintedPage> page;
|
scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt());
|
||||||
if (!document_->GetPage(page_number_.ToInt(), &page)) {
|
if (!page) {
|
||||||
// We need to wait for the page to be available.
|
// We need to wait for the page to be available.
|
||||||
base::MessageLoop::current()->PostDelayedTask(
|
base::MessageLoop::current()->PostDelayedTask(
|
||||||
FROM_HERE,
|
FROM_HERE,
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
#include "chrome/browser/printing/print_view_manager_base.h"
|
#include "chrome/browser/printing/print_view_manager_base.h"
|
||||||
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/memory/scoped_ptr.h"
|
#include "base/memory/scoped_ptr.h"
|
||||||
#include "base/prefs/pref_service.h"
|
#include "base/prefs/pref_service.h"
|
||||||
|
@ -34,12 +32,16 @@
|
||||||
using base::TimeDelta;
|
using base::TimeDelta;
|
||||||
using content::BrowserThread;
|
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.
|
// Limits memory usage by raster to 64 MiB.
|
||||||
const int kMaxRasterSizeInPixels = 16*1024*1024;
|
const int kMaxRasterSizeInPixels = 16*1024*1024;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace printing {
|
} // namespace
|
||||||
|
|
||||||
PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
|
PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
|
||||||
: content::WebContentsObserver(web_contents),
|
: content::WebContentsObserver(web_contents),
|
||||||
|
@ -49,7 +51,8 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
|
||||||
cookie_(0),
|
cookie_(0),
|
||||||
queue_(g_browser_process->print_job_manager()->queue()) {
|
queue_(g_browser_process->print_job_manager()->queue()) {
|
||||||
DCHECK(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;
|
expecting_first_page_ = true;
|
||||||
#endif
|
#endif
|
||||||
printing_enabled_ = true;
|
printing_enabled_ = true;
|
||||||
|
@ -103,7 +106,7 @@ void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintViewManagerBase::OnDidPrintPage(
|
void PrintViewManagerBase::OnDidPrintPage(
|
||||||
const PrintHostMsg_DidPrintPage_Params& params) {
|
const PrintHostMsg_DidPrintPage_Params& params) {
|
||||||
if (!OpportunisticallyCreatePrintJob(params.document_cookie))
|
if (!OpportunisticallyCreatePrintJob(params.document_cookie))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -114,9 +117,10 @@ void PrintViewManagerBase::OnDidPrintPage(
|
||||||
return;
|
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;
|
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_;
|
const bool metafile_must_be_valid = expecting_first_page_;
|
||||||
expecting_first_page_ = false;
|
expecting_first_page_ = false;
|
||||||
#endif
|
#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);
|
bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize);
|
||||||
int raster_size = std::min(params.page_size.GetArea(),
|
int raster_size =
|
||||||
kMaxRasterSizeInPixels);
|
std::min(params.page_size.GetArea(), kMaxRasterSizeInPixels);
|
||||||
if (big_emf) {
|
if (big_emf) {
|
||||||
scoped_ptr<NativeMetafile> raster_metafile(
|
scoped_ptr<NativeMetafile> raster_metafile(
|
||||||
metafile->RasterizeMetafile(raster_size));
|
metafile->RasterizeMetafile(raster_size));
|
||||||
|
@ -156,16 +160,39 @@ void PrintViewManagerBase::OnDidPrintPage(
|
||||||
return;
|
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.
|
// Update the rendered document. It will send notifications to the listener.
|
||||||
document->SetPage(params.page_number,
|
document->SetPage(params.page_number,
|
||||||
metafile.release(),
|
metafile.release(),
|
||||||
|
#if defined(OS_WIN)
|
||||||
params.actual_shrink,
|
params.actual_shrink,
|
||||||
|
#endif // OS_WIN
|
||||||
params.page_size,
|
params.page_size,
|
||||||
params.content_area);
|
params.content_area);
|
||||||
|
|
||||||
ShouldQuitFromInnerMessageLoop();
|
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) {
|
void PrintViewManagerBase::OnPrintingFailed(int cookie) {
|
||||||
|
@ -186,10 +213,6 @@ void PrintViewManagerBase::OnShowInvalidPrinterSettingsError() {
|
||||||
LOG(ERROR) << "Invalid printer settings";
|
LOG(ERROR) << "Invalid printer settings";
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintViewManagerBase::DidStartLoading(
|
|
||||||
content::RenderViewHost* render_view_host) {
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
|
bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
|
||||||
bool handled = true;
|
bool handled = true;
|
||||||
IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
|
IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
|
||||||
|
@ -363,7 +386,8 @@ void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
|
||||||
// DO NOT wait for the job to finish.
|
// DO NOT wait for the job to finish.
|
||||||
ReleasePrintJob();
|
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;
|
expecting_first_page_ = true;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ class RenderViewHost;
|
||||||
namespace printing {
|
namespace printing {
|
||||||
|
|
||||||
class JobEventDetails;
|
class JobEventDetails;
|
||||||
|
class PdfToEmfConverter;
|
||||||
class PrintJob;
|
class PrintJob;
|
||||||
class PrintJobWorkerOwner;
|
class PrintJobWorkerOwner;
|
||||||
class PrintQueriesQueue;
|
class PrintQueriesQueue;
|
||||||
|
@ -63,10 +64,6 @@ class PrintViewManagerBase : public content::NotificationObserver,
|
||||||
const content::NotificationSource& source,
|
const content::NotificationSource& source,
|
||||||
const content::NotificationDetails& details) OVERRIDE;
|
const content::NotificationDetails& details) OVERRIDE;
|
||||||
|
|
||||||
// content::WebContentsObserver implementation.
|
|
||||||
virtual void DidStartLoading(
|
|
||||||
content::RenderViewHost* render_view_host) OVERRIDE;
|
|
||||||
|
|
||||||
// Cancels the print job.
|
// Cancels the print job.
|
||||||
virtual void NavigationStopped() OVERRIDE;
|
virtual void NavigationStopped() OVERRIDE;
|
||||||
|
|
||||||
|
@ -143,7 +140,8 @@ class PrintViewManagerBase : public content::NotificationObserver,
|
||||||
// print settings are being loaded.
|
// print settings are being loaded.
|
||||||
bool inside_inner_message_loop_;
|
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.
|
// Set to true when OnDidPrintPage() should be expecting the first page.
|
||||||
bool expecting_first_page_;
|
bool expecting_first_page_;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -63,10 +63,9 @@ PrintingMessageFilter::PrintingMessageFilter(int render_process_id)
|
||||||
PrintingMessageFilter::~PrintingMessageFilter() {
|
PrintingMessageFilter::~PrintingMessageFilter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message,
|
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
|
||||||
bool* message_was_ok) {
|
|
||||||
bool handled = true;
|
bool handled = true;
|
||||||
IPC_BEGIN_MESSAGE_MAP_EX(PrintingMessageFilter, message, *message_was_ok)
|
IPC_BEGIN_MESSAGE_MAP(PrintingMessageFilter, message)
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
|
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -38,8 +38,7 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
|
||||||
explicit PrintingMessageFilter(int render_process_id);
|
explicit PrintingMessageFilter(int render_process_id);
|
||||||
|
|
||||||
// content::BrowserMessageFilter methods.
|
// content::BrowserMessageFilter methods.
|
||||||
virtual bool OnMessageReceived(const IPC::Message& message,
|
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
|
||||||
bool* message_was_ok) OVERRIDE;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual ~PrintingMessageFilter();
|
virtual ~PrintingMessageFilter();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue