| 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 "extensions/browser/api/printer_provider/printer_provider_api.h" | 5 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 14 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "device/core/device_client.h" | 22 #include "device/core/device_client.h" |
| 22 #include "device/usb/usb_device.h" | 23 #include "device/usb/usb_device.h" |
| 23 #include "device/usb/usb_service.h" | 24 #include "device/usb/usb_service.h" |
| 24 #include "extensions/browser/api/device_permissions_manager.h" | 25 #include "extensions/browser/api/device_permissions_manager.h" |
| 25 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 26 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 26 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api.h" | 27 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api.h" |
| 27 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api_observer.h" | 28 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api_observer.h" |
| 28 #include "extensions/browser/event_router.h" | 29 #include "extensions/browser/event_router.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 // generates a ID for the printer unique across extensions (assuming that the | 53 // generates a ID for the printer unique across extensions (assuming that the |
| 53 // printer id is unique in the extension's space). | 54 // printer id is unique in the extension's space). |
| 54 std::string GeneratePrinterId(const std::string& extension_id, | 55 std::string GeneratePrinterId(const std::string& extension_id, |
| 55 const std::string& internal_printer_id) { | 56 const std::string& internal_printer_id) { |
| 56 std::string result = extension_id; | 57 std::string result = extension_id; |
| 57 result.append(1, kPrinterIdSeparator); | 58 result.append(1, kPrinterIdSeparator); |
| 58 result.append(internal_printer_id); | 59 result.append(internal_printer_id); |
| 59 return result; | 60 return result; |
| 60 } | 61 } |
| 61 | 62 |
| 63 std::string GenerateUsbExtensionPrinterId(const std::string& extension_id, |
| 64 int usb_device_id) { |
| 65 std::string result("usb-"); |
| 66 result.append( |
| 67 GeneratePrinterId(extension_id, |
| 68 base::IntToString(usb_device_id))); |
| 69 return result; |
| 70 } |
| 71 |
| 62 // Parses an ID created using |GeneratePrinterId| to it's components: | 72 // Parses an ID created using |GeneratePrinterId| to it's components: |
| 63 // the extension ID and the printer ID internal to the extension. | 73 // the extension ID and the printer ID internal to the extension. |
| 64 // Returns whenter the ID was succesfully parsed. | 74 // Returns whenter the ID was succesfully parsed. |
| 65 bool ParsePrinterId(const std::string& printer_id, | 75 bool ParsePrinterId(const std::string& printer_id, |
| 66 std::string* extension_id, | 76 std::string* extension_id, |
| 67 std::string* internal_printer_id) { | 77 std::string* internal_printer_id) { |
| 68 size_t separator = printer_id.find_first_of(kPrinterIdSeparator); | 78 size_t separator = printer_id.find_first_of(kPrinterIdSeparator); |
| 69 if (separator == std::string::npos) | 79 if (separator == std::string::npos) |
| 70 return false; | 80 return false; |
| 71 *extension_id = printer_id.substr(0, separator); | 81 *extension_id = printer_id.substr(0, separator); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 PendingUsbPrinterInfoRequests(); | 225 PendingUsbPrinterInfoRequests(); |
| 216 ~PendingUsbPrinterInfoRequests(); | 226 ~PendingUsbPrinterInfoRequests(); |
| 217 | 227 |
| 218 // Adds a new request to the set. Only information needed is the callback | 228 // Adds a new request to the set. Only information needed is the callback |
| 219 // associated with the request. Returns the id assigned to the request. | 229 // associated with the request. Returns the id assigned to the request. |
| 220 int Add(const PrinterProviderAPI::UsbAccessGrantedCallback& callback); | 230 int Add(const PrinterProviderAPI::UsbAccessGrantedCallback& callback); |
| 221 | 231 |
| 222 // Completes the request with the provided request id. It runs the request | 232 // Completes the request with the provided request id. It runs the request |
| 223 // callback and removes the request from the set. | 233 // callback and removes the request from the set. |
| 224 void Complete(int request_id, | 234 void Complete(int request_id, |
| 225 const core_api::printer_provider::PrinterInfo* printer_info); | 235 const base::DictionaryValue* printer_info); |
| 226 | 236 |
| 227 // Runs all pending callbacks with empty capability value and clears the | 237 // Runs all pending callbacks with empty capability value and clears the |
| 228 // set of pending requests. | 238 // set of pending requests. |
| 229 void FailAll(); | 239 void FailAll(); |
| 230 | 240 |
| 231 private: | 241 private: |
| 232 int last_request_id_ = 0; | 242 int last_request_id_ = 0; |
| 233 std::map<int, PrinterProviderAPI::UsbAccessGrantedCallback> pending_requests_; | 243 std::map<int, PrinterProviderAPI::UsbAccessGrantedCallback> pending_requests_; |
| 234 }; | 244 }; |
| 235 | 245 |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 } | 508 } |
| 499 | 509 |
| 500 int PendingUsbPrinterInfoRequests::Add( | 510 int PendingUsbPrinterInfoRequests::Add( |
| 501 const PrinterProviderAPI::UsbAccessGrantedCallback& callback) { | 511 const PrinterProviderAPI::UsbAccessGrantedCallback& callback) { |
| 502 pending_requests_[++last_request_id_] = callback; | 512 pending_requests_[++last_request_id_] = callback; |
| 503 return last_request_id_; | 513 return last_request_id_; |
| 504 } | 514 } |
| 505 | 515 |
| 506 void PendingUsbPrinterInfoRequests::Complete( | 516 void PendingUsbPrinterInfoRequests::Complete( |
| 507 int request_id, | 517 int request_id, |
| 508 const core_api::printer_provider::PrinterInfo* printer_info) { | 518 const base::DictionaryValue* printer_info) { |
| 509 auto it = pending_requests_.find(request_id); | 519 auto it = pending_requests_.find(request_id); |
| 510 if (it == pending_requests_.end()) { | 520 if (it == pending_requests_.end()) { |
| 511 return; | 521 return; |
| 512 } | 522 } |
| 513 | 523 |
| 514 PrinterProviderAPI::UsbAccessGrantedCallback callback = it->second; | 524 PrinterProviderAPI::UsbAccessGrantedCallback callback = it->second; |
| 515 pending_requests_.erase(it); | 525 pending_requests_.erase(it); |
| 516 | 526 |
| 517 if (printer_info) { | 527 if (printer_info) { |
| 518 callback.Run(*printer_info->ToValue().get()); | 528 callback.Run(*printer_info); |
| 519 } else { | 529 } else { |
| 520 callback.Run(base::DictionaryValue()); | 530 callback.Run(base::DictionaryValue()); |
| 521 } | 531 } |
| 522 } | 532 } |
| 523 | 533 |
| 524 void PendingUsbPrinterInfoRequests::FailAll() { | 534 void PendingUsbPrinterInfoRequests::FailAll() { |
| 525 for (auto& request : pending_requests_) { | 535 for (auto& request : pending_requests_) { |
| 526 request.second.Run(base::DictionaryValue()); | 536 request.second.Run(base::DictionaryValue()); |
| 527 } | 537 } |
| 528 pending_requests_.clear(); | 538 pending_requests_.clear(); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 : core_api::printer_provider_internal::ToString(error); | 790 : core_api::printer_provider_internal::ToString(error); |
| 781 pending_print_requests_[extension->id()].Complete( | 791 pending_print_requests_[extension->id()].Complete( |
| 782 request_id, error == core_api::printer_provider_internal::PRINT_ERROR_OK, | 792 request_id, error == core_api::printer_provider_internal::PRINT_ERROR_OK, |
| 783 error_str); | 793 error_str); |
| 784 } | 794 } |
| 785 | 795 |
| 786 void PrinterProviderAPIImpl::OnUsbAccessGrantedResult( | 796 void PrinterProviderAPIImpl::OnUsbAccessGrantedResult( |
| 787 const Extension* extension, | 797 const Extension* extension, |
| 788 int request_id, | 798 int request_id, |
| 789 const core_api::printer_provider::PrinterInfo* printer_info) { | 799 const core_api::printer_provider::PrinterInfo* printer_info) { |
| 800 if (!printer_info) { |
| 801 pending_usb_printer_info_requests_[extension->id()].Complete( |
| 802 request_id, NULL); |
| 803 return; |
| 804 } |
| 805 |
| 806 scoped_ptr<base::DictionaryValue> printer(printer_info->ToValue()); |
| 807 std::string internal_printer_id; |
| 808 CHECK(printer->GetString("id", &internal_printer_id)); |
| 809 printer->SetString("id", |
| 810 GeneratePrinterId(extension->id(), internal_printer_id)); |
| 811 printer->SetString("extensionId", extension->id()); |
| 812 printer->SetString("extensionName", extension->name()); |
| 813 |
| 814 base::string16 printer_name; |
| 815 if (printer->GetString("name", &printer_name) && |
| 816 base::i18n::AdjustStringForLocaleDirection(&printer_name)) { |
| 817 printer->SetString("name", printer_name); |
| 818 } |
| 819 |
| 820 base::string16 printer_description; |
| 821 if (printer->GetString("description", &printer_description) && |
| 822 base::i18n::AdjustStringForLocaleDirection(&printer_description)) { |
| 823 printer->SetString("description", printer_description); |
| 824 } |
| 825 |
| 790 pending_usb_printer_info_requests_[extension->id()].Complete(request_id, | 826 pending_usb_printer_info_requests_[extension->id()].Complete(request_id, |
| 791 printer_info); | 827 printer.get()); |
| 792 } | 828 } |
| 793 | 829 |
| 794 void PrinterProviderAPIImpl::OnExtensionUnloaded( | 830 void PrinterProviderAPIImpl::OnExtensionUnloaded( |
| 795 content::BrowserContext* browser_context, | 831 content::BrowserContext* browser_context, |
| 796 const Extension* extension, | 832 const Extension* extension, |
| 797 UnloadedExtensionInfo::Reason reason) { | 833 UnloadedExtensionInfo::Reason reason) { |
| 798 pending_get_printers_requests_.FailAllForExtension(extension->id()); | 834 pending_get_printers_requests_.FailAllForExtension(extension->id()); |
| 799 | 835 |
| 800 auto print_it = pending_print_requests_.find(extension->id()); | 836 auto print_it = pending_print_requests_.find(extension->id()); |
| 801 if (print_it != pending_print_requests_.end()) { | 837 if (print_it != pending_print_requests_.end()) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 | 890 |
| 855 for (const auto& device : devices) { | 891 for (const auto& device : devices) { |
| 856 if (manifest_data->SupportsDevice(device)) { | 892 if (manifest_data->SupportsDevice(device)) { |
| 857 result_map[device].push_back(extension); | 893 result_map[device].push_back(extension); |
| 858 } | 894 } |
| 859 } | 895 } |
| 860 } | 896 } |
| 861 | 897 |
| 862 ListBuilder printer_list; | 898 ListBuilder printer_list; |
| 863 for (const auto& map_entry : result_map) { | 899 for (const auto& map_entry : result_map) { |
| 900 const scoped_refptr<UsbDevice>& device = map_entry.first; |
| 901 |
| 864 ListBuilder extension_list; | 902 ListBuilder extension_list; |
| 865 for (const scoped_refptr<const Extension>& extension : map_entry.second) { | 903 for (const scoped_refptr<const Extension>& extension : map_entry.second) { |
| 866 extension_list.Append(DictionaryBuilder() | 904 extension_list.Append( |
| 867 .Set("extensionId", extension->id()) | 905 DictionaryBuilder() |
| 868 .Set("extensionName", extension->name())); | 906 .Set("extensionId", extension->id()) |
| 907 .Set("extensionName", extension->name()) |
| 908 .Set("printerId", |
| 909 GenerateUsbExtensionPrinterId( |
| 910 extension->id(), |
| 911 static_cast<int>(device->unique_id())))); |
| 869 } | 912 } |
| 870 | 913 |
| 871 const scoped_refptr<UsbDevice>& device = map_entry.first; | |
| 872 printer_list.Append( | 914 printer_list.Append( |
| 873 DictionaryBuilder() | 915 DictionaryBuilder() |
| 874 .Set("name", DevicePermissionsManager::GetPermissionMessage( | 916 .Set("name", DevicePermissionsManager::GetPermissionMessage( |
| 875 device->vendor_id(), device->product_id(), | 917 device->vendor_id(), device->product_id(), |
| 876 device->manufacturer_string(), | 918 device->manufacturer_string(), |
| 877 device->product_string(), base::string16(), false)) | 919 device->product_string(), base::string16(), false)) |
| 878 .Set("usbDevice", static_cast<int>(device->unique_id())) | 920 .Set("usbDevice", static_cast<int>(device->unique_id())) |
| 879 .Set("extensions", extension_list.Pass())); | 921 .Set("extensions", extension_list.Pass()) |
| 922 .Set("needsUsbPermission", true)); |
| 880 } | 923 } |
| 881 | 924 |
| 882 pending_get_printers_requests_.CompleteUsbPrinters( | 925 pending_get_printers_requests_.CompleteUsbPrinters( |
| 883 request_id, *printer_list.Build().get()); | 926 request_id, *printer_list.Build().get()); |
| 884 } | 927 } |
| 885 | 928 |
| 886 } // namespace | 929 } // namespace |
| 887 | 930 |
| 888 // static | 931 // static |
| 889 PrinterProviderAPI* PrinterProviderAPI::Create( | 932 PrinterProviderAPI* PrinterProviderAPI::Create( |
| 890 content::BrowserContext* context) { | 933 content::BrowserContext* context) { |
| 891 return new PrinterProviderAPIImpl(context); | 934 return new PrinterProviderAPIImpl(context); |
| 892 } | 935 } |
| 893 | 936 |
| 894 // static | 937 // static |
| 895 std::string PrinterProviderAPI::GetDefaultPrintError() { | 938 std::string PrinterProviderAPI::GetDefaultPrintError() { |
| 896 return core_api::printer_provider_internal::ToString( | 939 return core_api::printer_provider_internal::ToString( |
| 897 core_api::printer_provider_internal::PRINT_ERROR_FAILED); | 940 core_api::printer_provider_internal::PRINT_ERROR_FAILED); |
| 898 } | 941 } |
| 899 | 942 |
| 900 } // namespace extensions | 943 } // namespace extensions |
| OLD | NEW |