This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2315: Adding support for site configurations permissions
- Loading branch information
Showing
4 changed files
with
338 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...missions/src/main/java/mozilla/components/feature/sitepermissions/SitePermissionsRules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* 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 mozilla.components.feature.sitepermissions | ||
|
||
import android.net.Uri | ||
import mozilla.components.concept.engine.permission.Permission | ||
import mozilla.components.concept.engine.permission.PermissionRequest | ||
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.ASK_TO_ALLOW | ||
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.BLOCKED | ||
|
||
/** | ||
* Indicate how site permissions must behave by permission category. | ||
*/ | ||
data class SitePermissionsRules( | ||
val camera: Action, | ||
val location: Action, | ||
val notification: Action, | ||
val microphone: Action, | ||
val exceptions: List<Uri>? = null | ||
) { | ||
enum class Action { | ||
BLOCKED, ASK_TO_ALLOW; | ||
} | ||
|
||
internal fun getActionFrom(request: PermissionRequest): Action { | ||
return if (request.containsVideoAndAudioSources()) { | ||
getActionForCombinedPermission() | ||
} else { | ||
getActionForSinglePermission(request.permissions.first()) | ||
} | ||
} | ||
|
||
internal fun isHostInExceptions(host: String): Boolean { | ||
if (exceptions == null || exceptions.isEmpty()) { | ||
return false | ||
} | ||
|
||
return exceptions.any { | ||
it.host == host | ||
} | ||
} | ||
|
||
private fun getActionForSinglePermission(permission: Permission): Action { | ||
return when (permission) { | ||
is Permission.ContentGeoLocation -> { | ||
location | ||
} | ||
is Permission.ContentNotification -> { | ||
notification | ||
} | ||
is Permission.ContentAudioCapture, is Permission.ContentAudioMicrophone -> { | ||
microphone | ||
} | ||
is Permission.ContentVideoCamera, is Permission.ContentVideoCapture -> { | ||
camera | ||
} | ||
else -> ASK_TO_ALLOW | ||
} | ||
} | ||
|
||
private fun getActionForCombinedPermission(): Action { | ||
return if (camera == BLOCKED || microphone == BLOCKED) { | ||
BLOCKED | ||
} else { | ||
ASK_TO_ALLOW | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.