refactor: implement Chrome Extension APIs without the remote module (#16686)
* refactor: implement Chrome Extension APIs without the remote module * remove unused potentiallyRemoteRequire
This commit is contained in:
parent
1898f91620
commit
7a3d220347
3 changed files with 79 additions and 77 deletions
|
@ -10,6 +10,7 @@ const { Buffer } = require('buffer')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
|
const util = require('util')
|
||||||
|
|
||||||
// Mapping between extensionId(hostname) and manifest.
|
// Mapping between extensionId(hostname) and manifest.
|
||||||
const manifestMap = {} // extensionId => manifest
|
const manifestMap = {} // extensionId => manifest
|
||||||
|
@ -226,6 +227,65 @@ ipcMainUtils.handleSync('CHROME_GET_MESSAGES', function (event, extensionId) {
|
||||||
return fs.readFileSync(messagesPath)
|
return fs.readFileSync(messagesPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const validStorageTypes = new Set(['sync', 'local'])
|
||||||
|
|
||||||
|
const getChromeStoragePath = (storageType, extensionId) => {
|
||||||
|
if (!validStorageTypes.has(storageType)) {
|
||||||
|
throw new Error(`Invalid storageType: ${storageType}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!manifestMap[extensionId]) {
|
||||||
|
throw new Error(`Invalid extensionId: ${extensionId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.join(app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const mkdirp = util.promisify((dir, callback) => {
|
||||||
|
fs.mkdir(dir, (error) => {
|
||||||
|
if (error && error.code === 'ENOENT') {
|
||||||
|
mkdirp(path.dirname(dir), (error) => {
|
||||||
|
if (!error) {
|
||||||
|
mkdirp(dir, callback)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (error && error.code === 'EEXIST') {
|
||||||
|
callback(null)
|
||||||
|
} else {
|
||||||
|
callback(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const readFile = util.promisify(fs.readFile)
|
||||||
|
const writeFile = util.promisify(fs.writeFile)
|
||||||
|
|
||||||
|
ipcMainUtils.handle('CHROME_STORAGE_READ', async function (event, storageType, extensionId) {
|
||||||
|
const filePath = getChromeStoragePath(storageType, extensionId)
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await readFile(filePath, 'utf8')
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMainUtils.handle('CHROME_STORAGE_WRITE', async function (event, storageType, extensionId, data) {
|
||||||
|
const filePath = getChromeStoragePath(storageType, extensionId)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mkdirp(path.dirname(filePath))
|
||||||
|
} catch (error) {
|
||||||
|
// we just ignore the errors of mkdir or mkdirp
|
||||||
|
}
|
||||||
|
|
||||||
|
return writeFile(filePath, data, 'utf8')
|
||||||
|
})
|
||||||
|
|
||||||
const isChromeExtension = function (pageURL) {
|
const isChromeExtension = function (pageURL) {
|
||||||
const { protocol } = url.parse(pageURL)
|
const { protocol } = url.parse(pageURL)
|
||||||
return protocol === 'chrome-extension:'
|
return protocol === 'chrome-extension:'
|
||||||
|
|
|
@ -1,70 +1,27 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { getRemote, potentiallyRemoteRequire } = require('@electron/internal/renderer/remote')
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||||
const fs = potentiallyRemoteRequire('fs')
|
|
||||||
const path = potentiallyRemoteRequire('path')
|
|
||||||
const app = getRemote('app')
|
|
||||||
|
|
||||||
const getChromeStoragePath = (storageType, extensionId) => {
|
const getStorage = (storageType, extensionId, callback) => {
|
||||||
return path.join(
|
if (typeof callback !== 'function') throw new TypeError('No callback provided')
|
||||||
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const mkdirp = (dir, callback) => {
|
|
||||||
fs.mkdir(dir, (error) => {
|
|
||||||
if (error && error.code === 'ENOENT') {
|
|
||||||
mkdirp(path.dirname(dir), (error) => {
|
|
||||||
if (!error) {
|
|
||||||
mkdirp(dir, callback)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else if (error && error.code === 'EEXIST') {
|
|
||||||
callback(null)
|
|
||||||
} else {
|
|
||||||
callback(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const readChromeStorageFile = (storageType, extensionId, cb) => {
|
|
||||||
const filePath = getChromeStoragePath(storageType, extensionId)
|
|
||||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
||||||
if (err && err.code === 'ENOENT') {
|
|
||||||
return cb(null, null)
|
|
||||||
}
|
|
||||||
cb(err, data)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
|
|
||||||
const filePath = getChromeStoragePath(storageType, extensionId)
|
|
||||||
|
|
||||||
mkdirp(path.dirname(filePath), err => {
|
|
||||||
if (err) { /* we just ignore the errors of mkdir or mkdirp */ }
|
|
||||||
fs.writeFile(filePath, data, cb)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const getStorage = (storageType, extensionId, cb) => {
|
|
||||||
readChromeStorageFile(storageType, extensionId, (err, data) => {
|
|
||||||
if (err) throw err
|
|
||||||
if (!cb) throw new TypeError('No callback provided')
|
|
||||||
|
|
||||||
|
ipcRendererUtils.invoke('CHROME_STORAGE_READ', storageType, extensionId)
|
||||||
|
.then(data => {
|
||||||
if (data !== null) {
|
if (data !== null) {
|
||||||
cb(JSON.parse(data))
|
callback(JSON.parse(data))
|
||||||
} else {
|
} else {
|
||||||
// Disabled due to false positive in StandardJS
|
// Disabled due to false positive in StandardJS
|
||||||
// eslint-disable-next-line standard/no-callback-literal
|
// eslint-disable-next-line standard/no-callback-literal
|
||||||
cb({})
|
callback({})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const setStorage = (storageType, extensionId, storage, cb) => {
|
const setStorage = (storageType, extensionId, storage, callback) => {
|
||||||
const json = JSON.stringify(storage)
|
const json = JSON.stringify(storage)
|
||||||
writeChromeStorageFile(storageType, extensionId, json, err => {
|
ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json)
|
||||||
if (err) throw err
|
.then(() => {
|
||||||
if (cb) cb()
|
if (callback) callback()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,18 +8,3 @@ exports.getRemote = function (name) {
|
||||||
}
|
}
|
||||||
return remote[name]
|
return remote[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.remoteRequire = function (name) {
|
|
||||||
if (!remote) {
|
|
||||||
throw new Error(`${name} requires remote, which is not enabled`)
|
|
||||||
}
|
|
||||||
return remote.require(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.potentiallyRemoteRequire = function (name) {
|
|
||||||
if (process.sandboxed) {
|
|
||||||
return exports.remoteRequire(name)
|
|
||||||
} else {
|
|
||||||
return require(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue