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