2020-01-31 05:37:03 +00:00
|
|
|
// Copyright (c) 2020 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_GUID_CONVERTER_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_GUID_CONVERTER_H_
|
2020-01-31 05:37:03 +00:00
|
|
|
|
2021-05-19 23:15:47 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "gin/converter.h"
|
|
|
|
|
2020-01-31 05:37:03 +00:00
|
|
|
#if defined(OS_WIN)
|
2021-08-11 19:43:33 +00:00
|
|
|
// c.f.:
|
|
|
|
// https://chromium-review.googlesource.com/c/chromium/src/+/3076480
|
|
|
|
// REFGUID is currently incorrectly inheriting its type
|
|
|
|
// from base::GUID, when it should be inheriting from ::GUID.
|
|
|
|
// This workaround prevents build errors until the CL is merged
|
|
|
|
#ifdef _REFGUID_DEFINED
|
|
|
|
#undef REFGUID
|
|
|
|
#endif
|
|
|
|
#define REFGUID const ::GUID&
|
|
|
|
#define _REFGUID_DEFINED
|
|
|
|
|
2020-01-31 05:37:03 +00:00
|
|
|
#include <rpc.h>
|
2020-09-17 22:17:44 +00:00
|
|
|
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
#include "base/win/win_util.h"
|
2020-01-31 05:37:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
typedef GUID UUID;
|
|
|
|
#else
|
|
|
|
typedef struct {
|
|
|
|
} UUID;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<UUID> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
UUID* out) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
std::string guid;
|
|
|
|
if (!gin::ConvertFromV8(isolate, val, &guid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UUID uid;
|
|
|
|
|
2020-10-22 20:24:59 +00:00
|
|
|
if (!guid.empty()) {
|
2020-09-17 22:17:44 +00:00
|
|
|
if (guid[0] == '{' && guid[guid.length() - 1] == '}') {
|
|
|
|
guid = guid.substr(1, guid.length() - 2);
|
|
|
|
}
|
2020-01-31 05:37:03 +00:00
|
|
|
unsigned char* uid_cstr = (unsigned char*)guid.c_str();
|
|
|
|
RPC_STATUS result = UuidFromStringA(uid_cstr, &uid);
|
|
|
|
if (result == RPC_S_INVALID_STRING_UUID) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*out = uid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return false;
|
2020-09-17 22:17:44 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, UUID val) {
|
|
|
|
#if defined(OS_WIN)
|
2021-08-11 19:43:33 +00:00
|
|
|
const GUID GUID_NULL = {};
|
2020-09-17 22:17:44 +00:00
|
|
|
if (val == GUID_NULL) {
|
|
|
|
return StringToV8(isolate, "");
|
|
|
|
} else {
|
|
|
|
std::wstring uid = base::win::WStringFromGUID(val);
|
|
|
|
return StringToV8(isolate, base::SysWideToUTF8(uid));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return v8::Undefined(isolate);
|
2020-01-31 05:37:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_GUID_CONVERTER_H_
|