]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Lay the foundation for using multi-process Firefox
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # Expose `Components` shortcuts a well as `Services` and `console` in all
22 # modules.
23 { classes: Cc, interfaces: Ci, utils: Cu } = Components
24
25 Cu.import('resource://gre/modules/Services.jsm')
26 Cu.import('resource://gre/modules/devtools/Console.jsm')
27
28 do (global = this) ->
29 isFrameScript = (typeof content != 'undefined')
30
31 if isFrameScript
32 [ global.__SCRIPT_URI_SPEC__ ] = sendSyncMessage('VimFx:tabCreated')
33 return if __SCRIPT_URI_SPEC__ == false
34
35 shutdownHandlers = []
36
37 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
38 baseURI = createURI(__SCRIPT_URI_SPEC__)
39
40 # Everything up to the first `!` is the absolute path to the .xpi.
41 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
42
43 require = (path, moduleRoot = '.', dir = '.') ->
44 unless path[0] == '.'
45 # Allow `require('module/lib/foo')` in additon to `require('module')`.
46 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
47 base = require.data[moduleRoot]?[name] ? moduleRoot
48 dir = "#{ base }/node_modules/#{ name }"
49 main = require.data[dir]?['']
50 path = subPath ? main ? 'index'
51 moduleRoot = dir
52
53 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
54
55 unless require.scopes[fullPath]?
56 module =
57 exports: {}
58 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
59 require.scopes[fullPath] = scope =
60 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
61 module: module
62 exports: module.exports
63 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
64
65 return require.scopes[fullPath].module.exports
66
67 require.scopes = {}
68 require.data = require('./require-data')
69
70 unless isFrameScript
71 # Set default prefs and apply migrations as early as possible.
72 { applyMigrations } = require('./lib/legacy')
73 migrations = require('./lib/migrations')
74 prefs = require('./lib/prefs')
75
76 prefs.default._init()
77 applyMigrations(migrations)
78
79 main = if isFrameScript then './lib/main-frame' else './lib/main'
80 global.startup = require(main)
81
82 global.shutdown = (data, reason) ->
83 for shutdownHandler in shutdownHandlers
84 try
85 shutdownHandler()
86 catch error
87 Cu.reportError(error)
88 shutdownHandlers = null
89 removeEventListener('unload', shutdown, true) if isFrameScript
90
91 # Release everything in `require`d modules. This must be done _after_ all
92 # shutdownHandlers, since they use variables in these scopes.
93 for path, scope of require.scopes
94 for name of scope
95 scope[name] = null
96 require.scopes = null
97
98 global.install = (data, reason) ->
99
100 global.uninstall = (data, reason) ->
101
102 if isFrameScript
103 addEventListener('unload', shutdown, true)
104 startup()
Imprint / Impressum