diff --git a/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtension.kt b/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtension.kt index 99f9ec17dd8..b4465e215d8 100644 --- a/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtension.kt +++ b/android-components/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtension.kt @@ -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(), diff --git a/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtensionTest.kt b/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtensionTest.kt index 09cd75105f2..dd53da92bb9 100644 --- a/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtensionTest.kt +++ b/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/GeckoWebExtensionTest.kt @@ -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", @@ -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) diff --git a/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/MockWebExtension.kt b/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/MockWebExtension.kt index 8784cda8bea..636b0c36cb3 100644 --- a/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/MockWebExtension.kt +++ b/android-components/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/webextension/MockWebExtension.kt @@ -33,6 +33,8 @@ fun mockNativeWebExtensionMetaData( icon: Image = mock(), permissions: Array = emptyArray(), optionalPermissions: Array = emptyArray(), + grantedOptionalPermissions: Array = emptyArray(), + grantedOptionalOrigins: Array = emptyArray(), optionalOrigins: Array = emptyArray(), origins: Array = emptyArray(), name: String? = null, @@ -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) diff --git a/android-components/components/concept/engine/src/main/java/mozilla/components/concept/engine/webextension/WebExtension.kt b/android-components/components/concept/engine/src/main/java/mozilla/components/concept/engine/webextension/WebExtension.kt index e9c1d7bc698..de5077dda1d 100644 --- a/android-components/components/concept/engine/src/main/java/mozilla/components/concept/engine/webextension/WebExtension.kt +++ b/android-components/components/concept/engine/src/main/java/mozilla/components/concept/engine/webextension/WebExtension.kt @@ -364,12 +364,23 @@ data class Metadata( */ val optionalPermissions: List, + /** + * Optional permissions granted to this extension: + * https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions + */ + val grantedOptionalPermissions: List, + /** * 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, + /** + * 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, /** * Required host permissions: * https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions diff --git a/android-components/components/feature/addons/src/main/java/mozilla/components/feature/addons/Addon.kt b/android-components/components/feature/addons/src/main/java/mozilla/components/feature/addons/Addon.kt index d1b4e1bdf31..5f0cefcabed 100644 --- a/android-components/components/feature/addons/src/main/java/mozilla/components/feature/addons/Addon.kt +++ b/android-components/components/feature/addons/src/main/java/mozilla/components/feature/addons/Addon.kt @@ -61,8 +61,8 @@ data class Addon( val downloadUrl: String = "", val version: String = "", val permissions: List = emptyList(), - val optionalPermissions: List = emptyList(), - val optionalOrigins: List = emptyList(), + val optionalPermissions: List = emptyList(), + val optionalOrigins: List = emptyList(), val translatableName: Map = emptyMap(), val translatableDescription: Map = emptyMap(), val translatableSummary: Map = emptyMap(), @@ -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. @@ -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, diff --git a/android-components/components/feature/addons/src/test/java/AddonTest.kt b/android-components/components/feature/addons/src/test/java/AddonTest.kt index 0b5298ac878..51d5d4a26c2 100644 --- a/android-components/components/feature/addons/src/test/java/AddonTest.kt +++ b/android-components/components/feature/addons/src/test/java/AddonTest.kt @@ -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) @@ -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])