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