From 04be1ac4a15700a5b51016af0101945a878854de Mon Sep 17 00:00:00 2001 From: Eryk Rakowski Date: Thu, 11 Jun 2020 05:57:51 +0200 Subject: [PATCH] fix: assign session to remote webContents (#23989) * fix: assign session to remote webContents * fix: test name --- shell/browser/api/electron_api_web_contents.cc | 3 +++ spec-main/extensions-spec.ts | 12 +++++++++++- .../persistent-background-page/background.js | 1 + .../persistent-background-page/manifest.json | 9 +++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 spec-main/fixtures/extensions/persistent-background-page/background.js create mode 100644 spec-main/fixtures/extensions/persistent-background-page/manifest.json diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 69b7efeb8f0..d8fcadd7635 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -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); diff --git a/spec-main/extensions-spec.ts b/spec-main/extensions-spec.ts index 1d43f0d648c..67b0622d1f0 100644 --- a/spec-main/extensions-spec.ts +++ b/spec-main/extensions-spec.ts @@ -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'; @@ -269,6 +269,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(() => { diff --git a/spec-main/fixtures/extensions/persistent-background-page/background.js b/spec-main/fixtures/extensions/persistent-background-page/background.js new file mode 100644 index 00000000000..2a49b2bd9e6 --- /dev/null +++ b/spec-main/fixtures/extensions/persistent-background-page/background.js @@ -0,0 +1 @@ +/* eslint-disable no-undef */ diff --git a/spec-main/fixtures/extensions/persistent-background-page/manifest.json b/spec-main/fixtures/extensions/persistent-background-page/manifest.json new file mode 100644 index 00000000000..bbfe42a5d94 --- /dev/null +++ b/spec-main/fixtures/extensions/persistent-background-page/manifest.json @@ -0,0 +1,9 @@ +{ + "name": "persistent-background-page", + "version": "1.0", + "background": { + "scripts": ["background.js"], + "persistent": true + }, + "manifest_version": 2 +}