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
commit 3ccb1bc0a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 632 additions and 293 deletions

View file

@ -87,7 +87,6 @@
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/browser/api/electron_api_browser_window.h"
#include "shell/browser/api/electron_api_debugger.h"
#include "shell/browser/api/electron_api_session.h"
#include "shell/browser/api/electron_api_web_frame_main.h"
#include "shell/browser/api/frame_subscriber.h"
#include "shell/browser/api/message_port.h"
@ -755,8 +754,7 @@ WebContents::WebContents(v8::Isolate* isolate,
script_executor_ = std::make_unique<extensions::ScriptExecutor>(web_contents);
#endif
auto session = Session::CreateFrom(isolate, GetBrowserContext());
session_.Reset(isolate, session.ToV8());
session_ = Session::CreateFrom(isolate, GetBrowserContext());
SetUserAgent(GetBrowserContext()->GetUserAgent());
@ -778,9 +776,9 @@ WebContents::WebContents(v8::Isolate* isolate,
{
DCHECK(type != Type::kRemote)
<< "Can't take ownership of a remote WebContents";
auto session = Session::CreateFrom(isolate, GetBrowserContext());
session_.Reset(isolate, session.ToV8());
InitWithSessionAndOptions(isolate, std::move(web_contents), session,
session_ = Session::CreateFrom(isolate, GetBrowserContext());
InitWithSessionAndOptions(isolate, std::move(web_contents),
session_->browser_context(),
gin::Dictionary::CreateEmpty(isolate));
}
@ -829,15 +827,15 @@ WebContents::WebContents(v8::Isolate* isolate,
// Obtain the session.
std::string partition;
gin_helper::Handle<api::Session> session;
if (options.Get("session", &session) && !session.IsEmpty()) {
api::Session* session = nullptr;
if (options.Get("session", &session) && session) {
} else if (options.Get("partition", &partition)) {
session = Session::FromPartition(isolate, partition);
} else {
// Use the default session if not specified.
session = Session::FromPartition(isolate, "");
}
session_.Reset(isolate, session.ToV8());
session_ = session;
std::unique_ptr<content::WebContents> web_contents;
if (is_guest()) {
@ -886,7 +884,8 @@ WebContents::WebContents(v8::Isolate* isolate,
web_contents = content::WebContents::Create(params);
}
InitWithSessionAndOptions(isolate, std::move(web_contents), session, options);
InitWithSessionAndOptions(isolate, std::move(web_contents),
session->browser_context(), options);
}
void WebContents::InitZoomController(content::WebContents* web_contents,
@ -907,10 +906,10 @@ void WebContents::InitZoomController(content::WebContents* web_contents,
void WebContents::InitWithSessionAndOptions(
v8::Isolate* isolate,
std::unique_ptr<content::WebContents> owned_web_contents,
gin_helper::Handle<api::Session> session,
ElectronBrowserContext* browser_context,
const gin_helper::Dictionary& options) {
Observe(owned_web_contents.get());
InitWithWebContents(std::move(owned_web_contents), session->browser_context(),
InitWithWebContents(std::move(owned_web_contents), browser_context,
is_guest());
inspectable_web_contents_->GetView()->SetDelegate(this);
@ -3754,7 +3753,12 @@ v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow(
}
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, session_);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> wrapper;
if (!session_->GetWrapper(isolate).ToLocal(&wrapper)) {
return v8::Null(isolate);
}
return v8::Local<v8::Value>::New(isolate, wrapper);
}
content::WebContents* WebContents::HostWebContents() const {