Store the original manifest file

This commit is contained in:
Cheng Zhao 2016-05-26 16:57:23 +09:00
parent 513b66f207
commit 99c1434051

View file

@ -3,7 +3,7 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const url = require('url') const url = require('url')
// Remove this when we have Object.values(). // TODO(zcbenz): Remove this when we have Object.values().
const objectValues = function (object) { const objectValues = function (object) {
return Object.keys(object).map(function (key) { return object[key] }) return Object.keys(object).map(function (key) { return object[key] })
} }
@ -22,27 +22,23 @@ const getPathForHost = function (host) {
return hostPathMap[host] return hostPathMap[host]
} }
// Cache extensionInfo. // Cache manifests.
let extensionInfoMap = {} let manifestMap = {}
const getExtensionInfoFromPath = function (srcDirectory) { const getManifestFromPath = function (srcDirectory) {
let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json'))) let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
if (extensionInfoMap[manifest.name] == null) { if (!manifestMap[manifest.name]) {
manifestMap[manifest.name] = manifest
// We can not use 'file://' directly because all resources in the extension // We can not use 'file://' directly because all resources in the extension
// will be treated as relative to the root in Chrome. // will be treated as relative to the root in Chrome.
let page = url.format({ manifest.startPage = url.format({
protocol: 'chrome-extension', protocol: 'chrome-extension',
slashes: true, slashes: true,
hostname: generateHostForPath(srcDirectory), hostname: generateHostForPath(srcDirectory),
pathname: manifest.devtools_page pathname: manifest.devtools_page
}) })
extensionInfoMap[manifest.name] = { manifest.srcDirectory = srcDirectory
startPage: page, return manifest
name: manifest.name,
srcDirectory: srcDirectory,
exposeExperimentalAPIs: true
}
return extensionInfoMap[manifest.name]
} }
} }
@ -52,13 +48,24 @@ const loadDevToolsExtensions = function (win, extensionInfoArray) {
win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`) win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)
} }
// 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
}
}
// The persistent path of "DevTools Extensions" preference file. // The persistent path of "DevTools Extensions" preference file.
let loadedExtensionsPath = null let loadedExtensionsPath = null
app.on('will-quit', function () { app.on('will-quit', function () {
try { try {
let loadedExtensions = Object.keys(extensionInfoMap).map(function (key) { let loadedExtensions = objectValues(manifestMap).map(function (manifest) {
return extensionInfoMap[key].srcDirectory return manifest.srcDirectory
}) })
if (loadedExtensions.length > 0) { if (loadedExtensions.length > 0) {
try { try {
@ -82,9 +89,9 @@ app.once('ready', function () {
try { try {
let loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath)) let loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
if (Array.isArray(loadedExtensions)) { if (Array.isArray(loadedExtensions)) {
// Preheat the extensionInfo cache. // Preheat the manifest cache.
for (let srcDirectory of loadedExtensions) { for (let srcDirectory of loadedExtensions) {
getExtensionInfoFromPath(srcDirectory) getManifestFromPath(srcDirectory)
} }
} }
} catch (error) { } catch (error) {
@ -109,16 +116,17 @@ app.once('ready', function () {
}) })
BrowserWindow.addDevToolsExtension = function (srcDirectory) { BrowserWindow.addDevToolsExtension = function (srcDirectory) {
let extensionInfo = getExtensionInfoFromPath(srcDirectory) const manifest = getManifestFromPath(srcDirectory)
if (extensionInfo) { if (manifest) {
const extensionInfo = manifestToExtensionInfo(manifest)
for (let win of BrowserWindow.getAllWindows()) { for (let win of BrowserWindow.getAllWindows()) {
loadDevToolsExtensions(win, [extensionInfo]) loadDevToolsExtensions(win, [extensionInfo])
} }
return extensionInfo.name return manifest.name
} }
} }
BrowserWindow.removeDevToolsExtension = function (name) { BrowserWindow.removeDevToolsExtension = function (name) {
delete extensionInfoMap[name] delete manifestMap[name]
} }
// Load persisted extensions when devtools is opened. // Load persisted extensions when devtools is opened.
@ -126,7 +134,7 @@ app.once('ready', function () {
BrowserWindow.prototype._init = function () { BrowserWindow.prototype._init = function () {
init.call(this) init.call(this)
this.webContents.on('devtools-opened', () => { this.webContents.on('devtools-opened', () => {
loadDevToolsExtensions(this, objectValues(extensionInfoMap)) loadDevToolsExtensions(this, objectValues(manifestMap).map(manifestToExtensionInfo))
}) })
} }
}) })