]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Update copyright notices
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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 # This file boots the main VimFx process, as well as each frame script. It tries
22 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
23 # for frame scripts. It defines a few global variables, and sets up a
24 # Node.js-style `require` module loader.
25
26 # `bootstrap.js` files of different add-ons do _not_ share scope. However, frame
27 # scripts for the same `<browser>` but from different add-ons _do_ share scope.
28 # In order not to pollute that global scope in frame scripts, everything is done
29 # in an IIFE here, and the `global` variable is handled with care.
30
31 do (global = this) ->
32
33 {classes: Cc, interfaces: Ci, utils: Cu} = Components
34 ADDON_PATH = 'chrome://vimfx'
35 IS_FRAME_SCRIPT = (typeof content != 'undefined')
36
37 if IS_FRAME_SCRIPT
38 # Tell the main process that this frame script was created, and get data
39 # back that only the main process has access to.
40 [ok] = sendSyncMessage("#{ADDON_PATH}/tabCreated")
41
42 # The main process told this frame script not to do anything (or there was
43 # an error and no message was received at all).
44 return unless ok
45
46 else
47 # Make `Services` and `console` available globally, just like they are in
48 # frame scripts by default.
49 Cu.import('resource://gre/modules/Services.jsm')
50 try
51 # TODO: Only use this path when Firefox 44 is released.
52 Cu.import('resource://gre/modules/Console.jsm')
53 catch
54 Cu.import('resource://gre/modules/devtools/Console.jsm')
55
56 shutdownHandlers = []
57
58 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
59
60 require = (path, moduleRoot = '.', dir = '.') ->
61 unless path[0] == '.'
62 # Allow `require('module/lib/foo')` in additon to `require('module')`.
63 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
64 base = require.data[moduleRoot]?[name] ? moduleRoot
65 dir = "#{base}/node_modules/#{name}"
66 main = require.data[dir]?['']
67 path = subPath ? main ? 'index'
68 moduleRoot = dir
69
70 prefix = "#{ADDON_PATH}/content"
71 uri = "#{prefix}/#{dir}/#{path}.js"
72 normalizedUri = Services.io.newURI(uri, null, null).spec
73 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
74
75 unless require.scopes[normalizedUri]?
76 module =
77 exports: {}
78 onShutdown: (fn) -> shutdownHandlers.push(fn)
79 require.scopes[normalizedUri] = scope = {
80 require: (path) -> require(path, moduleRoot, currentDir)
81 module, exports: module.exports
82 Cc, Ci, Cu
83 ADDON_PATH
84 IS_FRAME_SCRIPT
85 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
86 }
87 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
88
89 return require.scopes[normalizedUri].module.exports
90
91 global.startup = (args...) ->
92 require.scopes = {}
93 require.data = require('./require-data')
94
95 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
96 require(main)(args...)
97
98 global.shutdown = ->
99 require('./lib/message-manager').send('shutdown') unless IS_FRAME_SCRIPT
100
101 for shutdownHandler in shutdownHandlers
102 try
103 shutdownHandler()
104 catch error
105 Cu.reportError(error)
106 shutdownHandlers = null
107
108 # Release everything in `require`d modules. This must be done _after_ all
109 # shutdownHandlers, since they use variables in these scopes.
110 for path, scope of require.scopes
111 for name of scope
112 scope[name] = null
113 require.scopes = null
114
115 global.install = ->
116
117 global.uninstall = ->
118
119 if IS_FRAME_SCRIPT
120 global.startup()
121
122 # When updating the add-on, the previous version is going to shut down at
123 # the same time as the new version starts up. Add the shutdown listener in
124 # the next tick to prevent the previous version from triggering it.
125 content.setTimeout((->
126 require('./lib/message-manager').listenOnce('shutdown', global.shutdown)
127 ), 0)
Imprint / Impressum