author Santo Pfingsten
 *  see https import { settings } from "../../lib/settings" ; import { BrowsingData , browser } from "webextension-polyfill-ts" ; import { Cleaner } from "./cleaner" ; import { getAllCookieStoreIds } from "../backgroundHelpers" ; import { removeLocalStorageByHostname } from "../backgroundShared" ; import { CleanupType } from "../../lib/settingsSignature" ; import { TabWatcher } from "../tabWatcher" ; export class LocalStorageCleaner extends Cleaner { private readonly tabWatcher : TabWatcher ; public constructor ( tabWatcher ) { super ( ) ; this . tabWatcher = tabWatcher ; } public clean ( typeSet , startup ) { if ( typeSet . localStorage && removeLocalStorageByHostname ) { const protectOpenDomains = startup || settings . get ( "cleanAll.protectOpenDomains" ) ; if ( settings . get ( startup ? "startup.localStorage.applyRules" : "cleanAll.localStorage.applyRules" ) ) { typeSet . localStorage = false ; getAllCookieStoreIds ( ) . then ( ( ids ) => { const hostnames = this . getDomainsToClean ( startup , protectOpenDomains ) ; for ( const id of ids ) this . cleanDomains ( id , hostnames ) ; this . removeFromDomainsToClean ( hostnames ) ; } ) ; } else { settings . set ( "domainsToClean" , { } ) ; settings . save ( ) ; } } } public cleanDomainOnLeave ( storeId , domain ) { if ( settings . get ( "domainLeave.enabled" ) && settings . get ( "domainLeave.localStorage" ) && ! this . isLocalStorageProtected ( storeId , domain ) ) this . cleanDomain ( storeId , domain ) ; } public cleanDomain ( storeId , domain ) { const domains = [ domain ] ; this . cleanDomains ( storeId , domains ) ; this . removeFromDomainsToClean ( domains ) ; } private removeFromDomainsToClean ( hostnames ) { const domainsToClean = { ... settings . get ( "domainsToClean" ) } ; for ( const hostname of hostnames ) { if ( ! this . tabWatcher . containsDomain ( hostname ) ) delete domainsToClean [ hostname ] ; } settings . set ( "domainsToClean" , domainsToClean ) ; settings . save ( ) ; } public cleanDomains ( storeId , hostnames ) { if ( removeLocalStorageByHostname ) { browser . browsingData . remove ( { originTypes : { unprotectedWeb : true } , hostnames } , { localStorage : true } ) ; } } private isDomainProtected ( domain , ignoreStartupType , protectOpenDomains ) { if ( protectOpenDomains && this . tabWatcher . containsDomain ( domain ) ) return true ; return settings . isDomainProtected ( domain , ignoreStartupType ) ; } private getDomainsToClean ( ignoreStartupType , protectOpenDomains ) { const domainsToClean = settings . get ( "domainsToClean" ) ; const result = [ ] ; for ( const domain in domainsToClean ) { if ( domainsToClean . hasOwnProperty ( domain ) && ! this . isDomainProtected ( domain , ignoreStartupType , protectOpenDomains ) ) result . push ( domain ) ; } return result ; } public isLocalStorageProtected ( storeId , domain ) { if ( this . tabWatcher . cookieStoreContainsDomain ( storeId , domain , true ) ) return true ; const type = settings . getCleanupTypeForDomain ( domain ) ; return type === CleanupType . NEVER || type === CleanupType . STARTUP ; } }