mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
refactor: allocate api::Session on cpp heap (#48141)
This commit is contained in:
@@ -293,14 +293,15 @@ void HidChooserContext::RevokeDevicePermission(
|
||||
} else {
|
||||
RevokeEphemeralDevicePermission(origin, device);
|
||||
}
|
||||
api::Session* session = api::Session::FromBrowserContext(browser_context_);
|
||||
if (session) {
|
||||
gin::WeakCell<api::Session>* session =
|
||||
api::Session::FromBrowserContext(browser_context_);
|
||||
if (session && session->Get()) {
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
auto details = gin_helper::Dictionary::CreateEmpty(isolate);
|
||||
details.Set("device", device.Clone());
|
||||
details.Set("origin", origin.Serialize());
|
||||
session->Emit("hid-device-revoked", details);
|
||||
session->Get()->Emit("hid-device-revoked", details);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ const std::string& HidChooserController::PhysicalDeviceIdFromDeviceInfo(
|
||||
: device.physical_device_id;
|
||||
}
|
||||
|
||||
api::Session* HidChooserController::GetSession() {
|
||||
gin::WeakCell<api::Session>* HidChooserController::GetSession() {
|
||||
if (!web_contents()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -137,8 +137,8 @@ void HidChooserController::OnDeviceAdded(
|
||||
return;
|
||||
|
||||
if (AddDeviceInfo(device)) {
|
||||
api::Session* session = GetSession();
|
||||
if (session) {
|
||||
gin::WeakCell<api::Session>* session = GetSession();
|
||||
if (session && session->Get()) {
|
||||
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id_);
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
@@ -146,7 +146,7 @@ void HidChooserController::OnDeviceAdded(
|
||||
.Set("device", device.Clone())
|
||||
.Set("frame", rfh)
|
||||
.Build();
|
||||
session->Emit("hid-device-added", details);
|
||||
session->Get()->Emit("hid-device-added", details);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,8 +156,8 @@ void HidChooserController::OnDeviceRemoved(
|
||||
if (!base::Contains(items_, PhysicalDeviceIdFromDeviceInfo(device)))
|
||||
return;
|
||||
|
||||
api::Session* session = GetSession();
|
||||
if (session) {
|
||||
gin::WeakCell<api::Session>* session = GetSession();
|
||||
if (session && session->Get()) {
|
||||
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id_);
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
@@ -165,7 +165,7 @@ void HidChooserController::OnDeviceRemoved(
|
||||
.Set("device", device.Clone())
|
||||
.Set("frame", rfh)
|
||||
.Build();
|
||||
session->Emit("hid-device-removed", details);
|
||||
session->Get()->Emit("hid-device-removed", details);
|
||||
}
|
||||
RemoveDeviceInfo(device);
|
||||
}
|
||||
@@ -239,8 +239,8 @@ void HidChooserController::OnGotDevices(
|
||||
observation_.Observe(chooser_context_.get());
|
||||
|
||||
bool prevent_default = false;
|
||||
api::Session* session = GetSession();
|
||||
if (session) {
|
||||
gin::WeakCell<api::Session>* session = GetSession();
|
||||
if (session && session->Get()) {
|
||||
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id_);
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
@@ -248,10 +248,10 @@ void HidChooserController::OnGotDevices(
|
||||
.Set("deviceList", devicesToDisplay)
|
||||
.Set("frame", rfh)
|
||||
.Build();
|
||||
prevent_default =
|
||||
session->Emit("select-hid-device", details,
|
||||
base::BindRepeating(&HidChooserController::OnDeviceChosen,
|
||||
weak_factory_.GetWeakPtr()));
|
||||
prevent_default = session->Get()->Emit(
|
||||
"select-hid-device", details,
|
||||
base::BindRepeating(&HidChooserController::OnDeviceChosen,
|
||||
weak_factory_.GetWeakPtr()));
|
||||
}
|
||||
if (!prevent_default) {
|
||||
RunCallback({});
|
||||
|
||||
@@ -28,7 +28,9 @@ class WebContents;
|
||||
|
||||
namespace gin {
|
||||
class Arguments;
|
||||
}
|
||||
template <typename T>
|
||||
class WeakCell;
|
||||
} // namespace gin
|
||||
|
||||
namespace electron {
|
||||
namespace api {
|
||||
@@ -78,7 +80,7 @@ class HidChooserController
|
||||
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
|
||||
|
||||
private:
|
||||
api::Session* GetSession();
|
||||
gin::WeakCell<api::Session>* GetSession();
|
||||
void OnGotDevices(std::vector<device::mojom::HidDeviceInfoPtr> devices);
|
||||
bool DisplayDevice(const device::mojom::HidDeviceInfo& device) const;
|
||||
bool FilterMatchesAny(const device::mojom::HidDeviceInfo& device) const;
|
||||
|
||||
Reference in New Issue
Block a user