diff --git a/app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt b/app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt index e158fdc88e39..995281772f05 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt @@ -2425,9 +2425,7 @@ class BrowserTabFragment : id: String?, data: JSONObject?, ) { - appCoroutineScope.launch(dispatchers.main()) { - viewModel.processJsCallbackMessage(featureName, method, id, data, it.url) - } + viewModel.processJsCallbackMessage(featureName, method, id, data, it.url) } }, ) @@ -2440,9 +2438,7 @@ class BrowserTabFragment : id: String?, data: JSONObject?, ) { - appCoroutineScope.launch(dispatchers.main()) { - viewModel.processJsCallbackMessage(featureName, method, id, data, it.url) - } + viewModel.processJsCallbackMessage(featureName, method, id, data, it.url) } }, ) diff --git a/app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt b/app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt index 077bd38972d9..a393d47955f4 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt @@ -3167,7 +3167,7 @@ class BrowserTabViewModel @Inject constructor( ) } - suspend fun processJsCallbackMessage( + fun processJsCallbackMessage( featureName: String, method: String, id: String?, @@ -3200,7 +3200,7 @@ class BrowserTabViewModel @Inject constructor( when (featureName) { DUCK_PLAYER_FEATURE_NAME, DUCK_PLAYER_PAGE_FEATURE_NAME -> { - withContext(dispatchers.io()) { + viewModelScope.launch(dispatchers.io()) { val response = duckPlayerJSHelper.processJsCallbackMessage(featureName, method, id, data, url) withContext(dispatchers.main()) { response?.let { @@ -3230,15 +3230,17 @@ class BrowserTabViewModel @Inject constructor( id: String, data: JSONObject, ) { - val response = if (url == null) { - getDataForPermissionState(featureName, method, id, SitePermissionQueryResponse.Denied) - } else { - val permissionState = sitePermissionsManager.getPermissionsQueryResponse(url!!, tabId, data.optString("name")) - getDataForPermissionState(featureName, method, id, permissionState) - } + viewModelScope.launch(dispatchers.io()) { + val response = if (url == null) { + getDataForPermissionState(featureName, method, id, SitePermissionQueryResponse.Denied) + } else { + val permissionState = sitePermissionsManager.getPermissionsQueryResponse(url!!, tabId, data.optString("name")) + getDataForPermissionState(featureName, method, id, permissionState) + } - viewModelScope.launch(dispatchers.main()) { - command.value = SendResponseToJs(response) + withContext(dispatchers.main()) { + command.value = SendResponseToJs(response) + } } } diff --git a/site-permissions/site-permissions-api/src/main/java/com/duckduckgo/site/permissions/api/SitePermissionsManager.kt b/site-permissions/site-permissions-api/src/main/java/com/duckduckgo/site/permissions/api/SitePermissionsManager.kt index 95d1645123d3..d1ec96e26736 100644 --- a/site-permissions/site-permissions-api/src/main/java/com/duckduckgo/site/permissions/api/SitePermissionsManager.kt +++ b/site-permissions/site-permissions-api/src/main/java/com/duckduckgo/site/permissions/api/SitePermissionsManager.kt @@ -45,7 +45,7 @@ interface SitePermissionsManager { * @param queriedPermission permission being queried (note: this is different from WebView permissions, check link above) * @return state of the permission as expected by the API: 'granted', 'prompt', or 'denied' */ - fun getPermissionsQueryResponse(url: String, tabId: String, queriedPermission: String): SitePermissionQueryResponse + suspend fun getPermissionsQueryResponse(url: String, tabId: String, queriedPermission: String): SitePermissionQueryResponse data class SitePermissions( val autoAccept: List, diff --git a/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerImpl.kt b/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerImpl.kt index 76498af0f9c3..549f78168131 100644 --- a/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerImpl.kt +++ b/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerImpl.kt @@ -35,7 +35,7 @@ class SitePermissionsManagerImpl @Inject constructor( private val dispatcherProvider: DispatcherProvider, ) : SitePermissionsManager { - private fun getSitePermissionsGranted( + private suspend fun getSitePermissionsGranted( url: String, tabId: String, resources: Array, @@ -85,7 +85,7 @@ class SitePermissionsManagerImpl @Inject constructor( } } - override fun getPermissionsQueryResponse( + override suspend fun getPermissionsQueryResponse( url: String, tabId: String, queriedPermission: String, diff --git a/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepository.kt b/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepository.kt index b784bab7369e..88dcf983ab31 100644 --- a/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepository.kt +++ b/site-permissions/site-permissions-impl/src/main/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepository.kt @@ -39,8 +39,8 @@ interface SitePermissionsRepository { var askCameraEnabled: Boolean var askMicEnabled: Boolean var askDrmEnabled: Boolean - fun isDomainAllowedToAsk(url: String, permission: String): Boolean - fun isDomainGranted(url: String, tabId: String, permission: String): Boolean + suspend fun isDomainAllowedToAsk(url: String, permission: String): Boolean + suspend fun isDomainGranted(url: String, tabId: String, permission: String): Boolean fun sitePermissionGranted(url: String, tabId: String, permission: String) fun sitePermissionsWebsitesFlow(): Flow> fun sitePermissionsForAllWebsites(): List @@ -85,7 +85,7 @@ class SitePermissionsRepositoryImpl @Inject constructor( private val drmSessions = mutableMapOf() - override fun isDomainAllowedToAsk(url: String, permission: String): Boolean { + override suspend fun isDomainAllowedToAsk(url: String, permission: String): Boolean { val domain = url.extractDomain() ?: url val sitePermissionsForDomain = sitePermissionsDao.getSitePermissionsByDomain(domain) return when (permission) { @@ -111,7 +111,7 @@ class SitePermissionsRepositoryImpl @Inject constructor( } } - override fun isDomainGranted(url: String, tabId: String, permission: String): Boolean { + override suspend fun isDomainGranted(url: String, tabId: String, permission: String): Boolean { val domain = url.extractDomain() ?: url val sitePermissionForDomain = sitePermissionsDao.getSitePermissionsByDomain(domain) val permissionAllowedEntity = sitePermissionsAllowedDao.getSitePermissionAllowed(domain, tabId, permission) diff --git a/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerTest.kt b/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerTest.kt index c713f64bde8d..f218f124fd33 100644 --- a/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerTest.kt +++ b/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsManagerTest.kt @@ -98,6 +98,7 @@ class SitePermissionsManagerTest { whenever(mockSitePermissionsRepository.isDomainAllowedToAsk(url, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(true) whenever(mockSitePermissionsRepository.isDomainAllowedToAsk(url, PermissionRequest.RESOURCE_AUDIO_CAPTURE)).thenReturn(false) whenever(mockPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)).thenReturn(true) + whenever(mockSitePermissionsRepository.isDomainGranted(url, tabId, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(false) val permissionRequest: PermissionRequest = mock() whenever(permissionRequest.origin).thenReturn(url.toUri()) @@ -167,14 +168,14 @@ class SitePermissionsManagerTest { } @Test - fun whenDomainGrantedThenGetPermissionsQueryResponseReturnsGranted() { + fun whenDomainGrantedThenGetPermissionsQueryResponseReturnsGranted() = runTest { whenever(mockSitePermissionsRepository.isDomainGranted(url, tabId, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(true) assertEquals(SitePermissionQueryResponse.Granted, testee.getPermissionsQueryResponse(url, tabId, "camera")) } @Test - fun whenDomainAllowedToAskThenGetPermissionsQueryResponseReturnsPrompt() { + fun whenDomainAllowedToAskThenGetPermissionsQueryResponseReturnsPrompt() = runTest { whenever(mockSitePermissionsRepository.isDomainGranted(url, tabId, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(false) whenever(mockPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)).thenReturn(true) whenever(mockSitePermissionsRepository.isDomainAllowedToAsk(url, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(true) @@ -183,7 +184,7 @@ class SitePermissionsManagerTest { } @Test - fun whenDomainNotAllowedToAskThenGetPermissionsQueryResponseReturnsDenied() { + fun whenDomainNotAllowedToAskThenGetPermissionsQueryResponseReturnsDenied() = runTest { whenever(mockSitePermissionsRepository.isDomainGranted(url, tabId, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(false) whenever(mockPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)).thenReturn(true) whenever(mockSitePermissionsRepository.isDomainAllowedToAsk(url, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(false) @@ -192,7 +193,7 @@ class SitePermissionsManagerTest { } @Test - fun whenHardwareNotSupportedThenGetPermissionsQueryResponseReturnsDenied() { + fun whenHardwareNotSupportedThenGetPermissionsQueryResponseReturnsDenied() = runTest { whenever(mockSitePermissionsRepository.isDomainGranted(url, tabId, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(false) whenever(mockPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)).thenReturn(false) whenever(mockSitePermissionsRepository.isDomainAllowedToAsk(url, PermissionRequest.RESOURCE_VIDEO_CAPTURE)).thenReturn(true) @@ -201,7 +202,7 @@ class SitePermissionsManagerTest { } @Test - fun whenAndroidPermissionNotSupportedThenGetPermissionsQueryResponseReturnsDenied() { + fun whenAndroidPermissionNotSupportedThenGetPermissionsQueryResponseReturnsDenied() = runTest { assertEquals(SitePermissionQueryResponse.Denied, testee.getPermissionsQueryResponse(url, tabId, "unsupported")) } } diff --git a/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepositoryTest.kt b/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepositoryTest.kt index 06c28422dc3d..66e75f1eafa8 100644 --- a/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepositoryTest.kt +++ b/site-permissions/site-permissions-impl/src/test/java/com/duckduckgo/site/permissions/impl/SitePermissionsRepositoryTest.kt @@ -63,7 +63,7 @@ class SitePermissionsRepositoryTest { private val domain = "domain.com" @Test - fun givenPermissionNotSupportedThenDomainIsNotAllowedToAsk() { + fun givenPermissionNotSupportedThenDomainIsNotAllowedToAsk() = runTest { setInitialSettings() val permission = PermissionRequest.RESOURCE_MIDI_SYSEX @@ -71,7 +71,7 @@ class SitePermissionsRepositoryTest { } @Test - fun givenPermissionSupportedThenDomainIsAllowedToAsk() { + fun givenPermissionSupportedThenDomainIsAllowedToAsk() = runTest { setInitialSettings() val permission = PermissionRequest.RESOURCE_AUDIO_CAPTURE @@ -79,7 +79,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenAskForPermissionIsDisabledThenDomainIsNotAllowedToAsk() { + fun whenAskForPermissionIsDisabledThenDomainIsNotAllowedToAsk() = runTest { setInitialSettings(cameraEnabled = false) val permission = PermissionRequest.RESOURCE_VIDEO_CAPTURE @@ -87,7 +87,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenAskForPermissionDisabledButSitePermissionSettingIsAlwaysAllowThenIsAllowedToAsk() { + fun whenAskForPermissionDisabledButSitePermissionSettingIsAlwaysAllowThenIsAllowedToAsk() = runTest { val testEntity = SitePermissionsEntity(domain, askMicSetting = SitePermissionAskSettingType.ALLOW_ALWAYS.name) setInitialSettings(micEnabled = false, sitePermissionEntity = testEntity) val permission = PermissionRequest.RESOURCE_AUDIO_CAPTURE @@ -96,7 +96,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenSitePermissionSettingIsDenyAlwaysThenDomainIsNotAllowedToAsk() { + fun whenSitePermissionSettingIsDenyAlwaysThenDomainIsNotAllowedToAsk() = runTest { val testEntity = SitePermissionsEntity(domain, askCameraSetting = SitePermissionAskSettingType.DENY_ALWAYS.name) setInitialSettings(sitePermissionEntity = testEntity) val permission = PermissionRequest.RESOURCE_VIDEO_CAPTURE @@ -105,7 +105,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenNoSitePermissionSettingAndDrmBlockedThenDomainIsNotAllowedToAsk() { + fun whenNoSitePermissionSettingAndDrmBlockedThenDomainIsNotAllowedToAsk() = runTest { val permission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID whenever(mockDrmBlock.isDrmBlockedForUrl(url)).thenReturn(true) @@ -114,7 +114,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenSitePermissionSettingIsAskAndDrmBlockedThenDomainIsAllowedToAsk() { + fun whenSitePermissionSettingIsAskAndDrmBlockedThenDomainIsAllowedToAsk() = runTest { val testEntity = SitePermissionsEntity(domain, askDrmSetting = SitePermissionAskSettingType.ASK_EVERY_TIME.name) setInitialSettings(sitePermissionEntity = testEntity) val permission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID @@ -125,7 +125,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenSitePermissionsWasGrantedWithin24hThenReturnPermissionGranted() { + fun whenSitePermissionsWasGrantedWithin24hThenReturnPermissionGranted() = runTest { setInitialSettings() val permission = PermissionRequest.RESOURCE_VIDEO_CAPTURE val tabId = "tabId" @@ -136,7 +136,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenSitePermissionsWasMoreThen24hAgoThenReturnPermissionNotGranted() { + fun whenSitePermissionsWasMoreThen24hAgoThenReturnPermissionNotGranted() = runTest { setInitialSettings() val permission = PermissionRequest.RESOURCE_VIDEO_CAPTURE val tabId = "tabId" @@ -147,7 +147,7 @@ class SitePermissionsRepositoryTest { } @Test - fun whenSitePermissionsSettingIsAllowAlwaysThenReturnPermissionGranted() { + fun whenSitePermissionsSettingIsAllowAlwaysThenReturnPermissionGranted() = runTest { val testEntity = SitePermissionsEntity(domain, askCameraSetting = SitePermissionAskSettingType.ALLOW_ALWAYS.name) setInitialSettings(sitePermissionEntity = testEntity) val permission = PermissionRequest.RESOURCE_VIDEO_CAPTURE @@ -264,7 +264,7 @@ class SitePermissionsRepositoryTest { micEnabled: Boolean = true, drmEnabled: Boolean = true, sitePermissionEntity: SitePermissionsEntity? = null, - ) { + ) = runTest { whenever(mockSitePermissionsPreferences.askCameraEnabled).thenReturn(cameraEnabled) whenever(mockSitePermissionsPreferences.askMicEnabled).thenReturn(micEnabled) whenever(mockSitePermissionsPreferences.askDrmEnabled).thenReturn(drmEnabled) diff --git a/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissions/SitePermissionsDao.kt b/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissions/SitePermissionsDao.kt index 451682858e57..ebccee9e3050 100644 --- a/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissions/SitePermissionsDao.kt +++ b/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissions/SitePermissionsDao.kt @@ -36,7 +36,7 @@ interface SitePermissionsDao { fun getAllSitesPermissionsAsFlow(): Flow> @Query("select * from site_permissions where domain = :domain") - fun getSitePermissionsByDomain(domain: String): SitePermissionsEntity? + suspend fun getSitePermissionsByDomain(domain: String): SitePermissionsEntity? @Delete fun delete(sitePermissionsEntity: SitePermissionsEntity): Int diff --git a/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissionsallowed/SitePermissionsAllowedDao.kt b/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissionsallowed/SitePermissionsAllowedDao.kt index bd0a495eeb0a..e2a1742c9bdb 100644 --- a/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissionsallowed/SitePermissionsAllowedDao.kt +++ b/site-permissions/site-permissions-store/src/main/java/com/duckduckgo/site/permissions/store/sitepermissionsallowed/SitePermissionsAllowedDao.kt @@ -33,7 +33,7 @@ interface SitePermissionsAllowedDao { fun getAllSitesPermissionsAllowedAsFlow(): Flow> @Query("select * from site_permission_allowed where domain = :domain and tabId = :tabId and permissionAllowed = :permissionAllowed") - fun getSitePermissionAllowed(domain: String, tabId: String, permissionAllowed: String): SitePermissionAllowedEntity? + suspend fun getSitePermissionAllowed(domain: String, tabId: String, permissionAllowed: String): SitePermissionAllowedEntity? @Delete fun delete(sitePermissionsEntity: SitePermissionAllowedEntity): Int