electron/lib/browser/chrome-extension.js

184 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-05-26 09:58:18 +00:00
const {app, protocol, webContents, BrowserWindow} = require('electron')
const fs = require('fs')
const path = require('path')
const url = require('url')
2016-01-12 02:40:23 +00:00
2016-05-26 07:57:23 +00:00
// TODO(zcbenz): Remove this when we have Object.values().
const objectValues = function (object) {
return Object.keys(object).map(function (key) { return object[key] })
}
2016-01-14 18:35:29 +00:00
// Mapping between hostname and file path.
2016-05-26 22:43:23 +00:00
const hostPathMap = {}
let hostPathMapNextKey = 0
2016-01-12 02:40:23 +00:00
const generateHostForPath = function (path) {
2016-05-26 22:43:23 +00:00
const key = `extension-${++hostPathMapNextKey}`
hostPathMap[key] = path
return key
}
2016-01-12 02:40:23 +00:00
const getPathForHost = function (host) {
return hostPathMap[host]
}
2016-01-12 02:40:23 +00:00
2016-05-26 07:57:23 +00:00
// Cache manifests.
2016-05-26 22:43:23 +00:00
const manifestMap = {}
2016-01-12 02:40:23 +00:00
2016-05-26 07:57:23 +00:00
const getManifestFromPath = function (srcDirectory) {
2016-05-26 22:43:23 +00:00
const manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
2016-05-26 07:57:23 +00:00
if (!manifestMap[manifest.name]) {
2016-05-26 09:58:18 +00:00
const hostname = generateHostForPath(srcDirectory)
2016-05-26 07:57:23 +00:00
manifestMap[manifest.name] = manifest
2016-05-26 09:58:18 +00:00
Object.assign(manifest, {
srcDirectory: srcDirectory,
hostname: hostname,
// We can not use 'file://' directly because all resources in the extension
// will be treated as relative to the root in Chrome.
startPage: url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: hostname,
pathname: manifest.devtools_page
})
})
2016-05-26 07:57:23 +00:00
return manifest
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-05-26 09:58:18 +00:00
// Manage the background pages.
2016-05-26 22:43:23 +00:00
const backgroundPages = {}
2016-05-26 09:58:18 +00:00
const startBackgroundPages = function (manifest) {
if (backgroundPages[manifest.hostname] || !manifest.background) return
const scripts = manifest.background.scripts.map((name) => {
return `<script src="${name}"></script>`
}).join('')
const html = new Buffer(`<html><body>${scripts}</body></html>`)
const contents = webContents.create({})
backgroundPages[manifest.hostname] = { html: html, contents: contents }
contents.loadURL(url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: manifest.hostname,
pathname: '_generated_background_page.html'
}))
}
2016-05-26 07:57:23 +00:00
// Transfer the |manifest| to a format that can be recognized by the
// |DevToolsAPI.addExtensions|.
const manifestToExtensionInfo = function (manifest) {
return {
startPage: manifest.startPage,
srcDirectory: manifest.srcDirectory,
name: manifest.name,
exposeExperimentalAPIs: true
}
}
2016-05-26 09:58:18 +00:00
// Load the extensions for the window.
const loadDevToolsExtensions = function (win, manifests) {
if (!win.devToolsWebContents) return
2016-05-26 22:43:23 +00:00
for (const manifest of manifests) {
2016-05-26 09:58:18 +00:00
startBackgroundPages(manifest)
}
const extensionInfoArray = manifests.map(manifestToExtensionInfo)
win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)
}
// The persistent path of "DevTools Extensions" preference file.
let loadedExtensionsPath = null
2016-01-12 02:40:23 +00:00
app.on('will-quit', function () {
2016-01-12 02:40:23 +00:00
try {
2016-05-26 22:43:23 +00:00
const loadedExtensions = objectValues(manifestMap).map(function (manifest) {
2016-05-26 07:57:23 +00:00
return manifest.srcDirectory
})
if (loadedExtensions.length > 0) {
try {
fs.mkdirSync(path.dirname(loadedExtensionsPath))
} catch (error) {
// Ignore error
}
fs.writeFileSync(loadedExtensionsPath, JSON.stringify(loadedExtensions))
} else {
fs.unlinkSync(loadedExtensionsPath)
2016-01-12 02:40:23 +00:00
}
2016-01-19 22:49:40 +00:00
} catch (error) {
// Ignore error
2016-01-12 02:40:23 +00:00
}
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// We can not use protocol or BrowserWindow until app is ready.
app.once('ready', function () {
2016-02-04 00:22:16 +00:00
// Load persisted extensions.
loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')
2016-01-12 02:40:23 +00:00
try {
2016-05-26 22:43:23 +00:00
const loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
if (Array.isArray(loadedExtensions)) {
2016-05-26 07:57:23 +00:00
// Preheat the manifest cache.
2016-05-26 22:43:23 +00:00
for (const srcDirectory of loadedExtensions) {
2016-05-26 07:57:23 +00:00
getManifestFromPath(srcDirectory)
}
2016-01-12 02:40:23 +00:00
}
2016-01-19 22:49:40 +00:00
} catch (error) {
// Ignore error
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:35:29 +00:00
// The chrome-extension: can map a extension URL request to real file path.
const chromeExtensionHandler = function (request, callback) {
2016-05-26 22:43:23 +00:00
const parsed = url.parse(request.url)
if (!parsed.hostname || !parsed.path) return callback()
if (!/extension-\d+/.test(parsed.hostname)) return callback()
2016-05-26 22:43:23 +00:00
const directory = getPathForHost(parsed.hostname)
if (!directory) return callback()
2016-05-26 09:58:18 +00:00
if (parsed.path === '/_generated_background_page.html' &&
backgroundPages[parsed.hostname]) {
return callback({
mimeType: 'text/html',
data: backgroundPages[parsed.hostname].html
})
}
fs.readFile(path.join(directory, parsed.path), function (err, content) {
2016-05-26 22:43:23 +00:00
if (err) {
return callback(-6) // FILE_NOT_FOUND
} else {
2016-05-26 09:58:18 +00:00
return callback({mimeType: 'text/html', data: content})
2016-05-26 22:43:23 +00:00
}
2016-05-26 09:58:18 +00:00
})
}
2016-05-26 09:58:18 +00:00
protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) {
2016-01-12 02:40:23 +00:00
if (error) {
console.error(`Unable to register chrome-extension protocol: ${error}`)
2016-01-12 02:40:23 +00:00
}
})
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
2016-05-26 07:57:23 +00:00
const manifest = getManifestFromPath(srcDirectory)
if (manifest) {
2016-05-26 22:43:23 +00:00
for (const win of BrowserWindow.getAllWindows()) {
2016-05-26 09:58:18 +00:00
loadDevToolsExtensions(win, [manifest])
2016-01-12 02:40:23 +00:00
}
2016-05-26 07:57:23 +00:00
return manifest.name
2016-01-12 02:40:23 +00:00
}
}
BrowserWindow.removeDevToolsExtension = function (name) {
2016-05-26 07:57:23 +00:00
delete manifestMap[name]
}
2016-01-12 02:40:23 +00:00
2016-03-16 16:33:19 +00:00
// Load persisted extensions when devtools is opened.
2016-05-26 22:43:23 +00:00
const init = BrowserWindow.prototype._init
2016-03-29 00:40:40 +00:00
BrowserWindow.prototype._init = function () {
init.call(this)
this.webContents.on('devtools-opened', () => {
2016-05-26 09:58:18 +00:00
loadDevToolsExtensions(this, objectValues(manifestMap))
})
}
})