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