diff --git a/filenames.gni b/filenames.gni index 2a2ad43785f4..530e33c45864 100644 --- a/filenames.gni +++ b/filenames.gni @@ -479,6 +479,7 @@ filenames = { "shell/browser/osr/osr_web_contents_view.h", "shell/browser/plugins/plugin_utils.cc", "shell/browser/plugins/plugin_utils.h", + "shell/browser/preload_script.cc", "shell/browser/preload_script.h", "shell/browser/protocol_registry.cc", "shell/browser/protocol_registry.h", diff --git a/shell/browser/preload_script.cc b/shell/browser/preload_script.cc new file mode 100644 index 000000000000..0f60687eba06 --- /dev/null +++ b/shell/browser/preload_script.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2025 Salesforce, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/browser/preload_script.h" + +#include "base/containers/fixed_flat_map.h" +#include "base/files/file_path.h" +#include "base/uuid.h" +#include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_helper/dictionary.h" + +namespace gin { + +using electron::PreloadScript; + +// static +v8::Local Converter::ToV8( + v8::Isolate* isolate, + const PreloadScript::ScriptType& in) { + using Val = PreloadScript::ScriptType; + static constexpr auto Lookup = base::MakeFixedFlatMap({ + {Val::kWebFrame, "frame"}, + {Val::kServiceWorker, "service-worker"}, + }); + return StringToV8(isolate, Lookup.at(in)); +} + +// static +bool Converter::FromV8( + v8::Isolate* isolate, + v8::Local val, + PreloadScript::ScriptType* out) { + using Val = PreloadScript::ScriptType; + static constexpr auto Lookup = base::MakeFixedFlatMap({ + {"frame", Val::kWebFrame}, + {"service-worker", Val::kServiceWorker}, + }); + return FromV8WithLookup(isolate, val, Lookup, out); +} + +// static +v8::Local Converter::ToV8( + v8::Isolate* isolate, + const PreloadScript& script) { + gin::Dictionary dict(isolate, v8::Object::New(isolate)); + dict.Set("filePath", script.file_path.AsUTF8Unsafe()); + dict.Set("id", script.id); + dict.Set("type", script.script_type); + return ConvertToV8(isolate, dict).As(); +} + +// static +bool Converter::FromV8(v8::Isolate* isolate, + v8::Local val, + PreloadScript* out) { + gin_helper::Dictionary options; + if (!ConvertFromV8(isolate, val, &options)) + return false; + if (PreloadScript::ScriptType script_type; + options.Get("type", &script_type)) { + out->script_type = script_type; + } else { + return false; + } + if (base::FilePath file_path; options.Get("filePath", &file_path)) { + out->file_path = file_path; + } else { + return false; + } + if (std::string id; options.Get("id", &id)) { + out->id = id; + } else { + out->id = base::Uuid::GenerateRandomV4().AsLowercaseString(); + } + if (bool deprecated; options.Get("_deprecated", &deprecated)) { + out->deprecated = deprecated; + } + return true; +} + +} // namespace gin diff --git a/shell/browser/preload_script.h b/shell/browser/preload_script.h index bae243c7cafc..62e945a5698c 100644 --- a/shell/browser/preload_script.h +++ b/shell/browser/preload_script.h @@ -5,14 +5,11 @@ #ifndef ELECTRON_SHELL_BROWSER_PRELOAD_SCRIPT_H_ #define ELECTRON_SHELL_BROWSER_PRELOAD_SCRIPT_H_ -#include +#include -#include "base/containers/fixed_flat_map.h" #include "base/files/file_path.h" -#include "base/uuid.h" #include "gin/converter.h" -#include "shell/common/gin_converters/file_path_converter.h" -#include "shell/common/gin_helper/dictionary.h" +#include "v8/include/v8-forward.h" namespace electron { @@ -36,67 +33,19 @@ using electron::PreloadScript; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const PreloadScript::ScriptType& in) { - using Val = PreloadScript::ScriptType; - static constexpr auto Lookup = - base::MakeFixedFlatMap({ - {Val::kWebFrame, "frame"}, - {Val::kServiceWorker, "service-worker"}, - }); - return StringToV8(isolate, Lookup.at(in)); - } - + const PreloadScript::ScriptType& in); static bool FromV8(v8::Isolate* isolate, v8::Local val, - PreloadScript::ScriptType* out) { - using Val = PreloadScript::ScriptType; - static constexpr auto Lookup = - base::MakeFixedFlatMap({ - {"frame", Val::kWebFrame}, - {"service-worker", Val::kServiceWorker}, - }); - return FromV8WithLookup(isolate, val, Lookup, out); - } + PreloadScript::ScriptType* out); }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const PreloadScript& script) { - gin::Dictionary dict(isolate, v8::Object::New(isolate)); - dict.Set("filePath", script.file_path.AsUTF8Unsafe()); - dict.Set("id", script.id); - dict.Set("type", script.script_type); - return ConvertToV8(isolate, dict).As(); - } - + const PreloadScript& script); static bool FromV8(v8::Isolate* isolate, v8::Local val, - PreloadScript* out) { - gin_helper::Dictionary options; - if (!ConvertFromV8(isolate, val, &options)) - return false; - if (PreloadScript::ScriptType script_type; - options.Get("type", &script_type)) { - out->script_type = script_type; - } else { - return false; - } - if (base::FilePath file_path; options.Get("filePath", &file_path)) { - out->file_path = file_path; - } else { - return false; - } - if (std::string id; options.Get("id", &id)) { - out->id = id; - } else { - out->id = base::Uuid::GenerateRandomV4().AsLowercaseString(); - } - if (bool deprecated; options.Get("_deprecated", &deprecated)) { - out->deprecated = deprecated; - } - return true; - } + PreloadScript* out); }; } // namespace gin