| OLD | NEW | 
 | (Empty) | 
|    1 /* This Source Code Form is subject to the terms of the Mozilla Public |  | 
|    2  * License, v. 2.0. If a copy of the MPL was not distributed with this |  | 
|    3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |  | 
|    4  |  | 
|    5 const Cc = Components.classes; |  | 
|    6 const Ci = Components.interfaces; |  | 
|    7 const Cr = Components.results; |  | 
|    8 const Cu = Components.utils; |  | 
|    9  |  | 
|   10 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |  | 
|   11  |  | 
|   12 Cu.importGlobalProperties(["atob", "btoa", "File", "URL", |  | 
|   13     "TextDecoder", "TextEncoder", "XMLHttpRequest"]); |  | 
|   14  |  | 
|   15 let addonData = null; |  | 
|   16  |  | 
|   17 function startup(params, reason) |  | 
|   18 { |  | 
|   19   addonData = params; |  | 
|   20  |  | 
|   21   {%- if hasChromeRequires %} |  | 
|   22   Services.obs.addObserver(RequireObserver, "{{metadata.get('general', 'basename
     ')}}-require", true); |  | 
|   23   onShutdown.add(function() |  | 
|   24   { |  | 
|   25     Services.obs.removeObserver(RequireObserver, "{{metadata.get('general', 'bas
     ename')}}-require"); |  | 
|   26   }); |  | 
|   27   {%- set hasShutdownHandlers = True %} |  | 
|   28   {%- endif %} |  | 
|   29  |  | 
|   30   {%- if hasWebExtension %} |  | 
|   31   let port = params.webExtension.startup().then(({browser}) => |  | 
|   32   { |  | 
|   33     return new Promise((resolve, reject) => |  | 
|   34     { |  | 
|   35       browser.runtime.onConnect.addListener(resolve); |  | 
|   36     }); |  | 
|   37   }); |  | 
|   38   require.scopes.webextension = {exports:  port}; |  | 
|   39   {%- endif %} |  | 
|   40  |  | 
|   41   require("main"); |  | 
|   42 } |  | 
|   43  |  | 
|   44 function shutdown(params, reason) |  | 
|   45 { |  | 
|   46   {%- if chromeWindows %} |  | 
|   47   let windowNames = {{chromeWindows|json}}; |  | 
|   48   for (let i = 0; i < windowNames.length; i++) |  | 
|   49   { |  | 
|   50     let enumerator = Services.wm.getEnumerator(windowNames[i]); |  | 
|   51     while (enumerator.hasMoreElements()) |  | 
|   52     { |  | 
|   53       let window = enumerator.getNext().QueryInterface(Ci.nsIDOMWindow); |  | 
|   54       window.setTimeout("window.close()", 0); // Closing immediately might not w
     ork due to modal windows |  | 
|   55       try |  | 
|   56       { |  | 
|   57         window.close(); |  | 
|   58       } catch(e) {} |  | 
|   59     } |  | 
|   60   } |  | 
|   61   {%- endif %} |  | 
|   62  |  | 
|   63   {%- if hasShutdownHandlers %} |  | 
|   64   onShutdown.done = true; |  | 
|   65   for (let i = shutdownHandlers.length - 1; i >= 0; i --) |  | 
|   66   { |  | 
|   67     try |  | 
|   68     { |  | 
|   69       shutdownHandlers[i](); |  | 
|   70     } |  | 
|   71     catch (e) |  | 
|   72     { |  | 
|   73       Cu.reportError(e); |  | 
|   74     } |  | 
|   75   } |  | 
|   76   shutdownHandlers = null; |  | 
|   77   {%- endif %} |  | 
|   78  |  | 
|   79   // Make sure to release our ties to the modules even if the sandbox cannot be |  | 
|   80   // released for some reason. |  | 
|   81   for (let key in require.scopes) |  | 
|   82   { |  | 
|   83     let scope = require.scopes[key]; |  | 
|   84     let list = Object.keys(scope); |  | 
|   85     for (let i = 0; i < list.length; i++) |  | 
|   86       scope[list[i]] = null; |  | 
|   87   } |  | 
|   88   require.scopes = null; |  | 
|   89   addonData = null; |  | 
|   90 } |  | 
|   91  |  | 
|   92 function install(params, reason) {} |  | 
|   93  |  | 
|   94 function uninstall(params, reason) |  | 
|   95 { |  | 
|   96   {%- if 'currentVersion' in jsonRequires.get('prefs.json', {}).get('defaults', 
     {}) %} |  | 
|   97   const ADDON_UNINSTALL = 6;  // https://developer.mozilla.org/en/Extensions/Boo
     tstrapped_extensions#Reason_constants |  | 
|   98   if (reason == ADDON_UNINSTALL) |  | 
|   99   { |  | 
|  100     // Users often uninstall/reinstall extension to "fix" issues. Clear current |  | 
|  101     // version number on uninstall to rerun first-run actions in this scenario. |  | 
|  102     Services.prefs.clearUserPref("extensions.{{metadata.get('general', 'basename
     ')}}.currentVersion"); |  | 
|  103   } |  | 
|  104   {%- endif %} |  | 
|  105 } |  | 
|  106  |  | 
|  107 {%- if hasShutdownHandlers %} |  | 
|  108 let shutdownHandlers = []; |  | 
|  109 let onShutdown = |  | 
|  110 { |  | 
|  111   done: false, |  | 
|  112   add: function(handler) |  | 
|  113   { |  | 
|  114     if (shutdownHandlers.indexOf(handler) < 0) |  | 
|  115       shutdownHandlers.push(handler); |  | 
|  116   }, |  | 
|  117   remove: function(handler) |  | 
|  118   { |  | 
|  119     let index = shutdownHandlers.indexOf(handler); |  | 
|  120     if (index >= 0) |  | 
|  121       shutdownHandlers.splice(index, 1); |  | 
|  122   } |  | 
|  123 }; |  | 
|  124 {%- endif %} |  | 
|  125  |  | 
|  126 function require(module) |  | 
|  127 { |  | 
|  128   let scopes = require.scopes; |  | 
|  129   if (!(module in scopes)) |  | 
|  130   { |  | 
|  131     {%- if 'info' in requires %} |  | 
|  132     if (module == "info") |  | 
|  133     { |  | 
|  134       let applications = {{applications|json}}; |  | 
|  135       let appInfo = Services.appinfo; |  | 
|  136  |  | 
|  137       scopes[module] = {}; |  | 
|  138       scopes[module].exports = |  | 
|  139       { |  | 
|  140         addonID: addonData.id, |  | 
|  141         addonVersion: addonData.version, |  | 
|  142         addonRoot: addonData.resourceURI.spec, |  | 
|  143         addonName: "{{metadata.get('general', 'basename')}}", |  | 
|  144         application: (appInfo.ID in applications ? applications[appInfo.ID] : "o
     ther"), |  | 
|  145         applicationVersion: appInfo.version, |  | 
|  146         platform: "gecko", |  | 
|  147         platformVersion: appInfo.platformVersion |  | 
|  148       }; |  | 
|  149     } |  | 
|  150     else |  | 
|  151     { |  | 
|  152     {%- endif %} |  | 
|  153       let url = addonData.resourceURI.spec + "lib/" + module + ".js"; |  | 
|  154       scopes[module] = { |  | 
|  155         Cc, Ci, Cr, Cu, atob, btoa, File, URL, TextDecoder, TextEncoder, |  | 
|  156         XMLHttpRequest, require, |  | 
|  157         {% if hasShutdownHandlers %} |  | 
|  158         onShutdown, |  | 
|  159         {% endif %} |  | 
|  160         exports: {}}; |  | 
|  161       {%- if multicompartment %} |  | 
|  162       let principal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrin
     cipal); |  | 
|  163       scopes[module] = new Cu.Sandbox(principal, { |  | 
|  164         sandboxName: url, |  | 
|  165         sandboxPrototype: scopes[module], |  | 
|  166         wantXrays: false |  | 
|  167       }); |  | 
|  168       {%- endif %} |  | 
|  169       Services.scriptloader.loadSubScript(url, scopes[module]); |  | 
|  170     {%- if 'info' in requires %} |  | 
|  171     } |  | 
|  172     {%- endif %} |  | 
|  173   } |  | 
|  174   return scopes[module].exports; |  | 
|  175 } |  | 
|  176 require.scopes = Object.create(null); |  | 
|  177 {%- for name, data in jsonRequires.iteritems() %} |  | 
|  178 require.scopes[{{name|json}}] = {exports: {{data|json}}}; |  | 
|  179 {%- endfor %} |  | 
|  180  |  | 
|  181 {%- if hasChromeRequires %} |  | 
|  182 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |  | 
|  183  |  | 
|  184 let RequireObserver = |  | 
|  185 { |  | 
|  186   observe: function(subject, topic, data) |  | 
|  187   { |  | 
|  188     if (topic == "{{metadata.get('general', 'basename')}}-require") |  | 
|  189     { |  | 
|  190       subject.wrappedJSObject.exports = require(data); |  | 
|  191     } |  | 
|  192   }, |  | 
|  193  |  | 
|  194   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
     rver]) |  | 
|  195 }; |  | 
|  196 {%- endif %} |  | 
| OLD | NEW |