Clean up the Chrome API implementation code

This commit is contained in:
Cheng Zhao 2016-05-28 17:51:49 +09:00
parent 31628abadc
commit d55b96fdf5
3 changed files with 54 additions and 50 deletions

View file

@ -10,37 +10,27 @@ const objectValues = function (object) {
return Object.keys(object).map(function (key) { return object[key] }) return Object.keys(object).map(function (key) { return object[key] })
} }
// Mapping between hostname and file path. // Mapping between extensionId(hostname) and manifest.
const hostPathMap = {} const manifestMap = {} // extensionId => manifest
let hostPathMapNextKey = 0 const manifestNameMap = {} // name => manifest
const generateHostForPath = function (path) { let nextExtensionId = 0
const key = `extension-${++hostPathMapNextKey}`
hostPathMap[key] = path
return key
}
const getPathForHost = function (host) {
return hostPathMap[host]
}
// Cache manifests.
const manifestMap = {}
// Create or get manifest object from |srcDirectory|.
const getManifestFromPath = function (srcDirectory) { const getManifestFromPath = function (srcDirectory) {
const manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json'))) const manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
if (!manifestMap[manifest.name]) { if (!manifestNameMap[manifest.name]) {
const hostname = generateHostForPath(srcDirectory) const extensionId = `extension-${++nextExtensionId}`
manifestMap[manifest.name] = manifest manifestMap[extensionId] = manifestNameMap[manifest.name] = manifest
Object.assign(manifest, { Object.assign(manifest, {
srcDirectory: srcDirectory, srcDirectory: srcDirectory,
hostname: hostname, extensionId: extensionId,
// We can not use 'file://' directly because all resources in the extension // We can not use 'file://' directly because all resources in the extension
// will be treated as relative to the root in Chrome. // will be treated as relative to the root in Chrome.
startPage: url.format({ startPage: url.format({
protocol: 'chrome-extension', protocol: 'chrome-extension',
slashes: true, slashes: true,
hostname: hostname, hostname: extensionId,
pathname: manifest.devtools_page pathname: manifest.devtools_page
}) })
}) })
@ -52,7 +42,7 @@ const getManifestFromPath = function (srcDirectory) {
const backgroundPages = {} const backgroundPages = {}
const startBackgroundPages = function (manifest) { const startBackgroundPages = function (manifest) {
if (backgroundPages[manifest.hostname] || !manifest.background) return if (backgroundPages[manifest.extensionId] || !manifest.background) return
const scripts = manifest.background.scripts.map((name) => { const scripts = manifest.background.scripts.map((name) => {
return `<script src="${name}"></script>` return `<script src="${name}"></script>`
@ -60,30 +50,29 @@ const startBackgroundPages = function (manifest) {
const html = new Buffer(`<html><body>${scripts}</body></html>`) const html = new Buffer(`<html><body>${scripts}</body></html>`)
const contents = webContents.create({}) const contents = webContents.create({})
backgroundPages[manifest.hostname] = { html: html, webContents: contents } backgroundPages[manifest.extensionId] = { html: html, webContents: contents }
contents.loadURL(url.format({ contents.loadURL(url.format({
protocol: 'chrome-extension', protocol: 'chrome-extension',
slashes: true, slashes: true,
hostname: manifest.hostname, hostname: manifest.extensionId,
pathname: '_generated_background_page.html' pathname: '_generated_background_page.html'
})) }))
contents.openDevTools()
} }
const removeBackgroundPages = function (manifest) { const removeBackgroundPages = function (manifest) {
if (!backgroundPages[manifest.hostname]) return if (!backgroundPages[manifest.extensionId]) return
backgroundPages[manifest.hostname].webContents.destroy() backgroundPages[manifest.extensionId].webContents.destroy()
delete backgroundPages[manifest.hostname] delete backgroundPages[manifest.extensionId]
} }
// Handle the chrome.* API messages. // Handle the chrome.* API messages.
let nextId = 0 let nextId = 0
ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, hostname, connectInfo) { ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo) {
const page = backgroundPages[hostname] const page = backgroundPages[extensionId]
if (!page) { if (!page) {
console.error(`Connect to unkown extension ${hostname}`) console.error(`Connect to unkown extension ${extensionId}`)
return return
} }
@ -93,7 +82,7 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, hostname, connectInfo) {
event.sender.once('render-view-deleted', () => { event.sender.once('render-view-deleted', () => {
page.webContents.sendToAll(`CHROME_PORT_ONDISCONNECT_${portId}`) page.webContents.sendToAll(`CHROME_PORT_ONDISCONNECT_${portId}`)
}) })
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, hostname, connectInfo) page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, extensionId, connectInfo)
}) })
ipcMain.on('CHROME_PORT_DISCONNECT', function (event, webContentsId, portId) { ipcMain.on('CHROME_PORT_DISCONNECT', function (event, webContentsId, portId) {
@ -116,7 +105,7 @@ ipcMain.on('CHROME_PORT_POSTMESSAGE', function (event, webContentsId, portId, me
contents.sendToAll(`CHROME_PORT_ONMESSAGE_${portId}`, message) contents.sendToAll(`CHROME_PORT_ONMESSAGE_${portId}`, message)
}) })
ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, hostname, details) { ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, extensionId, details) {
const contents = webContents.fromId(webContentsId) const contents = webContents.fromId(webContentsId)
if (!contents) { if (!contents) {
console.error(`Sending message to unkown webContentsId ${webContentsId}`) console.error(`Sending message to unkown webContentsId ${webContentsId}`)
@ -125,17 +114,18 @@ ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsI
let code, url let code, url
if (details.file) { if (details.file) {
const manifest = manifestMap[extensionId]
code = String(fs.readFileSync(path.join(manifest.srcDirectory, details.file))) code = String(fs.readFileSync(path.join(manifest.srcDirectory, details.file)))
url = `chrome-extension://${hostname}/${details.file}` url = `chrome-extension://${extensionId}${details.file}`
} else { } else {
code = details.code code = details.code
url = `chrome-extension://${hostname}/${String(Math.random()).substr(2, 8)}.js` url = `chrome-extension://${extensionId}/${String(Math.random()).substr(2, 8)}.js`
} }
contents.send('CHROME_TABS_EXECUTESCRIPT', requestId, event.sender.id, hostname, url, code) contents.send('CHROME_TABS_EXECUTESCRIPT', requestId, event.sender.id, extensionId, url, code)
}) })
ipcMain.on(`CHROME_TABS_EXECUTESCRIPT_RESULT`, (event, requestId, webContentsId, result) => { ipcMain.on('CHROME_TABS_EXECUTESCRIPT_RESULT', (event, requestId, webContentsId, result) => {
const contents = webContents.fromId(webContentsId) const contents = webContents.fromId(webContentsId)
if (!contents) { if (!contents) {
console.error(`Sending message to unkown webContentsId ${webContentsId}`) console.error(`Sending message to unkown webContentsId ${webContentsId}`)
@ -153,7 +143,7 @@ const injectContentScripts = function (manifest) {
const readArrayOfFiles = function (relativePath) { const readArrayOfFiles = function (relativePath) {
return { return {
url: `chrome-extension://${manifest.hostname}/${relativePath}`, url: `chrome-extension://${manifest.extensionId}/${relativePath}`,
code: String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath))) code: String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
} }
} }
@ -168,6 +158,7 @@ const injectContentScripts = function (manifest) {
try { try {
const entry = { const entry = {
extensionId: manifest.extensionId,
contentScripts: manifest.content_scripts.map(contentScriptToEntry) contentScripts: manifest.content_scripts.map(contentScriptToEntry)
} }
contentScripts[manifest.name] = renderProcessPreferences.addEntry(entry) contentScripts[manifest.name] = renderProcessPreferences.addEntry(entry)
@ -195,9 +186,16 @@ const manifestToExtensionInfo = function (manifest) {
} }
// Load the extensions for the window. // Load the extensions for the window.
const loadExtension = function (manifest) {
startBackgroundPages(manifest)
injectContentScripts(manifest)
}
const loadDevToolsExtensions = function (win, manifests) { const loadDevToolsExtensions = function (win, manifests) {
if (!win.devToolsWebContents) return if (!win.devToolsWebContents) return
manifests.forEach(loadExtension)
const extensionInfoArray = manifests.map(manifestToExtensionInfo) const extensionInfoArray = manifests.map(manifestToExtensionInfo)
win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`) win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)
} }
@ -233,8 +231,8 @@ app.once('ready', function () {
if (!parsed.hostname || !parsed.path) return callback() if (!parsed.hostname || !parsed.path) return callback()
if (!/extension-\d+/.test(parsed.hostname)) return callback() if (!/extension-\d+/.test(parsed.hostname)) return callback()
const directory = getPathForHost(parsed.hostname) const manifest = manifestMap[parsed.hostname]
if (!directory) return callback() if (!manifest) return callback()
if (parsed.path === '/_generated_background_page.html' && if (parsed.path === '/_generated_background_page.html' &&
backgroundPages[parsed.hostname]) { backgroundPages[parsed.hostname]) {
@ -244,7 +242,7 @@ app.once('ready', function () {
}) })
} }
fs.readFile(path.join(directory, parsed.path), function (err, content) { fs.readFile(path.join(manifest.srcDirectory, parsed.path), function (err, content) {
if (err) { if (err) {
return callback(-6) // FILE_NOT_FOUND return callback(-6) // FILE_NOT_FOUND
} else { } else {
@ -266,8 +264,7 @@ app.once('ready', function () {
for (const srcDirectory of loadedExtensions) { for (const srcDirectory of loadedExtensions) {
// Start background pages and set content scripts. // Start background pages and set content scripts.
const manifest = getManifestFromPath(srcDirectory) const manifest = getManifestFromPath(srcDirectory)
startBackgroundPages(manifest) loadExtension(manifest)
injectContentScripts(manifest)
} }
} }
} catch (error) { } catch (error) {
@ -285,12 +282,13 @@ app.once('ready', function () {
} }
} }
BrowserWindow.removeDevToolsExtension = function (name) { BrowserWindow.removeDevToolsExtension = function (name) {
const manifest = manifestMap[name] const manifest = manifestNameMap[name]
if (!manifest) return if (!manifest) return
removeBackgroundPages(manifest) removeBackgroundPages(manifest)
removeContentScripts(manifest) removeContentScripts(manifest)
delete manifestMap[name] delete manifestMap[manifest.extensionId]
delete manifestNameMap[name]
} }
// Load extensions automatically when devtools is opened. // Load extensions automatically when devtools is opened.

View file

@ -44,6 +44,7 @@ class Port {
constructor (webContentsId, portId, extensionId, name) { constructor (webContentsId, portId, extensionId, name) {
this.webContentsId = webContentsId this.webContentsId = webContentsId
this.portId = portId this.portId = portId
this.disconnected = false
this.name = name this.name = name
this.onDisconnect = new Event() this.onDisconnect = new Event()
@ -60,6 +61,8 @@ class Port {
} }
disconnect () { disconnect () {
if (this.disconnected) return
ipcRenderer.send('CHROME_PORT_DISCONNECT', this.webContentsId, this.portId) ipcRenderer.send('CHROME_PORT_DISCONNECT', this.webContentsId, this.portId)
this._onDisconnect() this._onDisconnect()
} }
@ -69,6 +72,7 @@ class Port {
} }
_onDisconnect () { _onDisconnect () {
this.disconnected = true
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`) ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
this.onDisconnect.emit() this.onDisconnect.emit()
} }

View file

@ -12,26 +12,26 @@ const matchesPattern = function (pattern) {
// Run the code with chrome API integrated. // Run the code with chrome API integrated.
const runContentScript = function (extensionId, url, code) { const runContentScript = function (extensionId, url, code) {
const chrome = {} const context = {}
require('./chrome-api').injectTo(extensionId, chrome) require('./chrome-api').injectTo(extensionId, context)
const wrapper = `(function (chrome) {\n ${code}\n })` const wrapper = `(function (chrome) {\n ${code}\n })`
const compiledWrapper = runInThisContext(wrapper, { const compiledWrapper = runInThisContext(wrapper, {
filename: url, filename: url,
lineOffset: 1, lineOffset: 1,
displayErrors: true displayErrors: true
}) })
return compiledWrapper.call(this, chrome) return compiledWrapper.call(this, context.chrome)
} }
// Run injected scripts. // Run injected scripts.
// https://developer.chrome.com/extensions/content_scripts // https://developer.chrome.com/extensions/content_scripts
const injectContentScript = function (script) { const injectContentScript = function (extensionId, script) {
for (const match of script.matches) { for (const match of script.matches) {
if (!matchesPattern(match)) return if (!matchesPattern(match)) return
} }
for (const {url, code} of script.js) { for (const {url, code} of script.js) {
const fire = runContentScript.bind(window, script.extensionId, url, code) const fire = runContentScript.bind(window, extensionId, url, code)
if (script.runAt === 'document_start') { if (script.runAt === 'document_start') {
process.once('document-start', fire) process.once('document-start', fire)
} else if (script.runAt === 'document_end') { } else if (script.runAt === 'document_end') {
@ -53,7 +53,9 @@ const preferences = process.getRenderProcessPreferences()
if (preferences) { if (preferences) {
for (const pref of preferences) { for (const pref of preferences) {
if (pref.contentScripts) { if (pref.contentScripts) {
pref.contentScripts.forEach(injectContentScript) for (const script of pref.contentScripts) {
injectContentScript(pref.extensionId, script)
}
} }
} }
} }