]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Fix Extension Bootstrap for Firefox 84
[VimFx.git] / extension / bootstrap.coffee
1 # This file boots the main VimFx process, as well as each frame script. It tries
2 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
3 # for frame scripts. It defines a few global variables, and sets up a
4 # Node.js-style `require` module loader.
5
6 # `bootstrap.js` files of different add-ons do _not_ share scope. However, frame
7 # scripts for the same `<browser>` but from different add-ons _do_ share scope.
8 # In order not to pollute that global scope in frame scripts, everything is done
9 # in an IIFE here, and the `global` variable is handled with care.
10
11 do (global = this) ->
12
13 {classes: Cc, interfaces: Ci, utils: Cu} = Components
14 ADDON_PATH = do -> # @echo ADDON_PATH
15 IS_FRAME_SCRIPT = (typeof content != 'undefined')
16 BUILD_TIME = do -> # @echo BUILD_TIME
17 REQUIRE_DATA = do -> # @echo REQUIRE_DATA
18
19 # Make `Services` and `console` available globally and in frame scripts.
20 Cu.import('resource://gre/modules/Services.jsm')
21 Cu.import('resource://gre/modules/Console.jsm')
22
23 shutdownHandlers = []
24
25 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
26
27 require = (path, moduleRoot = '.', dir = '.') ->
28 unless path[0] == '.'
29 # Allow `require('module/lib/foo')` in additon to `require('module')`.
30 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
31 base = REQUIRE_DATA[moduleRoot]?[name] ? moduleRoot
32 dir = "#{base}/node_modules/#{name}"
33 main = REQUIRE_DATA[dir]?['']
34 path = subPath ? main ? 'index'
35 moduleRoot = dir
36
37 prefix = "#{ADDON_PATH}/content"
38 uri = "#{prefix}/#{dir}/#{path}.js"
39 normalizedUri = Services.io.newURI(uri, null, null).spec
40 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
41
42 unless require.scopes[normalizedUri]?
43 module = {
44 exports: {}
45 onShutdown: (fn) -> shutdownHandlers.push(fn)
46 }
47 require.scopes[normalizedUri] = scope = {
48 require: (path) -> require.call(null, path, moduleRoot, currentDir)
49 module, exports: module.exports
50 Cc, Ci, Cu, Services
51 ADDON_PATH, BUILD_TIME
52 IS_FRAME_SCRIPT
53 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
54 }
55 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
56
57 return require.scopes[normalizedUri].module.exports
58
59 require.scopes = {}
60
61 startup = (args...) ->
62 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
63 require(main)(args...)
64
65 shutdown = ->
66 for shutdownHandler in shutdownHandlers
67 try
68 shutdownHandler()
69 catch error
70 Cu.reportError(error)
71 shutdownHandlers = []
72
73 # Release everything in `require`d modules. This must be done _after_ all
74 # shutdownHandlers, since they use variables in these scopes.
75 for path, scope of require.scopes
76 for name of scope
77 scope[name] = null
78 require.scopes = {}
79
80 if IS_FRAME_SCRIPT
81 messageManager = require('./lib/message-manager')
82
83 # Tell the main process that this frame script was created, and ask if
84 # anything should be done in this frame.
85 messageManager.send('tabCreated', null, (ok) ->
86 # After dragging a tab from one window to another, `content` might have
87 # been set to `null` by Firefox when this runs. If so, simply return.
88 return unless ok and content?
89
90 startup()
91
92 messageManager.listenOnce('shutdown', shutdown)
93 )
94 else
95 global.startup = startup
96 global.shutdown = shutdown
97 global.install = ->
98 global.uninstall = ->
Imprint / Impressum