electron/lib/browser/chrome-extension.js

133 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-05-08 11:14:14 +00:00
const {app, protocol, BrowserWindow} = require('electron')
const fs = require('fs')
const path = require('path')
const url = require('url')
2016-01-12 02:40:23 +00:00
// 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.
let hostPathMap = {}
let hostPathMapNextKey = 0
2016-01-12 02:40:23 +00:00
const generateHostForPath = function (path) {
let 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-01-14 18:35:29 +00:00
// Cache extensionInfo.
let extensionInfoMap = {}
2016-01-12 02:40:23 +00:00
const getExtensionInfoFromPath = function (srcDirectory) {
let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
2016-01-12 02:40:23 +00:00
if (extensionInfoMap[manifest.name] == null) {
2016-01-14 18:44:21 +00:00
// We can not use 'file://' directly because all resources in the extension
// will be treated as relative to the root in Chrome.
let page = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'chrome-extension',
slashes: true,
hostname: generateHostForPath(srcDirectory),
2016-01-12 02:40:23 +00:00
pathname: manifest.devtools_page
})
2016-01-12 02:40:23 +00:00
extensionInfoMap[manifest.name] = {
startPage: page,
name: manifest.name,
srcDirectory: srcDirectory,
exposeExperimentalAPIs: true
}
return extensionInfoMap[manifest.name]
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
// Load the extensions for the window.
const loadDevToolsExtensions = function (win, extensionInfoArray) {
if (!win.devToolsWebContents) return
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 {
let loadedExtensions = Object.keys(extensionInfoMap).map(function (key) {
return extensionInfoMap[key].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 {
let loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
if (Array.isArray(loadedExtensions)) {
// Preheat the extensionInfo cache.
for (let srcDirectory of loadedExtensions) {
getExtensionInfoFromPath(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) {
let parsed = url.parse(request.url)
if (!parsed.hostname || !parsed.path) return callback()
if (!/extension-\d+/.test(parsed.hostname)) return callback()
let directory = getPathForHost(parsed.hostname)
if (!directory) return callback()
callback(path.join(directory, parsed.path))
}
protocol.registerFileProtocol('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) {
let extensionInfo = getExtensionInfoFromPath(srcDirectory)
2016-01-12 02:40:23 +00:00
if (extensionInfo) {
for (let win of BrowserWindow.getAllWindows()) {
loadDevToolsExtensions(win, [extensionInfo])
2016-01-12 02:40:23 +00:00
}
return extensionInfo.name
2016-01-12 02:40:23 +00:00
}
}
BrowserWindow.removeDevToolsExtension = function (name) {
delete extensionInfoMap[name]
}
2016-01-12 02:40:23 +00:00
2016-03-16 16:33:19 +00:00
// Load persisted extensions when devtools is opened.
let init = BrowserWindow.prototype._init
2016-03-29 00:40:40 +00:00
BrowserWindow.prototype._init = function () {
init.call(this)
this.webContents.on('devtools-opened', () => {
loadDevToolsExtensions(this, objectValues(extensionInfoMap))
})
}
})