]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Add MULTI_PROCESS_ENABLED global
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
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 # Expose `Components` shortcuts a well as `Services`, `console` and
27 # `IS_FRAME_SCRIPT` in all modules. The `@`s are needed for frame scripts.
28 { classes: @Cc, interfaces: @Ci, utils: @Cu } = Components
29
30 Cu.import('resource://gre/modules/Services.jsm')
31 Cu.import('resource://gre/modules/devtools/Console.jsm')
32
33 @IS_FRAME_SCRIPT = (typeof content != 'undefined')
34
35 do (global = this) ->
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 [ data ] = sendSyncMessage('VimFx: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 data
45
46 [ global.__SCRIPT_URI_SPEC__, global.MULTI_PROCESS_ENABLED ] = data
47 global.FRAME_SCRIPT_ENVIRONMENT = global
48
49 shutdownHandlers = []
50
51 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
52 baseURI = createURI(__SCRIPT_URI_SPEC__)
53
54 # Everything up to the first `!` is the absolute path to the .xpi.
55 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
56
57 require = (path, moduleRoot = '.', dir = '.') ->
58 unless path[0] == '.'
59 # Allow `require('module/lib/foo')` in additon to `require('module')`.
60 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
61 base = require.data[moduleRoot]?[name] ? moduleRoot
62 dir = "#{ base }/node_modules/#{ name }"
63 main = require.data[dir]?['']
64 path = subPath ? main ? 'index'
65 moduleRoot = dir
66
67 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
68
69 unless require.scopes[fullPath]?
70 module =
71 exports: {}
72 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
73 require.scopes[fullPath] = scope =
74 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
75 module: module
76 exports: module.exports
77 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
78
79 return require.scopes[fullPath].module.exports
80
81 require.scopes = {}
82 require.data = require('./require-data')
83
84 unless IS_FRAME_SCRIPT
85 # Set default prefs and apply migrations as early as possible.
86 { applyMigrations } = require('./lib/legacy')
87 migrations = require('./lib/migrations')
88 prefs = require('./lib/prefs')
89
90 global.MULTI_PROCESS_ENABLED =
91 prefs.root.get('browser.tabs.remote.autostart')
92 prefs.default._init()
93 applyMigrations(migrations)
94
95 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
96 global.startup = require(main)
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 require('./lib/message-manager').listenOnce('shutdown', shutdown)
121 startup()
Imprint / Impressum