DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetters(this, {
  AboutPages: "resource://normandy-content/AboutPages.jsm",
  AddonStudies: "resource://normandy/lib/AddonStudies.jsm",
  CleanupManager: "resource://normandy/lib/CleanupManager.jsm",
  LogManager: "resource://normandy/lib/LogManager.jsm",
  PreferenceExperiments: "resource://normandy/lib/PreferenceExperiments.jsm",
  RecipeRunner: "resource://normandy/lib/RecipeRunner.jsm",
  ShieldPreferences: "resource://normandy/lib/ShieldPreferences.jsm",
  TelemetryEvents: "resource://normandy/lib/TelemetryEvents.jsm",
});

var EXPORTED_SYMBOLS = ["Normandy"];

const UI_AVAILABLE_NOTIFICATION = "sessionstore-windows-restored";
const BOOTSTRAP_LOGGER_NAME = "app.normandy.bootstrap";
const SHIELD_INIT_NOTIFICATION = "shield-init-complete";

const PREF_PREFIX = "app.normandy";
const LEGACY_PREF_PREFIX = "extensions.shield-recipe-client";
const STARTUP_EXPERIMENT_PREFS_BRANCH = `${PREF_PREFIX}.startupExperimentPrefs.`;
const PREF_LOGGING_LEVEL = `${PREF_PREFIX}.logging.level`;

// Logging
const log = Log.repository.getLogger(BOOTSTRAP_LOGGER_NAME);
log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
log.level = Services.prefs.getIntPref(PREF_LOGGING_LEVEL, Log.Level.Warn);

let studyPrefsChanged = {};

var Normandy = {
  init() {
    // Initialization that needs to happen before the first paint on startup.
    this.migrateShieldPrefs();
    this.initExperimentPrefs();

    // Wait until the UI is available before finishing initialization.
    Services.obs.addObserver(this, UI_AVAILABLE_NOTIFICATION);
  },

  observe(subject, topic, data) {
    if (topic === UI_AVAILABLE_NOTIFICATION) {
      Services.obs.removeObserver(this, UI_AVAILABLE_NOTIFICATION);
      this.finishInit();
    }
  },

  async finishInit() {
    await PreferenceExperiments.recordOriginalValues(studyPrefsChanged);

    // Setup logging and listen for changes to logging prefs
    LogManager.configure(Services.prefs.getIntPref(PREF_LOGGING_LEVEL, Log.Level.Warn));
    Services.prefs.addObserver(PREF_LOGGING_LEVEL, LogManager.configure);
    CleanupManager.addCleanupHandler(
      () => Services.prefs.removeObserver(PREF_LOGGING_LEVEL, LogManager.configure),
    );

    try {
      TelemetryEvents.init();
    } catch (err) {
      log.error("Failed to initialize telemetry events:", err);
    }

    try {
      await AboutPages.init();
    } catch (err) {
      log.error("Failed to initialize about pages:", err);
    }

    try {
      await AddonStudies.init();
    } catch (err) {
      log.error("Failed to initialize addon studies:", err);
    }

    try {
      await PreferenceExperiments.init();
    } catch (err) {
      log.error("Failed to initialize preference experiments:", err);
    }

    try {
      ShieldPreferences.init();
    } catch (err) {
      log.error("Failed to initialize preferences UI:", err);
    }

    await RecipeRunner.init();
    Services.obs.notifyObservers(null, SHIELD_INIT_NOTIFICATION);
  },

  async uninit() {
    await CleanupManager.cleanup();
    Services.prefs.removeObserver(PREF_LOGGING_LEVEL, LogManager.configure);

    // In case the observer didn't run, clean it up.
    try {
      Services.obs.removeObserver(this, UI_AVAILABLE_NOTIFICATION);
    } catch (err) {
      // It must already be removed!
    }
  },

  migrateShieldPrefs() {
    const legacyBranch = Services.prefs.getBranch(LEGACY_PREF_PREFIX + ".");
    const newBranch = Services.prefs.getBranch(PREF_PREFIX + ".");

    for (const prefName of legacyBranch.getChildList("")) {
      const legacyPrefType = legacyBranch.getPrefType(prefName);
      const newPrefType = newBranch.getPrefType(prefName);

      // If new preference exists and is not the same as the legacy pref, skip it
      if (newPrefType !== Services.prefs.PREF_INVALID && newPrefType !== legacyPrefType) {
        log.error(`Error migrating normandy pref ${prefName}; pref type does not match.`);
        continue;
      }

      // Now move the value over. If it matches the default, this will be a no-op
      switch (legacyPrefType) {
        case Services.prefs.PREF_STRING:
          newBranch.setCharPref(prefName, legacyBranch.getCharPref(prefName));
          break;

        case Services.prefs.PREF_INT:
          newBranch.setIntPref(prefName, legacyBranch.getIntPref(prefName));
          break;

        case Services.prefs.PREF_BOOL:
          newBranch.setBoolPref(prefName, legacyBranch.getBoolPref(prefName));
          break;

        case Services.prefs.PREF_INVALID:
          // This should never happen.
          log.error(`Error migrating pref ${prefName}; pref type is invalid (${legacyPrefType}).`);
          break;

        default:
          // This should never happen either.
          log.error(`Error getting startup pref ${prefName}; unknown value type ${legacyPrefType}.`);
      }

      legacyBranch.clearUserPref(prefName);
    }
  },

  initExperimentPrefs() {
    studyPrefsChanged = {};
    const defaultBranch = Services.prefs.getDefaultBranch("");
    const experimentBranch = Services.prefs.getBranch(STARTUP_EXPERIMENT_PREFS_BRANCH);

    for (const prefName of experimentBranch.getChildList("")) {
      const experimentPrefType = experimentBranch.getPrefType(prefName);
      const realPrefType = defaultBranch.getPrefType(prefName);

      if (realPrefType !== Services.prefs.PREF_INVALID && realPrefType !== experimentPrefType) {
        log.error(`Error setting startup pref ${prefName}; pref type does not match.`);
        continue;
      }

      // record the value of the default branch before setting it
      try {
        switch (realPrefType) {
          case Services.prefs.PREF_STRING:
            studyPrefsChanged[prefName] = defaultBranch.getCharPref(prefName);
            break;

          case Services.prefs.PREF_INT:
            studyPrefsChanged[prefName] = defaultBranch.getIntPref(prefName);
            break;

          case Services.prefs.PREF_BOOL:
            studyPrefsChanged[prefName] = defaultBranch.getBoolPref(prefName);
            break;

          case Services.prefs.PREF_INVALID:
            studyPrefsChanged[prefName] = null;
            break;

          default:
            // This should never happen
            log.error(`Error getting startup pref ${prefName}; unknown value type ${experimentPrefType}.`);
        }
      } catch (e) {
        if (e.result === Cr.NS_ERROR_UNEXPECTED) {
          // There is a value for the pref on the user branch but not on the default branch. This is ok.
          studyPrefsChanged[prefName] = null;
        } else {
          // rethrow
          throw e;
        }
      }

      // now set the new default value
      switch (experimentPrefType) {
        case Services.prefs.PREF_STRING:
          defaultBranch.setCharPref(prefName, experimentBranch.getCharPref(prefName));
          break;

        case Services.prefs.PREF_INT:
          defaultBranch.setIntPref(prefName, experimentBranch.getIntPref(prefName));
          break;

        case Services.prefs.PREF_BOOL:
          defaultBranch.setBoolPref(prefName, experimentBranch.getBoolPref(prefName));
          break;

        case Services.prefs.PREF_INVALID:
          // This should never happen.
          log.error(`Error setting startup pref ${prefName}; pref type is invalid (${experimentPrefType}).`);
          break;

        default:
          // This should never happen either.
          log.error(`Error getting startup pref ${prefName}; unknown value type ${experimentPrefType}.`);
      }
    }
  },
};