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