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.
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.
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
19 Services = globalThis.Services or
20 ChromeUtils.import('resource://gre/modules/Services.jsm').Services # <=fx103
24 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
26 require = (path, moduleRoot = '.', dir = '.') ->
28 # Allow `require('module/lib/foo')` in additon to `require('module')`.
29 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
30 base = REQUIRE_DATA[moduleRoot]?[name] ? moduleRoot
31 dir = "#{base}/node_modules/#{name}"
32 main = REQUIRE_DATA[dir]?['']
33 path = subPath ? main ? 'index'
36 prefix = "#{ADDON_PATH}/content"
37 uri = "#{prefix}/#{dir}/#{path}.js"
38 normalizedUri = Services.io.newURI(uri, null, null).spec
39 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
41 unless require.scopes[normalizedUri]?
44 onShutdown: (fn) -> shutdownHandlers.push(fn)
46 require.scopes[normalizedUri] = scope = {
47 require: (path) -> require.call(null, path, moduleRoot, currentDir)
48 module, exports: module.exports
50 ADDON_PATH, BUILD_TIME
52 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
54 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
56 return require.scopes[normalizedUri].module.exports
60 startup = (args...) ->
61 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
62 require(main)(args...)
65 for shutdownHandler in shutdownHandlers
72 # Release everything in `require`d modules. This must be done _after_ all
73 # shutdownHandlers, since they use variables in these scopes.
74 for path, scope of require.scopes
80 messageManager = require('./lib/message-manager')
82 # Tell the main process that this frame script was created, and ask if
83 # anything should be done in this frame.
84 messageManager.send('tabCreated', null, (ok) ->
85 # After dragging a tab from one window to another, `content` might have
86 # been set to `null` by Firefox when this runs. If so, simply return.
87 return unless ok and content?
91 messageManager.listenOnce('shutdown', shutdown)
94 global.startup = startup
95 global.shutdown = shutdown