зеркало из https://github.com/mozilla/normandy.git
Fix bug 1371350: Delay almost all initialization until after first paint.
This commit is contained in:
Родитель
b77b6d1395
Коммит
a462febecc
|
@ -14,6 +14,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "LogManager",
|
|||
XPCOMUtils.defineLazyModuleGetter(this, "ShieldRecipeClient",
|
||||
"resource://shield-recipe-client/lib/ShieldRecipeClient.jsm");
|
||||
|
||||
const UI_AVAILABLE_NOTIFICATION = "sessionstore-windows-restored";
|
||||
const DEFAULT_PREFS = {
|
||||
"extensions.shield-recipe-client.api_url": "https://normandy.cdn.mozilla.net/api/v1",
|
||||
"extensions.shield-recipe-client.dev_mode": false,
|
||||
|
@ -29,10 +30,7 @@ const DEFAULT_PREFS = {
|
|||
"app.shield.optoutstudies.enabled": AppConstants.MOZ_DATA_REPORTING,
|
||||
};
|
||||
|
||||
this.install = function() {};
|
||||
|
||||
this.startup = function() {
|
||||
// Initialize preference defaults before anything else happens.
|
||||
function initializeShieldPreferences() {
|
||||
const prefBranch = Services.prefs.getDefaultBranch("");
|
||||
for (const [name, value] of Object.entries(DEFAULT_PREFS)) {
|
||||
switch (typeof value) {
|
||||
|
@ -49,13 +47,31 @@ this.startup = function() {
|
|||
throw new Error(`Invalid default preference type ${typeof value}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ShieldRecipeClient.startup();
|
||||
const uiAvailableObserver = {
|
||||
observe(subject, topic, data) {
|
||||
Services.obs.removeObserver(uiAvailableObserver, UI_AVAILABLE_NOTIFICATION);
|
||||
ShieldRecipeClient.startup();
|
||||
},
|
||||
};
|
||||
|
||||
this.install = function() {};
|
||||
|
||||
this.startup = function() {
|
||||
// Initialization that needs to happen before the first paint on startup.
|
||||
initializeShieldPreferences();
|
||||
|
||||
// Wait until the UI is available for remaining startup tasks
|
||||
Services.obs.addObserver(uiAvailableObserver, UI_AVAILABLE_NOTIFICATION);
|
||||
};
|
||||
|
||||
this.shutdown = function(data, reason) {
|
||||
ShieldRecipeClient.shutdown(reason);
|
||||
|
||||
// In case the observer didn't run, clean it up.
|
||||
Services.obs.addObserver(uiAvailableObserver, UI_AVAILABLE_NOTIFICATION);
|
||||
|
||||
// Unload add-on modules. We don't do this in ShieldRecipeClient so that
|
||||
// modules are not unloaded accidentally during tests.
|
||||
const log = LogManager.getLogger("bootstrap");
|
||||
|
|
|
@ -43,29 +43,21 @@ const prefs = Services.prefs.getBranch("extensions.shield-recipe-client.");
|
|||
const TIMER_NAME = "recipe-client-addon-run";
|
||||
const RUN_INTERVAL_PREF = "run_interval_seconds";
|
||||
const FIRST_RUN_PREF = "first_run";
|
||||
const UI_AVAILABLE_TOPIC = "sessionstore-windows-restored";
|
||||
const SHIELD_INIT_TOPIC = "shield-init-complete";
|
||||
const PREF_CHANGED_TOPIC = "nsPref:changed";
|
||||
|
||||
this.RecipeRunner = {
|
||||
init() {
|
||||
async init() {
|
||||
if (!this.checkPrefs()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prefs.getBoolPref("dev_mode")) {
|
||||
// Run right now in dev mode
|
||||
this.run();
|
||||
// Run immediately on first run, or if dev mode is enabled.
|
||||
if (prefs.getBoolPref(FIRST_RUN_PREF) || prefs.getBoolPref("dev_mode")) {
|
||||
await this.run();
|
||||
prefs.setBoolPref(FIRST_RUN_PREF, false);
|
||||
}
|
||||
|
||||
if (prefs.getBoolPref(FIRST_RUN_PREF)) {
|
||||
// Run once immediately after the UI is available. Do this before adding the
|
||||
// timer so we can't end up racing it.
|
||||
Services.obs.addObserver(this, UI_AVAILABLE_TOPIC);
|
||||
CleanupManager.addCleanupHandler(() => Services.obs.removeObserver(this, UI_AVAILABLE_TOPIC));
|
||||
} else {
|
||||
this.registerTimer();
|
||||
}
|
||||
this.registerTimer();
|
||||
},
|
||||
|
||||
registerTimer() {
|
||||
|
@ -103,9 +95,6 @@ this.RecipeRunner = {
|
|||
case PREF_CHANGED_TOPIC:
|
||||
this.observePrefChange(data);
|
||||
break;
|
||||
case UI_AVAILABLE_TOPIC:
|
||||
this.observeUIAvailable().catch(err => Cu.reportError(err));
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -120,16 +109,6 @@ this.RecipeRunner = {
|
|||
}
|
||||
},
|
||||
|
||||
async observeUIAvailable() {
|
||||
Services.obs.removeObserver(this, UI_AVAILABLE_TOPIC);
|
||||
|
||||
await this.run();
|
||||
this.registerTimer();
|
||||
prefs.setBoolPref(FIRST_RUN_PREF, false);
|
||||
|
||||
Services.obs.notifyObservers(null, SHIELD_INIT_TOPIC);
|
||||
},
|
||||
|
||||
updateRunInterval() {
|
||||
// Run once every `runInterval` wall-clock seconds. This is managed by setting a "last ran"
|
||||
// timestamp, and running if it is more than `runInterval` seconds ago. Even with very short
|
||||
|
|
|
@ -39,6 +39,7 @@ const REASONS = {
|
|||
};
|
||||
const PREF_DEV_MODE = "extensions.shield-recipe-client.dev_mode";
|
||||
const PREF_LOGGING_LEVEL = "extensions.shield-recipe-client.logging.level";
|
||||
const SHIELD_INIT_NOTIFICATION = "shield-init-complete";
|
||||
|
||||
let log = null;
|
||||
|
||||
|
@ -84,6 +85,7 @@ this.ShieldRecipeClient = {
|
|||
}
|
||||
|
||||
await RecipeRunner.init();
|
||||
Services.obs.notifyObservers(null, SHIELD_INIT_NOTIFICATION);
|
||||
},
|
||||
|
||||
shutdown(reason) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче