diff options
author | janekptacijarabaci <janekptacijarabaci@seznam.cz> | 2018-04-24 19:23:04 +0200 |
---|---|---|
committer | janekptacijarabaci <janekptacijarabaci@seznam.cz> | 2018-04-24 19:23:04 +0200 |
commit | 846bbb9930e6588516a28f58b2f80c2b04e1b372 (patch) | |
tree | ac06dde167b320f52677d5f1416981e9f88d21f0 /toolkit/forgetaboutsite/ForgetAboutSite.jsm | |
parent | bccb9c1708f007ada1ea8c4879db88a587f0a450 (diff) | |
download | UXP-846bbb9930e6588516a28f58b2f80c2b04e1b372.tar UXP-846bbb9930e6588516a28f58b2f80c2b04e1b372.tar.gz UXP-846bbb9930e6588516a28f58b2f80c2b04e1b372.tar.lz UXP-846bbb9930e6588516a28f58b2f80c2b04e1b372.tar.xz UXP-846bbb9930e6588516a28f58b2f80c2b04e1b372.zip |
moebius#30 and #37: ForgetAboutSite.jsm - promise - serialize vs. parallel + a comment
https://github.com/MoonchildProductions/moebius/pull/30
https://github.com/MoonchildProductions/moebius/pull/37
Diffstat (limited to 'toolkit/forgetaboutsite/ForgetAboutSite.jsm')
-rw-r--r-- | toolkit/forgetaboutsite/ForgetAboutSite.jsm | 244 |
1 files changed, 135 insertions, 109 deletions
diff --git a/toolkit/forgetaboutsite/ForgetAboutSite.jsm b/toolkit/forgetaboutsite/ForgetAboutSite.jsm index 5250ca75d..8c7825392 100644 --- a/toolkit/forgetaboutsite/ForgetAboutSite.jsm +++ b/toolkit/forgetaboutsite/ForgetAboutSite.jsm @@ -45,175 +45,201 @@ const Ci = Components.interfaces; const Cu = Components.utils; this.ForgetAboutSite = { - removeDataFromDomain: function CRH_removeDataFromDomain(aDomain) + removeDataFromDomain: Task.async(function* (aDomain) { PlacesUtils.history.removePagesFromHost(aDomain, true); + let promises = []; // Cache - let cs = Cc["@mozilla.org/netwerk/cache-storage-service;1"]. - getService(Ci.nsICacheStorageService); - // NOTE: there is no way to clear just that domain, so we clear out - // everything) - try { + promises.push(Task.spawn(function*() { + let cs = Cc["@mozilla.org/netwerk/cache-storage-service;1"]. + getService(Ci.nsICacheStorageService); + // NOTE: there is no way to clear just that domain, so we clear out + // everything) cs.clear(); - } catch (ex) { - Cu.reportError("Exception thrown while clearing the cache: " + - ex.toString()); - } + }).catch(ex => { + throw new Error("Exception thrown while clearing the cache: " + ex); + })); // Image Cache - let imageCache = Cc["@mozilla.org/image/tools;1"]. - getService(Ci.imgITools).getImgCacheForDocument(null); - try { + promises.push(Task.spawn(function*() { + let imageCache = Cc["@mozilla.org/image/tools;1"]. + getService(Ci.imgITools).getImgCacheForDocument(null); imageCache.clearCache(false); // true=chrome, false=content - } catch (ex) { - Cu.reportError("Exception thrown while clearing the image cache: " + - ex.toString()); - } + }).catch(ex => { + throw new Error("Exception thrown while clearing the image cache: " + ex); + })); // Cookies - let cm = Cc["@mozilla.org/cookiemanager;1"]. - getService(Ci.nsICookieManager2); - let enumerator = cm.getCookiesWithOriginAttributes(JSON.stringify({}), aDomain); - while (enumerator.hasMoreElements()) { - let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie); - cm.remove(cookie.host, cookie.name, cookie.path, false, cookie.originAttributes); - } + // Need to maximize the number of cookies cleaned here + promises.push(Task.spawn(function*() { + let cm = Cc["@mozilla.org/cookiemanager;1"]. + getService(Ci.nsICookieManager2); + let enumerator = cm.getCookiesWithOriginAttributes(JSON.stringify({}), aDomain); + while (enumerator.hasMoreElements()) { + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie); + cm.remove(cookie.host, cookie.name, cookie.path, false, cookie.originAttributes); + } + }).catch(ex => { + throw new Error("Exception thrown while clearning cookies: " + ex); + })); // EME - let mps = Cc["@mozilla.org/gecko-media-plugin-service;1"]. - getService(Ci.mozIGeckoMediaPluginChromeService); - mps.forgetThisSite(aDomain, JSON.stringify({})); + promises.push(Task.spawn(function*() { + let mps = Cc["@mozilla.org/gecko-media-plugin-service;1"]. + getService(Ci.mozIGeckoMediaPluginChromeService); + mps.forgetThisSite(aDomain, JSON.stringify({})); + }).catch(ex => { + throw new Error("Exception thrown while clearing Encrypted Media Extensions: " + ex); + })); // Plugin data const phInterface = Ci.nsIPluginHost; const FLAG_CLEAR_ALL = phInterface.FLAG_CLEAR_ALL; let ph = Cc["@mozilla.org/plugin/host;1"].getService(phInterface); let tags = ph.getPluginTags(); - let promises = []; for (let i = 0; i < tags.length; i++) { - let promise = new Promise(resolve => { - let tag = tags[i]; + promises.push(new Promise(resolve => { try { - ph.clearSiteData(tags[i], aDomain, FLAG_CLEAR_ALL, -1, function(rv) { - resolve(); - }); + ph.clearSiteData(tags[i], aDomain, FLAG_CLEAR_ALL, -1, resolve); } catch (e) { // Ignore errors from the plugin, but resolve the promise + // We cannot check if something is a bailout or an error resolve(); } - }); - promises.push(promise); + })); } // Downloads - Task.spawn(function*() { + promises.push(Task.spawn(function*() { let list = yield Downloads.getList(Downloads.ALL); list.removeFinished(download => hasRootDomain( - NetUtil.newURI(download.source.url).host, aDomain)); - }).then(null, Cu.reportError); + NetUtil.newURI(download.source.url).host, aDomain)); + }).catch(ex => { + throw new Error("Exception in clearing Downloads: " + ex); + })); // Passwords - let lm = Cc["@mozilla.org/login-manager;1"]. - getService(Ci.nsILoginManager); - // Clear all passwords for domain - try { + promises.push(Task.spawn(function*() { + let lm = Cc["@mozilla.org/login-manager;1"]. + getService(Ci.nsILoginManager); + // Clear all passwords for domain let logins = lm.getAllLogins(); - for (let i = 0; i < logins.length; i++) - if (hasRootDomain(logins[i].hostname, aDomain)) + for (let i = 0; i < logins.length; i++) { + if (hasRootDomain(logins[i].hostname, aDomain)) { lm.removeLogin(logins[i]); - } - // XXXehsan: is there a better way to do this rather than this - // hacky comparison? - catch (ex) { - if (!ex.message.includes("User canceled Master Password entry")) { - throw ex; + } } - } + }).catch(ex => { + // XXX: + // Is there a better way to do this rather than this hacky comparison? + // Copied this from toolkit/components/passwordmgr/crypto-SDR.js + if (!ex.message.includes("User canceled master password entry")) { + throw new Error("Exception occured in clearing passwords: " + ex); + } + })); // Permissions let pm = Cc["@mozilla.org/permissionmanager;1"]. getService(Ci.nsIPermissionManager); // Enumerate all of the permissions, and if one matches, remove it - enumerator = pm.enumerator; + let enumerator = pm.enumerator; while (enumerator.hasMoreElements()) { let perm = enumerator.getNext().QueryInterface(Ci.nsIPermission); - try { - if (hasRootDomain(perm.principal.URI.host, aDomain)) { - pm.removePermission(perm); + promises.push(new Promise((resolve, reject) => { + try { + if (hasRootDomain(perm.principal.URI.host, aDomain)) { + pm.removePermission(perm); + } + } catch (ex) { + // Ignore entry + } finally { + resolve(); } - } catch (e) { - /* Ignore entry */ - } + })); } // Offline Storages - let qms = Cc["@mozilla.org/dom/quota-manager-service;1"]. - getService(Ci.nsIQuotaManagerService); - // delete data from both HTTP and HTTPS sites - let caUtils = {}; - let scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. - getService(Ci.mozIJSSubScriptLoader); - scriptLoader.loadSubScript("chrome://global/content/contentAreaUtils.js", - caUtils); - let httpURI = caUtils.makeURI("http://" + aDomain); - let httpsURI = caUtils.makeURI("https://" + aDomain); - // Following code section has been reverted to the state before Bug 1238183, - // but added a new argument to clearStoragesForPrincipal() for indicating - // clear all storages under a given origin. - let httpPrincipal = Services.scriptSecurityManager - .createCodebasePrincipal(httpURI, {}); - let httpsPrincipal = Services.scriptSecurityManager - .createCodebasePrincipal(httpsURI, {}); - qms.clearStoragesForPrincipal(httpPrincipal, null, true); - qms.clearStoragesForPrincipal(httpsPrincipal, null, true); - - - function onContentPrefsRemovalFinished() { - // Everybody else (including extensions) - Services.obs.notifyObservers(null, "browser:purge-domain-data", aDomain); - } + promises.push(Task.spawn(function*() { + let qms = Cc["@mozilla.org/dom/quota-manager-service;1"]. + getService(Ci.nsIQuotaManagerService); + // delete data from both HTTP and HTTPS sites + let httpURI = NetUtil.newURI("http://" + aDomain); + let httpsURI = NetUtil.newURI("https://" + aDomain); + // Following code section has been reverted to the state before Bug 1238183, + // but added a new argument to clearStoragesForPrincipal() for indicating + // clear all storages under a given origin. + let httpPrincipal = Services.scriptSecurityManager + .createCodebasePrincipal(httpURI, {}); + let httpsPrincipal = Services.scriptSecurityManager + .createCodebasePrincipal(httpsURI, {}); + qms.clearStoragesForPrincipal(httpPrincipal, null, true); + qms.clearStoragesForPrincipal(httpsPrincipal, null, true); + }).catch(ex => { + throw new Error("Exception occured while clearing offline storages: " + ex); + })); // Content Preferences - let cps2 = Cc["@mozilla.org/content-pref/service;1"]. - getService(Ci.nsIContentPrefService2); - cps2.removeBySubdomain(aDomain, null, { - handleCompletion: () => onContentPrefsRemovalFinished(), - handleError: function() {} - }); + promises.push(Task.spawn(function*() { + let cps2 = Cc["@mozilla.org/content-pref/service;1"]. + getService(Ci.nsIContentPrefService2); + cps2.removeBySubdomain(aDomain, null, { + handleCompletion: (reason) => { + // Notify other consumers, including extensions + Services.obs.notifyObservers(null, "browser:purge-domain-data", aDomain); + if (reason === cps2.COMPLETE_ERROR) { + throw new Error("Exception occured while clearing content preferences"); + } + }, + handleError() {} + }); + })); // Predictive network data - like cache, no way to clear this per // domain, so just trash it all - let np = Cc["@mozilla.org/network/predictor;1"]. - getService(Ci.nsINetworkPredictor); - np.reset(); - - // Push notifications. - promises.push(new Promise(resolve => { - var push = Cc["@mozilla.org/push/Service;1"] - .getService(Ci.nsIPushService); + promises.push(Task.spawn(function*() { + let np = Cc["@mozilla.org/network/predictor;1"]. + getService(Ci.nsINetworkPredictor); + np.reset(); + }).catch(ex => { + throw new Error("Exception occured while clearing predictive network data: " + ex); + })); + + // Push notifications + promises.push(Task.spawn(function*() { + var push = Cc["@mozilla.org/push/Service;1"]. + getService(Ci.nsIPushService); push.clearForDomain(aDomain, status => { - (Components.isSuccessCode(status) ? resolve : reject)(status); + if (!Components.isSuccessCode(status)) { + throw new Error("Exception occured while clearing push notifications: " + status); + } }); - }).catch(e => { - Cu.reportError("Exception thrown while clearing Push notifications: " + - e.toString()); })); // HSTS and HPKP // TODO (bug 1290529): also remove HSTS/HPKP information for subdomains. // Since we can't enumerate the information in the site security service // (bug 1115712), we can't implement this right now. - try { + promises.push(Task.spawn(function*() { let sss = Cc["@mozilla.org/ssservice;1"]. getService(Ci.nsISiteSecurityService); + let httpsURI = NetUtil.newURI("https://" + aDomain); sss.removeState(Ci.nsISiteSecurityService.HEADER_HSTS, httpsURI, 0); sss.removeState(Ci.nsISiteSecurityService.HEADER_HPKP, httpsURI, 0); - } catch (e) { - Cu.reportError("Exception thrown while clearing HSTS/HPKP: " + - e.toString()); - } + }).catch(ex => { + throw new Error("Exception thrown while clearing HSTS/HPKP: " + ex); + })); - return Promise.all(promises); - } -}; + let ErrorCount = 0; + for (let promise of promises) { + try { + yield promise; + } catch (ex) { + Cu.reportError(ex); + ErrorCount++; + } + } + if (ErrorCount !== 0) + throw new Error(`There were a total of ${ErrorCount} errors during removal`); + }) +} |