| 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_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 17 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
| 18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
| 19 #include "components/cloud_devices/common/cloud_device_description.h" | 19 #include "components/cloud_devices/common/cloud_device_description.h" |
| 20 #include "components/cloud_devices/common/printer_description.h" | 20 #include "components/cloud_devices/common/printer_description.h" |
| 21 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 21 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 22 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
| 23 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 22 #include "printing/pdf_render_settings.h" | 24 #include "printing/pdf_render_settings.h" |
| 23 #include "printing/pwg_raster_settings.h" | 25 #include "printing/pwg_raster_settings.h" |
| 24 | 26 |
| 25 using local_discovery::PWGRasterConverter; | 27 using local_discovery::PWGRasterConverter; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const char kContentTypePdf[] = "application/pdf"; | 31 const char kContentTypePdf[] = "application/pdf"; |
| 30 const char kContentTypePWGRaster[] = "image/pwg-raster"; | 32 const char kContentTypePWGRaster[] = "image/pwg-raster"; |
| 31 const char kContentTypeAll[] = "*/*"; | 33 const char kContentTypeAll[] = "*/*"; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } | 82 } |
| 81 | 83 |
| 82 void ExtensionPrinterHandler::Reset() { | 84 void ExtensionPrinterHandler::Reset() { |
| 83 // TODO(tbarzic): Keep track of pending request ids issued by |this| and | 85 // TODO(tbarzic): Keep track of pending request ids issued by |this| and |
| 84 // cancel them from here. | 86 // cancel them from here. |
| 85 weak_ptr_factory_.InvalidateWeakPtrs(); | 87 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 86 } | 88 } |
| 87 | 89 |
| 88 void ExtensionPrinterHandler::StartGetPrinters( | 90 void ExtensionPrinterHandler::StartGetPrinters( |
| 89 const PrinterHandler::GetPrintersCallback& callback) { | 91 const PrinterHandler::GetPrintersCallback& callback) { |
| 90 extensions::PrinterProviderAPI::GetFactoryInstance() | 92 extensions::PrinterProviderAPIFactory::GetInstance() |
| 91 ->Get(browser_context_) | 93 ->GetForBrowserContext(browser_context_) |
| 92 ->DispatchGetPrintersRequested( | 94 ->DispatchGetPrintersRequested( |
| 93 base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback, | 95 base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback, |
| 94 weak_ptr_factory_.GetWeakPtr(), callback)); | 96 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 95 } | 97 } |
| 96 | 98 |
| 97 void ExtensionPrinterHandler::StartGetCapability( | 99 void ExtensionPrinterHandler::StartGetCapability( |
| 98 const std::string& destination_id, | 100 const std::string& destination_id, |
| 99 const PrinterHandler::GetCapabilityCallback& callback) { | 101 const PrinterHandler::GetCapabilityCallback& callback) { |
| 100 extensions::PrinterProviderAPI::GetFactoryInstance() | 102 extensions::PrinterProviderAPIFactory::GetInstance() |
| 101 ->Get(browser_context_) | 103 ->GetForBrowserContext(browser_context_) |
| 102 ->DispatchGetCapabilityRequested( | 104 ->DispatchGetCapabilityRequested( |
| 103 destination_id, | 105 destination_id, |
| 104 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, | 106 base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, |
| 105 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); | 107 weak_ptr_factory_.GetWeakPtr(), callback, destination_id)); |
| 106 } | 108 } |
| 107 | 109 |
| 108 void ExtensionPrinterHandler::StartPrint( | 110 void ExtensionPrinterHandler::StartPrint( |
| 109 const std::string& destination_id, | 111 const std::string& destination_id, |
| 110 const std::string& capability, | 112 const std::string& capability, |
| 111 const std::string& ticket_json, | 113 const std::string& ticket_json, |
| 112 const gfx::Size& page_size, | 114 const gfx::Size& page_size, |
| 113 const scoped_refptr<base::RefCountedMemory>& print_data, | 115 const scoped_refptr<base::RefCountedMemory>& print_data, |
| 114 const PrinterHandler::PrintCallback& callback) { | 116 const PrinterHandler::PrintCallback& callback) { |
| 115 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job( | 117 scoped_ptr<extensions::PrinterProviderPrintJob> print_job( |
| 116 new extensions::PrinterProviderAPI::PrintJob()); | 118 new extensions::PrinterProviderPrintJob()); |
| 117 print_job->printer_id = destination_id; | 119 print_job->printer_id = destination_id; |
| 118 print_job->ticket_json = ticket_json; | 120 print_job->ticket_json = ticket_json; |
| 119 | 121 |
| 120 cloud_devices::CloudDeviceDescription printer_description; | 122 cloud_devices::CloudDeviceDescription printer_description; |
| 121 printer_description.InitFromString(capability); | 123 printer_description.InitFromString(capability); |
| 122 | 124 |
| 123 cloud_devices::printer::ContentTypesCapability content_types; | 125 cloud_devices::printer::ContentTypesCapability content_types; |
| 124 content_types.LoadFrom(printer_description); | 126 content_types.LoadFrom(printer_description); |
| 125 | 127 |
| 126 const bool kUsePdf = content_types.Contains(kContentTypePdf) || | 128 const bool kUsePdf = content_types.Contains(kContentTypePdf) || |
| (...skipping 29 matching lines...) Expand all Loading... |
| 156 } | 158 } |
| 157 pwg_raster_converter_->Start( | 159 pwg_raster_converter_->Start( |
| 158 data.get(), | 160 data.get(), |
| 159 PWGRasterConverter::GetConversionSettings(printer_description, page_size), | 161 PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
| 160 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), | 162 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
| 161 base::Bind(&ReadConvertedPWGRasterFile, callback)); | 163 base::Bind(&ReadConvertedPWGRasterFile, callback)); |
| 162 } | 164 } |
| 163 | 165 |
| 164 void ExtensionPrinterHandler::DispatchPrintJob( | 166 void ExtensionPrinterHandler::DispatchPrintJob( |
| 165 const PrinterHandler::PrintCallback& callback, | 167 const PrinterHandler::PrintCallback& callback, |
| 166 scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, | 168 scoped_ptr<extensions::PrinterProviderPrintJob> print_job, |
| 167 const scoped_refptr<base::RefCountedMemory>& print_data) { | 169 const scoped_refptr<base::RefCountedMemory>& print_data) { |
| 168 if (!print_data) { | 170 if (!print_data) { |
| 169 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 171 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
| 170 return; | 172 return; |
| 171 } | 173 } |
| 172 | 174 |
| 173 print_job->document_bytes = print_data; | 175 print_job->document_bytes = print_data; |
| 174 | 176 |
| 175 extensions::PrinterProviderAPI::GetFactoryInstance() | 177 extensions::PrinterProviderAPIFactory::GetInstance() |
| 176 ->Get(browser_context_) | 178 ->GetForBrowserContext(browser_context_) |
| 177 ->DispatchPrintRequested( | 179 ->DispatchPrintRequested( |
| 178 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 180 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
| 179 weak_ptr_factory_.GetWeakPtr(), callback)); | 181 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void ExtensionPrinterHandler::WrapGetPrintersCallback( | 184 void ExtensionPrinterHandler::WrapGetPrintersCallback( |
| 183 const PrinterHandler::GetPrintersCallback& callback, | 185 const PrinterHandler::GetPrintersCallback& callback, |
| 184 const base::ListValue& printers, | 186 const base::ListValue& printers, |
| 185 bool done) { | 187 bool done) { |
| 186 callback.Run(printers, done); | 188 callback.Run(printers, done); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void ExtensionPrinterHandler::WrapGetCapabilityCallback( | 191 void ExtensionPrinterHandler::WrapGetCapabilityCallback( |
| 190 const PrinterHandler::GetCapabilityCallback& callback, | 192 const PrinterHandler::GetCapabilityCallback& callback, |
| 191 const std::string& destination_id, | 193 const std::string& destination_id, |
| 192 const base::DictionaryValue& capability) { | 194 const base::DictionaryValue& capability) { |
| 193 callback.Run(destination_id, capability); | 195 callback.Run(destination_id, capability); |
| 194 } | 196 } |
| 195 | 197 |
| 196 void ExtensionPrinterHandler::WrapPrintCallback( | 198 void ExtensionPrinterHandler::WrapPrintCallback( |
| 197 const PrinterHandler::PrintCallback& callback, | 199 const PrinterHandler::PrintCallback& callback, |
| 198 bool success, | 200 bool success, |
| 199 const std::string& status) { | 201 const std::string& status) { |
| 200 callback.Run(success, status); | 202 callback.Run(success, status); |
| 201 } | 203 } |
| OLD | NEW |