clearSiteData.js (3044B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const { SiteDataManager } = ChromeUtils.importESModule( 6 "resource:///modules/SiteDataManager.sys.mjs" 7 ); 8 9 ChromeUtils.defineESModuleGetters(this, { 10 DownloadUtils: "resource://gre/modules/DownloadUtils.sys.mjs", 11 }); 12 13 var gClearSiteDataDialog = { 14 _clearSiteDataCheckbox: null, 15 _clearCacheCheckbox: null, 16 17 onLoad() { 18 document.mozSubdialogReady = this.init(); 19 }, 20 21 async init() { 22 this._dialog = document.querySelector("dialog"); 23 this._clearSiteDataCheckbox = document.getElementById("clearSiteData"); 24 this._clearCacheCheckbox = document.getElementById("clearCache"); 25 26 // We'll block init() on this because the result values may impact 27 // subdialog sizing. 28 await Promise.all([ 29 SiteDataManager.getTotalUsage().then(bytes => { 30 let [amount, unit] = DownloadUtils.convertByteUnits(bytes); 31 document.l10n.setAttributes( 32 this._clearSiteDataCheckbox, 33 "clear-site-data-cookies-with-data", 34 { amount, unit } 35 ); 36 }), 37 SiteDataManager.getCacheSize().then(bytes => { 38 let [amount, unit] = DownloadUtils.convertByteUnits(bytes); 39 document.l10n.setAttributes( 40 this._clearCacheCheckbox, 41 "clear-site-data-cache-with-data", 42 { amount, unit } 43 ); 44 }), 45 ]); 46 await document.l10n.translateElements([ 47 this._clearCacheCheckbox, 48 this._clearSiteDataCheckbox, 49 ]); 50 51 document.addEventListener("dialogaccept", event => this.onClear(event)); 52 53 this._clearSiteDataCheckbox.addEventListener("command", e => 54 this.onCheckboxCommand(e) 55 ); 56 this._clearCacheCheckbox.addEventListener("command", e => 57 this.onCheckboxCommand(e) 58 ); 59 60 document 61 .getElementById("key_close") 62 .addEventListener("command", () => window.close()); 63 }, 64 65 onCheckboxCommand() { 66 this._dialog.toggleAttribute( 67 "buttondisabledaccept", 68 !(this._clearSiteDataCheckbox.checked || this._clearCacheCheckbox.checked) 69 ); 70 }, 71 72 onClear(event) { 73 let clearSiteData = this._clearSiteDataCheckbox.checked; 74 let clearCache = this._clearCacheCheckbox.checked; 75 76 if (clearSiteData) { 77 // Ask for confirmation before clearing site data 78 if (!SiteDataManager.promptSiteDataRemoval(window)) { 79 clearSiteData = false; 80 // Prevent closing the dialog when the data removal wasn't allowed. 81 event.preventDefault(); 82 } 83 } 84 85 if (clearSiteData) { 86 SiteDataManager.removeSiteData(); 87 } 88 if (clearCache) { 89 SiteDataManager.removeCache(); 90 91 // If we're not clearing site data, we need to tell the 92 // SiteDataManager to signal that it's updating. 93 if (!clearSiteData) { 94 SiteDataManager.updateSites(); 95 } 96 } 97 }, 98 }; 99 100 window.addEventListener("load", () => gClearSiteDataDialog.onLoad());