Bug 1448143 - Factor out a size getter from DownloadsViewUI.jsm so other consumers can access it draft
authorUrsula Sarracini <usarracini@mozilla.com>
Fri, 23 Mar 2018 14:21:48 -0400
changeset 771720 588614c750f40f78a751369a507c050a608be999
parent 771093 7771df14ea181add1dc4133f0f5559bf620bf976
push id103760
push userusarracini@mozilla.com
push dateFri, 23 Mar 2018 18:22:11 +0000
bugs1448143
milestone61.0a1
Bug 1448143 - Factor out a size getter from DownloadsViewUI.jsm so other consumers can access it MozReview-Commit-ID: EgyfXz1ntys
browser/components/downloads/DownloadsViewUI.jsm
--- a/browser/components/downloads/DownloadsViewUI.jsm
+++ b/browser/components/downloads/DownloadsViewUI.jsm
@@ -104,16 +104,38 @@ this.DownloadsViewUI.DownloadElementShel
    */
   get displayName() {
     if (!this.download.target.path) {
       return this.download.source.url;
     }
     return OS.Path.basename(this.download.target.path);
   },
 
+  /**
+   * The user-facing label for the size (if any) of the download. The return value
+   * is an object 'sizeStrings' with 2 strings:
+   *   1. stateLabel - The size with the units (e.g. "1.5 MB").
+   *   2. status - The status of the download (e.g. "Completed");
+   */
+  get sizeStrings() {
+    let s = DownloadsCommon.strings;
+    let sizeStrings = {};
+
+    if (this.download.target.size !== undefined) {
+      let [size, unit] = DownloadUtils.convertByteUnits(this.download.target.size);
+      sizeStrings.stateLabel = s.sizeWithUnits(size, unit);
+      sizeStrings.status = s.statusSeparator(s.stateCompleted, sizeStrings.stateLabel);
+    } else {
+      // History downloads may not have a size defined.
+      sizeStrings.stateLabel = s.sizeUnknown;
+      sizeStrings.status = s.stateCompleted;
+    }
+    return sizeStrings;
+  },
+
   get browserWindow() {
     return RecentWindow.getMostRecentBrowserWindow();
   },
 
   /**
    * The progress element for the download, or undefined in case the XBL binding
    * has not been applied yet.
    */
@@ -240,27 +262,20 @@ this.DownloadsViewUI.DownloadElementShel
       hoverStatus = status;
     } else {
       let stateLabel;
 
       if (this.download.succeeded && !this.download.target.exists) {
         stateLabel = s.fileMovedOrMissing;
         hoverStatus = stateLabel;
       } else if (this.download.succeeded) {
-        // For completed downloads, show the file size (e.g. "1.5 MB").
-        if (this.download.target.size !== undefined) {
-          let [size, unit] =
-            DownloadUtils.convertByteUnits(this.download.target.size);
-          stateLabel = s.sizeWithUnits(size, unit);
-          status = s.statusSeparator(s.stateCompleted, stateLabel);
-        } else {
-          // History downloads may not have a size defined.
-          stateLabel = s.sizeUnknown;
-          status = s.stateCompleted;
-        }
+        // For completed downloads, show the file size
+        let sizeStrings = this.sizeStrings;
+        stateLabel = sizeStrings.stateLabel;
+        status = sizeStrings.status;
         hoverStatus = status;
       } else if (this.download.canceled) {
         stateLabel = s.stateCanceled;
       } else if (this.download.error.becauseBlockedByParentalControls) {
         stateLabel = s.stateBlockedParentalControls;
       } else if (this.download.error.becauseBlockedByReputationCheck) {
         stateLabel = this.rawBlockedTitleAndDetails[0];
       } else {