]> git.gir.st - VimFx.git/blob - extension/lib/config.coffee
port to ESMs and set minimum firefox version to 115
[VimFx.git] / extension / lib / config.coffee
1 # This file loads the user config file: config.js and frame.js.
2
3 createConfigAPI = require('./api')
4 messageManager = require('./message-manager')
5 utils = require('./utils')
6 prefs = require('./prefs')
7
8 {FileUtils} =
9 ChromeUtils.importESModule('resource://gre/modules/FileUtils.sys.mjs')
10
11 load = (vimfx, options = null, callback = ->) ->
12 configDir = vimfx.options.config_file_directory
13
14 unless configDir
15 callback(null)
16 return
17
18 scope = {vimfx: createConfigAPI(vimfx, options)}
19
20 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
21 # shortcut is absolutely redundant and may make Firefox start slower. Do it
22 # once instead.
23 vimfx.skipCreateKeyTrees = true
24 error = loadFile(configDir, 'config.js', scope)
25 vimfx.skipCreateKeyTrees = false
26 vimfx.createKeyTrees()
27
28 if error
29 callback(false)
30 return
31
32 messageManager.send('loadConfig', null, callback)
33
34 sandboxPreventsAccess = (dir) ->
35 expandedDir = utils.expandPath(dir)
36
37 prefix = 'security.sandbox.content'
38 if prefs.root.get("#{prefix}.level") <= 2
39 return false
40
41 if Services.appinfo.OS == 'Darwin'
42 whitelisted = [
43 prefs.root.get("#{prefix}.mac.testing_read_path1"),
44 prefs.root.get("#{prefix}.mac.testing_read_path2")
45 ]
46 else
47 whitelisted = prefs.root.get("#{prefix}.read_path_whitelist").split(',')
48
49 return not whitelisted.some((e) -> e.startsWith(expandedDir))
50
51 loadFile = (dir, file, scope) ->
52 expandedPath = new FileUtils.File(utils.expandPath(dir))
53 dirUri = Services.io.newFileURI(expandedPath).spec
54 expandedPath.append(file)
55 uri = Services.io.newFileURI(expandedPath).spec
56 try
57 Services.scriptloader.loadSubScriptWithOptions(uri, {
58 target: Object.assign({
59 __dirname: dirUri,
60 Services: Services
61 }, scope)
62 charset: 'UTF-8'
63 ignoreCache: true
64 })
65 return null
66 catch error
67 # in e10s Firefox / Firefox Quantum the content process sandbox might
68 # prevent us from accessing frame.js. The error message is incomprehensible
69 # without explanation.
70 if typeof error == 'string' and
71 error.startsWith('Error opening input stream (invalid filename?)') and
72 sandboxPreventsAccess(dir)
73 # coffeelint: disable=max_line_length
74 docs = "#{HOMEPAGE}/blob/master/documentation/config-file.md#on-process-sandboxing"
75 # coffeelint: enable=max_line_length
76 console.error("VimFx: Error loading #{file} likely due to e10s sandbox")
77 console.info("Please consult VimFx' documentation: {docs}")
78 else
79 console.error("VimFx: Error loading #{file}", uri, error)
80 return error
81
82 module.exports = {
83 load
84 loadFile
85 }
Imprint / Impressum