Verified Commit 835a208b authored by ma1's avatar ma1
Browse files

Bug 41854: Allow overriding download spam protection.

parent 698ee78e
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ var { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);

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

XPCOMUtils.defineLazyModuleGetters(this, {
  BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
  Downloads: "resource://gre/modules/Downloads.jsm",
@@ -45,17 +47,18 @@ class DownloadSpamProtection {
    return this.list;
  }

  update(url) {
  update(url, principal) {
    if (this._blockedURLToDownloadSpam.has(url)) {
      let downloadSpam = this._blockedURLToDownloadSpam.get(url);
      this.spamList.remove(downloadSpam);
      downloadSpam.principal = principal;
      downloadSpam.blockedDownloadsCount += 1;
      this.spamList.add(downloadSpam);
      this._indicator.onDownloadStateChanged(downloadSpam);
      return;
    }

    let downloadSpam = new DownloadSpam(url);
    let downloadSpam = new DownloadSpam(url, principal, this);
    this.spamList.add(downloadSpam);
    this._blockedURLToDownloadSpam.set(url, downloadSpam);
    let hasActiveDownloads = DownloadsCommon.summarizeDownloads(
@@ -85,8 +88,10 @@ class DownloadSpamProtection {
 * @extends Download
 */
class DownloadSpam extends Download {
  constructor(url) {
  constructor(url, principal, protectionController) {
    super();
    this.protectionController = protectionController;
    this.principal = principal.QueryInterface(Ci.nsIPrincipal);
    this.hasBlockedData = true;
    this.stopped = true;
    this.error = new DownloadError({
@@ -97,4 +102,16 @@ class DownloadSpam extends Download {
    this.source = { url };
    this.blockedDownloadsCount = 1;
  }
  allow() {
    const pm = Services.perms;
    pm.addFromPrincipal(
      this.principal,
      "automatic-download",
      pm.ALLOW_ACTION,
      pm.EXPIRE_SESSION
    );
    this.hasBlockedData = this.hasPartialData = false;
    this.protectionController.clearDownloadSpam(this.source.url);
    this._notifyChange();
  }
}
+7 −2
Original line number Diff line number Diff line
@@ -717,6 +717,10 @@ Download.prototype = {
    }

    this._promiseUnblock = (async () => {
      if (this.allow) {
        this.allow();
        return;
      }
      try {
        await IOUtils.move(this.target.partFilePath, this.target.path);
        await this.target.refresh();
@@ -725,7 +729,6 @@ Download.prototype = {
        this._promiseUnblock = null;
        throw ex;
      }

      this.succeeded = true;
      this.hasBlockedData = false;
      this._notifyChange();
@@ -955,7 +958,9 @@ Download.prototype = {
            await this._promiseCanceled;
          }
          // Ask the saver object to remove any partial data.
          if (this.saver) {
            await this.saver.removeData();
          }
          // For completeness, clear the number of bytes transferred.
          if (this.currentBytes != 0 || this.hasPartialData) {
            this.currentBytes = 0;
+1 −1
Original line number Diff line number Diff line
@@ -1234,7 +1234,7 @@ var DownloadObserver = {
        ) {
          DownloadIntegration._initializeDownloadSpamProtection();
        }
        DownloadIntegration.downloadSpamProtection.update(aData);
        DownloadIntegration.downloadSpamProtection.update(aData, aSubject);
        break;
    }
  },
+2 −2
Original line number Diff line number Diff line
@@ -1975,7 +1975,7 @@ bool nsExternalAppHandler::IsDownloadSpam(nsIChannel* aChannel) {
      nsAutoCString cStringURI;
      loadInfo->TriggeringPrincipal()->GetPrePath(cStringURI);
      observerService->NotifyObservers(
          nullptr, "blocked-automatic-download",
          principal, "blocked-automatic-download",
          NS_ConvertASCIItoUTF16(cStringURI.get()).get());
      // FIXME: In order to escape memory leaks, currently we cancel blocked
      // downloads. This is temporary solution, because download data should be
@@ -1989,7 +1989,7 @@ bool nsExternalAppHandler::IsDownloadSpam(nsIChannel* aChannel) {
  if (!loadInfo->GetHasValidUserGestureActivation()) {
    permissionManager->AddFromPrincipal(
        principal, type, nsIPermissionManager::PROMPT_ACTION,
        nsIPermissionManager::EXPIRE_NEVER, 0 /* expire time */);
        nsIPermissionManager::EXPIRE_SESSION, 0 /* expire time */);
  }

  return false;