OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/values.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/location.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/task_runner_util.h" |
| 17 #include "base/threading/worker_pool.h" |
| 18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
10 #include "components/cloud_devices/common/cloud_device_description.h" | 19 #include "components/cloud_devices/common/cloud_device_description.h" |
11 #include "components/cloud_devices/common/printer_description.h" | 20 #include "components/cloud_devices/common/printer_description.h" |
12 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 21 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
13 #include "printing/print_job_constants.h" | 22 #include "printing/pdf_render_settings.h" |
| 23 #include "printing/pwg_raster_settings.h" |
| 24 |
| 25 using local_discovery::PWGRasterConverter; |
14 | 26 |
15 namespace { | 27 namespace { |
16 | 28 |
17 const char kContentTypePdf[] = "application/pdf"; | 29 const char kContentTypePdf[] = "application/pdf"; |
| 30 const char kContentTypePWGRaster[] = "image/pwg-raster"; |
18 const char kContentTypeAll[] = "*/*"; | 31 const char kContentTypeAll[] = "*/*"; |
19 | 32 |
20 const char kInvalidDataPrintError[] = "INVALID_DATA"; | 33 const char kInvalidDataPrintError[] = "INVALID_DATA"; |
21 | 34 |
| 35 // Reads raster data from file path |raster_path| and returns it as |
| 36 // RefCountedMemory. Returns NULL on error. |
| 37 scoped_refptr<base::RefCountedMemory> ReadConvertedPWGRasterFileOnWorkerThread( |
| 38 const base::FilePath& raster_path) { |
| 39 int64 file_size; |
| 40 if (base::GetFileSize(raster_path, &file_size) && |
| 41 file_size <= extensions::PrinterProviderAPI::kMaxDocumentSize) { |
| 42 std::string data; |
| 43 data.reserve(file_size); |
| 44 |
| 45 if (base::ReadFileToString(raster_path, &data)) { |
| 46 return scoped_refptr<base::RefCountedMemory>( |
| 47 base::RefCountedString::TakeString(&data)); |
| 48 } |
| 49 } else { |
| 50 LOG(ERROR) << "Invalid raster file size."; |
| 51 } |
| 52 return scoped_refptr<base::RefCountedMemory>(); |
| 53 } |
| 54 |
| 55 // Posts a task to read a file containing converted PWG raster data to the |
| 56 // worker pool. |
| 57 void ReadConvertedPWGRasterFile( |
| 58 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback, |
| 59 bool success, |
| 60 const base::FilePath& pwg_file_path) { |
| 61 if (!success) { |
| 62 callback.Run(scoped_refptr<base::RefCountedMemory>()); |
| 63 return; |
| 64 } |
| 65 |
| 66 base::PostTaskAndReplyWithResult( |
| 67 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, |
| 68 base::Bind(&ReadConvertedPWGRasterFileOnWorkerThread, pwg_file_path), |
| 69 callback); |
| 70 } |
| 71 |
22 } // namespace | 72 } // namespace |
23 | 73 |
24 ExtensionPrinterHandler::ExtensionPrinterHandler( | 74 ExtensionPrinterHandler::ExtensionPrinterHandler( |
25 content::BrowserContext* browser_context) | 75 content::BrowserContext* browser_context) |
26 : browser_context_(browser_context), weak_ptr_factory_(this) { | 76 : browser_context_(browser_context), weak_ptr_factory_(this) { |
27 } | 77 } |
28 | 78 |
29 ExtensionPrinterHandler::~ExtensionPrinterHandler() { | 79 ExtensionPrinterHandler::~ExtensionPrinterHandler() { |
30 } | 80 } |
31 | 81 |
(...skipping 20 matching lines...) Expand all Loading... |
52 ->DispatchGetCapabilityRequested( | 102 ->DispatchGetCapabilityRequested( |
53 destination_id, | 103 destination_id, |
54 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, | 104 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, |
55 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); | 105 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); |
56 } | 106 } |
57 | 107 |
58 void ExtensionPrinterHandler::StartPrint( | 108 void ExtensionPrinterHandler::StartPrint( |
59 const std::string& destination_id, | 109 const std::string& destination_id, |
60 const std::string& capability, | 110 const std::string& capability, |
61 const std::string& ticket_json, | 111 const std::string& ticket_json, |
| 112 const gfx::Size& page_size, |
62 const scoped_refptr<base::RefCountedMemory>& print_data, | 113 const scoped_refptr<base::RefCountedMemory>& print_data, |
63 const PrinterHandler::PrintCallback& callback) { | 114 const PrinterHandler::PrintCallback& callback) { |
64 extensions::PrinterProviderAPI::PrintJob print_job; | 115 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job( |
65 print_job.printer_id = destination_id; | 116 new extensions::PrinterProviderAPI::PrintJob()); |
66 print_job.ticket_json = ticket_json; | 117 print_job->printer_id = destination_id; |
| 118 print_job->ticket_json = ticket_json; |
67 | 119 |
68 cloud_devices::CloudDeviceDescription printer_description; | 120 cloud_devices::CloudDeviceDescription printer_description; |
69 printer_description.InitFromString(capability); | 121 printer_description.InitFromString(capability); |
70 | 122 |
71 cloud_devices::printer::ContentTypesCapability content_types; | 123 cloud_devices::printer::ContentTypesCapability content_types; |
72 content_types.LoadFrom(printer_description); | 124 content_types.LoadFrom(printer_description); |
73 | 125 |
74 const bool kUsePdf = content_types.Contains(kContentTypePdf) || | 126 const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
75 content_types.Contains(kContentTypeAll); | 127 content_types.Contains(kContentTypeAll); |
76 | 128 |
77 if (!kUsePdf) { | 129 if (kUsePdf) { |
78 // TODO(tbarzic): Convert data to PWG raster if the printer does not support | 130 print_job->content_type = kContentTypePdf; |
79 // PDF. | 131 DispatchPrintJob(callback, print_job.Pass(), print_data); |
| 132 return; |
| 133 } |
| 134 |
| 135 print_job->content_type = kContentTypePWGRaster; |
| 136 ConvertToPWGRaster(print_data, printer_description, ticket_json, page_size, |
| 137 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, |
| 138 weak_ptr_factory_.GetWeakPtr(), callback, |
| 139 base::Passed(&print_job))); |
| 140 } |
| 141 |
| 142 void ExtensionPrinterHandler::ConvertToPWGRaster( |
| 143 const scoped_refptr<base::RefCountedMemory>& data, |
| 144 const cloud_devices::CloudDeviceDescription& printer_description, |
| 145 const std::string& ticket_json, |
| 146 const gfx::Size& page_size, |
| 147 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { |
| 148 cloud_devices::CloudDeviceDescription ticket; |
| 149 if (!ticket.InitFromString(ticket_json)) { |
| 150 callback.Run(scoped_refptr<base::RefCountedMemory>()); |
| 151 return; |
| 152 } |
| 153 |
| 154 if (!pwg_raster_converter_) { |
| 155 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); |
| 156 } |
| 157 pwg_raster_converter_->Start( |
| 158 data.get(), |
| 159 PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
| 160 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
| 161 base::Bind(&ReadConvertedPWGRasterFile, callback)); |
| 162 } |
| 163 |
| 164 void ExtensionPrinterHandler::DispatchPrintJob( |
| 165 const PrinterHandler::PrintCallback& callback, |
| 166 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, |
| 167 const scoped_refptr<base::RefCountedMemory>& print_data) { |
| 168 if (!print_data) { |
80 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 169 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
81 return; | 170 return; |
82 } | 171 } |
83 | 172 |
84 print_job.content_type = kContentTypePdf; | 173 print_job->document_bytes = print_data; |
85 print_job.document_bytes = print_data; | 174 |
86 extensions::PrinterProviderAPI::GetFactoryInstance() | 175 extensions::PrinterProviderAPI::GetFactoryInstance() |
87 ->Get(browser_context_) | 176 ->Get(browser_context_) |
88 ->DispatchPrintRequested( | 177 ->DispatchPrintRequested( |
89 print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 178 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
90 weak_ptr_factory_.GetWeakPtr(), callback)); | 179 weak_ptr_factory_.GetWeakPtr(), callback)); |
91 } | 180 } |
92 | 181 |
93 void ExtensionPrinterHandler::WrapGetPrintersCallback( | 182 void ExtensionPrinterHandler::WrapGetPrintersCallback( |
94 const PrinterHandler::GetPrintersCallback& callback, | 183 const PrinterHandler::GetPrintersCallback& callback, |
95 const base::ListValue& printers, | 184 const base::ListValue& printers, |
96 bool done) { | 185 bool done) { |
97 callback.Run(printers, done); | 186 callback.Run(printers, done); |
98 } | 187 } |
99 | 188 |
100 void ExtensionPrinterHandler::WrapGetCapabilityCallback( | 189 void ExtensionPrinterHandler::WrapGetCapabilityCallback( |
101 const PrinterHandler::GetCapabilityCallback& callback, | 190 const PrinterHandler::GetCapabilityCallback& callback, |
102 const std::string& destination_id, | 191 const std::string& destination_id, |
103 const base::DictionaryValue& capability) { | 192 const base::DictionaryValue& capability) { |
104 callback.Run(destination_id, capability); | 193 callback.Run(destination_id, capability); |
105 } | 194 } |
106 | 195 |
107 void ExtensionPrinterHandler::WrapPrintCallback( | 196 void ExtensionPrinterHandler::WrapPrintCallback( |
108 const PrinterHandler::PrintCallback& callback, | 197 const PrinterHandler::PrintCallback& callback, |
109 bool success, | 198 bool success, |
110 const std::string& status) { | 199 const std::string& status) { |
111 callback.Run(success, status); | 200 callback.Run(success, status); |
112 } | 201 } |
OLD | NEW |