Bug 1141550 - Register an AsyncShutdown blocker to persist download changes. r=paolo, a=lsblakk
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 16 Mar 2015 14:40:00 -0700
changeset 250420 3eeb35bbafd2
parent 250419 199e30cb18f3
child 250421 202177831c59
push id4583
push userryanvm@gmail.com
push date2015-03-18 16:53 +0000
treeherdermozilla-beta@067f83e99f66 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo, lsblakk
bugs1141550
milestone37.0
Bug 1141550 - Register an AsyncShutdown blocker to persist download changes. r=paolo, a=lsblakk
toolkit/components/jsdownloads/src/DownloadIntegration.jsm
--- a/toolkit/components/jsdownloads/src/DownloadIntegration.jsm
+++ b/toolkit/components/jsdownloads/src/DownloadIntegration.jsm
@@ -20,16 +20,18 @@ this.EXPORTED_SYMBOLS = [
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 const Cr = Components.results;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
+                                  "resource://gre/modules/AsyncShutdown.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask",
                                   "resource://gre/modules/DeferredTask.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
                                   "resource://gre/modules/Downloads.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "DownloadStore",
                                   "resource://gre/modules/DownloadStore.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "DownloadImport",
                                   "resource://gre/modules/DownloadImport.jsm");
@@ -83,33 +85,21 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "@mozilla.org/telephony/volume-service;1",
                                    "nsIVolumeService");
 
 const Timer = Components.Constructor("@mozilla.org/timer;1", "nsITimer",
                                      "initWithCallback");
 
 /**
  * Indicates the delay between a change to the downloads data and the related
- * save operation.  This value is the result of a delicate trade-off, assuming
- * the host application uses the browser history instead of the download store
- * to save completed downloads.
- *
- * If a download takes less than this interval to complete (for example, saving
- * a page that is already displayed), then no input/output is triggered by the
- * download store except for an existence check, resulting in the best possible
- * efficiency.
+ * save operation.
  *
- * Conversely, if the browser is closed before this interval has passed, the
- * download will not be saved.  This prevents it from being restored in the next
- * session, and if there is partial data associated with it, then the ".part"
- * file will not be deleted when the browser starts again.
- *
- * In all cases, for best efficiency, this value should be high enough that the
- * input/output for opening or closing the target file does not overlap with the
- * one for saving the list of downloads.
+ * For best efficiency, this value should be high enough that the input/output
+ * for opening or closing the target file does not overlap with the one for
+ * saving the list of downloads.
  */
 const kSaveDelayMs = 1500;
 
 /**
  * This pref indicates if we have already imported (or attempted to import)
  * the downloads database from the previous SQLite storage.
  */
 const kPrefImportedFromSqlite = "browser.download.importedFromSqlite";
@@ -1211,16 +1201,18 @@ this.DownloadHistoryObserver = function 
  *        The DownloadStore object used for saving.
  */
 this.DownloadAutoSaveView = function (aList, aStore)
 {
   this._list = aList;
   this._store = aStore;
   this._downloadsMap = new Map();
   this._writer = new DeferredTask(() => this._store.save(), kSaveDelayMs);
+  AsyncShutdown.profileBeforeChange.addBlocker("DownloadAutoSaveView: writing data",
+                                               () => this._writer.finalize());
 }
 
 this.DownloadAutoSaveView.prototype = {
   /**
    * DownloadList object linked to this view.
    */
   _list: null,