]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
move from deprecated Cu.import to ChromeUtils.import
[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 {Services} = ChromeUtils.import('resource://gre/modules/Services.jsm')
20
21 shutdownHandlers = []
22
23 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
24
25 require = (path, moduleRoot = '.', dir = '.') ->
26 unless path[0] == '.'
27 # Allow `require('module/lib/foo')` in additon to `require('module')`.
28 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
29 base = REQUIRE_DATA[moduleRoot]?[name] ? moduleRoot
30 dir = "#{base}/node_modules/#{name}"
31 main = REQUIRE_DATA[dir]?['']
32 path = subPath ? main ? 'index'
33 moduleRoot = dir
34
35 prefix = "#{ADDON_PATH}/content"
36 uri = "#{prefix}/#{dir}/#{path}.js"
37 normalizedUri = Services.io.newURI(uri, null, null).spec
38 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
39
40 unless require.scopes[normalizedUri]?
41 module = {
42 exports: {}
43 onShutdown: (fn) -> shutdownHandlers.push(fn)
44 }
45 require.scopes[normalizedUri] = scope = {
46 require: (path) -> require.call(null, path, moduleRoot, currentDir)
47 module, exports: module.exports
48 Cc, Ci, Cu, Services
49 ADDON_PATH, BUILD_TIME
50 IS_FRAME_SCRIPT
51 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
52 }
53 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
54
55 return require.scopes[normalizedUri].module.exports
56
57 require.scopes = {}
58
59 startup = (args...) ->
60 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
61 require(main)(args...)
62
63 shutdown = ->
64 for shutdownHandler in shutdownHandlers
65 try
66 shutdownHandler()
67 catch error
68 Cu.reportError(error)
69 shutdownHandlers = []
70
71 # Release everything in `require`d modules. This must be done _after_ all
72 # shutdownHandlers, since they use variables in these scopes.
73 for path, scope of require.scopes
74 for name of scope
75 scope[name] = null
76 require.scopes = {}
77
78 if IS_FRAME_SCRIPT
79 messageManager = require('./lib/message-manager')
80
81 # Tell the main process that this frame script was created, and ask if
82 # anything should be done in this frame.
83 messageManager.send('tabCreated', null, (ok) ->
84 # After dragging a tab from one window to another, `content` might have
85 # been set to `null` by Firefox when this runs. If so, simply return.
86 return unless ok and content?
87
88 startup()
89
90 messageManager.listenOnce('shutdown', shutdown)
91 )
92 else
93 global.startup = startup
94 global.shutdown = shutdown
95 global.install = ->
96 global.uninstall = ->
Imprint / Impressum