Introduce extensions management APIs indépendant of Dev Tools Extensions

- introduce API BrowserWindow#[add,remove,get]Extension
- make [add,remove, get]DevToolsExtension use newly introduced API
- make the app persist only the extensions added via
#addDevToolsExtension
This commit is contained in:
Alexandre Lachèze 2017-07-03 20:01:08 +02:00
parent 1df72e42a5
commit cba53604eb

View file

@ -15,6 +15,7 @@ const objectValues = function (object) {
// Mapping between extensionId(hostname) and manifest.
const manifestMap = {} // extensionId => manifest
const manifestNameMap = {} // name => manifest
const devToolsExtensionNames = new Set()
const generateExtensionIdFromName = function (name) {
return name.replace(/[\W_]+/g, '-').toLowerCase()
@ -64,6 +65,7 @@ const getManifestFromPath = function (srcDirectory) {
return manifest
} else if (manifest && manifest.name) {
console.warn(`Attempted to load extension "${manifest.name}" that has already been loaded.`)
return manifest
}
}
@ -329,22 +331,21 @@ app.on('session-created', function (ses) {
})
// The persistent path of "DevTools Extensions" preference file.
let loadedExtensionsPath = null
let loadedDevToolsExtensionsPath = null
app.on('will-quit', function () {
try {
const loadedExtensions = objectValues(manifestMap).map(function (manifest) {
return manifest.srcDirectory
})
if (loadedExtensions.length > 0) {
const loadedDevToolsExtensions = Array.from(devToolsExtensionNames)
.map(name => manifestNameMap[name].srcDirectory)
if (loadedDevToolsExtensions.length > 0) {
try {
fs.mkdirSync(path.dirname(loadedExtensionsPath))
fs.mkdirSync(path.dirname(loadedDevToolsExtensionsPath))
} catch (error) {
// Ignore error
}
fs.writeFileSync(loadedExtensionsPath, JSON.stringify(loadedExtensions))
fs.writeFileSync(loadedDevToolsExtensionsPath, JSON.stringify(loadedDevToolsExtensions))
} else {
fs.unlinkSync(loadedExtensionsPath)
fs.unlinkSync(loadedDevToolsExtensionsPath)
}
} catch (error) {
// Ignore error
@ -354,14 +355,13 @@ app.on('will-quit', function () {
// We can not use protocol or BrowserWindow until app is ready.
app.once('ready', function () {
// Load persisted extensions.
loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')
loadedDevToolsExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')
try {
const loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
if (Array.isArray(loadedExtensions)) {
for (const srcDirectory of loadedExtensions) {
const loadedDevToolsExtensions = JSON.parse(fs.readFileSync(loadedDevToolsExtensionsPath))
if (Array.isArray(loadedDevToolsExtensions)) {
for (const srcDirectory of loadedDevToolsExtensions) {
// Start background pages and set content scripts.
const manifest = getManifestFromPath(srcDirectory)
loadExtension(manifest)
BrowserWindow.addDevToolsExtension(srcDirectory)
}
}
} catch (error) {
@ -369,7 +369,7 @@ app.once('ready', function () {
}
// The public API to add/remove extensions.
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
BrowserWindow.addExtension = function (srcDirectory) {
const manifest = getManifestFromPath(srcDirectory)
if (manifest) {
loadExtension(manifest)
@ -382,7 +382,7 @@ app.once('ready', function () {
}
}
BrowserWindow.removeDevToolsExtension = function (name) {
BrowserWindow.removeExtension = function (name) {
const manifest = manifestNameMap[name]
if (!manifest) return
@ -392,7 +392,7 @@ app.once('ready', function () {
delete manifestNameMap[name]
}
BrowserWindow.getDevToolsExtensions = function () {
BrowserWindow.getExtensions = function () {
const extensions = {}
Object.keys(manifestNameMap).forEach(function (name) {
const manifest = manifestNameMap[name]
@ -400,4 +400,27 @@ app.once('ready', function () {
})
return extensions
}
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
const manifestName = BrowserWindow.addExtension(srcDirectory)
if (manifestName) {
devToolsExtensionNames.add(manifestName)
}
return manifestName
}
BrowserWindow.removeDevToolsExtension = function (name) {
BrowserWindow.removeExtension(name)
devToolsExtensionNames.delete(name)
}
BrowserWindow.getDevToolsExtensions = function () {
const extensions = BrowserWindow.getExtensions()
const devExtensions = {}
Array.from(devToolsExtensionNames).forEach(function (name) {
if (extensions[name]) return
devExtensions[name] = extensions[name]
})
return devExtensions
}
})