Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/webusb/USBEndpoint.h" | |
| 7 | |
| 8 #include "bindings/core/v8/ExceptionState.h" | |
| 9 #include "core/dom/DOMException.h" | |
| 10 #include "core/dom/ExceptionCode.h" | |
| 11 #include "modules/webusb/USBAlternateInterface.h" | |
| 12 #include "public/platform/modules/webusb/WebUSBDevice.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 bool convertDirectionFromEnum(const String& direction, WebUSBDevice::TransferDir ection* webDirection) | |
| 19 { | |
| 20 if (direction == "in") | |
| 21 *webDirection = WebUSBDevice::TransferDirection::In; | |
| 22 else if (direction == "out") | |
| 23 *webDirection = WebUSBDevice::TransferDirection::Out; | |
| 24 else | |
| 25 return false; | |
| 26 return true; | |
| 27 } | |
| 28 | |
| 29 String convertDirectionToEnum(const WebUSBDevice::TransferDirection& direction) | |
| 30 { | |
| 31 switch (direction) { | |
| 32 case WebUSBDevice::TransferDirection::In: | |
| 33 return "in"; | |
| 34 case WebUSBDevice::TransferDirection::Out: | |
| 35 return "out"; | |
| 36 default: | |
| 37 ASSERT_NOT_REACHED(); | |
| 38 return ""; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 String convertTypeToEnum(const WebUSBDeviceInfo::Endpoint::Type& type) | |
| 43 { | |
| 44 switch (type) { | |
| 45 case WebUSBDeviceInfo::Endpoint::Type::Bulk: | |
| 46 return "bulk"; | |
| 47 case WebUSBDeviceInfo::Endpoint::Type::Interrupt: | |
| 48 return "interrupt"; | |
| 49 case WebUSBDeviceInfo::Endpoint::Type::Isochronous: | |
| 50 return "isochronous"; | |
| 51 default: | |
| 52 ASSERT_NOT_REACHED(); | |
| 53 return ""; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t endpointIndex) | |
| 60 { | |
| 61 return new USBEndpoint(alternate, endpointIndex); | |
| 62 } | |
| 63 | |
| 64 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t endpointNumber, const String& direction, ExceptionState& exceptionState) | |
| 65 { | |
| 66 WebUSBDevice::TransferDirection webDirection; | |
| 67 if (!convertDirectionFromEnum(direction, &webDirection)) { | |
| 68 exceptionState.throwRangeError("Invalid endpoint direction"); | |
|
Reilly Grant (use Gerrit)
2015/08/04 19:44:24
+ "."
Ken Rockot(use gerrit already)
2015/08/04 19:47:18
Done.
| |
| 69 return nullptr; | |
| 70 } | |
| 71 for (size_t i = 0; i < alternate->info().endpoints.size(); ++i) { | |
| 72 const WebUSBDeviceInfo::Endpoint& endpoint = alternate->info().endpoints [i]; | |
| 73 if (endpoint.endpointNumber == endpointNumber && endpoint.direction == w ebDirection) | |
| 74 return USBEndpoint::create(alternate, i); | |
| 75 } | |
| 76 exceptionState.throwRangeError("Invalid endpoint number."); | |
| 77 return nullptr; | |
| 78 } | |
| 79 | |
| 80 USBEndpoint::USBEndpoint(const USBAlternateInterface* alternate, size_t endpoint Index) | |
| 81 : m_alternate(alternate) | |
| 82 , m_endpointIndex(endpointIndex) | |
| 83 { | |
| 84 ASSERT(m_alternate); | |
| 85 ASSERT(m_endpointIndex < m_alternate->info().endpoints.size()); | |
| 86 } | |
| 87 | |
| 88 const WebUSBDeviceInfo::Endpoint& USBEndpoint::info() const | |
| 89 { | |
| 90 const WebUSBDeviceInfo::AlternateInterface& alternateInfo = m_alternate->inf o(); | |
| 91 ASSERT(m_endpointIndex < alternateInfo.endpoints.size()); | |
| 92 return alternateInfo.endpoints[m_endpointIndex]; | |
| 93 } | |
| 94 | |
| 95 uint8_t USBEndpoint::endpointNumber() const | |
| 96 { | |
| 97 return info().endpointNumber; | |
| 98 } | |
| 99 | |
| 100 String USBEndpoint::direction() const | |
| 101 { | |
| 102 return convertDirectionToEnum(info().direction); | |
| 103 } | |
| 104 | |
| 105 String USBEndpoint::type() const | |
| 106 { | |
| 107 return convertTypeToEnum(info().type); | |
| 108 } | |
| 109 | |
| 110 unsigned USBEndpoint::packetSize() const | |
| 111 { | |
| 112 return info().packetSize; | |
| 113 } | |
| 114 | |
| 115 DEFINE_TRACE(USBEndpoint) | |
| 116 { | |
| 117 visitor->trace(m_alternate); | |
| 118 } | |
| 119 | |
| 120 } // namespace blink | |
| OLD | NEW |