refactor: add EmitWarning(v8::Isolate*) helper (#43722)

* refactor: add EmitWarning(Isolate*, ...) warning

* chore: remove EmitWarning(node::Environment*, ...)

* chore: add code comments

* fixup! refactor: add EmitWarning(Isolate*, ...) warning

* chore: remove unused node #includes
This commit is contained in:
Charles Kerr 2024-09-16 15:53:04 -05:00 committed by GitHub
parent 7d4f202c1c
commit 05dfd14913
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 72 additions and 89 deletions

View file

@ -12,14 +12,14 @@
#include "base/command_line.h"
#include "base/environment.h"
#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "components/crash/core/common/crash_key.h"
#include "content/public/common/content_switches.h"
#include "electron/buildflags/buildflags.h"
#include "electron/fuses.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/electron_constants.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/common/process_util.h"
#include "third_party/crashpad/crashpad/client/annotation.h"
@ -54,13 +54,10 @@ void SetCrashKey(const std::string& key, const std::string& value) {
// Chrome DCHECK()s if we try to set an annotation with a name longer than
// the max.
if (key.size() >= kMaxCrashKeyNameLength) {
node::Environment* env =
node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate());
EmitWarning(
env,
base::StringPrintf("The crash key name, \"%s\", is longer than %" PRIu32
" bytes, ignoring it.",
key.c_str(), kMaxCrashKeyNameLength),
util::EmitWarning(
base::StrCat({"The crash key name, '", key, "', is longer than ",
base::NumberToString(kMaxCrashKeyNameLength),
" bytes, ignoring it."}),
"electron");
return;
}

View file

@ -15,7 +15,7 @@
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_converters/optional_converter.h"
#include "shell/common/node_includes.h"
#include "shell/common/process_util.h"
#include "shell/common/node_util.h"
namespace gin {
@ -146,13 +146,11 @@ v8::Local<v8::Value> Converter<electron::OffscreenSharedTextureValue>::ToV8(
data.SetSecondPassCallback([](const v8::WeakCallbackInfo<
OffscreenReleaseHolderMonitor>& data) {
auto* iso = data.GetIsolate();
node::Environment* env = node::Environment::GetCurrent(iso);
// Emit warning only once
static std::once_flag flag;
std::call_once(flag, [=] {
electron::EmitWarning(
env,
electron::util::EmitWarning(
iso,
"[OSR TEXTURE LEAKED] When using OSR with "
"`useSharedTexture`, `texture.release()` "
"must be called explicitly as soon as the texture is "

View file

@ -10,8 +10,6 @@
#include <sys/time.h>
#include <sys/types.h>
#include "shell/common/node_includes.h"
namespace electron {
NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)

View file

@ -6,6 +6,9 @@
#include "base/logging.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"
namespace electron::util {
@ -44,4 +47,22 @@ v8::MaybeLocal<v8::Value> CompileAndCall(
return ret;
}
void EmitWarning(const std::string_view warning_msg,
const std::string_view warning_type) {
EmitWarning(JavascriptEnvironment::GetIsolate(), warning_msg, warning_type);
}
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<void(std::string_view, std::string_view,
std::string_view)>
emit_warning;
process.Get("emitWarning", &emit_warning);
emit_warning.Run(warning_msg, warning_type, "");
}
} // namespace electron::util

View file

@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_COMMON_NODE_UTIL_H_
#define ELECTRON_SHELL_COMMON_NODE_UTIL_H_
#include <string_view>
#include <vector>
#include "v8/include/v8-forward.h"
@ -15,6 +16,15 @@ class Environment;
namespace electron::util {
// Emit a warning via node's process.emitWarning()
void EmitWarning(v8::Isolate* isolate,
std::string_view warning_msg,
std::string_view warning_type);
// Emit a warning via node's process.emitWarning(),
// using JavscriptEnvironment's isolate
void EmitWarning(std::string_view warning_msg, std::string_view warning_type);
// 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.

View file

@ -9,26 +9,9 @@
#include "base/command_line.h"
#include "content/public/common/content_switches.h"
#include "gin/dictionary.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/node_includes.h"
namespace electron {
void EmitWarning(node::Environment* env,
const std::string& warning_msg,
const std::string& warning_type) {
v8::HandleScope scope(env->isolate());
gin::Dictionary process(env->isolate(), env->process_object());
base::RepeatingCallback<void(std::string_view, std::string_view,
std::string_view)>
emit_warning;
process.Get("emitWarning", &emit_warning);
emit_warning.Run(warning_msg, warning_type, "");
}
std::string GetProcessType() {
auto* command_line = base::CommandLine::ForCurrentProcess();
return command_line->GetSwitchValueASCII(switches::kProcessType);

View file

@ -6,17 +6,10 @@
#define ELECTRON_SHELL_COMMON_PROCESS_UTIL_H_
#include <string>
namespace node {
class Environment;
}
#include <string_view>
namespace electron {
void EmitWarning(node::Environment* env,
const std::string& warning_msg,
const std::string& warning_type);
std::string GetProcessType();
bool IsBrowserProcess();

View file

@ -10,7 +10,6 @@
#include "base/strings/string_util.h"
#include "net/base/data_url.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/node_includes.h"
#include "shell/common/skia_util.h"
#include "shell/common/thread_restrictions.h"
#include "third_party/skia/include/core/SkBitmap.h"