From 64a62eb26a4a0f0d2ce149bd8fdf74f0bf5600ad Mon Sep 17 00:00:00 2001 From: "surya.kumar7" Date: Thu, 14 Nov 2019 15:45:42 +0530 Subject: [PATCH] [Add-on] Improve the approach to obtain db name for 'options' module 1. Options API shouldn't depend on the 'name' parameter to identify the DB file to use. Instead track in the call stack to identify the package ID of the add-on app using it 2. Cleaned up the add-on modules loading implementation Change-Id: I4008a7ac81e6711efdeb9c1ff2a3c988ffa40d19 Signed-off-by: surya.kumar7 --- wrt_app/addon/browser/addonapi.js | 16 ++---- wrt_app/addon/browser/module-list.js | 1 - wrt_app/addon/browser/modules/options.js | 36 -------------- wrt_app/addon/common/addonapi.js | 24 +++++++++ wrt_app/addon/common/module-list.js | 6 +++ wrt_app/addon/common/modules/options.js | 58 ++++++++++++++++++++++ wrt_app/addon/common/utils.js | 20 ++++++++ wrt_app/addon/renderer/addonapi.js | 16 ++---- .../apreload.js => common/config-search-paths.js} | 4 +- wrt_app/renderer/init.js | 1 + wrt_app/src/addon_manager.js | 14 ------ wrt_app/src/main.js | 1 + wrt_app/src/web_application.js | 4 +- 13 files changed, 122 insertions(+), 79 deletions(-) delete mode 100644 wrt_app/addon/browser/modules/options.js create mode 100644 wrt_app/addon/common/addonapi.js create mode 100644 wrt_app/addon/common/module-list.js create mode 100644 wrt_app/addon/common/modules/options.js create mode 100644 wrt_app/addon/common/utils.js rename wrt_app/{addon/renderer/apreload.js => common/config-search-paths.js} (72%) diff --git a/wrt_app/addon/browser/addonapi.js b/wrt_app/addon/browser/addonapi.js index 4a90338..ea6340b 100644 --- a/wrt_app/addon/browser/addonapi.js +++ b/wrt_app/addon/browser/addonapi.js @@ -1,20 +1,12 @@ 'use strict'; -const moduleList = require('./module-list'); - -const memoizedGetter = (getter) => { - let memoizedValue = null; +const common = require('../common/addonapi'); +common.defineProperties(exports); - return () => { - if (memoizedValue === null) { - memoizedValue = getter(); - } - return memoizedValue; - } -} +const moduleList = require('./module-list'); for (const module of moduleList) { Object.defineProperty(exports, module.name, { - get: memoizedGetter(() => require(`./modules/${module.file}.js`)) + get: common.memoizedGetter(() => require(`./modules/${module.file}`)) }); } diff --git a/wrt_app/addon/browser/module-list.js b/wrt_app/addon/browser/module-list.js index b213e0a..63cf2c8 100644 --- a/wrt_app/addon/browser/module-list.js +++ b/wrt_app/addon/browser/module-list.js @@ -3,6 +3,5 @@ // Browser side add-on modules, please sort alphabetically module.exports = [ { name: 'messaging', file: 'messaging' }, - { name: 'options', file: 'options' }, { name: 'window', file: 'window' } ] diff --git a/wrt_app/addon/browser/modules/options.js b/wrt_app/addon/browser/modules/options.js deleted file mode 100644 index 75e0d51..0000000 --- a/wrt_app/addon/browser/modules/options.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -const REPO_PATH = path.join(require('os').homedir(), 'data/electron/runtime_addon/'); -const DB_FILE = '_db.json'; - -module.exports = { - get: function (addon_name, key) { - const DB_PATH = REPO_PATH + addon_name + DB_FILE; - try { - const fileContents = fs.readFileSync(DB_PATH); - const jsonObject = JSON.parse(fileContents); - console.log(`read value: ${JSON.stringify(jsonObject)}`); - return jsonObject[key]; - } catch (err) { - console.log(`An error has occurred: ${err}`); - } - }, - - set: function (addon_name, key, value) { - const DB_PATH = REPO_PATH + addon_name + DB_FILE; - try { - const fileContents = fs.readFileSync(DB_PATH); - let jsonObject = JSON.parse(fileContents); - console.log(`current value: ${JSON.stringify(jsonObject)}`); - jsonObject[key] = value; - console.log(`new value: ${JSON.stringify(jsonObject)}`); - const jsonString = JSON.stringify(jsonObject); - fs.writeFileSync(DB_PATH, jsonString); - } catch (err) { - console.log(`An error has occurred: ${err}`); - } - } -} \ No newline at end of file diff --git a/wrt_app/addon/common/addonapi.js b/wrt_app/addon/common/addonapi.js new file mode 100644 index 0000000..96a7c8c --- /dev/null +++ b/wrt_app/addon/common/addonapi.js @@ -0,0 +1,24 @@ +'use strict'; + +const moduleList = require('./module-list'); + +exports.memoizedGetter = (getter) => { + let memoizedValue = null; + + return () => { + if (memoizedValue === null) { + memoizedValue = getter(); + } + return memoizedValue; + } +} + +exports.defineProperties = function (targetExports) { + const descriptors = {}; + for (const module of moduleList) { + descriptors[module.name] = { + get: exports.memoizedGetter(() => require(`./modules/${module.file}`)) + } + } + return Object.defineProperties(targetExports, descriptors); +} diff --git a/wrt_app/addon/common/module-list.js b/wrt_app/addon/common/module-list.js new file mode 100644 index 0000000..fc16071 --- /dev/null +++ b/wrt_app/addon/common/module-list.js @@ -0,0 +1,6 @@ +'use strict'; + +// Common add-on modules, please sort alphabetically +module.exports = [ + { name: 'options', file: 'options' } +]; diff --git a/wrt_app/addon/common/modules/options.js b/wrt_app/addon/common/modules/options.js new file mode 100644 index 0000000..b4366ac --- /dev/null +++ b/wrt_app/addon/common/modules/options.js @@ -0,0 +1,58 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const utils = require('../utils'); + +const REPO_PATH = path.join(require('os').homedir(), 'data/electron/runtime_addon/'); +const DB_FILE = '_db.json'; + +module.exports = { + get: function (key) { + const addon_name = utils.getPackageID(); + if (!addon_name) { + console.error(`Package ID not found`); + return undefined; + } + const DB_PATH = REPO_PATH + addon_name + DB_FILE; + console.log(`DB_PATH: ${DB_PATH}`); + try { + const fileContents = fs.readFileSync(DB_PATH); + const jsonObject = JSON.parse(fileContents); + console.log(`Read value: ${JSON.stringify(jsonObject)}`); + return jsonObject[key]; + } catch (err) { + console.log(`An error has occurred: ${err}`); + return undefined; + } + }, + + set: function (key, value) { + const addon_name = utils.getPackageID(); + if (!addon_name) { + console.error(`Package ID not found`); + return false; + } + const DB_PATH = REPO_PATH + addon_name + DB_FILE; + console.log(`DB_PATH: ${DB_PATH}`); + try { + let jsonObject; + try { + const fileContents = fs.readFileSync(DB_PATH); + jsonObject = JSON.parse(fileContents); + console.log(`Current value: ${JSON.stringify(jsonObject)}`); + } catch { + console.log(`Add-on DB file doesn't exist`); + jsonObject = {}; + } + jsonObject[key] = value; + console.log(`New value: ${JSON.stringify(jsonObject)}`); + const jsonString = JSON.stringify(jsonObject); + fs.writeFileSync(DB_PATH, jsonString); + return true; + } catch (err) { + console.log(`An error has occurred: ${err}`); + return false; + } + } +} \ No newline at end of file diff --git a/wrt_app/addon/common/utils.js b/wrt_app/addon/common/utils.js new file mode 100644 index 0000000..76048d0 --- /dev/null +++ b/wrt_app/addon/common/utils.js @@ -0,0 +1,20 @@ +const keyTerm = 'globalapps'; +const delimiter = '/'; + +function extractID(line) { + let keyTermAt = line.search(keyTerm); + let delimiterAt = line.indexOf(delimiter, keyTermAt); + return line.substr(delimiterAt + 1, 10); +} + +module.exports = { + getPackageID() { + let stack = new Error().stack; + stack = stack.split('\n'); + for (let i = 0; i < stack.length; ++i) { + if (stack[i].indexOf(keyTerm) !== -1) + return extractID(stack[i]); + } + return ''; + } +} \ No newline at end of file diff --git a/wrt_app/addon/renderer/addonapi.js b/wrt_app/addon/renderer/addonapi.js index 4a90338..ea6340b 100644 --- a/wrt_app/addon/renderer/addonapi.js +++ b/wrt_app/addon/renderer/addonapi.js @@ -1,20 +1,12 @@ 'use strict'; -const moduleList = require('./module-list'); - -const memoizedGetter = (getter) => { - let memoizedValue = null; +const common = require('../common/addonapi'); +common.defineProperties(exports); - return () => { - if (memoizedValue === null) { - memoizedValue = getter(); - } - return memoizedValue; - } -} +const moduleList = require('./module-list'); for (const module of moduleList) { Object.defineProperty(exports, module.name, { - get: memoizedGetter(() => require(`./modules/${module.file}.js`)) + get: common.memoizedGetter(() => require(`./modules/${module.file}`)) }); } diff --git a/wrt_app/addon/renderer/apreload.js b/wrt_app/common/config-search-paths.js similarity index 72% rename from wrt_app/addon/renderer/apreload.js rename to wrt_app/common/config-search-paths.js index ebb0d73..f059b43 100644 --- a/wrt_app/addon/renderer/apreload.js +++ b/wrt_app/common/config-search-paths.js @@ -1,8 +1,10 @@ 'use strict'; -const ADDONS_PATH = require('path').join(__dirname, 'addonapi.js'); +const path = require('path'); const Module = require('module'); const originalResolveFilename = Module._resolveFilename; +const ADDONS_PATH = path.join(__dirname, '..', 'addon', process.type, 'addonapi.js'); + Module._resolveFilename = function (request, parent, isMain) { if (request === 'addonapi') { return ADDONS_PATH; diff --git a/wrt_app/renderer/init.js b/wrt_app/renderer/init.js index 57f2e92..f0265e3 100755 --- a/wrt_app/renderer/init.js +++ b/wrt_app/renderer/init.js @@ -15,5 +15,6 @@ */ require('../common/exception_handling'); +require('../common/config-search-paths'); const wrtRenderer = require('./wrt_renderer'); require(wrtRenderer.getElectronPath() + '/renderer/init') diff --git a/wrt_app/src/addon_manager.js b/wrt_app/src/addon_manager.js index a156fa7..6449606 100644 --- a/wrt_app/src/addon_manager.js +++ b/wrt_app/src/addon_manager.js @@ -22,7 +22,6 @@ const ADN_PATH = path.join(require('os').homedir(), 'data/electron/runtime_addon'); const MANIFEST_FILE = 'manifest.json'; const ADDONS_DB_FILE = 'addons_db.json'; -const ADDONS_PATH = path.join(__dirname, '..', 'addon', 'browser', 'addonapi.js'); // A set of predefined events for addons const EventList = [ @@ -41,24 +40,11 @@ const {EventEmitter} = require('events'); class AddonManager { constructor() { - this.registerAPIModule(); this.addons_list_ = null; this.addons_ = null; this.evt_emitter_ = null; } - registerAPIModule() { - const Module = require('module'); - const originalResolveFilename = Module._resolveFilename; - Module._resolveFilename = function (request, parent, isMain) { - if (request === 'addonapi') { - return ADDONS_PATH; - } else { - return originalResolveFilename(request, parent, isMain); - } - } - } - loadJsonDB(db_path) { if (!db_path) { db_path = path.join(ADN_PATH, ADDONS_DB_FILE); diff --git a/wrt_app/src/main.js b/wrt_app/src/main.js index ae0de87..e334120 100755 --- a/wrt_app/src/main.js +++ b/wrt_app/src/main.js @@ -17,6 +17,7 @@ 'use strict'; require('../common/exception_handling'); +require('../common/config-search-paths'); new (require('./runtime'))({ devMode: false, diff --git a/wrt_app/src/web_application.js b/wrt_app/src/web_application.js index 289f238..1a1b186 100755 --- a/wrt_app/src/web_application.js +++ b/wrt_app/src/web_application.js @@ -21,7 +21,6 @@ const WAS_EVENT = require('./was_event'); const wrt = require('../browser/wrt'); const WRTWebContents = require('../browser/wrt_web_contents'); const WRTWindow = require('../browser/wrt_window'); -const PRELOAD_PATH = require('path').join(__dirname, '..', 'addon', 'renderer', 'apreload.js'); class WebApplication { constructor(options) { @@ -215,8 +214,7 @@ class WebApplication { show: false, webPreferences: { nodeIntegration: options.isAddonAvailable, - nodeIntegrationInWorker: false, - preload: (options.isAddonAvailable) ? PRELOAD_PATH : undefined + nodeIntegrationInWorker: false }, webContents: WRTWebContents.create(), 'web-preferences': { -- 2.7.4