2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
5 # This file is part of VimFx.
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.
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.
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/>.
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.
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
30 Cu.import('resource://gre/modules/Services.jsm')
31 Cu.import('resource://gre/modules/devtools/Console.jsm')
33 @IS_FRAME_SCRIPT = (typeof content != 'undefined')
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')
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).
46 [ global.__SCRIPT_URI_SPEC__, global.MULTI_PROCESS_ENABLED ] = data
47 global.FRAME_SCRIPT_ENVIRONMENT = global
51 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
52 baseURI = createURI(__SCRIPT_URI_SPEC__)
54 # Everything up to the first `!` is the absolute path to the .xpi.
55 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
57 require = (path, moduleRoot = '.', dir = '.') ->
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'
67 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
69 unless require.scopes[fullPath]?
72 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
73 require.scopes[fullPath] = scope =
74 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
76 exports: module.exports
77 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
79 return require.scopes[fullPath].module.exports
82 require.data = require('./require-data')
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')
90 global.MULTI_PROCESS_ENABLED =
91 prefs.root.get('browser.tabs.remote.autostart')
93 applyMigrations(migrations)
95 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
96 global.startup = require(main)
99 require('./lib/message-manager').send('shutdown') unless IS_FRAME_SCRIPT
101 for shutdownHandler in shutdownHandlers
105 Cu.reportError(error)
106 shutdownHandlers = null
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
113 require.scopes = null
117 global.uninstall = ->
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', shutdown)