1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-08 20:12:39 +01:00

Require main thread for cookiemanager calls

This commit is contained in:
Allan Wang 2021-01-24 17:19:08 -08:00
parent e0f289862b
commit c2f7e2e0c6
No known key found for this signature in database
GPG Key ID: 69D90B885D405BDB

View File

@ -29,14 +29,14 @@ import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.cookies import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin import com.pitchedapps.frost.utils.launchLogin
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.dsl.module import org.koin.dsl.module
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/** /**
* Created by Allan Wang on 2017-05-30. * Created by Allan Wang on 2017-05-30.
@ -64,7 +64,10 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
val messengerCookie: String? val messengerCookie: String?
get() = CookieManager.getInstance().getCookie(HTTPS_MESSENGER_COM) get() = CookieManager.getInstance().getCookie(HTTPS_MESSENGER_COM)
private suspend fun CookieManager.suspendSetWebCookie(domain: String, cookie: String?): Boolean { private suspend fun CookieManager.suspendSetWebCookie(
domain: String,
cookie: String?
): Boolean {
cookie ?: return true cookie ?: return true
return withContext(NonCancellable) { return withContext(NonCancellable) {
// Save all cookies regardless of result, then check if all succeeded // Save all cookies regardless of result, then check if all succeeded
@ -100,11 +103,13 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
suspend fun reset() { suspend fun reset() {
prefs.userId = -1L prefs.userId = -1L
withContext(Dispatchers.Main + NonCancellable) {
with(CookieManager.getInstance()) { with(CookieManager.getInstance()) {
removeAllCookies() removeAllCookies()
flush() flush()
} }
} }
}
suspend fun switchUser(id: Long) { suspend fun switchUser(id: Long) {
val cookie = cookieDao.selectById(id) ?: return L.e { "No cookie for id" } val cookie = cookieDao.selectById(id) ?: return L.e { "No cookie for id" }
@ -117,7 +122,7 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
return return
} }
val currentId = prefs.userId val currentId = prefs.userId
withContext(Dispatchers.IO + NonCancellable) { withContext(Dispatchers.Main + NonCancellable) {
L.d { "Switching User" } L.d { "Switching User" }
// Save current messenger cookie state. // Save current messenger cookie state.
cookieDao.updateMessengerCookie(currentId, messengerCookie) cookieDao.updateMessengerCookie(currentId, messengerCookie)