summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/ext-downloads.js
blob: 132814ae4b230fa08d639e8974d623b2d28a6819 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
"use strict";

var {classes: Cc, interfaces: Ci, utils: Cu} = Components;

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

XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
                                  "resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadPaths",
                                  "resource://gre/modules/DownloadPaths.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                  "resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
                                  "resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
                                  "resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
                                  "resource://devtools/shared/event-emitter.js");

Cu.import("resource://gre/modules/ExtensionUtils.jsm");
const {
  ignoreEvent,
  normalizeTime,
  runSafeSync,
  SingletonEventManager,
  PlatformInfo,
} = ExtensionUtils;

const DOWNLOAD_ITEM_FIELDS = ["id", "url", "referrer", "filename", "incognito",
                              "danger", "mime", "startTime", "endTime",
                              "estimatedEndTime", "state",
                              "paused", "canResume", "error",
                              "bytesReceived", "totalBytes",
                              "fileSize", "exists",
                              "byExtensionId", "byExtensionName"];

// Fields that we generate onChanged events for.
const DOWNLOAD_ITEM_CHANGE_FIELDS = ["endTime", "state", "paused", "canResume",
                                     "error", "exists"];

// From https://fetch.spec.whatwg.org/#forbidden-header-name
const FORBIDDEN_HEADERS = ["ACCEPT-CHARSET", "ACCEPT-ENCODING",
  "ACCESS-CONTROL-REQUEST-HEADERS", "ACCESS-CONTROL-REQUEST-METHOD",
  "CONNECTION", "CONTENT-LENGTH", "COOKIE", "COOKIE2", "DATE", "DNT",
  "EXPECT", "HOST", "KEEP-ALIVE", "ORIGIN", "REFERER", "TE", "TRAILER",
  "TRANSFER-ENCODING", "UPGRADE", "VIA"];

const FORBIDDEN_PREFIXES = /^PROXY-|^SEC-/i;

class DownloadItem {
  constructor(id, download, extension) {
    this.id = id;
    this.download = download;
    this.extension = extension;
    this.prechange = {};
  }

  get url() { return this.download.source.url; }
  get referrer() { return this.download.source.referrer; }
  get filename() { return this.download.target.path; }
  get incognito() { return this.download.source.isPrivate; }
  get danger() { return "safe"; } // TODO
  get mime() { return this.download.contentType; }
  get startTime() { return this.download.startTime; }
  get endTime() { return null; } // TODO
  get estimatedEndTime() { return null; } // TODO
  get state() {
    if (this.download.succeeded) {
      return "complete";
    }
    if (this.download.canceled) {
      return "interrupted";
    }
    return "in_progress";
  }
  get paused() {
    return this.download.canceled && this.download.hasPartialData && !this.download.error;
  }
  get canResume() {
    return (this.download.stopped || this.download.canceled) &&
      this.download.hasPartialData && !this.download.error;
  }
  get error() {
    if (!this.download.stopped || this.download.succeeded) {
      return null;
    }
    // TODO store this instead of calculating it

    if (this.download.error) {
      if (this.download.error.becauseSourceFailed) {
        return "NETWORK_FAILED"; // TODO
      }
      if (this.download.error.becauseTargetFailed) {
        return "FILE_FAILED"; // TODO
      }
      return "CRASH";
    }
    return "USER_CANCELED";
  }
  get bytesReceived() {
    return this.download.currentBytes;
  }
  get totalBytes() {
    return this.download.hasProgress ? this.download.totalBytes : -1;
  }
  get fileSize() {
    // todo: this is supposed to be post-compression
    return this.download.succeeded ? this.download.target.size : -1;
  }
  get exists() { return this.download.target.exists; }
  get byExtensionId() { return this.extension ? this.extension.id : undefined; }
  get byExtensionName() { return this.extension ? this.extension.name : undefined; }

  /**
   * Create a cloneable version of this object by pulling all the
   * fields into simple properties (instead of getters).
   *
   * @returns {object} A DownloadItem with flat properties,
   *                   suitable for cloning.
   */
  serialize() {
    let obj = {};
    for (let field of DOWNLOAD_ITEM_FIELDS) {
      obj[field] = this[field];
    }
    if (obj.startTime) {
      obj.startTime = obj.startTime.toISOString();
    }
    return obj;
  }

  // When a change event fires, handlers can look at how an individual
  // field changed by comparing item.fieldname with item.prechange.fieldname.
  // After all handlers have been invoked, this gets called to store the
  // current values of all fields ahead of the next event.
  _change() {
    for (let field of DOWNLOAD_ITEM_CHANGE_FIELDS) {
      this.prechange[field] = this[field];
    }
  }
}


// DownloadMap maps back and forth betwen the numeric identifiers used in
// the downloads WebExtension API and a Download object from the Downloads jsm.
// todo: make id and extension info persistent (bug 1247794)
const DownloadMap = {
  currentId: 0,
  loadPromise: null,

  // Maps numeric id -> DownloadItem
  byId: new Map(),

  // Maps Download object -> DownloadItem
  byDownload: new WeakMap(),

  lazyInit() {
    if (this.loadPromise == null) {
      EventEmitter.decorate(this);
      this.loadPromise = Downloads.getList(Downloads.ALL).then(list => {
        let self = this;
        return list.addView({
          onDownloadAdded(download) {
            const item = self.newFromDownload(download, null);
            self.emit("create", item);
          },

          onDownloadRemoved(download) {
            const item = self.byDownload.get(download);
            if (item != null) {
              self.emit("erase", item);
              self.byDownload.delete(download);
              self.byId.delete(item.id);
            }
          },

          onDownloadChanged(download) {
            const item = self.byDownload.get(download);
            if (item == null) {
              Cu.reportError("Got onDownloadChanged for unknown download object");
            } else {
              // We get the first one of these when the download is started.
              // In this case, don't emit anything, just initialize prechange.
              if (Object.keys(item.prechange).length > 0) {
                self.emit("change", item);
              }
              item._change();
            }
          },
        }).then(() => list.getAll())
          .then(downloads => {
            downloads.forEach(download => {
              this.newFromDownload(download, null);
            });
          })
          .then(() => list);
      });
    }
    return this.loadPromise;
  },

  getDownloadList() {
    return this.lazyInit();
  },

  getAll() {
    return this.lazyInit().then(() => this.byId.values());
  },

  fromId(id) {
    const download = this.byId.get(id);
    if (!download) {
      throw new Error(`Invalid download id ${id}`);
    }
    return download;
  },

  newFromDownload(download, extension) {
    if (this.byDownload.has(download)) {
      return this.byDownload.get(download);
    }

    const id = ++this.currentId;
    let item = new DownloadItem(id, download, extension);
    this.byId.set(id, item);
    this.byDownload.set(download, item);
    return item;
  },

  erase(item) {
    // This will need to get more complicated for bug 1255507 but for now we
    // only work with downloads in the DownloadList from getAll()
    return this.getDownloadList().then(list => {
      list.remove(item.download);
    });
  },
};

// Create a callable function that filters a DownloadItem based on a
// query object of the type passed to search() or erase().
function downloadQuery(query) {
  let queryTerms = [];
  let queryNegativeTerms = [];
  if (query.query != null) {
    for (let term of query.query) {
      if (term[0] == "-") {
        queryNegativeTerms.push(term.slice(1).toLowerCase());
      } else {
        queryTerms.push(term.toLowerCase());
      }
    }
  }

  function normalizeDownloadTime(arg, before) {
    if (arg == null) {
      return before ? Number.MAX_VALUE : 0;
    }
    return normalizeTime(arg).getTime();
  }

  const startedBefore = normalizeDownloadTime(query.startedBefore, true);
  const startedAfter = normalizeDownloadTime(query.startedAfter, false);
  // const endedBefore = normalizeDownloadTime(query.endedBefore, true);
  // const endedAfter = normalizeDownloadTime(query.endedAfter, false);

  const totalBytesGreater = query.totalBytesGreater || 0;
  const totalBytesLess = (query.totalBytesLess != null)
        ? query.totalBytesLess : Number.MAX_VALUE;

  // Handle options for which we can have a regular expression and/or
  // an explicit value to match.
  function makeMatch(regex, value, field) {
    if (value == null && regex == null) {
      return input => true;
    }

    let re;
    try {
      re = new RegExp(regex || "", "i");
    } catch (err) {
      throw new Error(`Invalid ${field}Regex: ${err.message}`);
    }
    if (value == null) {
      return input => re.test(input);
    }

    value = value.toLowerCase();
    if (re.test(value)) {
      return input => (value == input);
    }
    return input => false;
  }

  const matchFilename = makeMatch(query.filenameRegex, query.filename, "filename");
  const matchUrl = makeMatch(query.urlRegex, query.url, "url");

  return function(item) {
    const url = item.url.toLowerCase();
    const filename = item.filename.toLowerCase();

    if (!queryTerms.every(term => url.includes(term) || filename.includes(term))) {
      return false;
    }

    if (queryNegativeTerms.some(term => url.includes(term) || filename.includes(term))) {
      return false;
    }

    if (!matchFilename(filename) || !matchUrl(url)) {
      return false;
    }

    if (!item.startTime) {
      if (query.startedBefore != null || query.startedAfter != null) {
        return false;
      }
    } else if (item.startTime > startedBefore || item.startTime < startedAfter) {
      return false;
    }

    // todo endedBefore, endedAfter

    if (item.totalBytes == -1) {
      if (query.totalBytesGreater != null || query.totalBytesLess != null) {
        return false;
      }
    } else if (item.totalBytes <= totalBytesGreater || item.totalBytes >= totalBytesLess) {
      return false;
    }

    // todo: include danger
    const SIMPLE_ITEMS = ["id", "mime", "startTime", "endTime", "state",
                          "paused", "error",
                          "bytesReceived", "totalBytes", "fileSize", "exists"];
    for (let field of SIMPLE_ITEMS) {
      if (query[field] != null && item[field] != query[field]) {
        return false;
      }
    }

    return true;
  };
}

function queryHelper(query) {
  let matchFn;
  try {
    matchFn = downloadQuery(query);
  } catch (err) {
    return Promise.reject({message: err.message});
  }

  let compareFn;
  if (query.orderBy != null) {
    const fields = query.orderBy.map(field => field[0] == "-"
                                     ? {reverse: true, name: field.slice(1)}
                                     : {reverse: false, name: field});

    for (let field of fields) {
      if (!DOWNLOAD_ITEM_FIELDS.includes(field.name)) {
        return Promise.reject({message: `Invalid orderBy field ${field.name}`});
      }
    }

    compareFn = (dl1, dl2) => {
      for (let field of fields) {
        const val1 = dl1[field.name];
        const val2 = dl2[field.name];

        if (val1 < val2) {
          return field.reverse ? 1 : -1;
        } else if (val1 > val2) {
          return field.reverse ? -1 : 1;
        }
      }
      return 0;
    };
  }

  return DownloadMap.getAll().then(downloads => {
    if (compareFn) {
      downloads = Array.from(downloads);
      downloads.sort(compareFn);
    }
    let results = [];
    for (let download of downloads) {
      if (query.limit && results.length >= query.limit) {
        break;
      }
      if (matchFn(download)) {
        results.push(download);
      }
    }
    return results;
  });
}

extensions.registerSchemaAPI("downloads", "addon_parent", context => {
  let {extension} = context;
  return {
    downloads: {
      download(options) {
        let {filename} = options;
        if (filename && PlatformInfo.os === "win") {
          // cross platform javascript code uses "/"
          filename = filename.replace(/\//g, "\\");
        }

        if (filename != null) {
          if (filename.length == 0) {
            return Promise.reject({message: "filename must not be empty"});
          }

          let path = OS.Path.split(filename);
          if (path.absolute) {
            return Promise.reject({message: "filename must not be an absolute path"});
          }

          if (path.components.some(component => component == "..")) {
            return Promise.reject({message: "filename must not contain back-references (..)"});
          }
        }

        if (options.conflictAction == "prompt") {
          // TODO
          return Promise.reject({message: "conflictAction prompt not yet implemented"});
        }

        if (options.headers) {
          for (let {name} of options.headers) {
            if (FORBIDDEN_HEADERS.includes(name.toUpperCase()) || name.match(FORBIDDEN_PREFIXES)) {
              return Promise.reject({message: "Forbidden request header name"});
            }
          }
        }

        // Handle method, headers and body options.
        function adjustChannel(channel) {
          if (channel instanceof Ci.nsIHttpChannel) {
            const method = options.method || "GET";
            channel.requestMethod = method;

            if (options.headers) {
              for (let {name, value} of options.headers) {
                channel.setRequestHeader(name, value, false);
              }
            }

            if (options.body != null) {
              const stream = Cc["@mozilla.org/io/string-input-stream;1"]
                             .createInstance(Ci.nsIStringInputStream);
              stream.setData(options.body, options.body.length);

              channel.QueryInterface(Ci.nsIUploadChannel2);
              channel.explicitSetUploadStream(stream, null, -1, method, false);
            }
          }
          return Promise.resolve();
        }

        function createTarget(downloadsDir) {
          let target;
          if (filename) {
            target = OS.Path.join(downloadsDir, filename);
          } else {
            let uri = NetUtil.newURI(options.url);

            let remote = "download";
            if (uri instanceof Ci.nsIURL) {
              remote = uri.fileName;
            }
            target = OS.Path.join(downloadsDir, remote);
          }

          // Create any needed subdirectories if required by filename.
          const dir = OS.Path.dirname(target);
          return OS.File.makeDir(dir, {from: downloadsDir}).then(() => {
            return OS.File.exists(target);
          }).then(exists => {
            // This has a race, something else could come along and create
            // the file between this test and them time the download code
            // creates the target file.  But we can't easily fix it without
            // modifying DownloadCore so we live with it for now.
            if (exists) {
              switch (options.conflictAction) {
                case "uniquify":
                default:
                  target = DownloadPaths.createNiceUniqueFile(new FileUtils.File(target)).path;
                  break;

                case "overwrite":
                  break;
              }
            }
          }).then(() => {
            if (!options.saveAs) {
              return Promise.resolve(target);
            }

            // Setup the file picker Save As dialog.
            const picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
            const window = Services.wm.getMostRecentWindow("navigator:browser");
            picker.init(window, null, Ci.nsIFilePicker.modeSave);
            picker.displayDirectory = new FileUtils.File(dir);
            picker.appendFilters(Ci.nsIFilePicker.filterAll);
            picker.defaultString = OS.Path.basename(target);

            // Open the dialog and resolve/reject with the result.
            return new Promise((resolve, reject) => {
              picker.open(result => {
                if (result === Ci.nsIFilePicker.returnCancel) {
                  reject({message: "Download canceled by the user"});
                } else {
                  resolve(picker.file.path);
                }
              });
            });
          });
        }

        let download;
        return Downloads.getPreferredDownloadsDirectory()
          .then(downloadsDir => createTarget(downloadsDir))
          .then(target => {
            const source = {
              url: options.url,
            };

            if (options.method || options.headers || options.body) {
              source.adjustChannel = adjustChannel;
            }

            return Downloads.createDownload({
              source,
              target: {
                path: target,
                partFilePath: target + ".part",
              },
            });
          }).then(dl => {
            download = dl;
            return DownloadMap.getDownloadList();
          }).then(list => {
            list.add(download);

            // This is necessary to make pause/resume work.
            download.tryToKeepPartialData = true;
            download.start();

            const item = DownloadMap.newFromDownload(download, extension);
            return item.id;
          });
      },

      removeFile(id) {
        return DownloadMap.lazyInit().then(() => {
          let item;
          try {
            item = DownloadMap.fromId(id);
          } catch (err) {
            return Promise.reject({message: `Invalid download id ${id}`});
          }
          if (item.state !== "complete") {
            return Promise.reject({message: `Cannot remove incomplete download id ${id}`});
          }
          return OS.File.remove(item.filename, {ignoreAbsent: false}).catch((err) => {
            return Promise.reject({message: `Could not remove download id ${item.id} because the file doesn't exist`});
          });
        });
      },

      search(query) {
        return queryHelper(query)
          .then(items => items.map(item => item.serialize()));
      },

      pause(id) {
        return DownloadMap.lazyInit().then(() => {
          let item;
          try {
            item = DownloadMap.fromId(id);
          } catch (err) {
            return Promise.reject({message: `Invalid download id ${id}`});
          }
          if (item.state != "in_progress") {
            return Promise.reject({message: `Download ${id} cannot be paused since it is in state ${item.state}`});
          }

          return item.download.cancel();
        });
      },

      resume(id) {
        return DownloadMap.lazyInit().then(() => {
          let item;
          try {
            item = DownloadMap.fromId(id);
          } catch (err) {
            return Promise.reject({message: `Invalid download id ${id}`});
          }
          if (!item.canResume) {
            return Promise.reject({message: `Download ${id} cannot be resumed`});
          }

          return item.download.start();
        });
      },

      cancel(id) {
        return DownloadMap.lazyInit().then(() => {
          let item;
          try {
            item = DownloadMap.fromId(id);
          } catch (err) {
            return Promise.reject({message: `Invalid download id ${id}`});
          }
          if (item.download.succeeded) {
            return Promise.reject({message: `Download ${id} is already complete`});
          }
          return item.download.finalize(true);
        });
      },

      showDefaultFolder() {
        Downloads.getPreferredDownloadsDirectory().then(dir => {
          let dirobj = new FileUtils.File(dir);
          if (dirobj.isDirectory()) {
            dirobj.launch();
          } else {
            throw new Error(`Download directory ${dirobj.path} is not actually a directory`);
          }
        }).catch(Cu.reportError);
      },

      erase(query) {
        return queryHelper(query).then(items => {
          let results = [];
          let promises = [];
          for (let item of items) {
            promises.push(DownloadMap.erase(item));
            results.push(item.id);
          }
          return Promise.all(promises).then(() => results);
        });
      },

      open(downloadId) {
        return DownloadMap.lazyInit().then(() => {
          let download = DownloadMap.fromId(downloadId).download;
          if (download.succeeded) {
            return download.launch();
          }
          return Promise.reject({message: "Download has not completed."});
        }).catch((error) => {
          return Promise.reject({message: error.message});
        });
      },

      show(downloadId) {
        return DownloadMap.lazyInit().then(() => {
          let download = DownloadMap.fromId(downloadId);
          return download.download.showContainingDirectory();
        }).then(() => {
          return true;
        }).catch(error => {
          return Promise.reject({message: error.message});
        });
      },

      getFileIcon(downloadId, options) {
        return DownloadMap.lazyInit().then(() => {
          let size = options && options.size ? options.size : 32;
          let download = DownloadMap.fromId(downloadId).download;
          let pathPrefix = "";
          let path;

          if (download.succeeded) {
            let file = FileUtils.File(download.target.path);
            path = Services.io.newFileURI(file).spec;
          } else {
            path = OS.Path.basename(download.target.path);
            pathPrefix = "//";
          }

          return new Promise((resolve, reject) => {
            let chromeWebNav = Services.appShell.createWindowlessBrowser(true);
            chromeWebNav
              .QueryInterface(Ci.nsIInterfaceRequestor)
              .getInterface(Ci.nsIDocShell)
              .createAboutBlankContentViewer(Services.scriptSecurityManager.getSystemPrincipal());

            let img = chromeWebNav.document.createElement("img");
            img.width = size;
            img.height = size;

            let handleLoad;
            let handleError;
            const cleanup = () => {
              img.removeEventListener("load", handleLoad);
              img.removeEventListener("error", handleError);
              chromeWebNav.close();
              chromeWebNav = null;
            };

            handleLoad = () => {
              let canvas = chromeWebNav.document.createElement("canvas");
              canvas.width = size;
              canvas.height = size;
              let context = canvas.getContext("2d");
              context.drawImage(img, 0, 0, size, size);
              let dataURL = canvas.toDataURL("image/png");
              cleanup();
              resolve(dataURL);
            };

            handleError = (error) => {
              Cu.reportError(error);
              cleanup();
              reject(new Error("An unexpected error occurred"));
            };

            img.addEventListener("load", handleLoad);
            img.addEventListener("error", handleError);
            img.src = `moz-icon:${pathPrefix}${path}?size=${size}`;
          });
        }).catch((error) => {
          return Promise.reject({message: error.message});
        });
      },

      // When we do setShelfEnabled(), check for additional "downloads.shelf" permission.
      // i.e.:
      // setShelfEnabled(enabled) {
      //   if (!extension.hasPermission("downloads.shelf")) {
      //     throw new context.cloneScope.Error("Permission denied because 'downloads.shelf' permission is missing.");
      //   }
      //   ...
      // }

      onChanged: new SingletonEventManager(context, "downloads.onChanged", fire => {
        const handler = (what, item) => {
          let changes = {};
          const noundef = val => (val === undefined) ? null : val;
          DOWNLOAD_ITEM_CHANGE_FIELDS.forEach(fld => {
            if (item[fld] != item.prechange[fld]) {
              changes[fld] = {
                previous: noundef(item.prechange[fld]),
                current: noundef(item[fld]),
              };
            }
          });
          if (Object.keys(changes).length > 0) {
            changes.id = item.id;
            runSafeSync(context, fire, changes);
          }
        };

        let registerPromise = DownloadMap.getDownloadList().then(() => {
          DownloadMap.on("change", handler);
        });
        return () => {
          registerPromise.then(() => {
            DownloadMap.off("change", handler);
          });
        };
      }).api(),

      onCreated: new SingletonEventManager(context, "downloads.onCreated", fire => {
        const handler = (what, item) => {
          runSafeSync(context, fire, item.serialize());
        };
        let registerPromise = DownloadMap.getDownloadList().then(() => {
          DownloadMap.on("create", handler);
        });
        return () => {
          registerPromise.then(() => {
            DownloadMap.off("create", handler);
          });
        };
      }).api(),

      onErased: new SingletonEventManager(context, "downloads.onErased", fire => {
        const handler = (what, item) => {
          runSafeSync(context, fire, item.id);
        };
        let registerPromise = DownloadMap.getDownloadList().then(() => {
          DownloadMap.on("erase", handler);
        });
        return () => {
          registerPromise.then(() => {
            DownloadMap.off("erase", handler);
          });
        };
      }).api(),

      onDeterminingFilename: ignoreEvent(context, "downloads.onDeterminingFilename"),
    },
  };
});