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.

Git (975a35c0f5)

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
'use strict';

/**
 * Generate user.js
 */

var utils = require('./utils');

// Optional partner provided preference files. They will be added
// after the ones on the extenedPrefFiles and they will be read
// from the GAIA_DISTRIBUTION_DIR directory
var PARTNER_PREF_FILES = ['partner-prefs.js'];

var PreferencesBuilder = function() {
};

PreferencesBuilder.prototype.setConfig = function(config) {
  this.config = config;
  this.prefs = {};
  this.userPrefs = {};
  this.gaia = utils.gaia.getInstance(this.config);

  // Optional files that may be provided to extend the set of default
  // preferences installed for gaia.  If the preferences in these files
  // conflict, the result is undefined.
  this.extenedPrefFiles = ['custom-prefs.js', 'gps-prefs.js',
    'payment-prefs.js'];

  if (config.GAIA_APP_TARGET === 'engineering') {
    this.extenedPrefFiles.push('payment-dev-prefs.js');
  } else if (config.GAIA_APP_TARGET === 'dogfood') {
    this.extenedPrefFiles.push('dogfood-prefs.js');
  }

  if (config.DEBUG === '1') {
    this.extenedPrefFiles.push('debug-prefs.js');
  }
};

PreferencesBuilder.prototype.execute = function(config) {
  this.setConfig(config);
  this.preparePref();
  this.loadBuildPrefs();
  if (this.gaia.engine === 'xpcshell') {
    let jsPath = utils.getFile(this.config.PROFILE_DIR, 'user.js').path;
    this.writePrefs(jsPath, this.prefs, this.userPrefs);
  } else if (this.gaia.engine === 'b2g') {
    this.setPrefs();
  }
  this.writeDefaultUserJs();
};

PreferencesBuilder.prototype.writeDefaultUserJs = function() {
  if (this.config.BUILD_APP_NAME !== '*') {
    return;
  }

  // Create a folder to store data for B2G, this folder will copy to b2g output
  // folder.
  let prefDir = utils.getFile(this.config.PROFILE_DIR, 'defaults', 'pref');

  utils.ensureFolderExists(prefDir);
  let allPrefs = utils.cloneJSON(this.prefs);
  for (let pref in this.userPrefs) {
    allPrefs[pref] = this.userPrefs[pref];
  }

  this.writePrefs(utils.joinPath(prefDir.path, 'user.js'), allPrefs, {});
};

PreferencesBuilder.prototype.loadBuildPrefs = function() {
  var buildConfigDir = utils.getFile(this.config.GAIA_DIR, 'build', 'config');
  this.loadPrefFile(buildConfigDir, this.extenedPrefFiles);

  var deviceConfigDir = utils.getFile(this.config.GAIA_DIR, 'build', 'config',
    this.config.GAIA_DEVICE_TYPE);
  this.loadPrefFile(deviceConfigDir, this.extenedPrefFiles);

  try {
    var distDir = utils.getFile(this.config.GAIA_DISTRIBUTION_DIR);
    this.loadPrefFile(distDir, PARTNER_PREF_FILES);
  } catch (e) {
    // The utils.getFile will throw exception if GAIA_DISTRIBUTION_DIR does not
    // exist. In this case we just don't override preferences by
    // PARTNER_PREF_FILES.
  }
};

PreferencesBuilder.prototype.loadPrefFile = function(srcDir, srcPrefFiles) {
  // Scope object for hosting functions and variables.
  var self = this;
  var scope = {
    user_pref: function(key, value) {
      self.userPrefs[key] = self.customizePrefValue(key, value);
    },
    pref: function fillPref(key, value) {
      self.prefs[key] = self.customizePrefValue(key, value);
    }
  };

  // Read all files
  srcPrefFiles.forEach(function(filename) {
    var srcFile = utils.getFile(srcDir.path, filename);
    if (srcFile.exists()) {
      utils.scriptLoader.load(srcFile.path, scope, true);
    }
  });
};

PreferencesBuilder.prototype.customizePrefValue = function(key, value) {
  switch(key) {
    case 'devtools.responsiveUI.customWidth':
      // The responsive UI has padding and border at width.
      // width = 320 + 15(horizontal padding) * 2 + 1(vertical border) * 2
      return value + 32;
    case 'devtools.responsiveUI.customHeight':
      // The responsive UI has padding and border at width.
      // height = 480 + 60(top padding) + 1(top border)
      return value + 61;
  }
  return value;
};

PreferencesBuilder.prototype.preparePref = function() {
  this.system = this.config.SYSTEM;

  this.userPrefs['b2g.system_manifest_url'] = this.system + '/manifest.webapp';
  this.userPrefs['b2g.neterror.url'] = this.system + '/net_error.html';
  this.userPrefs['b2g.system_startup_url'] = this.system + '/index.html';
  this.userPrefs['network.http.max-connections-per-server'] = 15;
  this.userPrefs['dom.mozInputMethod.enabled'] = true;
  this.userPrefs['layout.css.scroll-behavior.enabled'] = true;
  this.userPrefs['layout.css.sticky.enabled'] = true;
  this.userPrefs['intl.uidirection.ar-x-psbidi'] = 'rtl';
  this.userPrefs['dom.webcomponents.enabled'] = true;

  // For https://bugzilla.mozilla.org/show_bug.cgi?id=811605 to let user know
  // what prefs is for ril debugging
  this.userPrefs['ril.debugging.enabled'] = false;
  // Gaia has no vCard/vCalendar for now. Override MMS version to v1.1:
  // TODO: remove this override after having vCard/vCalendar implemented in
  //       Gaia.
  // @see bug 885683 - [Messages] MMS doesn't support sending and receiving
  //                    vCard attachments.
  // @see bug 905548 - [Messages] MMS doesn't support sending and receiving
  //                    vCalendar attachments.
  this.userPrefs['dom.mms.version'] = 0x11;
  // TODO: Once platform enabled unsafe WPA-EAP, we have to remove this flag
  //       here.
  // @see Bug 790056 - Enable WPA Enterprise
  this.userPrefs['b2g.wifi.allow_unsafe_wpa_eap'] = true;

  if (this.config.DESKTOP === '1') {
    this.setDesktopPref();
  }
  if (this.config.DEBUG === '1') {
    this.setDebugPref();
  }
  if (this.config.DEVICE_DEBUG === '1') {
    this.setDeviceDebugPref();
  }
  // Turn on user timing output to logcat
  if (this.config.PERF_LOGGING === '1') {
    this.userPrefs['dom.performance.enable_user_timing_logging'] = true;
  }
  // Turn off hardware composing
  if (this.config.HARDWARE_COMPOSER === '0') {
    this.userPrefs['layers.composer2d.enabled'] = false;
  }

  // If this is already equal to the profile directory, no need to
  // write the preference
  if (this.config.COREWEBAPPS_DIR !== this.config.PROFILE_DIR) {
    this.userPrefs['b2g.coreappsdir'] =
        this.config.COREWEBAPPS_DIR;
  }
};

PreferencesBuilder.prototype.setDesktopPref = function() {
  this.userPrefs['extensions.autoDisableScopes'] = 0;
  this.userPrefs['xpinstall.signatures.required'] = false;
};

PreferencesBuilder.prototype.setDebugPref = function() {
  this.userPrefs['docshell.device_size_is_page_size'] = true;
  this.userPrefs['marionette.defaultPrefs.enabled'] = true;

  this.userPrefs['nglayout.debug.disable_xul_cache'] = true;
  this.userPrefs['nglayout.debug.disable_xul_fastload'] = true;

  this.userPrefs['javascript.options.showInConsole'] = true;
  this.userPrefs['browser.dom.window.dump.enabled'] = true;
  this.userPrefs['dom.report_all_js_exceptions'] = true;
  this.userPrefs['dom.w3c_touch_events.enabled'] = 1;
  this.userPrefs['dom.promise.enabled'] = true;
  this.userPrefs['dom.wakelock.enabled'] = true;
  this.userPrefs['image.mozsamplesize.enabled'] = true;
  this.userPrefs['webgl.verbose'] = true;

  // Turn off unresponsive script dialogs so test-agent can keep running...
  // https://bugzilla.mozilla.org/show_bug.cgi?id=872141
  this.userPrefs['dom.max_script_run_time'] = 0;

  // Identity debug messages
  this.userPrefs['toolkit.identity.debug'] = true;

  // Preferences for httpd
  // (Use JSON.stringify in order to avoid taking care of `\` escaping)
  this.userPrefs['extensions.gaia.dir'] = this.config.GAIA_DIR;
  this.userPrefs['extensions.gaia.port'] =
    parseInt(this.config.GAIA_PORT.replace(/:/g, ''), 10);
  this.userPrefs['extensions.gaia.appdirs'] = this.config.GAIA_APPDIRS;
  this.userPrefs['extensions.gaia.allappdirs'] = this.config.GAIA_ALLAPPDIRS;
  this.userPrefs['extensions.gaia.locales_debug_path'] =
    this.config.GAIA_LOCALES_PATH;
  this.userPrefs['extensions.gaia.official'] = Boolean(this.config.OFFICIAL);
  this.userPrefs['extensions.gaia.locales_file'] = this.config.LOCALES_FILE;
  // Bug 952901: remove getLocaleBasedir() if bug 952900 fixed.
  this.userPrefs['extensions.gaia.locale_basedir'] = utils.getLocaleBasedir(
    this.config.LOCALE_BASEDIR);

  var suffix = this.config.GAIA_DEV_PIXELS_PER_PX === '1' ?
    '' : '@' + this.config.GAIA_DEV_PIXELS_PER_PX + 'x';
  this.userPrefs['extensions.gaia.device_pixel_suffix'] = suffix;
  this.userPrefs['extensions.autoDisableScopes'] = 0;
  this.userPrefs['xpinstall.signatures.required'] = false;

  // electrolysis breaks the app:// protocol as registered by httpd.js
  // see Bug 1097912
  this.userPrefs['browser.tabs.remote.autostart'] = false;
  this.userPrefs['browser.tabs.remote.autostart.1'] = false;
  this.userPrefs['browser.tabs.remote.autostart.2'] = false;

  // Disable dialog asking to set firefox as default OS browser
  this.userPrefs['browser.shell.checkDefaultBrowser'] = false;
};

PreferencesBuilder.prototype.setDeviceDebugPref = function() {
  this.userPrefs['dom.apps.developer_mode'] = true;
  this.userPrefs['network.disable.ipc.security'] = true;
  this.userPrefs['devtools.debugger.prompt-connection'] = false;
  this.userPrefs['devtools.debugger.forbid-certified-apps'] = false;
  // Bug 1001348: This optimization prevents debugger to fetch script sources
  // of certified apps as well as chrome code:
  this.userPrefs['javascript.options.discardSystemSource'] = false;
  this.userPrefs['b2g.adb.timeout'] = 0;
};

PreferencesBuilder.prototype.writePrefs = function(jsPath, prefs, userPrefs) {
  var userJs = utils.getFile(jsPath);
  var content = '';
  var pref;
  // Output pref
  for (pref in prefs) {
    content += 'pref(\'' + pref + '\', ' + JSON.stringify(prefs[pref]) + ');\n';
  }
  // Output user_pref
  for (pref in userPrefs) {
    content += 'user_pref(\'' + pref + '\', ' +
      JSON.stringify(userPrefs[pref]) + ');\n';
  }
  utils.writeContent(userJs, content + '\n');
};

PreferencesBuilder.prototype.setPrefs = function() {
  [this.prefs, this.userPrefs].forEach(function(prefs) {
    for (var pref in prefs) {
      switch(typeof prefs[pref]) {
        case 'string':
          utils.Services.prefs.setCharPref(pref, prefs[pref]);
          break;
        case 'boolean':
          utils.Services.prefs.setBoolPref(pref, prefs[pref]);
          break;
        case 'number':
          utils.Services.prefs.setIntPref(pref, prefs[pref]);
          break;
        default:
          throw new Error('Unsupported pref type: ' + typeof prefs[pref]);
      }
    }
  });
};


exports.execute = function(config) {
  if (config.BUILD_APP_NAME !== '*') {
    return;
  }
  (new PreferencesBuilder()).execute(config);
};

exports.PreferencesBuilder = PreferencesBuilder;