preload_script.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2025 Salesforce, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_PRELOAD_SCRIPT_H_
  5. #define ELECTRON_SHELL_BROWSER_PRELOAD_SCRIPT_H_
  6. #include <string_view>
  7. #include "base/containers/fixed_flat_map.h"
  8. #include "base/files/file_path.h"
  9. #include "base/uuid.h"
  10. #include "gin/converter.h"
  11. #include "shell/common/gin_converters/file_path_converter.h"
  12. #include "shell/common/gin_helper/dictionary.h"
  13. namespace electron {
  14. struct PreloadScript {
  15. enum class ScriptType { kWebFrame, kServiceWorker };
  16. std::string id;
  17. ScriptType script_type;
  18. base::FilePath file_path;
  19. // If set, use the deprecated validation behavior of Session.setPreloads
  20. bool deprecated = false;
  21. };
  22. } // namespace electron
  23. namespace gin {
  24. using electron::PreloadScript;
  25. template <>
  26. struct Converter<PreloadScript::ScriptType> {
  27. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  28. const PreloadScript::ScriptType& in) {
  29. using Val = PreloadScript::ScriptType;
  30. static constexpr auto Lookup =
  31. base::MakeFixedFlatMap<Val, std::string_view>({
  32. {Val::kWebFrame, "frame"},
  33. {Val::kServiceWorker, "service-worker"},
  34. });
  35. return StringToV8(isolate, Lookup.at(in));
  36. }
  37. static bool FromV8(v8::Isolate* isolate,
  38. v8::Local<v8::Value> val,
  39. PreloadScript::ScriptType* out) {
  40. using Val = PreloadScript::ScriptType;
  41. static constexpr auto Lookup =
  42. base::MakeFixedFlatMap<std::string_view, Val>({
  43. {"frame", Val::kWebFrame},
  44. {"service-worker", Val::kServiceWorker},
  45. });
  46. return FromV8WithLookup(isolate, val, Lookup, out);
  47. }
  48. };
  49. template <>
  50. struct Converter<PreloadScript> {
  51. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  52. const PreloadScript& script) {
  53. gin::Dictionary dict(isolate, v8::Object::New(isolate));
  54. dict.Set("filePath", script.file_path.AsUTF8Unsafe());
  55. dict.Set("id", script.id);
  56. dict.Set("type", script.script_type);
  57. return ConvertToV8(isolate, dict).As<v8::Object>();
  58. }
  59. static bool FromV8(v8::Isolate* isolate,
  60. v8::Local<v8::Value> val,
  61. PreloadScript* out) {
  62. gin_helper::Dictionary options;
  63. if (!ConvertFromV8(isolate, val, &options))
  64. return false;
  65. if (PreloadScript::ScriptType script_type;
  66. options.Get("type", &script_type)) {
  67. out->script_type = script_type;
  68. } else {
  69. return false;
  70. }
  71. if (base::FilePath file_path; options.Get("filePath", &file_path)) {
  72. out->file_path = file_path;
  73. } else {
  74. return false;
  75. }
  76. if (std::string id; options.Get("id", &id)) {
  77. out->id = id;
  78. } else {
  79. out->id = base::Uuid::GenerateRandomV4().AsLowercaseString();
  80. }
  81. if (bool deprecated; options.Get("_deprecated", &deprecated)) {
  82. out->deprecated = deprecated;
  83. }
  84. return true;
  85. }
  86. };
  87. } // namespace gin
  88. #endif // ELECTRON_SHELL_BROWSER_PRELOAD_SCRIPT_H_