Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extensions): define platform info to prevent renderer crash #25357

Merged
merged 4 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ source_set("electron_lib") {
"shell/common/extensions/api",
"shell/common/extensions/api:extensions_features",
"//chrome/browser/resources:component_extension_resources",
"//components/update_client:update_client",
"//components/zoom",
"//extensions/browser",
"//extensions/browser:core_api_provider",
Expand Down
1 change: 1 addition & 0 deletions docs/api/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The following methods of `chrome.runtime` are supported:

- `chrome.runtime.getBackgroundPage`
- `chrome.runtime.getManifest`
- `chrome.runtime.getPlatformInfo`
- `chrome.runtime.getURL`
- `chrome.runtime.connect`
- `chrome.runtime.sendMessage`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>

#include "build/build_config.h"
#include "components/update_client/update_query_params.h"
#include "extensions/common/api/runtime.h"
#include "shell/browser/extensions/electron_extension_system.h"

Expand Down Expand Up @@ -42,10 +43,57 @@ bool ElectronRuntimeAPIDelegate::CheckForUpdates(
void ElectronRuntimeAPIDelegate::OpenURL(const GURL& uninstall_url) {}

bool ElectronRuntimeAPIDelegate::GetPlatformInfo(PlatformInfo* info) {
// TODO(nornagon): put useful information here.
#if defined(OS_LINUX)
info->os = api::runtime::PLATFORM_OS_LINUX;
#endif
const char* os = update_client::UpdateQueryParams::GetOS();
if (strcmp(os, "mac") == 0) {
info->os = extensions::api::runtime::PLATFORM_OS_MAC;
} else if (strcmp(os, "win") == 0) {
info->os = extensions::api::runtime::PLATFORM_OS_WIN;
} else if (strcmp(os, "linux") == 0) {
info->os = extensions::api::runtime::PLATFORM_OS_LINUX;
} else if (strcmp(os, "openbsd") == 0) {
info->os = extensions::api::runtime::PLATFORM_OS_OPENBSD;
} else {
NOTREACHED();
return false;
}

const char* arch = update_client::UpdateQueryParams::GetArch();
if (strcmp(arch, "arm") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_ARM;
} else if (strcmp(arch, "arm64") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_ARM64;
} else if (strcmp(arch, "x86") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_X86_32;
} else if (strcmp(arch, "x64") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_X86_64;
} else if (strcmp(arch, "mipsel") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_MIPS;
} else if (strcmp(arch, "mips64el") == 0) {
info->arch = extensions::api::runtime::PLATFORM_ARCH_MIPS64;
} else {
NOTREACHED();
return false;
}

const char* nacl_arch = update_client::UpdateQueryParams::GetNaclArch();
if (strcmp(nacl_arch, "arm") == 0) {
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_ARM;
} else if (strcmp(nacl_arch, "arm64") == 0) {
// Use ARM for ARM64 NaCl, as ARM64 NaCl is not available.
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_ARM;
} else if (strcmp(nacl_arch, "x86-32") == 0) {
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_X86_32;
} else if (strcmp(nacl_arch, "x86-64") == 0) {
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_X86_64;
} else if (strcmp(nacl_arch, "mips32") == 0) {
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_MIPS;
} else if (strcmp(nacl_arch, "mips64") == 0) {
info->nacl_arch = extensions::api::runtime::PLATFORM_NACL_ARCH_MIPS64;
} else {
NOTREACHED();
return false;
}
nornagon marked this conversation as resolved.
Show resolved Hide resolved

return true;
} // namespace extensions

Expand Down
44 changes: 27 additions & 17 deletions spec-main/extensions-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,38 @@ describe('chrome extensions', () => {
});

describe('chrome.runtime', () => {
let content: any;
before(async () => {
let w: BrowserWindow;
const exec = async (name: string) => {
const p = emittedOnce(ipcMain, 'success');
await w.webContents.executeJavaScript(`exec('${name}')`);
const [, result] = await p;
return result;
};
beforeEach(async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
await customSession.loadExtension(path.join(fixtures, 'extensions', 'chrome-runtime'));
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } });
try {
w.loadURL(url);
await emittedOnce(w.webContents, 'dom-ready');
content = JSON.parse(await w.webContents.executeJavaScript('document.documentElement.textContent'));
expect(content).to.be.an('object');
} finally {
w.destroy();
}
w = new BrowserWindow({ show: false, webPreferences: { session: customSession, nodeIntegration: true } });
w.loadURL(url);
await emittedOnce(w.webContents, 'dom-ready');
samuelmaddock marked this conversation as resolved.
Show resolved Hide resolved
});
it('getManifest()', async () => {
const result = await exec('getManifest');
expect(result).to.be.an('object').with.property('name', 'chrome-runtime');
});
it('getManifest()', () => {
expect(content.manifest).to.be.an('object').with.property('name', 'chrome-runtime');
it('id', async () => {
const result = await exec('id');
expect(result).to.be.a('string').with.lengthOf(32);
});
it('id', () => {
expect(content.id).to.be.a('string').with.lengthOf(32);
it('getURL()', async () => {
const result = await exec('getURL');
expect(result).to.be.a('string').and.match(/^chrome-extension:\/\/.*main.js$/);
});
it('getURL()', () => {
expect(content.url).to.be.a('string').and.match(/^chrome-extension:\/\/.*main.js$/);
it('getPlatformInfo()', async () => {
const result = await exec('getPlatformInfo');
expect(result).to.be.an('object');
expect(result.os).to.be.a('string');
expect(result.arch).to.be.a('string');
expect(result.nacl_arch).to.be.a('string');
});
});

Expand Down
12 changes: 12 additions & 0 deletions spec-main/fixtures/extensions/chrome-runtime/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global chrome */

chrome.runtime.onMessage.addListener((message, sender, reply) => {
switch (message) {
case 'getPlatformInfo':
chrome.runtime.getPlatformInfo(reply);
break;
}

// Respond asynchronously
return true;
});
41 changes: 37 additions & 4 deletions spec-main/fixtures/extensions/chrome-runtime/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
/* eslint-disable */
document.documentElement.textContent = JSON.stringify({
manifest: chrome.runtime.getManifest(),
id: chrome.runtime.id,
url: chrome.runtime.getURL('main.js')

codebytere marked this conversation as resolved.
Show resolved Hide resolved
function evalInMainWorld(fn) {
const script = document.createElement('script')
script.textContent = `((${fn})())`
document.documentElement.appendChild(script)
}

async function exec(name) {
let result
switch (name) {
case 'getManifest':
result = chrome.runtime.getManifest()
break
case 'id':
result = chrome.runtime.id
break
case 'getURL':
result = chrome.runtime.getURL('main.js')
break
case 'getPlatformInfo': {
result = await new Promise(resolve => {
chrome.runtime.sendMessage(name, resolve)
})
break
}
}

const funcStr = `() => { require('electron').ipcRenderer.send('success', ${JSON.stringify(result)}) }`
evalInMainWorld(funcStr)
}

window.addEventListener('message', event => {
exec(event.data.name)
})

evalInMainWorld(() => {
window.exec = name => window.postMessage({ name })
})
4 changes: 4 additions & 0 deletions spec-main/fixtures/extensions/chrome-runtime/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"run_at": "document_end"
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}