Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Unified Diff: chrome/utility/printing_handler.cc

Issue 2477283002: Convert printing IPCs to Mojo
Patch Set: Fix more Windows compile errors Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/utility/printing_handler.cc
diff --git a/chrome/utility/printing_handler.cc b/chrome/utility/printing_handler.cc
index 7dbae1a132df5b38ba8e615c28545bf39148fbd2..71974aa2128e8c47ee784f861eacb94e28a2c0ad 100644
--- a/chrome/utility/printing_handler.cc
+++ b/chrome/utility/printing_handler.cc
@@ -10,10 +10,12 @@
#include "base/files/file_util.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
-#include "chrome/common/chrome_utility_printing_messages.h"
#include "chrome/utility/cloud_print/bitmap_image.h"
#include "chrome/utility/cloud_print/pwg_encoder.h"
#include "content/public/utility/utility_thread.h"
+#include "mojo/public/cpp/bindings/message.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+#include "mojo/public/cpp/system/platform_handle.h"
#include "pdf/pdf.h"
#include "printing/features/features.h"
#include "printing/page_range.h"
@@ -33,104 +35,114 @@ namespace printing {
namespace {
-bool Send(IPC::Message* message) {
- return content::UtilityThread::Get()->Send(message);
-}
-
void ReleaseProcessIfNeeded() {
content::UtilityThread::Get()->ReleaseProcessIfNeeded();
}
+static mojom::FontPreCaching* g_font_pre_caching;
+
#if defined(OS_WIN)
void PreCacheFontCharacters(const LOGFONT* logfont,
const wchar_t* text,
size_t text_length) {
- Send(new ChromeUtilityHostMsg_PreCacheFontCharacters(
- *logfont, base::string16(text, text_length)));
+ if (g_font_pre_caching) {
+ g_font_pre_caching->PreCacheFontCharacters(
+ *logfont, base::string16(text, text_length));
+ }
}
#endif
} // namespace
-PrintingHandler::PrintingHandler() {
+PrintingHandler::PrintingHandler(mojom::FontPreCachingPtr font_pre_caching)
+ : font_pre_caching_(std::move(font_pre_caching)) {
+ g_font_pre_caching = font_pre_caching_.get();
#if defined(OS_WIN)
chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters);
#endif
}
-PrintingHandler::~PrintingHandler() {}
-
-bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
- bool handled = true;
- IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message)
+PrintingHandler::~PrintingHandler() {
#if defined(OS_WIN)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
- OnRenderPDFPagesToMetafile)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
- OnRenderPDFPagesToMetafileGetPage)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
- OnRenderPDFPagesToMetafileStop)
-#endif // OS_WIN
-#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster,
- OnRenderPDFPagesToPWGRaster)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults,
- OnGetPrinterCapsAndDefaults)
- IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults,
- OnGetPrinterSemanticCapsAndDefaults)
-#endif // ENABLE_PRINT_PREVIEW
- IPC_MESSAGE_UNHANDLED(handled = false)
- IPC_END_MESSAGE_MAP()
- return handled;
+ chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(nullptr);
+#endif
+ g_font_pre_caching = nullptr;
}
-#if defined(OS_WIN)
-void PrintingHandler::OnRenderPDFPagesToMetafile(
- IPC::PlatformFileForTransit pdf_transit,
+void PrintingHandler::PrintingHandlerFactory::MakePrinting(
+ mojom::PrintingRequest request,
+ mojom::FontPreCachingPtr font_pre_caching) {
+ auto impl = base::MakeUnique<PrintingHandler>(std::move(font_pre_caching));
+ base::Closure error_handler = base::Bind(&PrintingHandler::OnConnectionClosed,
+ base::Unretained(impl.get()));
+ auto binding = mojo::MakeStrongBinding(std::move(impl), std::move(request));
+ binding->set_connection_error_handler(error_handler);
+}
+
+// static
+void PrintingHandler::Create(mojom::PrintingFactoryRequest request) {
+ mojo::MakeStrongBinding(base::MakeUnique<PrintingHandlerFactory>(),
+ std::move(request));
+}
+
+// TODO(crbug.com/676224): Conditionally define some messages instead of
+// rejecting them at runtime.
+void PrintingHandler::RenderPDFPagesToMetafiles(
+ base::File pdf_file,
const PdfRenderSettings& settings,
- bool print_text_with_gdi) {
+ bool print_text_with_gdi,
+ const RenderPDFPagesToMetafilesCallback& callback) {
+#if defined(OS_WIN)
pdf_rendering_settings_ = settings;
chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi);
- base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit);
int page_count = LoadPDF(std::move(pdf_file));
- Send(
- new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count));
+ callback.Run(page_count);
+#else
+ mojo::ReportBadMessage("RenderPDFPagesToMetafiles is Windows only");
+#endif
}
-void PrintingHandler::OnRenderPDFPagesToMetafileGetPage(
+void PrintingHandler::RenderPDFPagesToMetafilesGetPage(
int page_number,
- IPC::PlatformFileForTransit output_file) {
- base::File emf_file = IPC::PlatformFileForTransitToFile(output_file);
+ base::File emf_file,
+ const RenderPDFPagesToMetafilesGetPageCallback& callback) {
+#if defined(OS_WIN)
float scale_factor = 1.0f;
bool success =
RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor);
- Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone(
- success, scale_factor));
+ callback.Run(success, scale_factor);
+#else
+ mojo::ReportBadMessage("RenderPDFPagesToMetafilesGetPage is Windows only");
+#endif
}
-void PrintingHandler::OnRenderPDFPagesToMetafileStop() {
+void PrintingHandler::OnConnectionClosed() {
+#if defined(OS_WIN)
ReleaseProcessIfNeeded();
+#else
+ NOTREACHED();
+#endif
}
-#endif // OS_WIN
-
-#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
-void PrintingHandler::OnRenderPDFPagesToPWGRaster(
- IPC::PlatformFileForTransit pdf_transit,
+void PrintingHandler::RenderPDFPagesToPWGRaster(
+ base::File pdf,
const PdfRenderSettings& settings,
const PwgRasterSettings& bitmap_settings,
- IPC::PlatformFileForTransit bitmap_transit) {
- base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit);
- base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit);
+ base::File bitmap,
+ const RenderPDFPagesToPWGRasterCallback& callback) {
+#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings,
std::move(bitmap))) {
- Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded());
+ callback.Run(true);
} else {
- Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed());
+ callback.Run(false);
}
ReleaseProcessIfNeeded();
-}
+#else
+ mojo::ReportBadMessage(
+ "RenderPDFPagesToPWGRaster requires print preview to be enabled");
#endif // ENABLE_PRINT_PREVIEW
+}
#if defined(OS_WIN)
int PrintingHandler::LoadPDF(base::File pdf_file) {
@@ -270,9 +282,12 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
}
return true;
}
+#endif // ENABLE_PRINT_PREVIEW
-void PrintingHandler::OnGetPrinterCapsAndDefaults(
- const std::string& printer_name) {
+void PrintingHandler::GetPrinterCapsAndDefaults(
+ const std::string& printer_name,
+ const GetPrinterCapsAndDefaultsCallback& callback) {
+#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(nullptr);
PrinterCapsAndDefaults printer_info;
@@ -281,17 +296,21 @@ void PrintingHandler::OnGetPrinterCapsAndDefaults(
print_backend->GetPrinterDriverInfo(printer_name));
if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) {
- Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded(
- printer_name, printer_info));
+ callback.Run(printer_info);
} else {
- Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed(
- printer_name));
+ callback.Run(base::nullopt);
}
ReleaseProcessIfNeeded();
+#else
+ mojo::ReportBadMessage(
+ "GetPrinterCapsAndDefaults requires print preview to be enabled");
+#endif // ENABLE_PRINT_PREVIEW
}
-void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults(
- const std::string& printer_name) {
+void PrintingHandler::GetPrinterSemanticCapsAndDefaults(
+ const std::string& printer_name,
+ const GetPrinterSemanticCapsAndDefaultsCallback& callback) {
+#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(nullptr);
PrinterSemanticCapsAndDefaults printer_info;
@@ -301,14 +320,15 @@ void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults(
if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name,
&printer_info)) {
- Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded(
- printer_name, printer_info));
+ callback.Run(printer_info);
} else {
- Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed(
- printer_name));
+ callback.Run(base::nullopt);
}
ReleaseProcessIfNeeded();
-}
+#else
+ mojo::ReportBadMessage(
+ "GetPrinterSemanticCapsAndDefaults requires print preview to be enabled");
#endif // ENABLE_PRINT_PREVIEW
+}
} // namespace printing

Powered by Google App Engine
This is Rietveld 408576698