Chromium Code Reviews| Index: extensions/browser/api/printer_provider/printer_provider_api.cc |
| diff --git a/extensions/browser/api/printer_provider/printer_provider_api.cc b/extensions/browser/api/printer_provider/printer_provider_api.cc |
| index cf1fda6ed4e7fde4a6d74c021b97c39b830ad54b..a8c1d5a21be1820155fe99b64f6c7c2ee66d979a 100644 |
| --- a/extensions/browser/api/printer_provider/printer_provider_api.cc |
| +++ b/extensions/browser/api/printer_provider/printer_provider_api.cc |
| @@ -18,6 +18,10 @@ |
| #include "base/strings/string16.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| +#include "device/core/device_client.h" |
| +#include "device/usb/usb_device.h" |
| +#include "device/usb/usb_service.h" |
| +#include "extensions/browser/api/device_permissions_manager.h" |
| #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| #include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api.h" |
| #include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api_observer.h" |
| @@ -26,8 +30,12 @@ |
| #include "extensions/browser/extension_registry_observer.h" |
| #include "extensions/common/api/printer_provider.h" |
| #include "extensions/common/api/printer_provider_internal.h" |
| +#include "extensions/common/api/usb.h" |
| #include "extensions/common/extension.h" |
| +using device::UsbDevice; |
| +using device::UsbService; |
| + |
| namespace extensions { |
| namespace { |
| @@ -114,8 +122,8 @@ class PendingGetPrintersRequests { |
| // called as if the extension reported empty set of printers. |
| void FailAllForExtension(const std::string& extension_id); |
| - // Adds an extension id to the list of the extensions that need to respond |
| - // to the event. |
| + // Adds an extension id to the list of the extensions that need to respond to |
| + // the event. |
| bool AddSource(int request_id, const std::string& extension_id); |
| private: |
| @@ -149,7 +157,7 @@ class PendingGetCapabilityRequests { |
| std::map<int, PrinterProviderAPI::GetCapabilityCallback> pending_requests_; |
| }; |
| -// Keeps track of pending chrome.printerProvider.ontPrintRequested requests |
| +// Keeps track of pending chrome.printerProvider.onPrintRequested requests |
| // for an extension. |
| class PendingPrintRequests { |
| public: |
| @@ -182,6 +190,31 @@ class PendingPrintRequests { |
| std::map<int, PrintRequest> pending_requests_; |
| }; |
| +// Keeps track of pending chrome.printerProvider.onUsbAccessGranted requests |
| +// for an extension. |
| +class PendingUsbPrinterInfoRequests { |
| + public: |
| + PendingUsbPrinterInfoRequests(); |
| + ~PendingUsbPrinterInfoRequests(); |
| + |
| + // Adds a new request to the set. Only information needed is the callback |
| + // associated with the request. Returns the id assigned to the request. |
| + int Add(const PrinterProviderAPI::UsbAccessGrantedCallback& callback); |
| + |
| + // Completes the request with the provided request id. It runs the request |
| + // callback and removes the request from the set. |
| + void Complete(int request_id, |
| + const core_api::printer_provider::PrinterInfo* printer_info); |
| + |
| + // Runs all pending callbacks with empty capability value and clears the |
| + // set of pending requests. |
| + void FailAll(); |
| + |
| + private: |
| + int last_request_id_ = 0; |
| + std::map<int, PrinterProviderAPI::UsbAccessGrantedCallback> pending_requests_; |
| +}; |
| + |
| // Implements chrome.printerProvider API events. |
| class PrinterProviderAPIImpl : public PrinterProviderAPI, |
| public PrinterProviderInternalAPIObserver, |
| @@ -202,6 +235,10 @@ class PrinterProviderAPIImpl : public PrinterProviderAPI, |
| const PrinterProviderAPI::PrintCallback& callback) override; |
| const PrinterProviderPrintJob* GetPrintJob(const Extension* extension, |
| int request_id) const override; |
| + void DispatchGrantUsbPrinterAccess( |
| + const std::string& extension_id, |
| + int device_id, |
| + const PrinterProviderAPI::UsbAccessGrantedCallback& callback) override; |
| // PrinterProviderInternalAPIObserver implementation: |
| void OnGetPrintersResult( |
| @@ -216,6 +253,10 @@ class PrinterProviderAPIImpl : public PrinterProviderAPI, |
| const Extension* extension, |
| int request_id, |
| core_api::printer_provider_internal::PrintError error) override; |
| + void OnUsbAccessGrantedResult( |
| + const Extension* extension, |
| + int request_id, |
| + const core_api::printer_provider::PrinterInfo* printer_info) override; |
| // ExtensionRegistryObserver implementation: |
| void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| @@ -241,6 +282,9 @@ class PrinterProviderAPIImpl : public PrinterProviderAPI, |
| std::map<std::string, PendingGetCapabilityRequests> |
| pending_capability_requests_; |
| + std::map<std::string, PendingUsbPrinterInfoRequests> |
| + pending_usb_printer_info_requests_; |
| + |
| ScopedObserver<PrinterProviderInternalAPI, PrinterProviderInternalAPIObserver> |
| internal_api_observer_; |
| @@ -401,6 +445,43 @@ void PendingPrintRequests::FailAll() { |
| pending_requests_.clear(); |
| } |
| +PendingUsbPrinterInfoRequests::PendingUsbPrinterInfoRequests() { |
| +} |
| + |
| +PendingUsbPrinterInfoRequests::~PendingUsbPrinterInfoRequests() { |
| +} |
| + |
| +int PendingUsbPrinterInfoRequests::Add( |
| + const PrinterProviderAPI::UsbAccessGrantedCallback& callback) { |
| + pending_requests_[++last_request_id_] = callback; |
| + return last_request_id_; |
| +} |
| + |
| +void PendingUsbPrinterInfoRequests::Complete( |
| + int request_id, |
| + const core_api::printer_provider::PrinterInfo* printer_info) { |
| + auto it = pending_requests_.find(request_id); |
| + if (it == pending_requests_.end()) { |
| + return; |
| + } |
| + |
| + PrinterProviderAPI::UsbAccessGrantedCallback callback = it->second; |
| + pending_requests_.erase(it); |
| + |
| + if (printer_info) { |
| + callback.Run(*printer_info->ToValue().get()); |
| + } else { |
| + callback.Run(base::DictionaryValue()); |
| + } |
| +} |
| + |
| +void PendingUsbPrinterInfoRequests::FailAll() { |
| + for (auto& request : pending_requests_) { |
| + request.second.Run(base::DictionaryValue()); |
| + } |
| + pending_requests_.clear(); |
| +} |
| + |
| PrinterProviderAPIImpl::PrinterProviderAPIImpl( |
| content::BrowserContext* browser_context) |
| : browser_context_(browser_context), |
| @@ -534,6 +615,49 @@ const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob( |
| return it->second.GetPrintJob(request_id); |
| } |
| +void PrinterProviderAPIImpl::DispatchGrantUsbPrinterAccess( |
| + const std::string& extension_id, |
| + int device_id, |
| + const PrinterProviderAPI::UsbAccessGrantedCallback& callback) { |
| + UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| + DCHECK(service); |
| + scoped_refptr<UsbDevice> device = service->GetDeviceById(device_id); |
| + if (!device) { |
| + callback.Run(base::DictionaryValue()); |
| + return; |
| + } |
| + |
| + DevicePermissionsManager* permissions_manager = |
| + DevicePermissionsManager::Get(browser_context_); |
| + DCHECK(permissions_manager); |
| + permissions_manager->AllowUsbDevice(extension_id, device); |
| + |
| + EventRouter* event_router = EventRouter::Get(browser_context_); |
| + if (!event_router->ExtensionHasEventListener( |
| + extension_id, |
| + core_api::printer_provider::OnUsbAccessGranted::kEventName)) { |
| + callback.Run(base::DictionaryValue()); |
| + return; |
| + } |
| + |
| + int request_id = |
| + pending_usb_printer_info_requests_[extension_id].Add(callback); |
| + core_api::usb::Device usb_device; |
| + usb_device.device = device->unique_id(); |
| + usb_device.vendor_id = device->vendor_id(); |
| + usb_device.product_id = device->product_id(); |
| + |
| + scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
| + // Request id is not part of the public API and it will be massaged out in |
| + // custom bindings. |
| + internal_args->AppendInteger(request_id); |
| + internal_args->Append(usb_device.ToValue().release()); |
| + scoped_ptr<Event> event( |
| + new Event(core_api::printer_provider::OnUsbAccessGranted::kEventName, |
| + internal_args.Pass())); |
| + event_router->DispatchEventToExtension(extension_id, event.Pass()); |
| +} |
| + |
| void PrinterProviderAPIImpl::OnGetPrintersResult( |
| const Extension* extension, |
| int request_id, |
| @@ -590,6 +714,14 @@ void PrinterProviderAPIImpl::OnPrintResult( |
| error_str); |
| } |
| +void PrinterProviderAPIImpl::OnUsbAccessGrantedResult( |
| + const Extension* extension, |
| + int request_id, |
| + const core_api::printer_provider::PrinterInfo* printer_info) { |
| + pending_usb_printer_info_requests_[extension->id()].Complete(request_id, |
|
tbarzic
2015/05/27 00:11:03
Can you reuse code from PrinterProviderAPIImpl::On
Reilly Grant (use Gerrit)
2015/05/28 21:04:10
Done.
|
| + printer_info); |
| +} |
| + |
| void PrinterProviderAPIImpl::OnExtensionUnloaded( |
| content::BrowserContext* browser_context, |
| const Extension* extension, |
| @@ -607,6 +739,12 @@ void PrinterProviderAPIImpl::OnExtensionUnloaded( |
| capability_it->second.FailAll(); |
| pending_capability_requests_.erase(capability_it); |
| } |
| + |
| + auto usb_it = pending_usb_printer_info_requests_.find(extension->id()); |
| + if (usb_it != pending_usb_printer_info_requests_.end()) { |
| + usb_it->second.FailAll(); |
| + pending_usb_printer_info_requests_.erase(usb_it); |
| + } |
| } |
| bool PrinterProviderAPIImpl::WillRequestPrinters( |