| 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 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 const char kContentTypePWGRaster[] = "image/pwg-raster"; | 51 const char kContentTypePWGRaster[] = "image/pwg-raster"; |
| 52 const char kContentTypeAll[] = "*/*"; | 52 const char kContentTypeAll[] = "*/*"; |
| 53 | 53 |
| 54 const char kInvalidDataPrintError[] = "INVALID_DATA"; | 54 const char kInvalidDataPrintError[] = "INVALID_DATA"; |
| 55 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; | 55 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; |
| 56 | 56 |
| 57 const char kProvisionalUsbLabel[] = "provisional-usb"; | 57 const char kProvisionalUsbLabel[] = "provisional-usb"; |
| 58 | 58 |
| 59 // Updates |job| with raster file path, size and last modification time. | 59 // Updates |job| with raster file path, size and last modification time. |
| 60 // Returns the updated print job. | 60 // Returns the updated print job. |
| 61 scoped_ptr<extensions::PrinterProviderPrintJob> UpdateJobFileInfoOnWorkerThread( | 61 std::unique_ptr<extensions::PrinterProviderPrintJob> |
| 62 UpdateJobFileInfoOnWorkerThread( |
| 62 const base::FilePath& raster_path, | 63 const base::FilePath& raster_path, |
| 63 scoped_ptr<extensions::PrinterProviderPrintJob> job) { | 64 std::unique_ptr<extensions::PrinterProviderPrintJob> job) { |
| 64 if (base::GetFileInfo(raster_path, &job->file_info)) | 65 if (base::GetFileInfo(raster_path, &job->file_info)) |
| 65 job->document_path = raster_path; | 66 job->document_path = raster_path; |
| 66 return job; | 67 return job; |
| 67 } | 68 } |
| 68 | 69 |
| 69 // Callback to PWG raster conversion. | 70 // Callback to PWG raster conversion. |
| 70 // Posts a task to update print job with info about file containing converted | 71 // Posts a task to update print job with info about file containing converted |
| 71 // PWG raster data. The task is posted to |slow_task_runner|. | 72 // PWG raster data. The task is posted to |slow_task_runner|. |
| 72 void UpdateJobFileInfo( | 73 void UpdateJobFileInfo( |
| 73 scoped_ptr<extensions::PrinterProviderPrintJob> job, | 74 std::unique_ptr<extensions::PrinterProviderPrintJob> job, |
| 74 const scoped_refptr<base::TaskRunner>& slow_task_runner, | 75 const scoped_refptr<base::TaskRunner>& slow_task_runner, |
| 75 const ExtensionPrinterHandler::PrintJobCallback& callback, | 76 const ExtensionPrinterHandler::PrintJobCallback& callback, |
| 76 bool success, | 77 bool success, |
| 77 const base::FilePath& pwg_file_path) { | 78 const base::FilePath& pwg_file_path) { |
| 78 if (!success) { | 79 if (!success) { |
| 79 callback.Run(std::move(job)); | 80 callback.Run(std::move(job)); |
| 80 return; | 81 return; |
| 81 } | 82 } |
| 82 | 83 |
| 83 base::PostTaskAndReplyWithResult( | 84 base::PostTaskAndReplyWithResult( |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 182 } |
| 182 | 183 |
| 183 void ExtensionPrinterHandler::StartPrint( | 184 void ExtensionPrinterHandler::StartPrint( |
| 184 const std::string& destination_id, | 185 const std::string& destination_id, |
| 185 const std::string& capability, | 186 const std::string& capability, |
| 186 const base::string16& job_title, | 187 const base::string16& job_title, |
| 187 const std::string& ticket_json, | 188 const std::string& ticket_json, |
| 188 const gfx::Size& page_size, | 189 const gfx::Size& page_size, |
| 189 const scoped_refptr<base::RefCountedMemory>& print_data, | 190 const scoped_refptr<base::RefCountedMemory>& print_data, |
| 190 const PrinterHandler::PrintCallback& callback) { | 191 const PrinterHandler::PrintCallback& callback) { |
| 191 scoped_ptr<extensions::PrinterProviderPrintJob> print_job( | 192 std::unique_ptr<extensions::PrinterProviderPrintJob> print_job( |
| 192 new extensions::PrinterProviderPrintJob()); | 193 new extensions::PrinterProviderPrintJob()); |
| 193 print_job->printer_id = destination_id; | 194 print_job->printer_id = destination_id; |
| 194 print_job->job_title = job_title; | 195 print_job->job_title = job_title; |
| 195 print_job->ticket_json = ticket_json; | 196 print_job->ticket_json = ticket_json; |
| 196 | 197 |
| 197 cloud_devices::CloudDeviceDescription printer_description; | 198 cloud_devices::CloudDeviceDescription printer_description; |
| 198 printer_description.InitFromString(capability); | 199 printer_description.InitFromString(capability); |
| 199 | 200 |
| 200 cloud_devices::printer::ContentTypesCapability content_types; | 201 cloud_devices::printer::ContentTypesCapability content_types; |
| 201 content_types.LoadFrom(printer_description); | 202 content_types.LoadFrom(printer_description); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 249 |
| 249 extensions::PrinterProviderAPIFactory::GetInstance() | 250 extensions::PrinterProviderAPIFactory::GetInstance() |
| 250 ->GetForBrowserContext(browser_context_) | 251 ->GetForBrowserContext(browser_context_) |
| 251 ->DispatchGetUsbPrinterInfoRequested( | 252 ->DispatchGetUsbPrinterInfoRequested( |
| 252 extension_id, device, | 253 extension_id, device, |
| 253 base::Bind(&ExtensionPrinterHandler::WrapGetPrinterInfoCallback, | 254 base::Bind(&ExtensionPrinterHandler::WrapGetPrinterInfoCallback, |
| 254 weak_ptr_factory_.GetWeakPtr(), callback)); | 255 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 255 } | 256 } |
| 256 | 257 |
| 257 void ExtensionPrinterHandler::SetPWGRasterConverterForTesting( | 258 void ExtensionPrinterHandler::SetPWGRasterConverterForTesting( |
| 258 scoped_ptr<PWGRasterConverter> pwg_raster_converter) { | 259 std::unique_ptr<PWGRasterConverter> pwg_raster_converter) { |
| 259 pwg_raster_converter_ = std::move(pwg_raster_converter); | 260 pwg_raster_converter_ = std::move(pwg_raster_converter); |
| 260 } | 261 } |
| 261 | 262 |
| 262 void ExtensionPrinterHandler::ConvertToPWGRaster( | 263 void ExtensionPrinterHandler::ConvertToPWGRaster( |
| 263 const scoped_refptr<base::RefCountedMemory>& data, | 264 const scoped_refptr<base::RefCountedMemory>& data, |
| 264 const cloud_devices::CloudDeviceDescription& printer_description, | 265 const cloud_devices::CloudDeviceDescription& printer_description, |
| 265 const cloud_devices::CloudDeviceDescription& ticket, | 266 const cloud_devices::CloudDeviceDescription& ticket, |
| 266 const gfx::Size& page_size, | 267 const gfx::Size& page_size, |
| 267 scoped_ptr<extensions::PrinterProviderPrintJob> job, | 268 std::unique_ptr<extensions::PrinterProviderPrintJob> job, |
| 268 const ExtensionPrinterHandler::PrintJobCallback& callback) { | 269 const ExtensionPrinterHandler::PrintJobCallback& callback) { |
| 269 if (!pwg_raster_converter_) { | 270 if (!pwg_raster_converter_) { |
| 270 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); | 271 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); |
| 271 } | 272 } |
| 272 pwg_raster_converter_->Start( | 273 pwg_raster_converter_->Start( |
| 273 data.get(), | 274 data.get(), |
| 274 PWGRasterConverter::GetConversionSettings(printer_description, page_size), | 275 PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
| 275 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), | 276 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
| 276 base::Bind(&UpdateJobFileInfo, base::Passed(&job), slow_task_runner_, | 277 base::Bind(&UpdateJobFileInfo, base::Passed(&job), slow_task_runner_, |
| 277 callback)); | 278 callback)); |
| 278 } | 279 } |
| 279 | 280 |
| 280 void ExtensionPrinterHandler::DispatchPrintJob( | 281 void ExtensionPrinterHandler::DispatchPrintJob( |
| 281 const PrinterHandler::PrintCallback& callback, | 282 const PrinterHandler::PrintCallback& callback, |
| 282 scoped_ptr<extensions::PrinterProviderPrintJob> print_job) { | 283 std::unique_ptr<extensions::PrinterProviderPrintJob> print_job) { |
| 283 if (print_job->document_path.empty() && !print_job->document_bytes) { | 284 if (print_job->document_path.empty() && !print_job->document_bytes) { |
| 284 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 285 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
| 285 return; | 286 return; |
| 286 } | 287 } |
| 287 | 288 |
| 288 extensions::PrinterProviderAPIFactory::GetInstance() | 289 extensions::PrinterProviderAPIFactory::GetInstance() |
| 289 ->GetForBrowserContext(browser_context_) | 290 ->GetForBrowserContext(browser_context_) |
| 290 ->DispatchPrintRequested( | 291 ->DispatchPrintRequested( |
| 291 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 292 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
| 292 weak_ptr_factory_.GetWeakPtr(), callback)); | 293 weak_ptr_factory_.GetWeakPtr(), callback)); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 .Set("provisional", true) | 367 .Set("provisional", true) |
| 367 .Build()); | 368 .Build()); |
| 368 } | 369 } |
| 369 } | 370 } |
| 370 } | 371 } |
| 371 | 372 |
| 372 DCHECK_GT(pending_enumeration_count_, 0); | 373 DCHECK_GT(pending_enumeration_count_, 0); |
| 373 pending_enumeration_count_--; | 374 pending_enumeration_count_--; |
| 374 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); | 375 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); |
| 375 } | 376 } |
| OLD | NEW |