@@ -19,6 +19,13 @@ const resourceHandler = ioService.getProtocolHandler('resource').
1919const systemPrincipal = CC ( '@mozilla.org/systemprincipal;1' , 'nsIPrincipal' ) ( ) ;
2020const scriptLoader = Cc [ '@mozilla.org/moz/jssubscript-loader;1' ] .
2121 getService ( Ci . mozIJSSubScriptLoader ) ;
22+ const prefService = Cc [ '@mozilla.org/preferences-service;1' ] .
23+ getService ( Ci . nsIPrefService ) ;
24+ const appInfo = Cc [ "@mozilla.org/xre/app-info;1" ] .
25+ getService ( Ci . nsIXULAppInfo ) ;
26+ const vc = Cc [ "@mozilla.org/xpcom/version-comparator;1" ] .
27+ getService ( Ci . nsIVersionComparator ) ;
28+
2229
2330const REASON = [ 'unknown' , 'startup' , 'shutdown' , 'enable' , 'disable' ,
2431 'install' , 'uninstall' , 'upgrade' , 'downgrade' ] ;
@@ -120,19 +127,54 @@ function startup(data, reasonCode) {
120127 if ( name == 'addon-sdk' )
121128 paths [ 'tests/' ] = prefixURI + name + '/tests/' ;
122129
123- // Maps sdk module folders to their resource folder
124- paths [ 'sdk/' ] = prefixURI + 'addon-sdk/lib/sdk/' ;
125- paths [ 'toolkit/' ] = prefixURI + 'addon-sdk/lib/toolkit/' ;
126- // test.js is usually found in root commonjs or SDK_ROOT/lib/ folder,
127- // so that it isn't shipped in the xpi. Keep a copy of it in sdk/ folder
128- // until we no longer support SDK modules in XPI:
129- paths [ 'test' ] = prefixURI + 'addon-sdk/lib/sdk/test.js' ;
130+ // Starting with Firefox 21.0a1, we start using modules shipped into firefox
131+ // Still allow using modules from the xpi if the manifest tell us to do so.
132+ // And only try to look for sdk modules in xpi if the xpi actually ship them
133+ if ( options [ 'is-sdk-bundled' ] &&
134+ ( vc . compare ( appInfo . version , '21.0a1' ) < 0 ||
135+ options [ 'force-use-bundled-sdk' ] ) ) {
136+ // Maps sdk module folders to their resource folder
137+ paths [ '' ] = prefixURI + 'addon-sdk/lib/' ;
138+ // test.js is usually found in root commonjs or SDK_ROOT/lib/ folder,
139+ // so that it isn't shipped in the xpi. Keep a copy of it in sdk/ folder
140+ // until we no longer support SDK modules in XPI:
141+ paths [ 'test' ] = prefixURI + 'addon-sdk/lib/sdk/test.js' ;
142+ }
143+
144+ // Retrieve list of module folder overloads based on preferences in order to
145+ // eventually used a local modules instead of files shipped into Firefox.
146+ let branch = prefService . getBranch ( 'extensions.modules.' + id + '.path' ) ;
147+ paths = branch . getChildList ( '' , { } ) . reduce ( function ( result , name ) {
148+ // Allows overloading of any sub folder by replacing . by / in pref name
149+ let path = name . substr ( 1 ) . split ( '.' ) . join ( '/' ) ;
150+ // Only accept overloading folder by ensuring always ending with `/`
151+ if ( path ) path += '/' ;
152+ let fileURI = branch . getCharPref ( name ) ;
153+
154+ // Maps the given file:// URI to a resource:// in order to avoid various
155+ // failure that happens with file:// URI and be close to production env
156+ let resourcesURI = ioService . newURI ( fileURI , null , null ) ;
157+ let resName = 'extensions.modules.' + domain + '.commonjs.path' + name ;
158+ resourceHandler . setSubstitution ( resName , resourcesURI ) ;
159+
160+ result [ path ] = 'resource://' + resName + '/' ;
161+ return result ;
162+ } , paths ) ;
130163
131164 // Make version 2 of the manifest
132165 let manifest = options . manifest ;
133166
134167 // Import `cuddlefish.js` module using a Sandbox and bootstrap loader.
135- let cuddlefishURI = prefixURI + options . loader ;
168+ let cuddlefishPath = 'loader/cuddlefish.js' ;
169+ let cuddlefishURI = 'resource://gre/modules/commonjs/sdk/' + cuddlefishPath ;
170+ if ( paths [ 'sdk/' ] ) { // sdk folder has been overloaded
171+ // (from pref, or cuddlefish is still in the xpi)
172+ cuddlefishURI = paths [ 'sdk/' ] + cuddlefishPath ;
173+ }
174+ else if ( paths [ '' ] ) { // root modules folder has been overloaded
175+ cuddlefishURI = paths [ '' ] + 'sdk/' + cuddlefishPath ;
176+ }
177+
136178 cuddlefishSandbox = loadSandbox ( cuddlefishURI ) ;
137179 let cuddlefish = cuddlefishSandbox . exports ;
138180
0 commit comments