Skip to content

Commit

Permalink
feat: add chrome.runtime.getManifest (#16891)
Browse files Browse the repository at this point in the history
* feat: add chrome.runtime.getManifest

* Add test for chrome.runtime.getManifest

* Use IPC utils for getManifest internals
  • Loading branch information
samuelmaddock authored and John Kleinschmidt committed Feb 13, 2019
1 parent 46f818b commit 8f6a543
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/browser/chrome-extension.js
Expand Up @@ -169,6 +169,14 @@ ipcMainInternal.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, conne
page.webContents._sendInternalToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo)
})

ipcMainUtils.handleSync('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
const manifest = manifestMap[extensionId]
if (!manifest) {
throw new Error(`Invalid extensionId: ${extensionId}`)
}
return manifest
})

let resultID = 1
ipcMainInternal.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message, originResultID) {
const page = backgroundPages[extensionId]
Expand Down
6 changes: 6 additions & 0 deletions lib/renderer/chrome-api.js
@@ -1,6 +1,7 @@
'use strict'

const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const Event = require('@electron/internal/renderer/extensions/event')
const url = require('url')

Expand Down Expand Up @@ -93,6 +94,11 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
})
},

getManifest: function () {
const manifest = ipcRendererUtils.invokeSync('CHROME_EXTENSION_MANIFEST', extensionId)
return manifest
},

connect (...args) {
if (isBackgroundPage) {
console.error('chrome.runtime.connect is not supported in background page')
Expand Down
48 changes: 48 additions & 0 deletions spec/chrome-api-spec.js
@@ -0,0 +1,48 @@
const fs = require('fs')
const path = require('path')

const { expect } = require('chai')
const { remote } = require('electron')

const { closeWindow } = require('./window-helpers')
const { emittedOnce } = require('./events-helpers')

const { BrowserWindow } = remote

describe('chrome api', () => {
const fixtures = path.resolve(__dirname, 'fixtures')
let w

before(() => {
BrowserWindow.addExtension(path.join(fixtures, 'extensions/chrome-api'))
})

after(() => {
BrowserWindow.removeExtension('chrome-api')
})

beforeEach(() => {
w = new BrowserWindow({
show: false
})
})

afterEach(() => closeWindow(w).then(() => { w = null }))

it('runtime.getManifest returns extension manifest', async () => {
const actualManifest = (() => {
const data = fs.readFileSync(path.join(fixtures, 'extensions/chrome-api/manifest.json'), 'utf-8')
return JSON.parse(data)
})()

w.loadURL('about:blank')

const p = emittedOnce(w.webContents, 'console-message')
w.webContents.executeJavaScript(`window.postMessage('getManifest', '*')`)
const [,, manifestString] = await p
const manifest = JSON.parse(manifestString)

expect(manifest.name).to.equal(actualManifest.name)
expect(manifest.content_scripts.length).to.equal(actualManifest.content_scripts.length)
})
})
15 changes: 15 additions & 0 deletions spec/fixtures/extensions/chrome-api/main.js
@@ -0,0 +1,15 @@
/* global chrome */

const testMap = {
getManifest () {
const manifest = chrome.runtime.getManifest()
console.log(JSON.stringify(manifest))
}
}

const dispatchTest = (event) => {
const testName = event.data
const test = testMap[testName]
test()
}
window.addEventListener('message', dispatchTest, false)
12 changes: 12 additions & 0 deletions spec/fixtures/extensions/chrome-api/manifest.json
@@ -0,0 +1,12 @@
{
"name": "chrome-api",
"version": "1.0",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["main.js"],
"run_at": "document_start"
}
],
"manifest_version": 2
}

0 comments on commit 8f6a543

Please sign in to comment.