| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 15 #include "base/location.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
| 17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 56 |
| 56 const char kProvisionalUsbLabel[] = "provisional-usb"; | 57 const char kProvisionalUsbLabel[] = "provisional-usb"; |
| 57 | 58 |
| 58 // Updates |job| with raster file path, size and last modification time. | 59 // Updates |job| with raster file path, size and last modification time. |
| 59 // Returns the updated print job. | 60 // Returns the updated print job. |
| 60 scoped_ptr<extensions::PrinterProviderPrintJob> UpdateJobFileInfoOnWorkerThread( | 61 scoped_ptr<extensions::PrinterProviderPrintJob> UpdateJobFileInfoOnWorkerThread( |
| 61 const base::FilePath& raster_path, | 62 const base::FilePath& raster_path, |
| 62 scoped_ptr<extensions::PrinterProviderPrintJob> job) { | 63 scoped_ptr<extensions::PrinterProviderPrintJob> job) { |
| 63 if (base::GetFileInfo(raster_path, &job->file_info)) | 64 if (base::GetFileInfo(raster_path, &job->file_info)) |
| 64 job->document_path = raster_path; | 65 job->document_path = raster_path; |
| 65 return job.Pass(); | 66 return job; |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Callback to PWG raster conversion. | 69 // Callback to PWG raster conversion. |
| 69 // Posts a task to update print job with info about file containing converted | 70 // Posts a task to update print job with info about file containing converted |
| 70 // PWG raster data. The task is posted to |slow_task_runner|. | 71 // PWG raster data. The task is posted to |slow_task_runner|. |
| 71 void UpdateJobFileInfo( | 72 void UpdateJobFileInfo( |
| 72 scoped_ptr<extensions::PrinterProviderPrintJob> job, | 73 scoped_ptr<extensions::PrinterProviderPrintJob> job, |
| 73 const scoped_refptr<base::TaskRunner>& slow_task_runner, | 74 const scoped_refptr<base::TaskRunner>& slow_task_runner, |
| 74 const ExtensionPrinterHandler::PrintJobCallback& callback, | 75 const ExtensionPrinterHandler::PrintJobCallback& callback, |
| 75 bool success, | 76 bool success, |
| 76 const base::FilePath& pwg_file_path) { | 77 const base::FilePath& pwg_file_path) { |
| 77 if (!success) { | 78 if (!success) { |
| 78 callback.Run(job.Pass()); | 79 callback.Run(std::move(job)); |
| 79 return; | 80 return; |
| 80 } | 81 } |
| 81 | 82 |
| 82 base::PostTaskAndReplyWithResult( | 83 base::PostTaskAndReplyWithResult( |
| 83 slow_task_runner.get(), FROM_HERE, | 84 slow_task_runner.get(), FROM_HERE, |
| 84 base::Bind(&UpdateJobFileInfoOnWorkerThread, pwg_file_path, | 85 base::Bind(&UpdateJobFileInfoOnWorkerThread, pwg_file_path, |
| 85 base::Passed(&job)), | 86 base::Passed(&job)), |
| 86 callback); | 87 callback); |
| 87 } | 88 } |
| 88 | 89 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 content_types.LoadFrom(printer_description); | 201 content_types.LoadFrom(printer_description); |
| 201 | 202 |
| 202 const bool kUsePdf = content_types.Contains(kContentTypePdf) || | 203 const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
| 203 content_types.Contains(kContentTypeAll); | 204 content_types.Contains(kContentTypeAll); |
| 204 | 205 |
| 205 if (kUsePdf) { | 206 if (kUsePdf) { |
| 206 // TODO(tbarzic): Consider writing larger PDF to disk and provide the data | 207 // TODO(tbarzic): Consider writing larger PDF to disk and provide the data |
| 207 // the same way as it's done with PWG raster. | 208 // the same way as it's done with PWG raster. |
| 208 print_job->content_type = kContentTypePdf; | 209 print_job->content_type = kContentTypePdf; |
| 209 print_job->document_bytes = print_data; | 210 print_job->document_bytes = print_data; |
| 210 DispatchPrintJob(callback, print_job.Pass()); | 211 DispatchPrintJob(callback, std::move(print_job)); |
| 211 return; | 212 return; |
| 212 } | 213 } |
| 213 | 214 |
| 214 cloud_devices::CloudDeviceDescription ticket; | 215 cloud_devices::CloudDeviceDescription ticket; |
| 215 if (!ticket.InitFromString(ticket_json)) { | 216 if (!ticket.InitFromString(ticket_json)) { |
| 216 WrapPrintCallback(callback, false, kInvalidTicketPrintError); | 217 WrapPrintCallback(callback, false, kInvalidTicketPrintError); |
| 217 return; | 218 return; |
| 218 } | 219 } |
| 219 | 220 |
| 220 print_job->content_type = kContentTypePWGRaster; | 221 print_job->content_type = kContentTypePWGRaster; |
| 221 ConvertToPWGRaster(print_data, printer_description, ticket, page_size, | 222 ConvertToPWGRaster(print_data, printer_description, ticket, page_size, |
| 222 print_job.Pass(), | 223 std::move(print_job), |
| 223 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, | 224 base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, |
| 224 weak_ptr_factory_.GetWeakPtr(), callback)); | 225 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 225 } | 226 } |
| 226 | 227 |
| 227 void ExtensionPrinterHandler::StartGrantPrinterAccess( | 228 void ExtensionPrinterHandler::StartGrantPrinterAccess( |
| 228 const std::string& printer_id, | 229 const std::string& printer_id, |
| 229 const PrinterHandler::GetPrinterInfoCallback& callback) { | 230 const PrinterHandler::GetPrinterInfoCallback& callback) { |
| 230 std::string extension_id; | 231 std::string extension_id; |
| 231 std::string device_guid; | 232 std::string device_guid; |
| 232 if (!ParseProvisionalUsbPrinterId(printer_id, &extension_id, &device_guid)) { | 233 if (!ParseProvisionalUsbPrinterId(printer_id, &extension_id, &device_guid)) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 248 extensions::PrinterProviderAPIFactory::GetInstance() | 249 extensions::PrinterProviderAPIFactory::GetInstance() |
| 249 ->GetForBrowserContext(browser_context_) | 250 ->GetForBrowserContext(browser_context_) |
| 250 ->DispatchGetUsbPrinterInfoRequested( | 251 ->DispatchGetUsbPrinterInfoRequested( |
| 251 extension_id, device, | 252 extension_id, device, |
| 252 base::Bind(&ExtensionPrinterHandler::WrapGetPrinterInfoCallback, | 253 base::Bind(&ExtensionPrinterHandler::WrapGetPrinterInfoCallback, |
| 253 weak_ptr_factory_.GetWeakPtr(), callback)); | 254 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void ExtensionPrinterHandler::SetPwgRasterConverterForTesting( | 257 void ExtensionPrinterHandler::SetPwgRasterConverterForTesting( |
| 257 scoped_ptr<local_discovery::PWGRasterConverter> pwg_raster_converter) { | 258 scoped_ptr<local_discovery::PWGRasterConverter> pwg_raster_converter) { |
| 258 pwg_raster_converter_ = pwg_raster_converter.Pass(); | 259 pwg_raster_converter_ = std::move(pwg_raster_converter); |
| 259 } | 260 } |
| 260 | 261 |
| 261 void ExtensionPrinterHandler::ConvertToPWGRaster( | 262 void ExtensionPrinterHandler::ConvertToPWGRaster( |
| 262 const scoped_refptr<base::RefCountedMemory>& data, | 263 const scoped_refptr<base::RefCountedMemory>& data, |
| 263 const cloud_devices::CloudDeviceDescription& printer_description, | 264 const cloud_devices::CloudDeviceDescription& printer_description, |
| 264 const cloud_devices::CloudDeviceDescription& ticket, | 265 const cloud_devices::CloudDeviceDescription& ticket, |
| 265 const gfx::Size& page_size, | 266 const gfx::Size& page_size, |
| 266 scoped_ptr<extensions::PrinterProviderPrintJob> job, | 267 scoped_ptr<extensions::PrinterProviderPrintJob> job, |
| 267 const ExtensionPrinterHandler::PrintJobCallback& callback) { | 268 const ExtensionPrinterHandler::PrintJobCallback& callback) { |
| 268 if (!pwg_raster_converter_) { | 269 if (!pwg_raster_converter_) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 .Set("extensionName", extension->name()) | 365 .Set("extensionName", extension->name()) |
| 365 .Set("provisional", true))); | 366 .Set("provisional", true))); |
| 366 } | 367 } |
| 367 } | 368 } |
| 368 } | 369 } |
| 369 | 370 |
| 370 DCHECK_GT(pending_enumeration_count_, 0); | 371 DCHECK_GT(pending_enumeration_count_, 0); |
| 371 pending_enumeration_count_--; | 372 pending_enumeration_count_--; |
| 372 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); | 373 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); |
| 373 } | 374 } |
| OLD | NEW |