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