]> git.gir.st - VimFx.git/blob - extension/lib/config.coffee
move from deprecated Cu.import to ChromeUtils.import
[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 {OS} = ChromeUtils.import('resource://gre/modules/osfile.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 = (expandedDir) ->
34 prefix = 'security.sandbox.content'
35 if prefs.root.get("#{prefix}.level") > 2
36 return true
37
38 if Services.appinfo.OS == 'Darwin'
39 whitelisted = [
40 prefs.root.get("#{prefix}.mac.testing_read_path1"),
41 prefs.root.get("#{prefix}.mac.testing_read_path2")
42 ]
43 else
44 whitelisted = prefs.root.get("#{prefix}.read_path_whitelist").split(',')
45 return not whitelisted.some((e) -> e.startsWith(expandedDir))
46
47 loadFile = (dir, file, scope) ->
48 expandedDir = utils.expandPath(dir)
49 uri = OS.Path.toFileURI(OS.Path.join(expandedDir, file))
50 try
51 Services.scriptloader.loadSubScriptWithOptions(uri, {
52 target: Object.assign({
53 __dirname: OS.Path.toFileURI(expandedDir),
54 Services: Services
55 }, scope)
56 charset: 'UTF-8'
57 ignoreCache: true
58 })
59 return null
60 catch error
61 # in e10s Firefox / Firefox Quantum the content process sandbox might
62 # prevent us from accessing frame.js. The error message is incomprehensible
63 # without explanation.
64 if typeof error == 'string' and
65 error.startsWith('Error opening input stream (invalid filename?)') and
66 checkSandbox(expandedDir)
67 console.error("VimFx: Error loading #{file} likely due to e10s sandbox")
68 console.info("Please consult VimFx' documentation on config files.")
69 else
70 console.error("VimFx: Error loading #{file}", uri, error)
71 return error
72
73 module.exports = {
74 load
75 loadFile
76 }
Imprint / Impressum