Skip to content

Commit 1e2a200

Browse files
authored
feat: add support for webContents option in BrowserView (electron#26802)
* feat: add support for webContents option in BrowserView * tests: add tests
1 parent 6932e17 commit 1e2a200

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

shell/browser/api/electron_api_browser_view.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,17 @@ BrowserView::BrowserView(gin::Arguments* args,
7878
gin::Dictionary::CreateEmpty(isolate);
7979
options.Get(options::kWebPreferences, &web_preferences);
8080
web_preferences.Set("type", "browserView");
81-
gin::Handle<class WebContents> web_contents =
82-
WebContents::New(isolate, web_preferences);
81+
82+
v8::Local<v8::Value> value;
83+
84+
// Copy the webContents option to webPreferences. This is only used internally
85+
// to implement nativeWindowOpen option.
86+
if (options.Get("webContents", &value)) {
87+
web_preferences.SetHidden("webContents", value);
88+
}
89+
90+
auto web_contents =
91+
WebContents::CreateFromWebPreferences(args->isolate(), web_preferences);
8392

8493
web_contents_.Reset(isolate, web_contents.ToV8());
8594
api_web_contents_ = web_contents.get();

shell/browser/api/electron_api_web_contents.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,6 +3738,33 @@ gin::Handle<WebContents> WebContents::FromOrCreate(
37383738
return gin::CreateHandle(isolate, api_web_contents);
37393739
}
37403740

3741+
// static
3742+
gin::Handle<WebContents> WebContents::CreateFromWebPreferences(
3743+
v8::Isolate* isolate,
3744+
const gin_helper::Dictionary& web_preferences) {
3745+
// Check if webPreferences has |webContents| option.
3746+
gin::Handle<WebContents> web_contents;
3747+
if (web_preferences.GetHidden("webContents", &web_contents) &&
3748+
!web_contents.IsEmpty()) {
3749+
// Set webPreferences from options if using an existing webContents.
3750+
// These preferences will be used when the webContent launches new
3751+
// render processes.
3752+
auto* existing_preferences =
3753+
WebContentsPreferences::From(web_contents->web_contents());
3754+
base::DictionaryValue web_preferences_dict;
3755+
if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(),
3756+
&web_preferences_dict)) {
3757+
existing_preferences->Clear();
3758+
existing_preferences->Merge(web_preferences_dict);
3759+
}
3760+
} else {
3761+
// Create one if not.
3762+
web_contents = WebContents::New(isolate, web_preferences);
3763+
}
3764+
3765+
return web_contents;
3766+
}
3767+
37413768
// static
37423769
WebContents* WebContents::FromID(int32_t id) {
37433770
return GetAllWebContents().Lookup(id);

shell/browser/api/electron_api_web_contents.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class WebContents : public gin::Wrappable<WebContents>,
134134
v8::Isolate* isolate,
135135
content::WebContents* web_contents);
136136

137+
static gin::Handle<WebContents> CreateFromWebPreferences(
138+
v8::Isolate* isolate,
139+
const gin_helper::Dictionary& web_preferences);
140+
137141
// gin::Wrappable
138142
static gin::WrapperInfo kWrapperInfo;
139143
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(

shell/browser/api/electron_api_web_contents_view.cc

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,9 @@ v8::Local<v8::Function> WebContentsView::GetConstructor(v8::Isolate* isolate) {
9797
gin_helper::WrappableBase* WebContentsView::New(
9898
gin_helper::Arguments* args,
9999
const gin_helper::Dictionary& web_preferences) {
100-
// Check if BrowserWindow has passend |webContents| option to us.
101-
gin::Handle<WebContents> web_contents;
102-
if (web_preferences.GetHidden("webContents", &web_contents) &&
103-
!web_contents.IsEmpty()) {
104-
// Set webPreferences from options if using an existing webContents.
105-
// These preferences will be used when the webContent launches new
106-
// render processes.
107-
auto* existing_preferences =
108-
WebContentsPreferences::From(web_contents->web_contents());
109-
base::DictionaryValue web_preferences_dict;
110-
if (gin::ConvertFromV8(args->isolate(), web_preferences.GetHandle(),
111-
&web_preferences_dict)) {
112-
existing_preferences->Clear();
113-
existing_preferences->Merge(web_preferences_dict);
114-
}
115-
} else {
116-
// Create one if not.
117-
web_contents = WebContents::New(args->isolate(), web_preferences);
118-
}
100+
auto web_contents =
101+
WebContents::CreateFromWebPreferences(args->isolate(), web_preferences);
102+
119103
// Constructor call.
120104
auto* view = new WebContentsView(args->isolate(), web_contents);
121105
view->InitWithArgs(args);

spec-main/api-browser-view-spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ describe('BrowserView module', () => {
3939
expect(webContents.getAllWebContents()).to.have.length(0);
4040
});
4141

42+
it('can be created with an existing webContents', async () => {
43+
const wc = (webContents as any).create({ sandbox: true });
44+
await wc.loadURL('about:blank');
45+
46+
view = new BrowserView({ webContents: wc } as any);
47+
48+
expect(view.webContents.getURL()).to.equal('about:blank');
49+
});
50+
4251
describe('BrowserView.setBackgroundColor()', () => {
4352
it('does not throw for valid args', () => {
4453
view = new BrowserView();

0 commit comments

Comments
 (0)