Compare commits

..

1 Commits

Author SHA1 Message Date
Charles Kerr
14fbf5086f refactor: replace std::to_string() with base::NumberToString() 2026-02-25 09:57:33 -06:00
6 changed files with 60 additions and 33 deletions

View File

@@ -148,6 +148,15 @@ BaseWindow::~BaseWindow() {
void BaseWindow::InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
gin_helper::TrackableObject<BaseWindow>::InitWith(isolate, wrapper);
// We can only append this window to parent window's child windows after this
// window's JS wrapper gets initialized.
if (!parent_window_.IsEmpty()) {
gin_helper::Handle<BaseWindow> parent;
gin::ConvertFromV8(isolate, GetParentWindow(), &parent);
DCHECK(!parent.IsEmpty());
parent->child_windows_.Set(isolate, weak_map_id(), wrapper);
}
// Reference this object in case it got garbage collected.
self_ref_.Reset(isolate, wrapper);
}
@@ -173,7 +182,7 @@ void BaseWindow::OnWindowClosed() {
Emit("closed");
parent_window_.Reset();
RemoveFromParentChildWindows();
// Destroy the native class when window is closed.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
@@ -761,11 +770,14 @@ void BaseWindow::SetParentWindow(v8::Local<v8::Value> value,
gin_helper::Handle<BaseWindow> parent;
if (value->IsNull() || value->IsUndefined()) {
RemoveFromParentChildWindows();
parent_window_.Reset();
window_->SetParentWindow(nullptr);
} else if (gin::ConvertFromV8(isolate(), value, &parent)) {
parent_window_.Reset(isolate(), parent.ToV8());
window_->SetParentWindow(parent->window());
RemoveFromParentChildWindows();
parent_window_.Reset(isolate(), value);
window_->SetParentWindow(parent->window_.get());
parent->child_windows_.Set(isolate(), weak_map_id(), GetWrapper());
} else {
args->ThrowTypeError("Must pass BaseWindow instance or null");
}
@@ -977,28 +989,15 @@ v8::Local<v8::Value> BaseWindow::GetContentView() const {
return v8::Local<v8::Value>::New(isolate(), content_view_);
}
BaseWindow* BaseWindow::GetParentWindow() const {
v8::Local<v8::Value> BaseWindow::GetParentWindow() const {
if (parent_window_.IsEmpty())
return nullptr;
v8::HandleScope scope{isolate()};
auto local = v8::Local<v8::Value>::New(isolate(), parent_window_);
BaseWindow* parent = nullptr;
gin::ConvertFromV8(isolate(), local, &parent);
return parent;
return v8::Null(isolate());
else
return v8::Local<v8::Value>::New(isolate(), parent_window_);
}
std::vector<BaseWindow*> BaseWindow::GetChildWindows() const {
std::vector<BaseWindow*> children;
auto* const isolate = this->isolate();
v8::HandleScope scope{isolate};
for (auto wrapper : BaseWindow::GetAll(isolate)) {
BaseWindow* win = nullptr;
gin::ConvertFromV8(isolate, wrapper, &win);
if (win && win->GetParentWindow() == this)
children.emplace_back(win);
}
return children;
std::vector<v8::Local<v8::Object>> BaseWindow::GetChildWindows() const {
return child_windows_.Values(isolate());
}
bool BaseWindow::IsModal() const {
@@ -1146,6 +1145,21 @@ void BaseWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options,
}
#endif
void BaseWindow::RemoveFromParentChildWindows() {
if (parent_window_.IsEmpty())
return;
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin_helper::Handle<BaseWindow> parent;
if (!gin::ConvertFromV8(isolate, GetParentWindow(), &parent) ||
parent.IsEmpty()) {
return;
}
parent->child_windows_.Remove(weak_map_id());
}
// static
gin_helper::WrappableBase* BaseWindow::New(gin::Arguments* const args) {
auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());

View File

@@ -8,6 +8,7 @@
#include <array>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
@@ -232,8 +233,8 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
// Public getters of NativeWindow.
v8::Local<v8::Value> GetContentView() const;
BaseWindow* GetParentWindow() const;
std::vector<BaseWindow*> GetChildWindows() const;
v8::Local<v8::Value> GetParentWindow() const;
std::vector<v8::Local<v8::Object>> GetChildWindows() const;
bool IsModal() const;
// Extra APIs added in JS.
@@ -269,6 +270,9 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
private:
// Helpers.
// Remove this window from parent window's |child_windows_|.
void RemoveFromParentChildWindows();
template <typename... Args>
void EmitEventSoon(std::string_view eventName) {
content::GetUIThreadTaskRunner({})->PostTask(
@@ -285,6 +289,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
v8::Global<v8::Value> content_view_;
v8::Global<v8::Value> menu_;
v8::Global<v8::Value> parent_window_;
KeyWeakMap<int> child_windows_;
std::unique_ptr<NativeWindow> window_;

View File

@@ -209,8 +209,12 @@ void BrowserWindow::UpdateWindowControlsOverlay(
void BrowserWindow::CloseImmediately() {
// Close all child windows before closing current window.
for (BaseWindow* child : GetChildWindows())
child->window()->CloseImmediately();
v8::HandleScope handle_scope(isolate());
for (v8::Local<v8::Value> value : GetChildWindows()) {
gin_helper::Handle<BrowserWindow> child;
if (gin::ConvertFromV8(isolate(), value, &child) && !child.IsEmpty())
child->window()->CloseImmediately();
}
BaseWindow::CloseImmediately();
}

View File

@@ -9,6 +9,7 @@
#include "base/environment.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/browser/browser_task_traits.h"
@@ -279,7 +280,8 @@ void OnDeploymentCompleted(std::unique_ptr<DeploymentCallbackData> data,
HRESULT error_code;
hr = async_info->get_ErrorCode(&error_code);
if (SUCCEEDED(hr)) {
error += " (" + std::to_string(static_cast<int>(error_code)) + ")";
error +=
" (" + base::NumberToString(static_cast<int>(error_code)) + ")";
}
}
}
@@ -798,10 +800,10 @@ v8::Local<v8::Value> GetPackageInfo() {
ABI::Windows::ApplicationModel::PackageVersion pkg_version;
hr = package_id->get_Version(&pkg_version);
if (SUCCEEDED(hr)) {
std::string version = std::to_string(pkg_version.Major) + "." +
std::to_string(pkg_version.Minor) + "." +
std::to_string(pkg_version.Build) + "." +
std::to_string(pkg_version.Revision);
std::string version = base::NumberToString(pkg_version.Major) + "." +
base::NumberToString(pkg_version.Minor) + "." +
base::NumberToString(pkg_version.Build) + "." +
base::NumberToString(pkg_version.Revision);
result.Set("version", version);
}
}

View File

@@ -23,6 +23,7 @@
#include "base/json/json_reader.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/threading/scoped_blocking_call.h"
@@ -2659,7 +2660,7 @@ void WebContents::RestoreHistory(
thrower.ThrowError(
"Failed to restore navigation history: Invalid navigation entry at "
"index " +
std::to_string(index) + ".");
base::NumberToString(index) + ".");
return;
}

View File

@@ -18,6 +18,7 @@
#include "base/environment.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
@@ -192,7 +193,7 @@ void V8OOMErrorCallback(const char* location, const v8::OOMDetails& details) {
#if !IS_MAS_BUILD()
electron::crash_keys::SetCrashKey("electron.v8-oom.is_heap_oom",
std::to_string(details.is_heap_oom));
base::NumberToString(details.is_heap_oom));
if (location) {
electron::crash_keys::SetCrashKey("electron.v8-oom.location", location);
}