Chromium Code Reviews| Index: chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.cc |
| diff --git a/chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.cc b/chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c6a0325289f2fe2fe73914c3b496b13015b29a9d |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/usb_internals/usb_internals_page_handler.h" |
| + |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "device/core/device_client.h" |
| +#include "device/usb/usb_device.h" |
| +#include "device/usb/usb_device_handle.h" |
| +#include "device/usb/usb_service.h" |
| +#include "device/usb/webusb_descriptors.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +class TestUsbDevice : public device::UsbDevice { |
| + public: |
| + TestUsbDevice(const std::string& name, |
| + const std::string& serial_number, |
| + const GURL& landing_page, |
| + const GURL& allowed_origin); |
| + |
| + // device::UsbDevice overrides: |
| + void Open(const OpenCallback& callback) override; |
| + |
| + private: |
| + ~TestUsbDevice() override; |
| +}; |
|
Robert Sesek
2016/08/08 19:11:36
DISALLOW_COPY_AND_ASSIGN
Reilly Grant (use Gerrit)
2016/08/08 20:48:24
Done.
|
| + |
| +TestUsbDevice::TestUsbDevice(const std::string& name, |
| + const std::string& serial_number, |
| + const GURL& landing_page, |
| + const GURL& allowed_origin) |
| + : UsbDevice(0x0210, |
| + 0xff, |
| + 0xff, |
| + 0xff, |
| + 0x0000, |
| + 0x000, |
| + 0x0100, |
| + base::string16(), |
| + base::UTF8ToUTF16(name), |
| + base::UTF8ToUTF16(serial_number)) { |
| + webusb_allowed_origins_.reset(new device::WebUsbAllowedOrigins()); |
| + webusb_allowed_origins_->origins.push_back(allowed_origin); |
| + webusb_landing_page_ = landing_page; |
| +} |
| + |
| +void TestUsbDevice::Open(const OpenCallback& callback) { |
| + callback.Run(nullptr); |
| +} |
| + |
| +TestUsbDevice::~TestUsbDevice() {} |
| + |
| +} // namespace |
| + |
| +UsbInternalsPageHandler::UsbInternalsPageHandler( |
| + mojom::UsbInternalsPageHandlerRequest request) |
| + : binding_(this, std::move(request)) {} |
| + |
| +UsbInternalsPageHandler::~UsbInternalsPageHandler() {} |
| + |
| +void UsbInternalsPageHandler::AddDeviceForTesting( |
| + const std::string& name, |
| + const std::string& serial_number, |
| + const std::string& landing_page, |
| + const std::string& allowed_origin, |
| + const AddDeviceForTestingCallback& callback) { |
| + device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| + if (service) { |
| + GURL landing_page_url(landing_page); |
| + if (!landing_page_url.is_valid()) { |
| + callback.Run(false, "Landing page URL is invalid."); |
| + return; |
| + } |
| + |
| + GURL allowed_origin_url(allowed_origin); |
| + if (!allowed_origin_url.is_valid()) { |
| + callback.Run(false, "Allowed origin is invalid."); |
| + return; |
| + } |
| + |
| + service->AddDeviceForTesting(new TestUsbDevice( |
| + name, serial_number, landing_page_url, allowed_origin_url)); |
| + callback.Run(true, "Added."); |
| + } else { |
| + callback.Run(false, "USB service unavailable."); |
| + } |
| +} |
| + |
| +void UsbInternalsPageHandler::RemoveDeviceForTesting( |
| + const std::string& guid, |
| + const RemoveDeviceForTestingCallback& callback) { |
| + device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| + if (service) |
| + service->RemoveDeviceForTesting(guid); |
| + callback.Run(); |
| +} |
| + |
| +void UsbInternalsPageHandler::GetTestDevices( |
| + const GetTestDevicesCallback& callback) { |
| + std::vector<scoped_refptr<device::UsbDevice>> devices; |
| + device::UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| + if (service) |
| + service->GetTestDevices(&devices); |
| + std::vector<mojom::TestDeviceInfoPtr> result; |
| + result.reserve(devices.size()); |
| + for (const auto& device : devices) { |
| + auto device_info = mojom::TestDeviceInfo::New(); |
| + device_info->guid = device->guid(); |
| + device_info->name = base::UTF16ToUTF8(device->product_string()); |
| + device_info->serial_number = base::UTF16ToUTF8(device->serial_number()); |
| + device_info->landing_page = device->webusb_landing_page().spec(); |
| + if (device->webusb_allowed_origins() && |
| + !device->webusb_allowed_origins()->origins.empty()) { |
| + device_info->allowed_origin = |
| + device->webusb_allowed_origins()->origins.front().spec(); |
| + } |
| + result.push_back(std::move(device_info)); |
| + } |
| + callback.Run(std::move(result)); |
| +} |