refactor: allocate api::Session on cpp heap (#48141)

This commit is contained in:
Robo
2025-08-25 18:52:06 +09:00
committed by GitHub
parent 0917ed5f6f
commit 3ccb1bc0a8
32 changed files with 632 additions and 293 deletions

View File

@@ -149,17 +149,16 @@ void SerialChooserContext::RevokePortPermissionWebInitiated(
auto* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
api::Session* session =
gin::WeakCell<api::Session>* session =
api::Session::FromBrowserContext(web_contents->GetBrowserContext());
if (session) {
if (session && session->Get()) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
auto details = gin_helper::Dictionary::CreateEmpty(isolate);
details.Set("port", it->second);
details.SetGetter("frame", render_frame_host);
details.Set("origin", origin.Serialize());
session->Emit("serial-port-revoked", details);
session->Get()->Emit("serial-port-revoked", details);
}
}

View File

@@ -143,7 +143,7 @@ SerialChooserController::~SerialChooserController() {
RunCallback(/*port=*/nullptr);
}
api::Session* SerialChooserController::GetSession() {
gin::WeakCell<api::Session>* SerialChooserController::GetSession() {
if (!web_contents_) {
return nullptr;
}
@@ -180,9 +180,10 @@ void SerialChooserController::OnPortAdded(
ports_.push_back(port.Clone());
api::Session* session = GetSession();
if (session) {
session->Emit("serial-port-added", port.Clone(), web_contents_.get());
gin::WeakCell<api::Session>* session = GetSession();
if (session && session->Get()) {
session->Get()->Emit("serial-port-added", port.Clone(),
web_contents_.get());
}
}
@@ -191,8 +192,11 @@ void SerialChooserController::OnPortRemoved(
const auto it = std::ranges::find(ports_, port.token,
&device::mojom::SerialPortInfo::token);
if (it != ports_.end()) {
if (api::Session* session = GetSession())
session->Emit("serial-port-removed", port.Clone(), web_contents_.get());
gin::WeakCell<api::Session>* session = GetSession();
if (session && session->Get()) {
session->Get()->Emit("serial-port-removed", port.Clone(),
web_contents_.get());
}
ports_.erase(it);
}
}
@@ -236,8 +240,9 @@ void SerialChooserController::OnGetDevices(
}
bool prevent_default = false;
if (api::Session* session = GetSession()) {
prevent_default = session->Emit(
gin::WeakCell<api::Session>* session = GetSession();
if (session && session->Get()) {
prevent_default = session->Get()->Emit(
"select-serial-port", ports_, web_contents_.get(),
base::BindRepeating(&SerialChooserController::OnDeviceChosen,
weak_factory_.GetWeakPtr()));

View File

@@ -24,6 +24,11 @@ class RenderFrameHost;
class WebContents;
} // namespace content
namespace gin {
template <typename T>
class WeakCell;
} // namespace gin
namespace electron {
namespace api {
@@ -64,7 +69,7 @@ class SerialChooserController final
bool powered) override;
private:
api::Session* GetSession();
gin::WeakCell<api::Session>* GetSession();
void GetDevices();
void OnGetDevices(std::vector<device::mojom::SerialPortInfoPtr> ports);
bool DisplayDevice(const device::mojom::SerialPortInfo& port) const;