summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/DownloadNotifications.jsm
blob: 39b5209791672b6a27ecff953fa9f6d8a7394ef8 (plain)
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
/* 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";

this.EXPORTED_SYMBOLS = ["DownloadNotifications"];

const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "Downloads", "resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", "resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Notifications", "resource://gre/modules/Notifications.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Snackbars", "resource://gre/modules/Snackbars.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry", "resource://gre/modules/UITelemetry.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "ParentalControls",
  "@mozilla.org/parental-controls-service;1", "nsIParentalControlsService");

var Log = Cu.import("resource://gre/modules/AndroidLog.jsm", {}).AndroidLog.i.bind(null, "DownloadNotifications");

XPCOMUtils.defineLazyGetter(this, "strings",
                            () => Services.strings.createBundle("chrome://browser/locale/browser.properties"));

Object.defineProperty(this, "window",
                      { get: () => Services.wm.getMostRecentWindow("navigator:browser") });

const kButtons = {
  PAUSE: new DownloadNotificationButton("pause",
                                        "drawable://pause",
                                        "alertDownloadsPause"),
  RESUME: new DownloadNotificationButton("resume",
                                         "drawable://play",
                                         "alertDownloadsResume"),
  CANCEL: new DownloadNotificationButton("cancel",
                                         "drawable://close",
                                         "alertDownloadsCancel")
};

var notifications = new Map();

var DownloadNotifications = {
  _notificationKey: "downloads",

  init: function () {
    Downloads.getList(Downloads.ALL)
             .then(list => list.addView(this))
             .then(() => this._viewAdded = true, Cu.reportError);

    // All click, cancel, and button presses will be handled by this handler as part of the Notifications callback API.
    Notifications.registerHandler(this._notificationKey, this);
  },

  onDownloadAdded: function (download) {
    // Don't create notifications for pre-existing succeeded downloads.
    // We still add notifications for canceled downloads in case the
    // user decides to retry the download.
    if (download.succeeded && !this._viewAdded) {
      return;
    }

    if (!ParentalControls.isAllowed(ParentalControls.DOWNLOAD)) {
      download.cancel().catch(Cu.reportError);
      download.removePartialData().catch(Cu.reportError);
      Snackbars.show(strings.GetStringFromName("downloads.disabledInGuest"), Snackbars.LENGTH_LONG);
      return;
    }

    let notification = new DownloadNotification(download);
    notifications.set(download, notification);
    notification.showOrUpdate();

    // If this is a new download, show a snackbar as well.
    if (this._viewAdded) {
      Snackbars.show(strings.GetStringFromName("alertDownloadsToast"), Snackbars.LENGTH_LONG);
    }
  },

  onDownloadChanged: function (download) {
    let notification = notifications.get(download);

    if (download.succeeded) {
      let file = new FileUtils.File(download.target.path);

      Snackbars.show(strings.formatStringFromName("alertDownloadSucceeded", [file.leafName], 1), Snackbars.LENGTH_LONG, {
        action: {
          label: strings.GetStringFromName("helperapps.open"),
          callback: () => {
            UITelemetry.addEvent("launch.1", "toast", null, "downloads");
            try {
              file.launch();
            } catch (ex) {
              this.showInAboutDownloads(download);
            }
            if (notification) {
              notification.hide();
            }
          }
        }});
    }

    if (notification) {
      notification.showOrUpdate();
    }
  },

  onDownloadRemoved: function (download) {
    let notification = notifications.get(download);
    if (!notification) {
      Cu.reportError("Download doesn't have a notification.");
      return;
    }

    notification.hide();
    notifications.delete(download);
  },

  _findDownloadForCookie: function(cookie) {
    return Downloads.getList(Downloads.ALL)
      .then(list => list.getAll())
      .then((downloads) => {
        for (let download of downloads) {
          let cookie2 = getCookieFromDownload(download);
          if (cookie2 === cookie) {
            return download;
          }
        }

        throw "Couldn't find download for " + cookie;
      });
  },

  onCancel: function(cookie) {
    // TODO: I'm not sure what we do here...
  },

  showInAboutDownloads: function (download) {
    let hash = "#" + window.encodeURIComponent(download.target.path);

    // Force using string equality to find a tab
    window.BrowserApp.selectOrAddTab("about:downloads" + hash, null, { startsWith: true });
  },

  onClick: function(cookie) {
    this._findDownloadForCookie(cookie).then((download) => {
      if (download.succeeded) {
        // We don't call Download.launch(), because there's (currently) no way to
        // tell if the file was actually launched or not, and we want to show
        // about:downloads if the launch failed.
        let file = new FileUtils.File(download.target.path);
        try {
          file.launch();
        } catch (ex) {
          this.showInAboutDownloads(download);
        }
      } else {
        ConfirmCancelPrompt.show(download);
      }
    }).catch(Cu.reportError);
  },

  onButtonClick: function(button, cookie) {
    this._findDownloadForCookie(cookie).then((download) => {
      if (button === kButtons.PAUSE.buttonId) {
        download.cancel().catch(Cu.reportError);
      } else if (button === kButtons.RESUME.buttonId) {
        download.start().catch(Cu.reportError);
      } else if (button === kButtons.CANCEL.buttonId) {
        download.cancel().catch(Cu.reportError);
        download.removePartialData().catch(Cu.reportError);
      }
    }).catch(Cu.reportError);
  },
};

function getCookieFromDownload(download) {
  return download.target.path +
         download.source.url +
         download.startTime;
}

function DownloadNotification(download) {
  this.download = download;
  this._fileName = OS.Path.basename(download.target.path);

  this.id = null;
}

DownloadNotification.prototype = {
  _updateFromDownload: function () {
    this._downloading = !this.download.stopped;
    this._paused = this.download.canceled && this.download.hasPartialData;
    this._succeeded = this.download.succeeded;

    this._show = this._downloading || this._paused || this._succeeded;
  },

  get options() {
    if (!this._show) {
      return null;
    }

    let options = {
      icon: "drawable://alert_download",
      cookie: getCookieFromDownload(this.download),
      handlerKey: DownloadNotifications._notificationKey
    };

    if (this._downloading) {
      options.icon = "drawable://alert_download_animation";
      if (this.download.currentBytes == 0) {
        this._updateOptionsForStatic(options, "alertDownloadsStart2");
      } else {
        let buttons = this.download.hasPartialData ? [kButtons.PAUSE, kButtons.CANCEL] :
                                                     [kButtons.CANCEL]
        this._updateOptionsForOngoing(options, buttons);
      }
    } else if (this._paused) {
      this._updateOptionsForOngoing(options, [kButtons.RESUME, kButtons.CANCEL]);
    } else if (this._succeeded) {
      options.persistent = false;
      this._updateOptionsForStatic(options, "alertDownloadsDone2");
    }

    return options;
  },

  _updateOptionsForStatic : function (options, titleName) {
    options.title = strings.GetStringFromName(titleName);
    options.message = this._fileName;
  },

  _updateOptionsForOngoing: function (options, buttons) {
    options.title = this._fileName;
    options.message = this.download.progress + "%";
    options.buttons = buttons;
    options.ongoing = true;
    options.progress = this.download.progress;
    options.persistent = true;
  },

  showOrUpdate: function () {
    this._updateFromDownload();

    if (this._show) {
      if (!this.id) {
        this.id = Notifications.create(this.options);
      } else if (!this.options.ongoing) {
        // We need to explictly cancel ongoing notifications,
        // since updating them to be non-ongoing doesn't seem
        // to work. See bug 1130834.
        Notifications.cancel(this.id);
        this.id = Notifications.create(this.options);
      } else {
        Notifications.update(this.id, this.options);
      }
    } else {
      this.hide();
    }
  },

  hide: function () {
    if (this.id) {
      Notifications.cancel(this.id);
      this.id = null;
    }
  },
};

var ConfirmCancelPrompt = {
  show: function (download) {
    // Open a prompt that offers a choice to cancel the download
    let title = strings.GetStringFromName("downloadCancelPromptTitle1");
    let message = strings.GetStringFromName("downloadCancelPromptMessage1");

    if (Services.prompt.confirm(null, title, message)) {
      download.cancel().catch(Cu.reportError);
      download.removePartialData().catch(Cu.reportError);
    }
  }
};

function DownloadNotificationButton(buttonId, iconUrl, titleStringName, onClicked) {
  this.buttonId = buttonId;
  this.title = strings.GetStringFromName(titleStringName);
  this.icon = iconUrl;
}