Skip to content

Commit

Permalink
fix: assign session to remote webContents (electron#23989)
Browse files Browse the repository at this point in the history
* fix: assign session to remote webContents

* fix: test name
  • Loading branch information
sentialx committed Jul 30, 2020
1 parent 8a4d8da commit 2d44745
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ WebContents::WebContents(v8::Isolate* isolate,
: content::WebContentsObserver(web_contents),
type_(Type::REMOTE),
weak_factory_(this) {
auto session = Session::CreateFrom(isolate, GetBrowserContext());
session_.Reset(isolate, session.ToV8());

web_contents->SetUserAgentOverride(blink::UserAgentOverride::UserAgentOnly(
GetBrowserContext()->GetUserAgent()),
false);
Expand Down
12 changes: 11 additions & 1 deletion spec-main/extensions-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { session, BrowserWindow, ipcMain, WebContents, Extension } from 'electron/main';
import { app, session, BrowserWindow, ipcMain, WebContents, Extension } from 'electron/main';
import { closeAllWindows, closeWindow } from './window-helpers';
import * as http from 'http';
import { AddressInfo } from 'net';
Expand Down Expand Up @@ -282,6 +282,16 @@ describe('chrome extensions', () => {
});
});

it('has session in background page', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
await customSession.loadExtension(path.join(fixtures, 'extensions', 'persistent-background-page'));
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } });
const promise = emittedOnce(app, 'web-contents-created');
await w.loadURL(`about:blank`);
const [, bgPageContents] = await promise;
expect(bgPageContents.session).to.not.equal(undefined);
});

describe('devtools extensions', () => {
let showPanelTimeoutId: any = null;
afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable no-undef */
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "persistent-background-page",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"manifest_version": 2
}

0 comments on commit 2d44745

Please sign in to comment.