This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
DownloadsManager.jsm
163 lines (145 loc) · 5.19 KB
/
DownloadsManager.jsm
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
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
const {actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/Actions.jsm", {});
ChromeUtils.defineModuleGetter(this, "DownloadsViewUI",
"resource:///modules/DownloadsViewUI.jsm");
ChromeUtils.defineModuleGetter(this, "DownloadsCommon",
"resource:///modules/DownloadsCommon.jsm");
const DOWNLOAD_CHANGED_DELAY_TIME = 1000; // time in ms to delay timer for downloads changed events
class DownloadElement extends DownloadsViewUI.DownloadElementShell {
constructor(download, browser) {
super();
this._download = download;
this.element = browser;
this.element._shell = this;
}
get download() {
return this._download;
}
downloadsCmd_copyLocation() {
let clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
clipboard.copyString(this.download.source.url);
}
}
this.DownloadsManager = class DownloadsManager {
constructor(store) {
this._downloadData = null;
this._store = null;
this._viewableDownloadItems = new Map();
this._downloadTimer = null;
}
setTimeout(callback, delay) {
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(callback, delay, Ci.nsITimer.TYPE_ONE_SHOT);
return timer;
}
formatDownload(element) {
const downloadedItem = element.download;
const description = element.sizeStrings.stateLabel;
return {
hostname: new URL(downloadedItem.source.url).hostname,
url: downloadedItem.source.url,
path: downloadedItem.target.path,
title: element.displayName,
description,
referrer: downloadedItem.source.referrer,
date_added: downloadedItem.endTime
};
}
init(store) {
this._store = store;
this._browser = Services.appShell.hiddenDOMWindow;
this._downloadData = DownloadsCommon.getData(this._browser.ownerGlobal, true, false, true);
this._downloadData.addView(this);
}
onDownloadAdded(download) {
const elem = new DownloadElement(download, this._browser);
const downloadedItem = elem.download;
if (!this._viewableDownloadItems.has(downloadedItem.source.url)) {
this._viewableDownloadItems.set(downloadedItem.source.url, elem);
// On startup, all existing downloads fire this notification, so debounce them
if (this._downloadTimer) {
this._downloadTimer.delay = DOWNLOAD_CHANGED_DELAY_TIME;
} else {
this._downloadTimer = this.setTimeout(() => {
this._downloadTimer = null;
this._store.dispatch({type: at.DOWNLOAD_CHANGED});
}, DOWNLOAD_CHANGED_DELAY_TIME);
}
}
}
onDownloadRemoved(download) {
if (this._viewableDownloadItems.has(download.source.url)) {
this._viewableDownloadItems.delete(download.source.url);
this._store.dispatch({type: at.DOWNLOAD_CHANGED});
}
}
async getDownloads(threshold, {numItems = this._viewableDownloadItems.size, onlySucceeded = false, onlyExists = false}) {
if (!threshold) {
return [];
}
let results = [];
// Only get downloads within the time threshold specified and sort by recency
const downloadThreshold = Date.now() - threshold;
let downloads = [...this._viewableDownloadItems.values()]
.filter(elem => elem.download.endTime > downloadThreshold)
.sort((elem1, elem2) => elem1.download.endTime < elem2.download.endTime);
for (const elem of downloads) {
// Only include downloads where the file still exists
if (onlyExists) {
// Refresh download to ensure the 'exists' attribute is up to date
await elem.download.refresh();
if (!elem.download.target.exists) { continue; }
}
// Only include downloads that were completed successfully
if (onlySucceeded) {
if (!elem.download.succeeded) { continue; }
}
const formattedDownloadForHighlights = this.formatDownload(elem);
results.push(formattedDownloadForHighlights);
if (results.length === numItems) {
break;
}
}
return results;
}
uninit() {
if (this._downloadData) {
this._downloadData.removeView(this);
this._downloadData = null;
}
if (this._downloadTimer) {
this._downloadTimer.cancel();
this._downloadTimer = null;
}
}
onAction(action) {
let downloadsCmd;
switch (action.type) {
case at.COPY_DOWNLOAD_LINK:
downloadsCmd = "downloadsCmd_copyLocation";
break;
case at.REMOVE_DOWNLOAD_FILE:
downloadsCmd = "downloadsCmd_delete";
break;
case at.SHOW_DOWNLOAD_FILE:
downloadsCmd = "downloadsCmd_show";
break;
case at.OPEN_DOWNLOAD_FILE:
downloadsCmd = "downloadsCmd_open";
break;
case at.UNINIT:
this.uninit();
break;
}
// Call the appropriate downloads command function based on the event we received
if (downloadsCmd) {
let elem = this._viewableDownloadItems.get(action.data.url);
if (elem) {
elem[downloadsCmd]();
}
}
}
};
this.EXPORTED_SYMBOLS = ["DownloadsManager"];