From ee64c6ab86179d722b8b02aaff3faec4c12fea31 Mon Sep 17 00:00:00 2001 From: Micha Hanselmann Date: Fri, 2 Aug 2019 12:34:30 -0700 Subject: [PATCH] refactor: move file path gin converter to new file (#19575) * move file path gin converter to new file * move string16 gin conv to new file --- filenames.gni | 2 + .../gin_converters/file_path_converter.h | 37 +++++++++++++++++ .../gin_converters/string16_converter.h | 41 +++++++++++++++++++ .../file_path_converter.h | 31 +------------- .../string16_converter.h | 33 +-------------- 5 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 shell/common/gin_converters/file_path_converter.h create mode 100644 shell/common/gin_converters/string16_converter.h diff --git a/filenames.gni b/filenames.gni index 27db8b6fd7..0b43edceb6 100644 --- a/filenames.gni +++ b/filenames.gni @@ -510,10 +510,12 @@ filenames = { "shell/common/crash_reporter/win/crash_service_main.cc", "shell/common/crash_reporter/win/crash_service_main.h", "shell/common/gin_converters/file_dialog_converter_gin_adapter.h", + "shell/common/gin_converters/file_path_converter.h", "shell/common/gin_converters/image_converter_gin_adapter.h", "shell/common/gin_converters/message_box_converter.cc", "shell/common/gin_converters/message_box_converter.h", "shell/common/gin_converters/net_converter_gin_adapter.h", + "shell/common/gin_converters/string16_converter.h", "shell/common/gin_util.h", "shell/common/heap_snapshot.cc", "shell/common/heap_snapshot.h", diff --git a/shell/common/gin_converters/file_path_converter.h b/shell/common/gin_converters/file_path_converter.h new file mode 100644 index 0000000000..4db7dd1dda --- /dev/null +++ b/shell/common/gin_converters/file_path_converter.h @@ -0,0 +1,37 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_GIN_CONVERTERS_FILE_PATH_CONVERTER_H_ +#define SHELL_COMMON_GIN_CONVERTERS_FILE_PATH_CONVERTER_H_ + +#include "base/files/file_path.h" +#include "shell/common/gin_converters/string16_converter.h" + +namespace gin { + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const base::FilePath& val) { + return Converter::ToV8(isolate, val.value()); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + base::FilePath* out) { + if (val->IsNull()) + return true; + + base::FilePath::StringType path; + if (Converter::FromV8(isolate, val, &path)) { + *out = base::FilePath(path); + return true; + } else { + return false; + } + } +}; + +} // namespace gin + +#endif // SHELL_COMMON_GIN_CONVERTERS_FILE_PATH_CONVERTER_H_ diff --git a/shell/common/gin_converters/string16_converter.h b/shell/common/gin_converters/string16_converter.h new file mode 100644 index 0000000000..be8af36751 --- /dev/null +++ b/shell/common/gin_converters/string16_converter.h @@ -0,0 +1,41 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_GIN_CONVERTERS_STRING16_CONVERTER_H_ +#define SHELL_COMMON_GIN_CONVERTERS_STRING16_CONVERTER_H_ + +#include "base/strings/string16.h" +#include "gin/converter.h" + +namespace gin { + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const base::string16& val) { + return v8::String::NewFromTwoByte( + isolate, reinterpret_cast(val.data()), + v8::NewStringType::kNormal, val.size()) + .ToLocalChecked(); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + base::string16* out) { + if (!val->IsString()) + return false; + + v8::String::Value s(isolate, val); + out->assign(reinterpret_cast(*s), s.length()); + return true; + } +}; + +inline v8::Local StringToV8(v8::Isolate* isolate, + const base::string16& input) { + return ConvertToV8(isolate, input).As(); +} + +} // namespace gin + +#endif // SHELL_COMMON_GIN_CONVERTERS_STRING16_CONVERTER_H_ diff --git a/shell/common/native_mate_converters/file_path_converter.h b/shell/common/native_mate_converters/file_path_converter.h index 9600e08ca6..3b6e1ebe88 100644 --- a/shell/common/native_mate_converters/file_path_converter.h +++ b/shell/common/native_mate_converters/file_path_converter.h @@ -5,36 +5,7 @@ #ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_ #define SHELL_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_ -#include - -#include "base/files/file_path.h" -#include "shell/common/native_mate_converters/string16_converter.h" - -namespace gin { - -template <> -struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, - const base::FilePath& val) { - return Converter::ToV8(isolate, val.value()); - } - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - base::FilePath* out) { - if (val->IsNull()) - return true; - - base::FilePath::StringType path; - if (Converter::FromV8(isolate, val, &path)) { - *out = base::FilePath(path); - return true; - } else { - return false; - } - } -}; - -} // namespace gin +#include "shell/common/gin_converters/file_path_converter.h" namespace mate { diff --git a/shell/common/native_mate_converters/string16_converter.h b/shell/common/native_mate_converters/string16_converter.h index 6827d5e711..0ec5fdf01e 100644 --- a/shell/common/native_mate_converters/string16_converter.h +++ b/shell/common/native_mate_converters/string16_converter.h @@ -5,39 +5,8 @@ #ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_ #define SHELL_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_ -#include "base/strings/string16.h" -#include "gin/converter.h" #include "native_mate/converter.h" - -namespace gin { - -template <> -struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, - const base::string16& val) { - return v8::String::NewFromTwoByte( - isolate, reinterpret_cast(val.data()), - v8::NewStringType::kNormal, val.size()) - .ToLocalChecked(); - } - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - base::string16* out) { - if (!val->IsString()) - return false; - - v8::String::Value s(isolate, val); - out->assign(reinterpret_cast(*s), s.length()); - return true; - } -}; - -inline v8::Local StringToV8(v8::Isolate* isolate, - const base::string16& input) { - return ConvertToV8(isolate, input).As(); -} - -} // namespace gin +#include "shell/common/gin_converters/string16_converter.h" namespace mate {