| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "device/core/device_client.h" |
| 11 #include "device/usb/usb_device.h" |
| 12 #include "device/usb/usb_device_handle.h" |
| 13 #include "device/usb/usb_service.h" |
| 14 #include "device/usb/webusb_descriptors.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class TestUsbDevice : public device::UsbDevice { |
| 20 public: |
| 21 TestUsbDevice(const std::string& name, |
| 22 const std::string& serial_number, |
| 23 const GURL& landing_page, |
| 24 const GURL& allowed_origin); |
| 25 |
| 26 // device::UsbDevice overrides: |
| 27 void Open(const OpenCallback& callback) override; |
| 28 |
| 29 private: |
| 30 ~TestUsbDevice() override; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(TestUsbDevice); |
| 33 }; |
| 34 |
| 35 TestUsbDevice::TestUsbDevice(const std::string& name, |
| 36 const std::string& serial_number, |
| 37 const GURL& landing_page, |
| 38 const GURL& allowed_origin) |
| 39 : UsbDevice(0x0210, |
| 40 0xff, |
| 41 0xff, |
| 42 0xff, |
| 43 0x0000, |
| 44 0x000, |
| 45 0x0100, |
| 46 base::string16(), |
| 47 base::UTF8ToUTF16(name), |
| 48 base::UTF8ToUTF16(serial_number)) { |
| 49 webusb_allowed_origins_.reset(new device::WebUsbAllowedOrigins()); |
| 50 webusb_allowed_origins_->origins.push_back(allowed_origin); |
| 51 webusb_landing_page_ = landing_page; |
| 52 } |
| 53 |
| 54 void TestUsbDevice::Open(const OpenCallback& callback) { |
| 55 callback.Run(nullptr); |
| 56 } |
| 57 |
| 58 TestUsbDevice::~TestUsbDevice() {} |
| 59 |
| 60 } // namespace |
| 61 |
| 62 UsbInternalsPageHandler::UsbInternalsPageHandler( |
| 63 mojom::UsbInternalsPageHandlerRequest request) |
| 64 : binding_(this, std::move(request)) {} |
| 65 |
| 66 UsbInternalsPageHandler::~UsbInternalsPageHandler() {} |
| 67 |
| 68 void UsbInternalsPageHandler::AddDeviceForTesting( |
| 69 const std::string& name, |
| 70 const std::string& serial_number, |
| 71 const std::string& landing_page, |
| 72 const std::string& allowed_origin, |
| 73 const AddDeviceForTestingCallback& callback) { |
| 74 device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 75 if (service) { |
| 76 GURL landing_page_url(landing_page); |
| 77 if (!landing_page_url.is_valid()) { |
| 78 callback.Run(false, "Landing page URL is invalid."); |
| 79 return; |
| 80 } |
| 81 |
| 82 GURL allowed_origin_url(allowed_origin); |
| 83 if (!allowed_origin_url.is_valid()) { |
| 84 callback.Run(false, "Allowed origin is invalid."); |
| 85 return; |
| 86 } |
| 87 |
| 88 service->AddDeviceForTesting(new TestUsbDevice( |
| 89 name, serial_number, landing_page_url, allowed_origin_url)); |
| 90 callback.Run(true, "Added."); |
| 91 } else { |
| 92 callback.Run(false, "USB service unavailable."); |
| 93 } |
| 94 } |
| 95 |
| 96 void UsbInternalsPageHandler::RemoveDeviceForTesting( |
| 97 const std::string& guid, |
| 98 const RemoveDeviceForTestingCallback& callback) { |
| 99 device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 100 if (service) |
| 101 service->RemoveDeviceForTesting(guid); |
| 102 callback.Run(); |
| 103 } |
| 104 |
| 105 void UsbInternalsPageHandler::GetTestDevices( |
| 106 const GetTestDevicesCallback& callback) { |
| 107 std::vector<scoped_refptr<device::UsbDevice>> devices; |
| 108 device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 109 if (service) |
| 110 service->GetTestDevices(&devices); |
| 111 std::vector<mojom::TestDeviceInfoPtr> result; |
| 112 result.reserve(devices.size()); |
| 113 for (const auto& device : devices) { |
| 114 auto device_info = mojom::TestDeviceInfo::New(); |
| 115 device_info->guid = device->guid(); |
| 116 device_info->name = base::UTF16ToUTF8(device->product_string()); |
| 117 device_info->serial_number = base::UTF16ToUTF8(device->serial_number()); |
| 118 device_info->landing_page = device->webusb_landing_page(); |
| 119 if (device->webusb_allowed_origins() && |
| 120 !device->webusb_allowed_origins()->origins.empty()) { |
| 121 device_info->allowed_origin = |
| 122 url::Origin(device->webusb_allowed_origins()->origins.front()); |
| 123 } |
| 124 result.push_back(std::move(device_info)); |
| 125 } |
| 126 callback.Run(std::move(result)); |
| 127 } |
| OLD | NEW |