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 (0ec836eceb96)

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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/* 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";

// Based on:
// https://bugzilla.mozilla.org/show_bug.cgi?id=549539
// https://bug549539.bugzilla.mozilla.org/attachment.cgi?id=429661
// https://developer.mozilla.org/en/XPCOM/XPCOM_changes_in_Gecko_1.9.3
// https://developer.mozilla.org/en/how_to_build_an_xpcom_component_in_javascript

/* import-globals-from SpecialPowersObserverAPI.js */

var EXPORTED_SYMBOLS = ["SpecialPowersObserver"];

var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

const CHILD_SCRIPT_API = "resource://specialpowers/specialpowersFrameScript.js";
const CHILD_LOGGER_SCRIPT = "resource://specialpowers/MozillaLogger.js";


// Glue to add in the observer API to this object.  This allows us to share code with chrome tests
Services.scriptloader.loadSubScript("resource://specialpowers/SpecialPowersObserverAPI.js");

/* XPCOM gunk */
function SpecialPowersObserver() {
  this._isFrameScriptLoaded = false;
  this._messageManager = Services.mm;
  this._serviceWorkerListener = null;
}


SpecialPowersObserver.prototype = new SpecialPowersObserverAPI();

SpecialPowersObserver.prototype.QueryInterface = ChromeUtils.generateQI([Ci.nsIObserver]);

SpecialPowersObserver.prototype.observe = function(aSubject, aTopic, aData) {
  switch (aTopic) {
    case "chrome-document-global-created":
      this._loadFrameScript();
      break;

    case "http-on-modify-request":
      if (aSubject instanceof Ci.nsIChannel) {
        let uri = aSubject.URI.spec;
        this._sendAsyncMessage("specialpowers-http-notify-request", { uri });
      }
      break;

    default:
      this._observe(aSubject, aTopic, aData);
      break;
  }
};

SpecialPowersObserver.prototype._loadFrameScript = function() {
  if (!this._isFrameScriptLoaded) {
    // Register for any messages our API needs us to handle
    this._messageManager.addMessageListener("SPPrefService", this);
    this._messageManager.addMessageListener("SPProcessCrashManagerWait", this);
    this._messageManager.addMessageListener("SPProcessCrashService", this);
    this._messageManager.addMessageListener("SPPingService", this);
    this._messageManager.addMessageListener("SpecialPowers.Quit", this);
    this._messageManager.addMessageListener("SpecialPowers.Focus", this);
    this._messageManager.addMessageListener("SpecialPowers.CreateFiles", this);
    this._messageManager.addMessageListener("SpecialPowers.RemoveFiles", this);
    this._messageManager.addMessageListener("SPPermissionManager", this);
    this._messageManager.addMessageListener("SPObserverService", this);
    this._messageManager.addMessageListener("SPLoadChromeScript", this);
    this._messageManager.addMessageListener("SPImportInMainProcess", this);
    this._messageManager.addMessageListener("SPChromeScriptMessage", this);
    this._messageManager.addMessageListener("SPQuotaManager", this);
    this._messageManager.addMessageListener("SPSetTestPluginEnabledState", this);
    this._messageManager.addMessageListener("SPLoadExtension", this);
    this._messageManager.addMessageListener("SPStartupExtension", this);
    this._messageManager.addMessageListener("SPUnloadExtension", this);
    this._messageManager.addMessageListener("SPExtensionMessage", this);
    this._messageManager.addMessageListener("SPCleanUpSTSData", this);
    this._messageManager.addMessageListener("SPRequestDumpCoverageCounters", this);
    this._messageManager.addMessageListener("SPRequestResetCoverageCounters", this);
    this._messageManager.addMessageListener("SPCheckServiceWorkers", this);
    this._messageManager.addMessageListener("SPRemoveAllServiceWorkers", this);
    this._messageManager.addMessageListener("SPRemoveServiceWorkerDataForExampleDomain", this);

    this._messageManager.loadFrameScript(CHILD_LOGGER_SCRIPT, true);
    this._messageManager.loadFrameScript(CHILD_SCRIPT_API, true);
    this._isFrameScriptLoaded = true;
    this._createdFiles = null;
  }
};

SpecialPowersObserver.prototype._sendAsyncMessage = function(msgname, msg) {
  this._messageManager.broadcastAsyncMessage(msgname, msg);
};

SpecialPowersObserver.prototype._receiveMessage = function(aMessage) {
  return this._receiveMessageAPI(aMessage);
};

SpecialPowersObserver.prototype.init = function() {
  var obs = Services.obs;
  obs.addObserver(this, "chrome-document-global-created");

  // Register special testing modules.
  var testsURI = Services.dirsvc.get("ProfD", Ci.nsIFile);
  testsURI.append("tests.manifest");
  var manifestFile = Services.io.newFileURI(testsURI).
                       QueryInterface(Ci.nsIFileURL).file;

  Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
                 autoRegister(manifestFile);

  obs.addObserver(this, "http-on-modify-request");

  // We would like to check that tests don't leave service workers around
  // after they finish, but that information lives in the parent process.
  // Ideally, we'd be able to tell the child processes whenever service
  // workers are registered or unregistered so they would know at all times,
  // but service worker lifetimes are complicated enough to make that
  // difficult. For the time being, let the child process know when a test
  // registers a service worker so it can ask, synchronously, at the end if
  // the service worker had unregister called on it.
  let swm = Cc["@mozilla.org/serviceworkers/manager;1"]
              .getService(Ci.nsIServiceWorkerManager);
  let self = this;
  this._serviceWorkerListener = {
    onRegister() {
      self.onRegister();
    },

    onUnregister() {
      // no-op
    },
  };
  swm.addListener(this._serviceWorkerListener);

  this._loadFrameScript();
};

SpecialPowersObserver.prototype.uninit = function() {
  var obs = Services.obs;
  obs.removeObserver(this, "chrome-document-global-created");
  obs.removeObserver(this, "http-on-modify-request");
  this._registerObservers._topics.forEach((element) => {
    obs.removeObserver(this._registerObservers, element);
  });
  this._removeProcessCrashObservers();

  let swm = Cc["@mozilla.org/serviceworkers/manager;1"]
              .getService(Ci.nsIServiceWorkerManager);
  swm.removeListener(this._serviceWorkerListener);

  if (this._isFrameScriptLoaded) {
    this._messageManager.removeMessageListener("SPPrefService", this);
    this._messageManager.removeMessageListener("SPProcessCrashManagerWait", this);
    this._messageManager.removeMessageListener("SPProcessCrashService", this);
    this._messageManager.removeMessageListener("SPPingService", this);
    this._messageManager.removeMessageListener("SpecialPowers.Quit", this);
    this._messageManager.removeMessageListener("SpecialPowers.Focus", this);
    this._messageManager.removeMessageListener("SpecialPowers.CreateFiles", this);
    this._messageManager.removeMessageListener("SpecialPowers.RemoveFiles", this);
    this._messageManager.removeMessageListener("SPPermissionManager", this);
    this._messageManager.removeMessageListener("SPObserverService", this);
    this._messageManager.removeMessageListener("SPLoadChromeScript", this);
    this._messageManager.removeMessageListener("SPImportInMainProcess", this);
    this._messageManager.removeMessageListener("SPChromeScriptMessage", this);
    this._messageManager.removeMessageListener("SPQuotaManager", this);
    this._messageManager.removeMessageListener("SPSetTestPluginEnabledState", this);
    this._messageManager.removeMessageListener("SPLoadExtension", this);
    this._messageManager.removeMessageListener("SPStartupExtension", this);
    this._messageManager.removeMessageListener("SPUnloadExtension", this);
    this._messageManager.removeMessageListener("SPExtensionMessage", this);
    this._messageManager.removeMessageListener("SPCleanUpSTSData", this);
    this._messageManager.removeMessageListener("SPRequestDumpCoverageCounters", this);
    this._messageManager.removeMessageListener("SPRequestResetCoverageCounters", this);
    this._messageManager.removeMessageListener("SPCheckServiceWorkers", this);
    this._messageManager.removeMessageListener("SPRemoveAllServiceWorkers", this);
    this._messageManager.removeMessageListener("SPRemoveServiceWorkerDataForExampleDomain", this);

    this._messageManager.removeDelayedFrameScript(CHILD_LOGGER_SCRIPT);
    this._messageManager.removeDelayedFrameScript(CHILD_SCRIPT_API);
    this._isFrameScriptLoaded = false;
  }
};

SpecialPowersObserver.prototype._addProcessCrashObservers = function() {
  if (this._processCrashObserversRegistered) {
    return;
  }

  Services.obs.addObserver(this, "plugin-crashed");
  Services.obs.addObserver(this, "ipc:content-shutdown");
  this._processCrashObserversRegistered = true;
};

SpecialPowersObserver.prototype._removeProcessCrashObservers = function() {
  if (!this._processCrashObserversRegistered) {
    return;
  }

  Services.obs.removeObserver(this, "plugin-crashed");
  Services.obs.removeObserver(this, "ipc:content-shutdown");
  this._processCrashObserversRegistered = false;
};

SpecialPowersObserver.prototype._registerObservers = {
  _self: null,
  _topics: [],
  _add(topic) {
    if (!this._topics.includes(topic)) {
      this._topics.push(topic);
      Services.obs.addObserver(this, topic);
    }
  },
  observe(aSubject, aTopic, aData) {
    var msg = { aData };
    switch (aTopic) {
      case "perm-changed":
        var permission = aSubject.QueryInterface(Ci.nsIPermission);

        // specialPowersAPI will consume this value, and it is used as a
        // fake permission, but only type and principal.appId will be used.
        //
        // We need to ensure that it looks the same as a real permission,
        // so we fake these properties.
        msg.permission = {
          principal: {
            originAttributes: {appId: permission.principal.appId},
          },
          type: permission.type,
        };
      default:
        this._self._sendAsyncMessage("specialpowers-" + aTopic, msg);
    }
  },
};

/**
 * messageManager callback function
 * This will get requests from our API in the window and process them in chrome for it
 **/
SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
  switch (aMessage.name) {
    case "SPPingService":
      if (aMessage.json.op == "ping") {
        aMessage.target.frameLoader
                .messageManager
                .sendAsyncMessage("SPPingService", { op: "pong" });
      }
      break;
    case "SpecialPowers.Quit":
      Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
      break;
    case "SpecialPowers.Focus":
      aMessage.target.focus();
      break;
    case "SpecialPowers.CreateFiles":
      let filePaths = [];
      if (!this._createdFiles) {
        this._createdFiles = [];
      }
      let createdFiles = this._createdFiles;
      try {
        let promises = [];
        aMessage.data.forEach(function(request) {
          const filePerms = 0o666;
          let testFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
          if (request.name) {
            testFile.appendRelativePath(request.name);
          } else {
            testFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, filePerms);
          }
          let outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
          outStream.init(testFile, 0x02 | 0x08 | 0x20, // PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
                         filePerms, 0);
          if (request.data) {
            outStream.write(request.data, request.data.length);
          }
          outStream.close();
          promises.push(File.createFromFileName(testFile.path, request.options).then(function(file) {
            filePaths.push(file);
          }));
          createdFiles.push(testFile);
        });

        Promise.all(promises).then(function() {
          aMessage.target.frameLoader
                  .messageManager
                  .sendAsyncMessage("SpecialPowers.FilesCreated", filePaths);
        }, function(e) {
          aMessage.target.frameLoader
                  .messageManager
                  .sendAsyncMessage("SpecialPowers.FilesError", e.toString());
        });
      } catch (e) {
          aMessage.target.frameLoader
                  .messageManager
                  .sendAsyncMessage("SpecialPowers.FilesError", e.toString());
      }

      break;
    case "SpecialPowers.RemoveFiles":
      if (this._createdFiles) {
        this._createdFiles.forEach(function(testFile) {
          try {
            testFile.remove(false);
          } catch (e) {}
        });
        this._createdFiles = null;
      }
      break;
    default:
      return this._receiveMessage(aMessage);
  }
  return undefined;
};

SpecialPowersObserver.prototype.onRegister = function() {
  this._sendAsyncMessage("SPServiceWorkerRegistered",
    { registered: true });
};