Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Bug 1882404 - Expose granted permissions on AC. #5793

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ class GeckoWebExtension(
version = it.version,
permissions = it.permissions.toList(),
optionalPermissions = it.optionalPermissions.toList(),
grantedOptionalPermissions = it.grantedOptionalPermissions.toList(),
grantedOptionalOrigins = it.grantedOptionalOrigins.toList(),
optionalOrigins = it.optionalOrigins.toList(),
// Origins is marked as @NonNull but may be null: https://bugzilla.mozilla.org/show_bug.cgi?id=1629957
hostPermissions = it.origins.orEmpty().toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ class GeckoWebExtensionTest {
temporary = true,
permissions = arrayOf("p1", "p2"),
optionalPermissions = arrayOf("clipboardRead"),
grantedOptionalPermissions = arrayOf("clipboardRead"),
optionalOrigins = arrayOf("*://*.example.com/*", "*://opt-host-perm.example.com/*"),
grantedOptionalOrigins = arrayOf("*://*.example.com/*"),
fullDescription = "fullDescription",
downloadUrl = "downloadUrl",
reviewUrl = "reviewUrl",
Expand All @@ -432,7 +434,9 @@ class GeckoWebExtensionTest {

assertEquals("1.0", metadata.version)
assertEquals(listOf("clipboardRead"), metadata.optionalPermissions)
assertEquals(listOf("clipboardRead"), metadata.grantedOptionalPermissions)
assertEquals(listOf("*://*.example.com/*", "*://opt-host-perm.example.com/*"), metadata.optionalOrigins)
assertEquals(listOf("*://*.example.com/*"), metadata.grantedOptionalOrigins)
assertEquals(listOf("p1", "p2"), metadata.permissions)
assertEquals(listOf("o1", "o2"), metadata.hostPermissions)
assertEquals("desc", metadata.description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ fun mockNativeWebExtensionMetaData(
icon: Image = mock(),
permissions: Array<String> = emptyArray(),
optionalPermissions: Array<String> = emptyArray(),
grantedOptionalPermissions: Array<String> = emptyArray(),
grantedOptionalOrigins: Array<String> = emptyArray(),
optionalOrigins: Array<String> = emptyArray(),
origins: Array<String> = emptyArray(),
name: String? = null,
Expand Down Expand Up @@ -63,7 +65,9 @@ fun mockNativeWebExtensionMetaData(
ReflectionUtils.setField(metadata, "icon", icon)
ReflectionUtils.setField(metadata, "permissions", permissions)
ReflectionUtils.setField(metadata, "optionalPermissions", optionalPermissions)
ReflectionUtils.setField(metadata, "grantedOptionalPermissions", grantedOptionalPermissions)
ReflectionUtils.setField(metadata, "optionalOrigins", optionalOrigins)
ReflectionUtils.setField(metadata, "grantedOptionalOrigins", grantedOptionalOrigins)
ReflectionUtils.setField(metadata, "origins", origins)
ReflectionUtils.setField(metadata, "name", name)
ReflectionUtils.setField(metadata, "description", description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,23 @@ data class Metadata(
*/
val optionalPermissions: List<String>,

/**
* Optional permissions granted to this extension:
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions
*/
val grantedOptionalPermissions: List<String>,

/**
* Optional origin permissions requested or granted to this extension:
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions
*/
val optionalOrigins: List<String>,

/**
* Optional origin permissions granted to this extension:
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions
*/
val grantedOptionalOrigins: List<String>,
/**
* Required host permissions:
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ data class Addon(
val downloadUrl: String = "",
val version: String = "",
val permissions: List<String> = emptyList(),
val optionalPermissions: List<String> = emptyList(),
val optionalOrigins: List<String> = emptyList(),
val optionalPermissions: List<Permission> = emptyList(),
val optionalOrigins: List<Permission> = emptyList(),
val translatableName: Map<String, String> = emptyMap(),
val translatableDescription: Map<String, String> = emptyMap(),
val translatableSummary: Map<String, String> = emptyMap(),
Expand Down Expand Up @@ -112,6 +112,19 @@ data class Addon(
val reviews: Int,
) : Parcelable

/**
* Required or optional permission.
*
* @property name The name of this permission.
* @property granted Indicate if this permission is granted or not.
*/
@SuppressLint("ParcelCreator")
@Parcelize
data class Permission(
val name: String,
val granted: Boolean,
) : Parcelable

/**
* Returns a list of id resources per each item on the [Addon.permissions] list.
* Holds the state of the installed web extension of this add-on.
Expand Down Expand Up @@ -350,13 +363,29 @@ data class Addon(
else -> Incognito.SPANNING
}

val grantedOptionalPermissions = metadata?.grantedOptionalPermissions ?: emptyList()
val grantedOptionalOrigins = metadata?.grantedOptionalOrigins ?: emptyList()
val optionalPermissions = metadata?.optionalPermissions?.map { permission ->
Permission(
name = permission,
granted = grantedOptionalPermissions.contains(permission),
)
} ?: emptyList()

val optionalOrigins = metadata?.optionalOrigins?.map { origin ->
Permission(
name = origin,
granted = grantedOptionalOrigins.contains(origin),
)
} ?: emptyList()

return Addon(
id = extension.id,
author = author,
version = metadata?.version.orEmpty(),
permissions = permissions,
optionalPermissions = metadata?.optionalPermissions.orEmpty(),
optionalOrigins = metadata?.optionalOrigins.orEmpty(),
optionalPermissions = optionalPermissions,
optionalOrigins = optionalOrigins,
downloadUrl = metadata?.downloadUrl.orEmpty(),
rating = Rating(averageRating, reviewCount),
homepageUrl = homepageUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ class AddonTest {
whenever(metadata.version).thenReturn(version)
whenever(metadata.permissions).thenReturn(permissions)
whenever(metadata.optionalPermissions).thenReturn(listOf("clipboardRead"))
whenever(metadata.grantedOptionalPermissions).thenReturn(listOf("clipboardRead"))
whenever(metadata.optionalOrigins).thenReturn(listOf("*://*.example.com/*", "*://opt-host-perm.example.com/*"))
whenever(metadata.grantedOptionalOrigins).thenReturn(listOf("*://*.example.com/*"))
whenever(metadata.hostPermissions).thenReturn(hostPermissions)
whenever(metadata.name).thenReturn(name)
whenever(metadata.description).thenReturn(description)
Expand All @@ -388,8 +390,14 @@ class AddonTest {
assertEquals("some-url", addon.homepageUrl)
assertEquals("some-download-url", addon.downloadUrl)
assertEquals(permissions + hostPermissions, addon.permissions)
assertEquals(listOf("clipboardRead"), addon.optionalPermissions)
assertEquals(listOf("*://*.example.com/*", "*://opt-host-perm.example.com/*"), addon.optionalOrigins)
assertEquals(
listOf(Addon.Permission(name = "clipboardRead", granted = true)),
addon.optionalPermissions,
)
assertEquals(
listOf(Addon.Permission(name = "*://*.example.com/*", granted = true), Addon.Permission(name = "*://opt-host-perm.example.com/*", granted = false)),
addon.optionalOrigins,
)
assertEquals("", addon.updatedAt)
assertEquals("some name", addon.translatableName[Addon.DEFAULT_LOCALE])
assertEquals("fullDescription", addon.translatableDescription[Addon.DEFAULT_LOCALE])
Expand Down
Loading