Chromium Code Reviews| 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.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
| 17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.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 "device/core/device_client.h" | |
| 22 #include "device/usb/usb_device.h" | |
| 23 #include "device/usb/usb_service.h" | |
| 24 #include "extensions/browser/api/device_permissions_manager.h" | |
| 21 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 25 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 22 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h " | 26 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h " |
| 23 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 27 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 28 #include "extensions/browser/extension_registry.h" | |
| 29 #include "extensions/common/api/printer_provider/usb_printer_manifest_data.h" | |
| 30 #include "extensions/common/permissions/permissions_data.h" | |
| 31 #include "extensions/common/permissions/usb_device_permission.h" | |
| 32 #include "extensions/common/permissions/usb_device_permission_data.h" | |
| 33 #include "extensions/common/value_builder.h" | |
| 24 #include "printing/pdf_render_settings.h" | 34 #include "printing/pdf_render_settings.h" |
| 25 #include "printing/pwg_raster_settings.h" | 35 #include "printing/pwg_raster_settings.h" |
| 26 | 36 |
| 37 using device::UsbDevice; | |
| 38 using extensions::DevicePermissionsManager; | |
| 39 using extensions::DictionaryBuilder; | |
| 40 using extensions::Extension; | |
| 41 using extensions::ExtensionRegistry; | |
| 42 using extensions::ListBuilder; | |
| 43 using extensions::UsbPrinterManifestData; | |
| 27 using local_discovery::PWGRasterConverter; | 44 using local_discovery::PWGRasterConverter; |
| 28 | 45 |
| 29 namespace { | 46 namespace { |
| 30 | 47 |
| 31 const char kContentTypePdf[] = "application/pdf"; | 48 const char kContentTypePdf[] = "application/pdf"; |
| 32 const char kContentTypePWGRaster[] = "image/pwg-raster"; | 49 const char kContentTypePWGRaster[] = "image/pwg-raster"; |
| 33 const char kContentTypeAll[] = "*/*"; | 50 const char kContentTypeAll[] = "*/*"; |
| 34 | 51 |
| 35 const char kInvalidDataPrintError[] = "INVALID_DATA"; | 52 const char kInvalidDataPrintError[] = "INVALID_DATA"; |
| 36 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; | 53 const char kInvalidTicketPrintError[] = "INVALID_TICKET"; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 slow_task_runner_(slow_task_runner), | 92 slow_task_runner_(slow_task_runner), |
| 76 weak_ptr_factory_(this) { | 93 weak_ptr_factory_(this) { |
| 77 } | 94 } |
| 78 | 95 |
| 79 ExtensionPrinterHandler::~ExtensionPrinterHandler() { | 96 ExtensionPrinterHandler::~ExtensionPrinterHandler() { |
| 80 } | 97 } |
| 81 | 98 |
| 82 void ExtensionPrinterHandler::Reset() { | 99 void ExtensionPrinterHandler::Reset() { |
| 83 // TODO(tbarzic): Keep track of pending request ids issued by |this| and | 100 // TODO(tbarzic): Keep track of pending request ids issued by |this| and |
| 84 // cancel them from here. | 101 // cancel them from here. |
| 102 printer_enumeration_complete_ = false; | |
| 103 wait_for_usb_enumeration_ = false; | |
| 85 weak_ptr_factory_.InvalidateWeakPtrs(); | 104 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 86 } | 105 } |
| 87 | 106 |
| 88 void ExtensionPrinterHandler::StartGetPrinters( | 107 void ExtensionPrinterHandler::StartGetPrinters( |
| 89 const PrinterHandler::GetPrintersCallback& callback) { | 108 const PrinterHandler::GetPrintersCallback& callback) { |
| 109 // Assume that there can only be one printer enumeration occuring at once. | |
| 110 DCHECK(!printer_enumeration_complete_); | |
| 111 DCHECK(!wait_for_usb_enumeration_); | |
| 112 | |
| 113 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | |
| 114 for (const auto& extension : registry->enabled_extensions()) { | |
| 115 if (UsbPrinterManifestData::Get(extension.get()) && | |
| 116 extension->permissions_data() && | |
| 117 extension->permissions_data()->HasAPIPermission( | |
| 118 extensions::APIPermission::kPrinterProvider) && | |
| 119 extension->permissions_data()->HasAPIPermission( | |
| 120 extensions::APIPermission::kUsb)) { | |
| 121 wait_for_usb_enumeration_ = true; | |
|
tbarzic
2015/05/28 00:20:00
break;
Reilly Grant (use Gerrit)
2015/05/28 21:45:00
Done.
| |
| 122 } | |
| 123 } | |
| 124 | |
| 125 if (wait_for_usb_enumeration_) { | |
| 126 device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); | |
| 127 DCHECK(service); | |
| 128 service->GetDevices( | |
| 129 base::Bind(&ExtensionPrinterHandler::OnUsbDevicesEnumerated, | |
| 130 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 131 } | |
| 132 | |
| 90 extensions::PrinterProviderAPIFactory::GetInstance() | 133 extensions::PrinterProviderAPIFactory::GetInstance() |
| 91 ->GetForBrowserContext(browser_context_) | 134 ->GetForBrowserContext(browser_context_) |
| 92 ->DispatchGetPrintersRequested( | 135 ->DispatchGetPrintersRequested( |
| 93 base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback, | 136 base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback, |
| 94 weak_ptr_factory_.GetWeakPtr(), callback)); | 137 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 95 } | 138 } |
| 96 | 139 |
| 97 void ExtensionPrinterHandler::StartGetCapability( | 140 void ExtensionPrinterHandler::StartGetCapability( |
| 98 const std::string& destination_id, | 141 const std::string& destination_id, |
| 99 const PrinterHandler::GetCapabilityCallback& callback) { | 142 const PrinterHandler::GetCapabilityCallback& callback) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 ->GetForBrowserContext(browser_context_) | 228 ->GetForBrowserContext(browser_context_) |
| 186 ->DispatchPrintRequested( | 229 ->DispatchPrintRequested( |
| 187 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, | 230 *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, |
| 188 weak_ptr_factory_.GetWeakPtr(), callback)); | 231 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 189 } | 232 } |
| 190 | 233 |
| 191 void ExtensionPrinterHandler::WrapGetPrintersCallback( | 234 void ExtensionPrinterHandler::WrapGetPrintersCallback( |
| 192 const PrinterHandler::GetPrintersCallback& callback, | 235 const PrinterHandler::GetPrintersCallback& callback, |
| 193 const base::ListValue& printers, | 236 const base::ListValue& printers, |
| 194 bool done) { | 237 bool done) { |
| 238 if (done) { | |
|
Vitaly Buka (NO REVIEWS)
2015/05/28 16:34:12
maybe simpler is just to have integer counters:
i
Reilly Grant (use Gerrit)
2015/05/28 21:45:00
Done.
| |
| 239 printer_enumeration_complete_ = true; | |
|
tbarzic
2015/05/28 00:20:00
nit: no {}
| |
| 240 } | |
| 241 if (wait_for_usb_enumeration_) { | |
|
tbarzic
2015/05/28 00:19:59
remove this and invoke callback as
callback.Run(p
Reilly Grant (use Gerrit)
2015/05/28 21:45:00
Done.
| |
| 242 done = false; | |
| 243 } | |
| 195 callback.Run(printers, done); | 244 callback.Run(printers, done); |
| 196 } | 245 } |
| 197 | 246 |
| 198 void ExtensionPrinterHandler::WrapGetCapabilityCallback( | 247 void ExtensionPrinterHandler::WrapGetCapabilityCallback( |
| 199 const PrinterHandler::GetCapabilityCallback& callback, | 248 const PrinterHandler::GetCapabilityCallback& callback, |
| 200 const std::string& destination_id, | 249 const std::string& destination_id, |
| 201 const base::DictionaryValue& capability) { | 250 const base::DictionaryValue& capability) { |
| 202 callback.Run(destination_id, capability); | 251 callback.Run(destination_id, capability); |
| 203 } | 252 } |
| 204 | 253 |
| 205 void ExtensionPrinterHandler::WrapPrintCallback( | 254 void ExtensionPrinterHandler::WrapPrintCallback( |
| 206 const PrinterHandler::PrintCallback& callback, | 255 const PrinterHandler::PrintCallback& callback, |
| 207 bool success, | 256 bool success, |
| 208 const std::string& status) { | 257 const std::string& status) { |
| 209 callback.Run(success, status); | 258 callback.Run(success, status); |
| 210 } | 259 } |
| 260 | |
| 261 void ExtensionPrinterHandler::OnUsbDevicesEnumerated( | |
| 262 const PrinterHandler::GetPrintersCallback& callback, | |
| 263 const std::vector<scoped_refptr<UsbDevice>>& devices) { | |
| 264 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | |
| 265 DevicePermissionsManager* permissions_manager = | |
| 266 DevicePermissionsManager::Get(browser_context_); | |
| 267 | |
| 268 ListBuilder printer_list; | |
| 269 | |
| 270 for (const auto& extension : registry->enabled_extensions()) { | |
| 271 const UsbPrinterManifestData* manifest_data = | |
| 272 UsbPrinterManifestData::Get(extension.get()); | |
| 273 const extensions::DevicePermissions* device_permissions = | |
| 274 permissions_manager->GetForExtension(extension->id()); | |
| 275 | |
| 276 if (!manifest_data || !extension->permissions_data() || | |
| 277 !extension->permissions_data()->HasAPIPermission( | |
| 278 extensions::APIPermission::kPrinterProvider) || | |
|
tbarzic
2015/05/28 00:20:00
extract to a separate method (you have the same co
Reilly Grant (use Gerrit)
2015/05/28 21:45:00
Done.
| |
| 279 !extension->permissions_data()->HasAPIPermission( | |
| 280 extensions::APIPermission::kUsb)) { | |
| 281 continue; | |
| 282 } | |
| 283 | |
| 284 for (const auto& device : devices) { | |
| 285 if (manifest_data->SupportsDevice(device)) { | |
| 286 extensions::UsbDevicePermission::CheckParam param( | |
| 287 device->vendor_id(), device->product_id(), | |
| 288 extensions::UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | |
| 289 if (device_permissions->FindUsbDeviceEntry(device) || | |
| 290 extension->permissions_data()->CheckAPIPermissionWithParam( | |
| 291 extensions::APIPermission::kUsbDevice, ¶m)) { | |
| 292 // Skip devices the extension already has permission to access. | |
| 293 continue; | |
| 294 } | |
| 295 | |
| 296 printer_list.Append( | |
| 297 DictionaryBuilder() | |
| 298 .Set("id", base::StringPrintf("provisional-usb:%s:%u", | |
| 299 extension->id().c_str(), | |
| 300 device->unique_id())) | |
| 301 .Set("name", | |
| 302 DevicePermissionsManager::GetPermissionMessage( | |
| 303 device->vendor_id(), device->product_id(), | |
| 304 device->manufacturer_string(), | |
| 305 device->product_string(), base::string16(), false)) | |
| 306 .Set("extensionId", extension->id()) | |
| 307 .Set("extensionName", extension->name())); | |
|
tbarzic
2015/05/28 00:20:00
set a flag "isProvisional" (so it can be distingui
Reilly Grant (use Gerrit)
2015/05/28 21:45:00
Done.
| |
| 308 } | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 wait_for_usb_enumeration_ = false; | |
| 313 callback.Run(*printer_list.Build().get(), printer_enumeration_complete_); | |
| 314 } | |
| OLD | NEW |