feat: add site instance

This commit is contained in:
alice
2024-10-24 14:00:09 -07:00
parent 739df86774
commit 6c81459aad
2 changed files with 22 additions and 2 deletions

View File

@@ -33,6 +33,11 @@
same `partition`. If there is no `persist:` prefix, the page will use an
in-memory session. By assigning the same `partition`, multiple pages can share
the same session. Default is the default session.
* `sameSiteInstanceAs` WebContents (optional) - Must be a WebContents in the same `session`
or `partition` as this webPreferences. Used to minimize overhead when creating a webContents
for a known URL as it allow some of the resources to be shared, namely the second webContents
will be created in the same process as the original webContents as long as they are same-site.
This is an advanced property we recommend reading about in [Chromium's Process Model](https://chromium.googlesource.com/chromium/src/+/main/docs/process_model_and_site_isolation.md)
* `zoomFactor` number (optional) - The default zoom factor of the page, `3.0` represents
`300%`. Default is `1.0`.
* `javascript` boolean (optional) - Enables JavaScript support. Default is `true`.

View File

@@ -785,6 +785,19 @@ WebContents::WebContents(v8::Isolate* isolate,
}
session_.Reset(isolate, session.ToV8());
content::WebContents* site_instance_web_contents;
scoped_refptr<content::SiteInstance> site_instance_to_use;
if (options.Get("sameSiteInstanceAs", &site_instance_web_contents)) {
// Has to be the same BrowserContext or strange things happen
if (site_instance_web_contents->GetBrowserContext() ==
session->browser_context()) {
auto* instance = site_instance_web_contents->GetSiteInstance();
if (instance) {
site_instance_to_use = instance;
}
}
}
std::unique_ptr<content::WebContents> web_contents;
if (is_guest()) {
scoped_refptr<content::SiteInstance> site_instance =
@@ -816,7 +829,8 @@ WebContents::WebContents(v8::Isolate* isolate,
options.GetHidden(options::kBackgroundColor, &background_color);
bool transparent = ParseCSSColor(background_color) == SK_ColorTRANSPARENT;
content::WebContents::CreateParams params(session->browser_context());
content::WebContents::CreateParams params(session->browser_context(),
site_instance_to_use);
auto* view = new OffScreenWebContentsView(
transparent,
base::BindRepeating(&WebContents::OnPaint, base::Unretained(this)));
@@ -826,7 +840,8 @@ WebContents::WebContents(v8::Isolate* isolate,
web_contents = content::WebContents::Create(params);
view->SetWebContents(web_contents.get());
} else {
content::WebContents::CreateParams params(session->browser_context());
content::WebContents::CreateParams params(session->browser_context(),
site_instance_to_use);
params.initially_hidden = !initially_shown;
web_contents = content::WebContents::Create(params);
}