Index: chrome/browser/ui/webui/print_preview/extension_printer_handler.cc |
diff --git a/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc b/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc |
index 4f032c87704de7027e234e2b21efe1641a1b0b95..3dd2209ee38a5a9e4485b6a51f48495aaf55c428 100644 |
--- a/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc |
+++ b/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc |
@@ -4,21 +4,69 @@ |
#include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
+#include <algorithm> |
+ |
#include "base/bind.h" |
#include "base/callback.h" |
-#include "base/values.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/location.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/threading/worker_pool.h" |
+#include "chrome/browser/local_discovery/pwg_raster_converter.h" |
#include "components/cloud_devices/common/cloud_device_description.h" |
#include "components/cloud_devices/common/printer_description.h" |
#include "extensions/browser/api/printer_provider/printer_provider_api.h" |
-#include "printing/print_job_constants.h" |
+#include "printing/pdf_render_settings.h" |
+#include "printing/pwg_raster_settings.h" |
+ |
+using local_discovery::PWGRasterConverter; |
namespace { |
const char kContentTypePdf[] = "application/pdf"; |
+const char kContentTypePWGRaster[] = "image/pwg-raster"; |
const char kContentTypeAll[] = "*/*"; |
const char kInvalidDataPrintError[] = "INVALID_DATA"; |
+// Reads raster data from file path |raster_path| to a RefCountedMemory object, |
+// |result|. |
+void ReadConvertedPWGRasterFileOnWorkerThread( |
+ const base::FilePath& raster_path, |
+ const scoped_refptr<base::RefCountedString>& result) { |
+ int64 file_size; |
+ if (base::GetFileSize(raster_path, &file_size) && |
+ file_size <= extensions::PrinterProviderAPI::kMaxDocumentSize) { |
+ std::string data; |
+ data.reserve(file_size); |
+ |
+ if (base::ReadFileToString(raster_path, &data)) |
+ result->data().swap(data); |
+ } else { |
+ LOG(ERROR) << "Invalid raster file size."; |
+ } |
+} |
+ |
+// Posts a task to read a file containing converted PWG raster data to the |
+// worker pool. |
+void ReadConvertedPWGRasterFile( |
+ const ExtensionPrinterHandler::RefCountedMemoryCallback& callback, |
+ bool success, |
+ const base::FilePath& pwg_file_path) { |
+ if (!success) { |
+ callback.Run(scoped_refptr<base::RefCountedMemory>()); |
+ return; |
+ } |
+ |
+ scoped_refptr<base::RefCountedString> data(new base::RefCountedString); |
+ base::WorkerPool::GetTaskRunner(true)->PostTaskAndReply( |
Vitaly Buka (NO REVIEWS)
2015/02/18 20:00:57
I thought about
scoped_refptr<base::RefCountedStr
tbarzic
2015/02/18 20:37:03
D'oh, I forgot we have that method :/
tbarzic
2015/02/18 20:37:34
Done.
|
+ FROM_HERE, base::Bind(&ReadConvertedPWGRasterFileOnWorkerThread, |
+ pwg_file_path, data), |
+ base::Bind(callback, data)); |
+} |
+ |
} // namespace |
ExtensionPrinterHandler::ExtensionPrinterHandler( |
@@ -59,11 +107,13 @@ void ExtensionPrinterHandler::StartPrint( |
const std::string& destination_id, |
const std::string& capability, |
const std::string& ticket_json, |
+ const gfx::Size& page_size, |
const scoped_refptr<base::RefCountedMemory>& print_data, |
const PrinterHandler::PrintCallback& callback) { |
- extensions::PrinterProviderAPI::PrintJob print_job; |
- print_job.printer_id = destination_id; |
- print_job.ticket_json = ticket_json; |
+ scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job( |
+ new extensions::PrinterProviderAPI::PrintJob()); |
+ print_job->printer_id = destination_id; |
+ print_job->ticket_json = ticket_json; |
cloud_devices::CloudDeviceDescription printer_description; |
printer_description.InitFromString(capability); |
@@ -74,20 +124,57 @@ void ExtensionPrinterHandler::StartPrint( |
const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
content_types.Contains(kContentTypeAll); |
- if (!kUsePdf) { |
- // TODO(tbarzic): Convert data to PWG raster if the printer does not support |
- // PDF. |
+ if (kUsePdf) { |
+ print_job->content_type = kContentTypePdf; |
+ DispatchPrintJob(callback, print_job.Pass(), print_data); |
+ return; |
+ } |
+ |
+ print_job->content_type = kContentTypePWGRaster; |
+ ConvertToPWGRaster(print_data, printer_description, ticket_json, page_size, |
+ base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, |
+ weak_ptr_factory_.GetWeakPtr(), callback, |
+ base::Passed(&print_job))); |
+} |
+ |
+void ExtensionPrinterHandler::ConvertToPWGRaster( |
+ const scoped_refptr<base::RefCountedMemory>& data, |
+ const cloud_devices::CloudDeviceDescription& printer_description, |
+ const std::string& ticket_json, |
+ const gfx::Size& page_size, |
+ const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { |
+ cloud_devices::CloudDeviceDescription ticket; |
+ if (!ticket.InitFromString(ticket_json)) { |
+ callback.Run(scoped_refptr<base::RefCountedMemory>()); |
+ return; |
+ } |
+ |
+ if (!pwg_raster_converter_) { |
+ pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); |
+ } |
+ pwg_raster_converter_->Start( |
+ data.get(), |
+ PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
+ PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
+ base::Bind(&ReadConvertedPWGRasterFile, callback)); |
+} |
+ |
+void ExtensionPrinterHandler::DispatchPrintJob( |
+ const PrinterHandler::PrintCallback& callback, |
+ scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, |
+ const scoped_refptr<base::RefCountedMemory>& print_data) { |
+ if (!print_data || print_data->size() == 0u) { |
WrapPrintCallback(callback, false, kInvalidDataPrintError); |
return; |
} |
- print_job.content_type = kContentTypePdf; |
- print_job.document_bytes = print_data; |
+ print_job->document_bytes = print_data; |
+ |
extensions::PrinterProviderAPI::GetFactoryInstance() |
->Get(browser_context_) |
->DispatchPrintRequested( |
- print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
- weak_ptr_factory_.GetWeakPtr(), callback)); |
+ *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
} |
void ExtensionPrinterHandler::WrapGetPrintersCallback( |