From 99c1434051026399a125706a8e113d67bf15da83 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 May 2016 16:57:23 +0900 Subject: [PATCH] Store the original manifest file --- lib/browser/chrome-extension.js | 52 +++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index 26734ca809e8..e882b1862d4e 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -3,7 +3,7 @@ const fs = require('fs') const path = require('path') const url = require('url') -// Remove this when we have Object.values(). +// TODO(zcbenz): Remove this when we have Object.values(). const objectValues = function (object) { return Object.keys(object).map(function (key) { return object[key] }) } @@ -22,27 +22,23 @@ const getPathForHost = function (host) { return hostPathMap[host] } -// Cache extensionInfo. -let extensionInfoMap = {} +// Cache manifests. +let manifestMap = {} -const getExtensionInfoFromPath = function (srcDirectory) { +const getManifestFromPath = function (srcDirectory) { 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 // will be treated as relative to the root in Chrome. - let page = url.format({ + manifest.startPage = url.format({ protocol: 'chrome-extension', slashes: true, hostname: generateHostForPath(srcDirectory), pathname: manifest.devtools_page }) - extensionInfoMap[manifest.name] = { - startPage: page, - name: manifest.name, - srcDirectory: srcDirectory, - exposeExperimentalAPIs: true - } - return extensionInfoMap[manifest.name] + manifest.srcDirectory = srcDirectory + return manifest } } @@ -52,13 +48,24 @@ const loadDevToolsExtensions = function (win, 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. let loadedExtensionsPath = null app.on('will-quit', function () { try { - let loadedExtensions = Object.keys(extensionInfoMap).map(function (key) { - return extensionInfoMap[key].srcDirectory + let loadedExtensions = objectValues(manifestMap).map(function (manifest) { + return manifest.srcDirectory }) if (loadedExtensions.length > 0) { try { @@ -82,9 +89,9 @@ app.once('ready', function () { try { let loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath)) if (Array.isArray(loadedExtensions)) { - // Preheat the extensionInfo cache. + // Preheat the manifest cache. for (let srcDirectory of loadedExtensions) { - getExtensionInfoFromPath(srcDirectory) + getManifestFromPath(srcDirectory) } } } catch (error) { @@ -109,16 +116,17 @@ app.once('ready', function () { }) BrowserWindow.addDevToolsExtension = function (srcDirectory) { - let extensionInfo = getExtensionInfoFromPath(srcDirectory) - if (extensionInfo) { + const manifest = getManifestFromPath(srcDirectory) + if (manifest) { + const extensionInfo = manifestToExtensionInfo(manifest) for (let win of BrowserWindow.getAllWindows()) { loadDevToolsExtensions(win, [extensionInfo]) } - return extensionInfo.name + return manifest.name } } BrowserWindow.removeDevToolsExtension = function (name) { - delete extensionInfoMap[name] + delete manifestMap[name] } // Load persisted extensions when devtools is opened. @@ -126,7 +134,7 @@ app.once('ready', function () { BrowserWindow.prototype._init = function () { init.call(this) this.webContents.on('devtools-opened', () => { - loadDevToolsExtensions(this, objectValues(extensionInfoMap)) + loadDevToolsExtensions(this, objectValues(manifestMap).map(manifestToExtensionInfo)) }) } })