From 9e53112ee1ceb89a86d4beab471206b6b95072ff Mon Sep 17 00:00:00 2001 From: Eryk Rakowski Date: Wed, 19 Aug 2020 02:41:47 +0200 Subject: [PATCH] fix(extensions): enable WebSQL in background pages (#24798) * fix(extensions): enable WebSQL in background pages * fix: apply suggestions * fix: remove duplicate include * fix: remove trailing spaces --- shell/browser/electron_browser_client.cc | 10 ++++++++++ spec-main/extensions-spec.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/shell/browser/electron_browser_client.cc b/shell/browser/electron_browser_client.cc index a001361c0a..daacf11b52 100644 --- a/shell/browser/electron_browser_client.cc +++ b/shell/browser/electron_browser_client.cc @@ -772,6 +772,16 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches( command_line->AppendSwitch("profile-electron-init"); } + // Extension background pages don't have WebContentsPreferences, but they + // support WebSQL by default. +#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) + content::RenderProcessHost* process = + content::RenderProcessHost::FromID(process_id); + if (extensions::ProcessMap::Get(process->GetBrowserContext()) + ->Contains(process_id)) + command_line->AppendSwitch(switches::kEnableWebSQL); +#endif + content::WebContents* web_contents = GetWebContentsFromProcessID(process_id); if (web_contents) { diff --git a/spec-main/extensions-spec.ts b/spec-main/extensions-spec.ts index 1f2596c5fa..812cde9ba0 100644 --- a/spec-main/extensions-spec.ts +++ b/spec-main/extensions-spec.ts @@ -37,6 +37,18 @@ describe('chrome extensions', () => { }); }); + it('can open WebSQLDatabase in a background page', async () => { + const customSession = session.fromPartition(`persist:${require('uuid').v4()}`); + const w = new BrowserWindow({ show: false, webPreferences: { session: customSession, sandbox: true } }); + w.loadURL('about:blank'); + + await emittedOnce(w.webContents, 'dom-ready'); + await customSession.loadExtension(path.join(fixtures, 'extensions', 'persistent-background-page')); + const args: any = await emittedOnce(app, 'web-contents-created'); + const wc: Electron.WebContents = args[1]; + await expect(wc.executeJavaScript('(()=>{try{openDatabase("t", "1.0", "test", 2e5);return true;}catch(e){throw e}})()')).to.not.be.rejected(); + }); + function fetch (contents: WebContents, url: string) { return contents.executeJavaScript(`fetch(${JSON.stringify(url)})`); }