This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
/
DeleteAndQuit.kt
55 lines (52 loc) · 2.19 KB
/
DeleteAndQuit.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.utils
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import mozilla.components.concept.engine.Engine
import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.ext.components
/**
* Deletes selected browsing data and finishes the activity
*/
fun Context.deleteAndQuit(coroutineScope: CoroutineScope) {
coroutineScope.launch {
runBlocking {
if (Settings.getInstance(this@deleteAndQuit).deleteCacheOnQuit) {
withContext(coroutineContext) {
this@deleteAndQuit.components.core.engine.clearData(Engine.BrowsingData.allCaches())
}
}
if (Settings.getInstance(this@deleteAndQuit).deleteTabsOnQuit) {
withContext(coroutineContext) {
this@deleteAndQuit.components.useCases.tabsUseCases.removeAllTabs.invoke()
}
}
if (Settings.getInstance(this@deleteAndQuit).deletePermissionsOnQuit) {
launch(Dispatchers.IO) {
this@deleteAndQuit.components.core.permissionStorage.deleteAllSitePermissions()
}
}
if (Settings.getInstance(this@deleteAndQuit).deleteCookiesOnQuit) {
withContext(coroutineContext) {
this@deleteAndQuit.components.core.engine.clearData(
Engine.BrowsingData.select(
Engine.BrowsingData.COOKIES
)
)
}
}
if (Settings.getInstance(this@deleteAndQuit).deleteHistoryOnQuit) {
withContext(coroutineContext) {
this@deleteAndQuit.components.core.historyStorage.deleteEverything()
}
}
}
this@deleteAndQuit.asActivity()?.finish()
}
}