From: yh106.jung Date: Thu, 7 Sep 2017 07:53:17 +0000 (+0900) Subject: [PDNCF] Introduce extensions management APIs indépendant of Dev Tools Extensions X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F50%2F148250%2F2;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git [PDNCF] 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 Change-Id: I61225348c4541fa5852cb617a59b15ccb0a0b0d1 Signed-off-by: yh106.jung --- diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index a49b8f2..d90a360 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -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 + } })