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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const PREF_SVG_DISABLED = "svg.disabled";
const PREF_AVIF_ENABLED = "image.avif.enabled";
const PDF_MIME = "application/pdf";
const OCTET_MIME = "application/octet-stream";
const XML_MIME = "text/xml";
const SVG_MIME = "image/svg+xml";
const AVIF_MIME = "image/avif";
const { Integration } = ChromeUtils.importESModule(
"resource://gre/modules/Integration.sys.mjs"
);
const {
DownloadsViewableInternally,
PREF_ENABLED_TYPES,
PREF_BRANCH_WAS_REGISTERED,
PREF_BRANCH_PREVIOUS_ACTION,
PREF_BRANCH_PREVIOUS_ASK,
} = ChromeUtils.importESModule(
"resource:///modules/DownloadsViewableInternally.sys.mjs"
);
/* global DownloadIntegration */
Integration.downloads.defineESModuleGetter(
this,
"DownloadIntegration",
"resource://gre/modules/DownloadIntegration.sys.mjs"
);
const HandlerService = Cc[
"@mozilla.org/uriloader/handler-service;1"
].getService(Ci.nsIHandlerService);
const MIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
function checkPreferInternal(mime, ext, expectedPreferInternal) {
const handler = MIMEService.getFromTypeAndExtension(mime, ext);
if (expectedPreferInternal) {
Assert.equal(
handler?.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
`checking ${mime} preferredAction == handleInternally`
);
} else {
Assert.notEqual(
handler?.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
`checking ${mime} preferredAction != handleInternally`
);
}
}
function shouldView(mime, ext) {
return DownloadIntegration.shouldViewDownloadInternally(mime, ext);
}
function checkShouldView(mime, ext, expectedShouldView) {
Assert.equal(
shouldView(mime, ext),
expectedShouldView,
`checking ${mime} shouldViewDownloadInternally`
);
}
function checkWasRegistered(ext, expectedWasRegistered) {
Assert.equal(
Services.prefs.getBoolPref(PREF_BRANCH_WAS_REGISTERED + ext, false),
expectedWasRegistered,
`checking ${ext} was registered pref`
);
}
function checkAll(mime, ext, expected) {
checkPreferInternal(mime, ext, expected && ext != "xml" && ext != "svg");
checkShouldView(mime, ext, expected);
if (ext != "xml" && ext != "svg") {
checkWasRegistered(ext, expected);
}
}
add_task(async function test_viewable_internally() {
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml , svg,avif");
Services.prefs.setBoolPref(PREF_SVG_DISABLED, false);
Services.prefs.setBoolPref(PREF_AVIF_ENABLED, true);
checkAll(XML_MIME, "xml", false);
checkAll(SVG_MIME, "svg", false);
checkAll(AVIF_MIME, "avif", false);
DownloadsViewableInternally.register();
checkAll(XML_MIME, "xml", true);
checkAll(SVG_MIME, "svg", true);
checkAll(AVIF_MIME, "avif", true);
// Disable xml, and avif, check that avif becomes disabled
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "svg");
// (XML is externally managed)
checkAll(XML_MIME, "xml", true);
// Avif should be disabled
checkAll(AVIF_MIME, "avif", false);
// SVG shouldn't be cleared as it's still enabled
checkAll(SVG_MIME, "svg", true);
Assert.ok(
shouldView(PDF_MIME),
"application/pdf should be unaffected by pref"
);
Assert.ok(
shouldView(OCTET_MIME, "pdf"),
".pdf should be accepted by extension"
);
Assert.ok(
shouldView(OCTET_MIME, "PDF"),
".pdf should be detected case-insensitively"
);
Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted");
Assert.ok(!shouldView(AVIF_MIME), "image/avif should be disabled by pref");
// Enable, check that everything is enabled again
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml,svg,avif");
checkAll(XML_MIME, "xml", true);
checkAll(SVG_MIME, "svg", true);
checkPreferInternal(AVIF_MIME, "avif", true);
Assert.ok(
shouldView(PDF_MIME),
"application/pdf should be unaffected by pref"
);
Assert.ok(shouldView(XML_MIME), "text/xml should be enabled by pref");
Assert.ok(
shouldView("application/xml"),
"alternate MIME type application/xml should be accepted"
);
Assert.ok(
shouldView(OCTET_MIME, "xml"),
".xml should be accepted by extension"
);
// Disable viewable internally, pre-set handlers.
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "");
for (const [mime, ext, action, ask] of [
[XML_MIME, "xml", Ci.nsIHandlerInfo.useSystemDefault, true],
[SVG_MIME, "svg", Ci.nsIHandlerInfo.saveToDisk, true],
]) {
let handler = MIMEService.getFromTypeAndExtension(mime, ext);
handler.preferredAction = action;
handler.alwaysAskBeforeHandling = ask;
HandlerService.store(handler);
checkPreferInternal(mime, ext, false);
// Expect to read back the same values
handler = MIMEService.getFromTypeAndExtension(mime, ext);
Assert.equal(handler.preferredAction, action);
Assert.equal(handler.alwaysAskBeforeHandling, ask);
}
// Enable viewable internally, SVG and XML should not be replaced.
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "svg,xml");
Assert.equal(
Services.prefs.prefHasUserValue(PREF_BRANCH_PREVIOUS_ACTION + "svg"),
false,
"svg action should not be stored"
);
Assert.equal(
Services.prefs.prefHasUserValue(PREF_BRANCH_PREVIOUS_ASK + "svg"),
false,
"svg ask should not be stored"
);
{
let handler = MIMEService.getFromTypeAndExtension(SVG_MIME, "svg");
Assert.equal(
handler.preferredAction,
Ci.nsIHandlerInfo.saveToDisk,
"svg action should be preserved"
);
Assert.equal(
!!handler.alwaysAskBeforeHandling,
true,
"svg ask should be preserved"
);
// Clean up
HandlerService.remove(handler);
handler = MIMEService.getFromTypeAndExtension(XML_MIME, "xml");
Assert.equal(
handler.preferredAction,
Ci.nsIHandlerInfo.useSystemDefault,
"xml action should be preserved"
);
Assert.equal(
!!handler.alwaysAskBeforeHandling,
true,
"xml ask should be preserved"
);
// Clean up
HandlerService.remove(handler);
}
// It should still be possible to view XML internally
checkShouldView(XML_MIME, "xml", true);
checkAll(SVG_MIME, "svg", true);
// Disable SVG to test SVG enabled check (depends on the pref)
Services.prefs.setBoolPref(PREF_SVG_DISABLED, true);
checkAll(SVG_MIME, "svg", false);
Services.prefs.setBoolPref(PREF_SVG_DISABLED, false);
{
let handler = MIMEService.getFromTypeAndExtension(SVG_MIME, "svg");
handler.preferredAction = Ci.nsIHandlerInfo.saveToDisk;
handler.alwaysAskBeforeHandling = false;
HandlerService.store(handler);
}
checkAll(SVG_MIME, "svg", true);
Assert.ok(!shouldView(null, "pdf"), "missing MIME shouldn't be accepted");
Assert.ok(!shouldView(null, "xml"), "missing MIME shouldn't be accepted");
Assert.ok(!shouldView(OCTET_MIME), "unsupported MIME shouldn't be accepted");
Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted");
});
registerCleanupFunction(() => {
// Clear all types to remove any saved values
Services.prefs.setCharPref(PREF_ENABLED_TYPES, "");
// Reset to the defaults
Services.prefs.clearUserPref(PREF_ENABLED_TYPES);
Services.prefs.clearUserPref(PREF_SVG_DISABLED);
});
|