electron/lib/browser/chrome-extension.js

143 lines
4.3 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
2016-01-14 18:35:29 +00:00
// Mapping between hostname and file path.
var hostPathMap = {}
var hostPathMapNextKey = 0
2016-01-12 02:40:23 +00:00
var getHostForPath = function (path) {
var key
key = 'extension-' + (++hostPathMapNextKey)
hostPathMap[key] = path
return key
}
2016-01-12 02:40:23 +00:00
var getPathForHost = function (host) {
return hostPathMap[host]
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Cache extensionInfo.
var extensionInfoMap = {}
2016-01-12 02:40:23 +00:00
var getExtensionInfoFromPath = function (srcDirectory) {
var manifest, page
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.
2016-01-12 02:40:23 +00:00
page = url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: getHostForPath(srcDirectory),
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
2016-01-14 18:35:29 +00:00
// The loaded extensions cache and its persistent path.
var loadedExtensions = null
var loadedExtensionsPath = null
2016-01-12 02:40:23 +00:00
app.on('will-quit', function () {
2016-01-12 02:40:23 +00:00
try {
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-05-08 11:14:14 +00:00
var chromeExtensionHandler, i, init, len, srcDirectory
2016-01-12 02:40:23 +00:00
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 {
loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
2016-01-12 02:40:23 +00:00
if (!Array.isArray(loadedExtensions)) {
loadedExtensions = []
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:35:29 +00:00
// Preheat the extensionInfo cache.
2016-01-12 02:40:23 +00:00
for (i = 0, len = loadedExtensions.length; i < len; i++) {
srcDirectory = loadedExtensions[i]
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.
chromeExtensionHandler = function (request, callback) {
var directory, parsed
parsed = url.parse(request.url)
2016-01-12 02:40:23 +00:00
if (!(parsed.hostname && (parsed.path != null))) {
return callback()
2016-01-12 02:40:23 +00:00
}
if (!/extension-\d+/.test(parsed.hostname)) {
return callback()
2016-01-12 02:40:23 +00:00
}
directory = getPathForHost(parsed.hostname)
2016-01-12 02:40:23 +00:00
if (directory == null) {
return callback()
2016-01-12 02:40:23 +00:00
}
return callback(path.join(directory, parsed.path))
}
protocol.registerFileProtocol('chrome-extension', chromeExtensionHandler, function (error) {
2016-01-12 02:40:23 +00:00
if (error) {
return console.error('Unable to register chrome-extension protocol')
2016-01-12 02:40:23 +00:00
}
})
BrowserWindow.prototype._loadDevToolsExtensions = function (extensionInfoArray) {
var ref
return (ref = this.devToolsWebContents) != null ? ref.executeJavaScript('DevToolsAPI.addExtensions(' + (JSON.stringify(extensionInfoArray)) + ');') : void 0
}
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
var extensionInfo, j, len1, ref, window
extensionInfo = getExtensionInfoFromPath(srcDirectory)
2016-01-12 02:40:23 +00:00
if (extensionInfo) {
ref = BrowserWindow.getAllWindows()
2016-01-12 02:40:23 +00:00
for (j = 0, len1 = ref.length; j < len1; j++) {
window = ref[j]
window._loadDevToolsExtensions([extensionInfo])
2016-01-12 02:40:23 +00:00
}
return extensionInfo.name
2016-01-12 02:40:23 +00:00
}
}
BrowserWindow.removeDevToolsExtension = function (name) {
return 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.
init = BrowserWindow.prototype._init
2016-03-29 00:40:40 +00:00
BrowserWindow.prototype._init = function () {
init.call(this)
2016-05-11 10:54:52 +00:00
return this.webContents.on('devtools-opened', () => {
return this._loadDevToolsExtensions(Object.keys(extensionInfoMap).map(function (key) {
return extensionInfoMap[key]
}))
})
}
})