]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Enable the `missing_fat_arrow` coffeelint rule
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 IS_FRAME_SCRIPT = (typeof content != 'undefined')
35
36 if IS_FRAME_SCRIPT
37 # Tell the main process that this frame script was created, and get data
38 # back that only the main process has access to.
39 [ data ] = sendSyncMessage('VimFx:tabCreated')
40
41 # The main process told this frame script not to do anything (or there was
42 # an error and no message was received at all).
43 return unless data
44
45 FRAME_SCRIPT_ENVIRONMENT = global
46 global = {}
47 [ global.__SCRIPT_URI_SPEC__, MULTI_PROCESS_ENABLED ] = data
48
49 else
50 # Make `Services` and `console` available globally, just like they are in
51 # frame scripts by default.
52 Cu.import('resource://gre/modules/Services.jsm')
53 Cu.import('resource://gre/modules/devtools/Console.jsm')
54
55 FRAME_SCRIPT_ENVIRONMENT = null
56 MULTI_PROCESS_ENABLED =
57 Services.prefs.getBoolPref('browser.tabs.remote.autostart')
58
59 shutdownHandlers = []
60
61 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
62 baseURI = createURI(global.__SCRIPT_URI_SPEC__)
63
64 # Everything up to the first `!` is the absolute path to the .xpi.
65 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
66
67 require = (path, moduleRoot = '.', dir = '.') ->
68 unless path[0] == '.'
69 # Allow `require('module/lib/foo')` in additon to `require('module')`.
70 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
71 base = require.data[moduleRoot]?[name] ? moduleRoot
72 dir = "#{ base }/node_modules/#{ name }"
73 main = require.data[dir]?['']
74 path = subPath ? main ? 'index'
75 moduleRoot = dir
76
77 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
78
79 unless require.scopes[fullPath]?
80 module =
81 exports: {}
82 onShutdown: (fn) -> shutdownHandlers.push(fn)
83 require.scopes[fullPath] = scope = {
84 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
85 module, exports: module.exports
86 Cc, Ci, Cu
87 MULTI_PROCESS_ENABLED, IS_FRAME_SCRIPT, FRAME_SCRIPT_ENVIRONMENT
88 }
89 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
90
91 return require.scopes[fullPath].module.exports
92
93 require.scopes = {}
94 require.data = require('./require-data')
95
96 unless IS_FRAME_SCRIPT
97 # Set default prefs and apply migrations as early as possible.
98 { applyMigrations } = require('./lib/legacy')
99 migrations = require('./lib/migrations')
100 prefs = require('./lib/prefs')
101
102 prefs.default.init()
103 applyMigrations(migrations)
104
105 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
106 global.startup = require(main)
107
108 global.shutdown = ->
109 require('./lib/message-manager').send('shutdown') unless IS_FRAME_SCRIPT
110
111 for shutdownHandler in shutdownHandlers
112 try
113 shutdownHandler()
114 catch error
115 Cu.reportError(error)
116 shutdownHandlers = null
117
118 # Release everything in `require`d modules. This must be done _after_ all
119 # shutdownHandlers, since they use variables in these scopes.
120 for path, scope of require.scopes
121 for name of scope
122 scope[name] = null
123 require.scopes = null
124
125 global.install = ->
126
127 global.uninstall = ->
128
129 if IS_FRAME_SCRIPT
130 global.startup()
131
132 # When updating the add-on, the previous version is going to shut down at
133 # the same time as the new version starts up. Add the shutdown listener in
134 # the next tick to prevent the previous version from triggering it.
135 content.setTimeout((->
136 require('./lib/message-manager').listenOnce('shutdown', global.shutdown)
137 ), 0)
Imprint / Impressum