Bug 899125 - Part 2 of 2 - Allow using the JavaScript API instead of nsIDownloadManager when forgetting about sites. r=paolo
authorNeil Deakin <neil@mozilla.com>
Sat, 17 Aug 2013 15:20:01 +0200
changeset 143036 47ce79f945643f161c9055255413de0f1c909c11
parent 143035 16038b9480774c991f1efbf8954fbd08a0c84482
child 143037 df040913f3c44d910ae13c909767643e6796a5ea
push id32605
push userphilringnalda@gmail.com
push dateMon, 19 Aug 2013 00:51:46 +0000
treeherdermozilla-inbound@7f882e063eaf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs899125
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 899125 - Part 2 of 2 - Allow using the JavaScript API instead of nsIDownloadManager when forgetting about sites. r=paolo
toolkit/forgetaboutsite/ForgetAboutSite.jsm
--- a/toolkit/forgetaboutsite/ForgetAboutSite.jsm
+++ b/toolkit/forgetaboutsite/ForgetAboutSite.jsm
@@ -1,16 +1,20 @@
 /* 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/. */
 
 Components.utils.import("resource://gre/modules/Services.jsm");
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+Components.utils.import("resource://gre/modules/NetUtil.jsm");
+Components.utils.import("resource://gre/modules/Task.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
                                   "resource://gre/modules/PlacesUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
+                                  "resource://gre/modules/Downloads.jsm");
 
 this.EXPORTED_SYMBOLS = ["ForgetAboutSite"];
 
 /**
  * Returns true if the string passed in is part of the root domain of the
  * current string.  For example, if this is "www.mozilla.org", and we pass in
  * "mozilla.org", this will return true.  It would return false the other way
  * around.
@@ -87,60 +91,77 @@ this.ForgetAboutSite = {
           ph.clearSiteData(tags[i], aDomain, FLAG_CLEAR_ALL, -1);
         } catch (e) {
           // Ignore errors from the plugin
         }
       }
     }
 
     // Downloads
-    let (dm = Cc["@mozilla.org/download-manager;1"].
-              getService(Ci.nsIDownloadManager)) {
-      // Active downloads
-      for (let enumerator of [dm.activeDownloads, dm.activePrivateDownloads]) {
-        while (enumerator.hasMoreElements()) {
-          let dl = enumerator.getNext().QueryInterface(Ci.nsIDownload);
-          if (hasRootDomain(dl.source.host, aDomain)) {
-            dl.cancel();
-            dl.remove();
+    let useJSTransfer = false;
+    try {
+      useJSTransfer = Services.prefs.getBoolPref("browser.download.useJSTransfer");
+    } catch(ex) { }
+
+    if (useJSTransfer) {
+      Task.spawn(function() {
+        for (let promiseList of [Downloads.getPublicDownloadList(),
+                                 Downloads.getPrivateDownloadList()]) {
+          let list = yield promiseList;
+          list.removeFinished(download => hasRootDomain(
+               NetUtil.newURI(download.source.url).host, aDomain));
+        }
+      }).then(null, Cu.reportError);
+    }
+    else {
+      let (dm = Cc["@mozilla.org/download-manager;1"].
+                getService(Ci.nsIDownloadManager)) {
+        // Active downloads
+        for (let enumerator of [dm.activeDownloads, dm.activePrivateDownloads]) {
+          while (enumerator.hasMoreElements()) {
+            let dl = enumerator.getNext().QueryInterface(Ci.nsIDownload);
+            if (hasRootDomain(dl.source.host, aDomain)) {
+              dl.cancel();
+              dl.remove();
+            }
           }
         }
-      }
 
-      function deleteAllLike(db) {
-        // NOTE: This is lossy, but we feel that it is OK to be lossy here and not
-        //       invoke the cost of creating a URI for each download entry and
-        //       ensure that the hostname matches.
-        let stmt = db.createStatement(
-          "DELETE FROM moz_downloads " +
-          "WHERE source LIKE ?1 ESCAPE '/' " +
-          "AND state NOT IN (?2, ?3, ?4)"
-        );
-        let pattern = stmt.escapeStringForLIKE(aDomain, "/");
-        stmt.bindByIndex(0, "%" + pattern + "%");
-        stmt.bindByIndex(1, Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING);
-        stmt.bindByIndex(2, Ci.nsIDownloadManager.DOWNLOAD_PAUSED);
-        stmt.bindByIndex(3, Ci.nsIDownloadManager.DOWNLOAD_QUEUED);
-        try {
-          stmt.execute();
+        function deleteAllLike(db) {
+          // NOTE: This is lossy, but we feel that it is OK to be lossy here and not
+          //       invoke the cost of creating a URI for each download entry and
+          //       ensure that the hostname matches.
+          let stmt = db.createStatement(
+            "DELETE FROM moz_downloads " +
+            "WHERE source LIKE ?1 ESCAPE '/' " +
+            "AND state NOT IN (?2, ?3, ?4)"
+          );
+          let pattern = stmt.escapeStringForLIKE(aDomain, "/");
+          stmt.bindByIndex(0, "%" + pattern + "%");
+          stmt.bindByIndex(1, Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING);
+          stmt.bindByIndex(2, Ci.nsIDownloadManager.DOWNLOAD_PAUSED);
+          stmt.bindByIndex(3, Ci.nsIDownloadManager.DOWNLOAD_QUEUED);
+          try {
+            stmt.execute();
+          }
+          finally {
+            stmt.finalize();
+          }
         }
-        finally {
-          stmt.finalize();
-        }
-      }
+
+        // Completed downloads
+        deleteAllLike(dm.DBConnection);
+        deleteAllLike(dm.privateDBConnection);
 
-      // Completed downloads
-      deleteAllLike(dm.DBConnection);
-      deleteAllLike(dm.privateDBConnection);
-
-      // We want to rebuild the list if the UI is showing, so dispatch the
-      // observer topic
-      let os = Cc["@mozilla.org/observer-service;1"].
-               getService(Ci.nsIObserverService);
-      os.notifyObservers(null, "download-manager-remove-download", null);
+        // We want to rebuild the list if the UI is showing, so dispatch the
+        // observer topic
+        let os = Cc["@mozilla.org/observer-service;1"].
+                 getService(Ci.nsIObserverService);
+        os.notifyObservers(null, "download-manager-remove-download", null);
+      }
     }
 
     // Passwords
     let (lm = Cc["@mozilla.org/login-manager;1"].
               getService(Ci.nsILoginManager)) {
       // Clear all passwords for domain
       try {
         let logins = lm.getAllLogins();