test_DownloadsViewableInternally.js (7236B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const PREF_SVG_DISABLED = "svg.disabled"; 5 const PDF_MIME = "application/pdf"; 6 const OCTET_MIME = "application/octet-stream"; 7 const XML_MIME = "text/xml"; 8 const SVG_MIME = "image/svg+xml"; 9 const AVIF_MIME = "image/avif"; 10 11 const { Integration } = ChromeUtils.importESModule( 12 "resource://gre/modules/Integration.sys.mjs" 13 ); 14 const { 15 DownloadsViewableInternally, 16 PREF_ENABLED_TYPES, 17 PREF_BRANCH_WAS_REGISTERED, 18 PREF_BRANCH_PREVIOUS_ACTION, 19 PREF_BRANCH_PREVIOUS_ASK, 20 } = ChromeUtils.importESModule( 21 "moz-src:///browser/components/downloads/DownloadsViewableInternally.sys.mjs" 22 ); 23 24 /* global DownloadIntegration */ 25 Integration.downloads.defineESModuleGetter( 26 this, 27 "DownloadIntegration", 28 "resource://gre/modules/DownloadIntegration.sys.mjs" 29 ); 30 31 const HandlerService = Cc[ 32 "@mozilla.org/uriloader/handler-service;1" 33 ].getService(Ci.nsIHandlerService); 34 const MIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); 35 36 function checkPreferInternal(mime, ext, expectedPreferInternal) { 37 const handler = MIMEService.getFromTypeAndExtension(mime, ext); 38 if (expectedPreferInternal) { 39 Assert.equal( 40 handler?.preferredAction, 41 Ci.nsIHandlerInfo.handleInternally, 42 `checking ${mime} preferredAction == handleInternally` 43 ); 44 } else { 45 Assert.notEqual( 46 handler?.preferredAction, 47 Ci.nsIHandlerInfo.handleInternally, 48 `checking ${mime} preferredAction != handleInternally` 49 ); 50 } 51 } 52 53 function shouldView(mime, ext) { 54 return DownloadIntegration.shouldViewDownloadInternally(mime, ext); 55 } 56 57 function checkShouldView(mime, ext, expectedShouldView) { 58 Assert.equal( 59 shouldView(mime, ext), 60 expectedShouldView, 61 `checking ${mime} shouldViewDownloadInternally` 62 ); 63 } 64 65 function checkWasRegistered(ext, expectedWasRegistered) { 66 Assert.equal( 67 Services.prefs.getBoolPref(PREF_BRANCH_WAS_REGISTERED + ext, false), 68 expectedWasRegistered, 69 `checking ${ext} was registered pref` 70 ); 71 } 72 73 function checkAll(mime, ext, expected) { 74 checkPreferInternal(mime, ext, expected && ext != "xml" && ext != "svg"); 75 checkShouldView(mime, ext, expected); 76 if (ext != "xml" && ext != "svg") { 77 checkWasRegistered(ext, expected); 78 } 79 } 80 81 add_task(async function test_viewable_internally() { 82 Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml , svg,avif"); 83 Services.prefs.setBoolPref(PREF_SVG_DISABLED, false); 84 85 checkAll(XML_MIME, "xml", false); 86 checkAll(SVG_MIME, "svg", false); 87 checkAll(AVIF_MIME, "avif", false); 88 89 DownloadsViewableInternally.register(); 90 91 checkAll(XML_MIME, "xml", true); 92 checkAll(SVG_MIME, "svg", true); 93 checkAll(AVIF_MIME, "avif", true); 94 95 // Disable xml, and avif, check that avif becomes disabled 96 Services.prefs.setCharPref(PREF_ENABLED_TYPES, "svg"); 97 98 // (XML is externally managed) 99 checkAll(XML_MIME, "xml", true); 100 101 // Avif should be disabled 102 checkAll(AVIF_MIME, "avif", false); 103 104 // SVG shouldn't be cleared as it's still enabled 105 checkAll(SVG_MIME, "svg", true); 106 107 Assert.ok( 108 shouldView(PDF_MIME), 109 "application/pdf should be unaffected by pref" 110 ); 111 Assert.ok( 112 shouldView(OCTET_MIME, "pdf"), 113 ".pdf should be accepted by extension" 114 ); 115 Assert.ok( 116 shouldView(OCTET_MIME, "PDF"), 117 ".pdf should be detected case-insensitively" 118 ); 119 Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted"); 120 121 Assert.ok(!shouldView(AVIF_MIME), "image/avif should be disabled by pref"); 122 123 // Enable, check that everything is enabled again 124 Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml,svg,avif"); 125 126 checkAll(XML_MIME, "xml", true); 127 checkAll(SVG_MIME, "svg", true); 128 checkPreferInternal(AVIF_MIME, "avif", true); 129 130 Assert.ok( 131 shouldView(PDF_MIME), 132 "application/pdf should be unaffected by pref" 133 ); 134 Assert.ok(shouldView(XML_MIME), "text/xml should be enabled by pref"); 135 Assert.ok( 136 shouldView("application/xml"), 137 "alternate MIME type application/xml should be accepted" 138 ); 139 Assert.ok( 140 shouldView(OCTET_MIME, "xml"), 141 ".xml should be accepted by extension" 142 ); 143 144 // Disable viewable internally, pre-set handlers. 145 Services.prefs.setCharPref(PREF_ENABLED_TYPES, ""); 146 147 for (const [mime, ext, action, ask] of [ 148 [XML_MIME, "xml", Ci.nsIHandlerInfo.useSystemDefault, true], 149 [SVG_MIME, "svg", Ci.nsIHandlerInfo.saveToDisk, true], 150 ]) { 151 let handler = MIMEService.getFromTypeAndExtension(mime, ext); 152 handler.preferredAction = action; 153 handler.alwaysAskBeforeHandling = ask; 154 155 HandlerService.store(handler); 156 checkPreferInternal(mime, ext, false); 157 158 // Expect to read back the same values 159 handler = MIMEService.getFromTypeAndExtension(mime, ext); 160 Assert.equal(handler.preferredAction, action); 161 Assert.equal(handler.alwaysAskBeforeHandling, ask); 162 } 163 164 // Enable viewable internally, SVG and XML should not be replaced. 165 Services.prefs.setCharPref(PREF_ENABLED_TYPES, "svg,xml"); 166 167 Assert.equal( 168 Services.prefs.prefHasUserValue(PREF_BRANCH_PREVIOUS_ACTION + "svg"), 169 false, 170 "svg action should not be stored" 171 ); 172 Assert.equal( 173 Services.prefs.prefHasUserValue(PREF_BRANCH_PREVIOUS_ASK + "svg"), 174 false, 175 "svg ask should not be stored" 176 ); 177 178 { 179 let handler = MIMEService.getFromTypeAndExtension(SVG_MIME, "svg"); 180 Assert.equal( 181 handler.preferredAction, 182 Ci.nsIHandlerInfo.saveToDisk, 183 "svg action should be preserved" 184 ); 185 Assert.equal( 186 !!handler.alwaysAskBeforeHandling, 187 true, 188 "svg ask should be preserved" 189 ); 190 // Clean up 191 HandlerService.remove(handler); 192 handler = MIMEService.getFromTypeAndExtension(XML_MIME, "xml"); 193 Assert.equal( 194 handler.preferredAction, 195 Ci.nsIHandlerInfo.useSystemDefault, 196 "xml action should be preserved" 197 ); 198 Assert.equal( 199 !!handler.alwaysAskBeforeHandling, 200 true, 201 "xml ask should be preserved" 202 ); 203 // Clean up 204 HandlerService.remove(handler); 205 } 206 // It should still be possible to view XML internally 207 checkShouldView(XML_MIME, "xml", true); 208 209 checkAll(SVG_MIME, "svg", true); 210 211 // Disable SVG to test SVG enabled check (depends on the pref) 212 Services.prefs.setBoolPref(PREF_SVG_DISABLED, true); 213 checkAll(SVG_MIME, "svg", false); 214 Services.prefs.setBoolPref(PREF_SVG_DISABLED, false); 215 { 216 let handler = MIMEService.getFromTypeAndExtension(SVG_MIME, "svg"); 217 handler.preferredAction = Ci.nsIHandlerInfo.saveToDisk; 218 handler.alwaysAskBeforeHandling = false; 219 HandlerService.store(handler); 220 } 221 222 checkAll(SVG_MIME, "svg", true); 223 224 Assert.ok(!shouldView(null, "pdf"), "missing MIME shouldn't be accepted"); 225 Assert.ok(!shouldView(null, "xml"), "missing MIME shouldn't be accepted"); 226 Assert.ok(!shouldView(OCTET_MIME), "unsupported MIME shouldn't be accepted"); 227 Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted"); 228 }); 229 230 registerCleanupFunction(() => { 231 // Clear all types to remove any saved values 232 Services.prefs.setCharPref(PREF_ENABLED_TYPES, ""); 233 // Reset to the defaults 234 Services.prefs.clearUserPref(PREF_ENABLED_TYPES); 235 Services.prefs.clearUserPref(PREF_SVG_DISABLED); 236 });