Index: lib/ui.js |
=================================================================== |
--- a/lib/ui.js |
+++ b/lib/ui.js |
@@ -272,75 +272,67 @@ let notificationTimer = null; |
let UI = exports.UI = |
{ |
/** |
* Gets called on startup, initializes UI integration. |
*/ |
init: function() |
{ |
- // We should call initDone once both overlay and filters are loaded |
- let overlayLoaded = false; |
- let filtersLoaded = false; |
- let sessionRestored = false; |
+ // We have to wait for multiple events before running start-up actions |
+ let prerequisites = []; |
// Start loading overlay |
- let request = new XMLHttpRequest(); |
- request.mozBackgroundRequest = true; |
- request.open("GET", "chrome://adblockplus/content/ui/overlay.xul"); |
- request.channel.owner = Utils.systemPrincipal; |
- request.addEventListener("load", function(event) |
+ prerequisites.push(new Promise((resolve, reject) => |
{ |
- if (onShutdown.done) |
- return; |
+ let request = new XMLHttpRequest(); |
+ request.mozBackgroundRequest = true; |
+ request.open("GET", "chrome://adblockplus/content/ui/overlay.xul"); |
+ request.channel.owner = Utils.systemPrincipal; |
+ request.addEventListener("load", event => |
+ { |
+ if (onShutdown.done) |
+ return; |
- this.processOverlay(request.responseXML.documentElement); |
+ this.processOverlay(request.responseXML.documentElement); |
- // Don't wait for the rest of the startup sequence, add icon already |
- this.addToolbarButton(); |
+ // Don't wait for the rest of the startup sequence, add icon already |
+ this.addToolbarButton(); |
- overlayLoaded = true; |
- if (overlayLoaded && filtersLoaded && sessionRestored) |
- this.initDone(); |
- }.bind(this), false); |
- request.send(null); |
+ resolve(); |
+ }, false); |
+ |
+ request.addEventListener("error", event => |
+ { |
+ reject(new Error("Unexpected: Failed to load overlay.xul")); |
+ }); |
+ |
+ request.send(null); |
+ })); |
// Wait for filters to load |
if (FilterStorage._loading) |
+ prerequisites.push(FilterNotifier.once("load")); |
+ |
+ // Wait for session to be restored |
+ prerequisites.push(new Promise((resolve, reject) => |
{ |
- let listener = function(action) |
+ let window = this.currentWindow; |
+ if (!window && "nsISessionStore" in Ci) |
{ |
- if (action != "load") |
- return; |
+ // No application windows yet, the application must be starting up. Wait |
+ // for session to be restored before initializing our UI. |
+ new SessionRestoreObserver(resolve); |
+ } |
+ else |
+ resolve(); |
+ })); |
- FilterNotifier.removeListener(listener); |
- filtersLoaded = true; |
- if (overlayLoaded && filtersLoaded && sessionRestored) |
- this.initDone(); |
- }.bind(this); |
- FilterNotifier.addListener(listener); |
- } |
- else |
- filtersLoaded = true; |
- |
- // Initialize UI after the session is restored |
- let window = this.currentWindow; |
- if (!window && "nsISessionStore" in Ci) |
- { |
- // No application windows yet, the application must be starting up. Wait |
- // for session to be restored before initializing our UI. |
- new SessionRestoreObserver(function() |
- { |
- sessionRestored = true; |
- if (overlayLoaded && filtersLoaded && sessionRestored) |
- this.initDone(); |
- }.bind(this)); |
- } |
- else |
- sessionRestored = true; |
+ Promise.all(prerequisites).then(() => this.initDone()) |
+ .catch(e => Cu.reportError(e)); |
}, |
/** |
* Provesses overlay document data and initializes overlay property. |
*/ |
processOverlay: function(/**Element*/ root) |
{ |
Utils.splitAllLabels(root); |