]> git.gir.st - VimFx.git/blob - extension/lib/config.coffee
Port osfile.jsm usage to IOUtils
[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 load = (vimfx, options = null, callback = ->) ->
9 configDir = vimfx.options.config_file_directory
10
11 unless configDir
12 callback(null)
13 return
14
15 scope = {vimfx: createConfigAPI(vimfx, options)}
16
17 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
18 # shortcut is absolutely redundant and may make Firefox start slower. Do it
19 # once instead.
20 vimfx.skipCreateKeyTrees = true
21 error = loadFile(configDir, 'config.js', scope)
22 vimfx.skipCreateKeyTrees = false
23 vimfx.createKeyTrees()
24
25 if error
26 callback(false)
27 return
28
29 messageManager.send('loadConfig', null, callback)
30
31 checkSandbox = (expandedDir) ->
32 prefix = 'security.sandbox.content'
33 if prefs.root.get("#{prefix}.level") > 2
34 return true
35
36 if Services.appinfo.OS == 'Darwin'
37 whitelisted = [
38 prefs.root.get("#{prefix}.mac.testing_read_path1"),
39 prefs.root.get("#{prefix}.mac.testing_read_path2")
40 ]
41 else
42 whitelisted = prefs.root.get("#{prefix}.read_path_whitelist").split(',')
43 return not whitelisted.some((e) -> e.startsWith(expandedDir))
44
45 loadFile = (dir, file, scope) ->
46 expandedDir = utils.expandPath(dir)
47 uri = PathUtils.toFileURI(PathUtils.join(expandedDir, file))
48 try
49 Services.scriptloader.loadSubScriptWithOptions(uri, {
50 target: Object.assign({
51 __dirname: PathUtils.toFileURI(expandedDir),
52 Services: Services
53 }, scope)
54 charset: 'UTF-8'
55 ignoreCache: true
56 })
57 return null
58 catch error
59 # in e10s Firefox / Firefox Quantum the content process sandbox might
60 # prevent us from accessing frame.js. The error message is incomprehensible
61 # without explanation.
62 if typeof error == 'string' and
63 error.startsWith('Error opening input stream (invalid filename?)') and
64 checkSandbox(expandedDir)
65 console.error("VimFx: Error loading #{file} likely due to e10s sandbox")
66 console.info("Please consult VimFx' documentation on config files.")
67 else
68 console.error("VimFx: Error loading #{file}", uri, error)
69 return error
70
71 module.exports = {
72 load
73 loadFile
74 }
Imprint / Impressum