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> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/location.h" | 14 #include "base/location.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
16 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
17 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
18 #include "components/cloud_devices/common/cloud_device_description.h" | 19 #include "components/cloud_devices/common/cloud_device_description.h" |
19 #include "components/cloud_devices/common/printer_description.h" | 20 #include "components/cloud_devices/common/printer_description.h" |
20 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 21 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
21 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 22 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
22 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 23 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
23 #include "printing/pdf_render_settings.h" | 24 #include "printing/pdf_render_settings.h" |
24 #include "printing/pwg_raster_settings.h" | 25 #include "printing/pwg_raster_settings.h" |
25 | 26 |
26 using local_discovery::PWGRasterConverter; | 27 using local_discovery::PWGRasterConverter; |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 const char kContentTypePdf[] = "application/pdf"; | 31 const char kContentTypePdf[] = "application/pdf"; |
31 const char kContentTypePWGRaster[] = "image/pwg-raster"; | 32 const char kContentTypePWGRaster[] = "image/pwg-raster"; |
32 const char kContentTypeAll[] = "*/*"; | 33 const char kContentTypeAll[] = "*/*"; |
33 | 34 |
34 const char kInvalidDataPrintError[] = "INVALID_DATA"; | 35 const char kInvalidDataPrintError[] = "INVALID_DATA"; |
35 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; | 36 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; |
36 | 37 |
37 // Reads raster data from file path |raster_path| and returns it as | 38 // Updates |job| with raster file path, size and last modification time. |
38 // RefCountedMemory. Returns NULL on error. | 39 // Returns the updated print job. |
39 scoped_refptr<base::RefCountedMemory> ReadConvertedPWGRasterFileOnWorkerThread( | 40 scoped_ptr<extensions::PrinterProviderPrintJob> UpdateJobFileInfoOnWorkerThread( |
40 const base::FilePath& raster_path) { | 41 const base::FilePath& raster_path, |
41 int64 file_size; | 42 scoped_ptr<extensions::PrinterProviderPrintJob> job) { |
42 if (base::GetFileSize(raster_path, &file_size) && | 43 if (base::GetFileInfo(raster_path, &job->file_info)) |
43 file_size <= extensions::PrinterProviderAPI::kMaxDocumentSize) { | 44 job->document_path = raster_path; |
44 std::string data; | 45 return job.Pass(); |
45 data.reserve(file_size); | |
46 | |
47 if (base::ReadFileToString(raster_path, &data)) { | |
48 return scoped_refptr<base::RefCountedMemory>( | |
49 base::RefCountedString::TakeString(&data)); | |
50 } | |
51 } else { | |
52 LOG(ERROR) << "Invalid raster file size."; | |
53 } | |
54 return scoped_refptr<base::RefCountedMemory>(); | |
55 } | 46 } |
56 | 47 |
57 // Posts a task to read a file containing converted PWG raster data to the | 48 // Callback to PWG raster conversion. |
58 // worker pool. | 49 // Posts a task to update print job with info about file containing converted |
59 void ReadConvertedPWGRasterFile( | 50 // PWG raster data. The task is posted to |slow_task_runner|. |
| 51 void UpdateJobFileInfo( |
| 52 scoped_ptr<extensions::PrinterProviderPrintJob> job, |
60 const scoped_refptr<base::TaskRunner>& slow_task_runner, | 53 const scoped_refptr<base::TaskRunner>& slow_task_runner, |
61 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback, | 54 const ExtensionPrinterHandler::PrintJobCallback& callback, |
62 bool success, | 55 bool success, |
63 const base::FilePath& pwg_file_path) { | 56 const base::FilePath& pwg_file_path) { |
64 if (!success) { | 57 if (!success) { |
65 callback.Run(scoped_refptr<base::RefCountedMemory>()); | 58 callback.Run(job.Pass()); |
66 return; | 59 return; |
67 } | 60 } |
68 | 61 |
69 base::PostTaskAndReplyWithResult( | 62 base::PostTaskAndReplyWithResult( |
70 slow_task_runner.get(), FROM_HERE, | 63 slow_task_runner.get(), FROM_HERE, |
71 base::Bind(&ReadConvertedPWGRasterFileOnWorkerThread, pwg_file_path), | 64 base::Bind(&UpdateJobFileInfoOnWorkerThread, pwg_file_path, |
| 65 base::Passed(&job)), |
72 callback); | 66 callback); |
73 } | 67 } |
74 | 68 |
75 } // namespace | 69 } // namespace |
76 | 70 |
77 ExtensionPrinterHandler::ExtensionPrinterHandler( | 71 ExtensionPrinterHandler::ExtensionPrinterHandler( |
78 content::BrowserContext* browser_context, | 72 content::BrowserContext* browser_context, |
79 const scoped_refptr<base::TaskRunner>& slow_task_runner) | 73 const scoped_refptr<base::TaskRunner>& slow_task_runner) |
80 : browser_context_(browser_context), | 74 : browser_context_(browser_context), |
81 slow_task_runner_(slow_task_runner), | 75 slow_task_runner_(slow_task_runner), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 cloud_devices::CloudDeviceDescription printer_description; | 120 cloud_devices::CloudDeviceDescription printer_description; |
127 printer_description.InitFromString(capability); | 121 printer_description.InitFromString(capability); |
128 | 122 |
129 cloud_devices::printer::ContentTypesCapability content_types; | 123 cloud_devices::printer::ContentTypesCapability content_types; |
130 content_types.LoadFrom(printer_description); | 124 content_types.LoadFrom(printer_description); |
131 | 125 |
132 const bool kUsePdf = content_types.Contains(kContentTypePdf) || | 126 const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
133 content_types.Contains(kContentTypeAll); | 127 content_types.Contains(kContentTypeAll); |
134 | 128 |
135 if (kUsePdf) { | 129 if (kUsePdf) { |
| 130 // TODO(tbarzic): Consider writing larger PDF to disk and provide the data |
| 131 // the same way as it's done with PWG raster. |
136 print_job->content_type = kContentTypePdf; | 132 print_job->content_type = kContentTypePdf; |
137 DispatchPrintJob(callback, print_job.Pass(), print_data); | 133 print_job->document_bytes = print_data; |
| 134 DispatchPrintJob(callback, print_job.Pass()); |
138 return; | 135 return; |
139 } | 136 } |
140 | 137 |
141 cloud_devices::CloudDeviceDescription ticket; | 138 cloud_devices::CloudDeviceDescription ticket; |
142 if (!ticket.InitFromString(ticket_json)) { | 139 if (!ticket.InitFromString(ticket_json)) { |
143 WrapPrintCallback(callback, false, kInvalidTicketPrintError); | 140 WrapPrintCallback(callback, false, kInvalidTicketPrintError); |
144 return; | 141 return; |
145 } | 142 } |
146 | 143 |
147 print_job->content_type = kContentTypePWGRaster; | 144 print_job->content_type = kContentTypePWGRaster; |
148 ConvertToPWGRaster(print_data, printer_description, ticket, page_size, | 145 ConvertToPWGRaster(print_data, printer_description, ticket, page_size, |
| 146 print_job.Pass(), |
149 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, | 147 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, |
150 weak_ptr_factory_.GetWeakPtr(), callback, | 148 weak_ptr_factory_.GetWeakPtr(), callback)); |
151 base::Passed(&print_job))); | |
152 } | 149 } |
153 | 150 |
154 void ExtensionPrinterHandler::SetPwgRasterConverterForTesting( | 151 void ExtensionPrinterHandler::SetPwgRasterConverterForTesting( |
155 scoped_ptr<local_discovery::PWGRasterConverter> pwg_raster_converter) { | 152 scoped_ptr<local_discovery::PWGRasterConverter> pwg_raster_converter) { |
156 pwg_raster_converter_ = pwg_raster_converter.Pass(); | 153 pwg_raster_converter_ = pwg_raster_converter.Pass(); |
157 } | 154 } |
158 | 155 |
159 void ExtensionPrinterHandler::ConvertToPWGRaster( | 156 void ExtensionPrinterHandler::ConvertToPWGRaster( |
160 const scoped_refptr<base::RefCountedMemory>& data, | 157 const scoped_refptr<base::RefCountedMemory>& data, |
161 const cloud_devices::CloudDeviceDescription& printer_description, | 158 const cloud_devices::CloudDeviceDescription& printer_description, |
162 const cloud_devices::CloudDeviceDescription& ticket, | 159 const cloud_devices::CloudDeviceDescription& ticket, |
163 const gfx::Size& page_size, | 160 const gfx::Size& page_size, |
164 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { | 161 scoped_ptr<extensions::PrinterProviderPrintJob> job, |
| 162 const ExtensionPrinterHandler::PrintJobCallback& callback) { |
165 if (!pwg_raster_converter_) { | 163 if (!pwg_raster_converter_) { |
166 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); | 164 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); |
167 } | 165 } |
168 pwg_raster_converter_->Start( | 166 pwg_raster_converter_->Start( |
169 data.get(), | 167 data.get(), |
170 PWGRasterConverter::GetConversionSettings(printer_description, page_size), | 168 PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
171 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), | 169 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
172 base::Bind(&ReadConvertedPWGRasterFile, slow_task_runner_, callback)); | 170 base::Bind(&UpdateJobFileInfo, base::Passed(&job), slow_task_runner_, |
| 171 callback)); |
173 } | 172 } |
174 | 173 |
175 void ExtensionPrinterHandler::DispatchPrintJob( | 174 void ExtensionPrinterHandler::DispatchPrintJob( |
176 const PrinterHandler::PrintCallback& callback, | 175 const PrinterHandler::PrintCallback& callback, |
177 scoped_ptr<extensions::PrinterProviderPrintJob> print_job, | 176 scoped_ptr<extensions::PrinterProviderPrintJob> print_job) { |
178 const scoped_refptr<base::RefCountedMemory>& print_data) { | 177 if (print_job->document_path.empty() && !print_job->document_bytes) { |
179 if (!print_data) { | |
180 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 178 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
181 return; | 179 return; |
182 } | 180 } |
183 | 181 |
184 print_job->document_bytes = print_data; | |
185 | |
186 extensions::PrinterProviderAPIFactory::GetInstance() | 182 extensions::PrinterProviderAPIFactory::GetInstance() |
187 ->GetForBrowserContext(browser_context_) | 183 ->GetForBrowserContext(browser_context_) |
188 ->DispatchPrintRequested( | 184 ->DispatchPrintRequested( |
189 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 185 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
190 weak_ptr_factory_.GetWeakPtr(), callback)); | 186 weak_ptr_factory_.GetWeakPtr(), callback)); |
191 } | 187 } |
192 | 188 |
193 void ExtensionPrinterHandler::WrapGetPrintersCallback( | 189 void ExtensionPrinterHandler::WrapGetPrintersCallback( |
194 const PrinterHandler::GetPrintersCallback& callback, | 190 const PrinterHandler::GetPrintersCallback& callback, |
195 const base::ListValue& printers, | 191 const base::ListValue& printers, |
196 bool done) { | 192 bool done) { |
197 callback.Run(printers, done); | 193 callback.Run(printers, done); |
198 } | 194 } |
199 | 195 |
200 void ExtensionPrinterHandler::WrapGetCapabilityCallback( | 196 void ExtensionPrinterHandler::WrapGetCapabilityCallback( |
201 const PrinterHandler::GetCapabilityCallback& callback, | 197 const PrinterHandler::GetCapabilityCallback& callback, |
202 const std::string& destination_id, | 198 const std::string& destination_id, |
203 const base::DictionaryValue& capability) { | 199 const base::DictionaryValue& capability) { |
204 callback.Run(destination_id, capability); | 200 callback.Run(destination_id, capability); |
205 } | 201 } |
206 | 202 |
207 void ExtensionPrinterHandler::WrapPrintCallback( | 203 void ExtensionPrinterHandler::WrapPrintCallback( |
208 const PrinterHandler::PrintCallback& callback, | 204 const PrinterHandler::PrintCallback& callback, |
209 bool success, | 205 bool success, |
210 const std::string& status) { | 206 const std::string& status) { |
211 callback.Run(success, status); | 207 callback.Run(success, status); |
212 } | 208 } |
OLD | NEW |