author Santo Pfingsten
 *  see https import { browser } from "webextension-polyfill-ts" ; import { settings } from "../lib/settings" ; import DelayedExecution from "../lib/delayedExecution" ; import { messageUtil } from "../lib/messageUtil" ; import { wetLayer } from "wet-layer" ; const COOKIE_CLEANUP_NOTIFICATION_ID = "CookieCleanupNotification" ; const CLEAR_NOTIFICATION_TIME = 3000 ; const DELAY_NOTIFICATION_UPDATE_ON_START = 100 ; const DELAY_NOTIFICATION = 500 ; export class NotificationHandler { private enabled : boolean ; private readonly delayNotification = new DelayedExecution ( this . showNotification . bind ( this ) ) ; private readonly delayClearNotification = new DelayedExecution ( this . clearNotification . bind ( this ) ) ; private removalCountsByDomain : { [ s ] : number ; } = { } ; private starting = false ; private updateOnStart = false ; public constructor ( ) { this . onNotificationClosed = this . onNotificationClosed . bind ( this ) ; browser . notifications . onClosed . addListener ( this . onNotificationClosed ) ; this . enabled = settings . get ( "showCookieRemovalNotification" ) ; messageUtil . receive ( "settingsChanged" , ( ) => { this . enabled = settings . get ( "showCookieRemovalNotification" ) ; } ) ; messageUtil . receive ( "cookieRemoved" , this . onCookieRemoved . bind ( this ) ) ; } private onNotificationClosed ( id ) { this . starting = false ; this . removalCountsByDomain = { } ; this . delayClearNotification . cancel ( ) ; } private clearNotification ( ) { browser . notifications . clear ( COOKIE_CLEANUP_NOTIFICATION_ID ) ; this . starting = false ; this . removalCountsByDomain = { } ; } private showNotification ( ) { if ( this . starting ) { this . updateOnStart = true ; return ; } const lines = [ ] ; let totalCount = 0 ; for ( const domain in this . removalCountsByDomain ) { const count = this . removalCountsByDomain [ domain ] ; lines . push ( wetLayer . getMessage ( "cookie_cleanup_notification_line" , [ domain , count . toString ( ) ] ) ) ; totalCount += count ; } this . starting = true ; this . updateOnStart = false ; browser . notifications . create ( COOKIE_CLEANUP_NOTIFICATION_ID , { type : "basic" , iconUrl : browser . extension . getURL ( "icons/icon96.png" ) , title : wetLayer . getMessage ( "cookie_cleanup_notification_title" , totalCount . toString ( ) ) , message : lines . join ( "\n" ) } ) . then ( ( s ) => { this . starting = false ; if ( this . updateOnStart ) this . delayNotification . restart ( DELAY_NOTIFICATION_UPDATE_ON_START ) ; } ) ; this . delayClearNotification . restart ( CLEAR_NOTIFICATION_TIME ) ; } private onCookieRemoved ( domain ) { if ( this . enabled ) { this . removalCountsByDomain [ domain ] = ( this . removalCountsByDomain [ domain ] || 0 ) + 1 ; this . delayNotification . restart ( DELAY_NOTIFICATION ) ; } } }