From c24f330b4588f39a7619fa1563cfdf93cbcd7a6e Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 1 May 2025 09:44:59 -0500 Subject: [PATCH] refactor: add EmitDeprecationWarning helper (#46879) * refactor: add EmitDeprecationWarning helper Also switches EmitWarning to using Node's ProcessEmitWarningGeneric Co-authored-by: David Sanders * chore: use node namespace for function call Co-authored-by: David Sanders --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: David Sanders --- shell/browser/api/electron_api_web_request.cc | 5 ++-- shell/browser/native_window_mac.mm | 5 ++-- shell/common/api/electron_api_native_image.cc | 5 ++-- shell/common/node_util.cc | 26 ++++++++++++------- shell/common/node_util.h | 12 ++++++++- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/shell/browser/api/electron_api_web_request.cc b/shell/browser/api/electron_api_web_request.cc index 095f96397aee..c091e91489e2 100644 --- a/shell/browser/api/electron_api_web_request.cc +++ b/shell/browser/api/electron_api_web_request.cc @@ -666,10 +666,9 @@ void WebRequest::SetListener(Event event, } if (filter_include_patterns.empty()) { - util::EmitWarning( + util::EmitDeprecationWarning( "The urls array in WebRequestFilter is empty, which is deprecated. " - "Please use '' to match all URLs.", - "DeprecationWarning"); + "Please use '' to match all URLs."); filter_include_patterns.insert(""); } diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index ff97e84c0b1a..62b350fda7fa 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -182,9 +182,8 @@ NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options, #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" if (windowType == "textured" && (transparent() || !has_frame())) { - util::EmitWarning( - "The 'textured' window type is deprecated and will be removed", - "DeprecationWarning"); + util::EmitDeprecationWarning( + "The 'textured' window type is deprecated and will be removed"); styleMask |= NSWindowStyleMaskTexturedBackground; } #pragma clang diagnostic pop diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index da34a3158c3f..ffe3267b2999 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -286,9 +286,8 @@ v8::Local NativeImage::GetBitmap(gin::Arguments* args) { if (!deprecated_warning_issued) { deprecated_warning_issued = true; - util::EmitWarning(isolate_, - "getBitmap() is deprecated, use toBitmap() instead.", - "DeprecationWarning"); + util::EmitDeprecationWarning( + isolate_, "getBitmap() is deprecated, use toBitmap() instead."); } return ToBitmap(args); diff --git a/shell/common/node_util.cc b/shell/common/node_util.cc index 055670cc4ddf..89eaf8894219 100644 --- a/shell/common/node_util.cc +++ b/shell/common/node_util.cc @@ -12,10 +12,10 @@ #include "base/strings/string_number_conversions.h" #include "base/values.h" #include "gin/converter.h" -#include "gin/dictionary.h" #include "shell/browser/javascript_environment.h" #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/node_includes.h" +#include "third_party/electron_node/src/node_process-inl.h" namespace electron::util { @@ -65,14 +65,22 @@ void EmitWarning(const std::string_view warning_msg, void EmitWarning(v8::Isolate* isolate, const std::string_view warning_msg, const std::string_view warning_type) { - v8::HandleScope scope{isolate}; - gin::Dictionary process{ - isolate, node::Environment::GetCurrent(isolate)->process_object()}; - base::RepeatingCallback - emit_warning; - process.Get("emitWarning", &emit_warning); - emit_warning.Run(warning_msg, warning_type, ""); + node::ProcessEmitWarningGeneric(node::Environment::GetCurrent(isolate), + warning_msg, warning_type); +} + +void EmitDeprecationWarning(const std::string_view warning_msg, + const std::string_view deprecation_code) { + EmitDeprecationWarning(JavascriptEnvironment::GetIsolate(), warning_msg, + deprecation_code); +} + +void EmitDeprecationWarning(v8::Isolate* isolate, + const std::string_view warning_msg, + const std::string_view deprecation_code) { + node::ProcessEmitWarningGeneric(node::Environment::GetCurrent(isolate), + warning_msg, "DeprecationWarning", + deprecation_code); } node::Environment* CreateEnvironment(v8::Isolate* isolate, diff --git a/shell/common/node_util.h b/shell/common/node_util.h index 36fcfdf940ba..fdc92c0f6b9e 100644 --- a/shell/common/node_util.h +++ b/shell/common/node_util.h @@ -30,9 +30,19 @@ void EmitWarning(v8::Isolate* isolate, std::string_view warning_type); // Emit a warning via node's process.emitWarning(), -// using JavscriptEnvironment's isolate +// using JavascriptEnvironment's isolate void EmitWarning(std::string_view warning_msg, std::string_view warning_type); +// Emit a deprecation warning via node's process.emitWarning() +void EmitDeprecationWarning(v8::Isolate* isolate, + std::string_view warning_msg, + std::string_view deprecation_code = ""); + +// Emit a deprecation warning via node's process.emitWarning(), +// using JavascriptEnvironment's isolate +void EmitDeprecationWarning(std::string_view warning_msg, + std::string_view deprecation_code = ""); + // Run a script with JS source bundled inside the binary as if it's wrapped // in a function called with a null receiver and arguments specified in C++. // The returned value is empty if an exception is encountered.