-
Notifications
You must be signed in to change notification settings - Fork 15k
/
serial_port_info_converter.h
43 lines (37 loc) · 1.64 KB
/
serial_port_info_converter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2022 Microsoft, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_SERIAL_PORT_INFO_CONVERTER_H_
#define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_SERIAL_PORT_INFO_CONVERTER_H_
#include "gin/converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "third_party/blink/public/mojom/serial/serial.mojom.h"
namespace gin {
template <>
struct Converter<device::mojom::SerialPortInfoPtr> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const device::mojom::SerialPortInfoPtr& port) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("portId", port->token.ToString());
dict.Set("portName", port->path.BaseName().LossyDisplayName());
if (port->display_name && !port->display_name->empty())
dict.Set("displayName", *port->display_name);
if (port->has_vendor_id)
dict.Set("vendorId", base::StringPrintf("%u", port->vendor_id));
if (port->has_product_id)
dict.Set("productId", base::StringPrintf("%u", port->product_id));
if (port->serial_number && !port->serial_number->empty())
dict.Set("serialNumber", *port->serial_number);
#if BUILDFLAG(IS_MAC)
if (port->usb_driver_name && !port->usb_driver_name->empty())
dict.Set("usbDriverName", *port->usb_driver_name);
#elif BUILDFLAG(IS_WIN)
if (!port->device_instance_id.empty())
dict.Set("deviceInstanceId", port->device_instance_id);
#endif
return gin::ConvertToV8(isolate, dict);
}
};
} // namespace gin
#endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_SERIAL_PORT_INFO_CONVERTER_H_