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/single_thread_task_runner.h" | |
17 #include "base/thread_task_runner_handle.h" | |
18 #include "base/threading/worker_pool.h" | |
19 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | |
10 #include "components/cloud_devices/common/cloud_device_description.h" | 20 #include "components/cloud_devices/common/cloud_device_description.h" |
11 #include "components/cloud_devices/common/printer_description.h" | 21 #include "components/cloud_devices/common/printer_description.h" |
12 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 22 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
13 #include "printing/print_job_constants.h" | 23 #include "printing/pdf_render_settings.h" |
14 | 24 #include "printing/pwg_raster_settings.h" |
25 #include "printing/units.h" | |
26 #include "ui/gfx/geometry/rect.h" | |
27 #include "ui/gfx/geometry/size.h" | |
15 namespace { | 28 namespace { |
16 | 29 |
17 const char kContentTypePdf[] = "application/pdf"; | 30 const char kContentTypePdf[] = "application/pdf"; |
31 const char kContentTypePWGRaster[] = "image/pwg-raster"; | |
18 const char kContentTypeAll[] = "*/*"; | 32 const char kContentTypeAll[] = "*/*"; |
19 | 33 |
20 const char kInvalidDataPrintError[] = "INVALID_DATA"; | 34 const char kInvalidDataPrintError[] = "INVALID_DATA"; |
21 | 35 |
36 // Reads raster data from file path |raster_path| to a RefCountedMemory object, | |
37 // which is then passed to |callback| on |response_task_runner|. | |
38 void ReadConvertedPWGRasterFileOnWorkerThread( | |
39 const base::FilePath& raster_path, | |
40 const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner, | |
41 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { | |
42 scoped_refptr<base::RefCountedMemory> result; | |
43 int64 file_size; | |
44 if (base::GetFileSize(raster_path, &file_size) && | |
45 file_size <= extensions::PrinterProviderAPI::kMaxDocumentSize) { | |
46 scoped_ptr<std::string> data(new std::string()); | |
47 data->reserve(file_size); | |
48 | |
49 if (base::ReadFileToString(raster_path, data.get())) | |
50 result = base::RefCountedString::TakeString(data.release()); | |
51 } else { | |
52 LOG(ERROR) << "Invalid raster file size."; | |
53 } | |
54 | |
55 response_task_runner->PostTask(FROM_HERE, base::Bind(callback, result)); | |
56 } | |
57 | |
58 // Posts a task to read a file containing converted PWG raster data to the | |
59 // worker pool. | |
60 void ReadConvertedPWGRasterFile( | |
61 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback, | |
62 bool success, | |
63 const base::FilePath& pwg_file_path) { | |
64 if (!success) { | |
65 callback.Run(scoped_refptr<base::RefCountedMemory>()); | |
66 return; | |
67 } | |
68 | |
69 base::WorkerPool::GetTaskRunner(true)->PostTask( | |
Vitaly Buka (NO REVIEWS)
2015/02/17 19:43:54
PostTaskAndReplyWithResult is appropriate here
tbarzic
2015/02/18 02:55:49
Done.
| |
70 FROM_HERE, | |
71 base::Bind(&ReadConvertedPWGRasterFileOnWorkerThread, pwg_file_path, | |
72 base::ThreadTaskRunnerHandle::Get(), callback)); | |
73 } | |
74 | |
22 } // namespace | 75 } // namespace |
23 | 76 |
24 ExtensionPrinterHandler::ExtensionPrinterHandler( | 77 ExtensionPrinterHandler::ExtensionPrinterHandler( |
25 content::BrowserContext* browser_context) | 78 content::BrowserContext* browser_context) |
26 : browser_context_(browser_context), weak_ptr_factory_(this) { | 79 : browser_context_(browser_context), weak_ptr_factory_(this) { |
27 } | 80 } |
28 | 81 |
29 ExtensionPrinterHandler::~ExtensionPrinterHandler() { | 82 ExtensionPrinterHandler::~ExtensionPrinterHandler() { |
30 } | 83 } |
31 | 84 |
(...skipping 20 matching lines...) Expand all Loading... | |
52 ->DispatchGetCapabilityRequested( | 105 ->DispatchGetCapabilityRequested( |
53 destination_id, | 106 destination_id, |
54 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, | 107 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, |
55 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); | 108 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); |
56 } | 109 } |
57 | 110 |
58 void ExtensionPrinterHandler::StartPrint( | 111 void ExtensionPrinterHandler::StartPrint( |
59 const std::string& destination_id, | 112 const std::string& destination_id, |
60 const std::string& capability, | 113 const std::string& capability, |
61 const std::string& ticket_json, | 114 const std::string& ticket_json, |
115 const gfx::Size& size, | |
62 const scoped_refptr<base::RefCountedMemory>& print_data, | 116 const scoped_refptr<base::RefCountedMemory>& print_data, |
63 const PrinterHandler::PrintCallback& callback) { | 117 const PrinterHandler::PrintCallback& callback) { |
64 extensions::PrinterProviderAPI::PrintJob print_job; | 118 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job( |
65 print_job.printer_id = destination_id; | 119 new extensions::PrinterProviderAPI::PrintJob()); |
66 print_job.ticket_json = ticket_json; | 120 print_job->printer_id = destination_id; |
121 print_job->ticket_json = ticket_json; | |
67 | 122 |
68 cloud_devices::CloudDeviceDescription printer_description; | 123 cloud_devices::CloudDeviceDescription printer_description; |
69 printer_description.InitFromString(capability); | 124 printer_description.InitFromString(capability); |
70 | 125 |
71 cloud_devices::printer::ContentTypesCapability content_types; | 126 cloud_devices::printer::ContentTypesCapability content_types; |
72 content_types.LoadFrom(printer_description); | 127 content_types.LoadFrom(printer_description); |
73 | 128 |
74 const bool kUsePdf = content_types.Contains(kContentTypePdf) || | 129 const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
75 content_types.Contains(kContentTypeAll); | 130 content_types.Contains(kContentTypeAll); |
76 | 131 |
77 if (!kUsePdf) { | 132 if (kUsePdf) { |
78 // TODO(tbarzic): Convert data to PWG raster if the printer does not support | 133 print_job->content_type = kContentTypePdf; |
79 // PDF. | 134 DispatchPrintJob(callback, print_job.Pass(), print_data); |
135 return; | |
136 } | |
137 | |
138 print_job->content_type = kContentTypePWGRaster; | |
139 ConvertToPWGRaster(print_data, printer_description, size, | |
140 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, | |
141 weak_ptr_factory_.GetWeakPtr(), callback, | |
142 base::Passed(&print_job))); | |
143 } | |
144 | |
145 void ExtensionPrinterHandler::ConvertToPWGRaster( | |
146 const scoped_refptr<base::RefCountedMemory>& data, | |
147 const cloud_devices::CloudDeviceDescription& printer_description, | |
148 const gfx::Size& size, | |
Vitaly Buka (NO REVIEWS)
2015/02/17 19:43:54
looks like this code is very similar to privet ver
tbarzic
2015/02/18 02:55:49
Yeah, meant to do that before sending the cl for r
| |
149 const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { | |
150 | |
151 int dpi = printing::kDefaultPdfDpi; | |
152 cloud_devices::printer::DpiCapability dpis; | |
153 if (dpis.LoadFrom(printer_description)) | |
154 dpi = std::max(dpis.GetDefault().horizontal, dpis.GetDefault().vertical); | |
155 | |
156 double scale = dpi; | |
157 scale /= printing::kPointsPerInch; | |
158 | |
159 gfx::Rect area(std::min(size.width(), size.height()) * scale, | |
160 std::max(size.width(), size.height()) * scale); | |
161 | |
162 if (!pwg_raster_converter_) { | |
163 pwg_raster_converter_ = | |
164 local_discovery::PWGRasterConverter::CreateDefault(); | |
165 } | |
166 pwg_raster_converter_->Start( | |
167 data.get(), printing::PdfRenderSettings(area, dpi, true), | |
168 printing::PwgRasterSettings(), | |
169 base::Bind(&ReadConvertedPWGRasterFile, callback)); | |
170 | |
171 } | |
172 | |
173 void ExtensionPrinterHandler::DispatchPrintJob( | |
174 const PrinterHandler::PrintCallback& callback, | |
175 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, | |
176 const scoped_refptr<base::RefCountedMemory>& print_data) { | |
177 if (!print_data) { | |
80 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 178 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
81 return; | 179 return; |
82 } | 180 } |
83 | 181 |
84 print_job.content_type = kContentTypePdf; | 182 print_job->document_bytes = print_data; |
85 print_job.document_bytes = print_data; | 183 |
86 extensions::PrinterProviderAPI::GetFactoryInstance() | 184 extensions::PrinterProviderAPI::GetFactoryInstance() |
87 ->Get(browser_context_) | 185 ->Get(browser_context_) |
88 ->DispatchPrintRequested( | 186 ->DispatchPrintRequested( |
89 print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 187 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
90 weak_ptr_factory_.GetWeakPtr(), callback)); | 188 weak_ptr_factory_.GetWeakPtr(), callback)); |
91 } | 189 } |
92 | 190 |
93 void ExtensionPrinterHandler::WrapGetPrintersCallback( | 191 void ExtensionPrinterHandler::WrapGetPrintersCallback( |
94 const PrinterHandler::GetPrintersCallback& callback, | 192 const PrinterHandler::GetPrintersCallback& callback, |
95 const base::ListValue& printers, | 193 const base::ListValue& printers, |
96 bool done) { | 194 bool done) { |
97 callback.Run(printers, done); | 195 callback.Run(printers, done); |
98 } | 196 } |
99 | 197 |
100 void ExtensionPrinterHandler::WrapGetCapabilityCallback( | 198 void ExtensionPrinterHandler::WrapGetCapabilityCallback( |
101 const PrinterHandler::GetCapabilityCallback& callback, | 199 const PrinterHandler::GetCapabilityCallback& callback, |
102 const std::string& destination_id, | 200 const std::string& destination_id, |
103 const base::DictionaryValue& capability) { | 201 const base::DictionaryValue& capability) { |
104 callback.Run(destination_id, capability); | 202 callback.Run(destination_id, capability); |
105 } | 203 } |
106 | 204 |
107 void ExtensionPrinterHandler::WrapPrintCallback( | 205 void ExtensionPrinterHandler::WrapPrintCallback( |
108 const PrinterHandler::PrintCallback& callback, | 206 const PrinterHandler::PrintCallback& callback, |
109 bool success, | 207 bool success, |
110 const std::string& status) { | 208 const std::string& status) { |
111 callback.Run(success, status); | 209 callback.Run(success, status); |
112 } | 210 } |
OLD | NEW |