Commit 788c945b authored by James Hugman's avatar James Hugman Committed by jhugman
Browse files

Fixes #3725 - Intercept app-links with whitelisted schemes

parent 471f0cde
......@@ -40,7 +40,8 @@ class AppLinksFeature(
private val context: Context,
private val sessionManager: SessionManager,
private val sessionId: String? = null,
private val interceptLinkClicks: Boolean = true,
private val interceptLinkClicks: Boolean = false,
private val whitelistedSchemes: Set<String> = setOf("mailto", "market", "sms", "tel"),
private val fragmentManager: FragmentManager? = null,
private var dialog: RedirectDialogFragment = SimpleRedirectDialogFragment.newInstance(),
private val useCases: AppLinksUseCases = AppLinksUseCases(context)
......@@ -62,7 +63,7 @@ class AppLinksFeature(
* Starts observing app links on the selected session.
*/
override fun start() {
if (interceptLinkClicks) {
if (interceptLinkClicks || whitelistedSchemes.isNotEmpty()) {
observer.observeIdOrSelected(sessionId)
}
findPreviousDialogFragment()?.let {
......@@ -71,7 +72,7 @@ class AppLinksFeature(
}
override fun stop() {
if (interceptLinkClicks) {
if (interceptLinkClicks || whitelistedSchemes.isNotEmpty()) {
observer.stop()
}
}
......@@ -97,6 +98,12 @@ class AppLinksFeature(
return
}
redirect.appIntent?.data?.scheme?.let {
if (!interceptLinkClicks && !whitelistedSchemes.contains(it)) {
return
}
}
val doOpenApp = {
useCases.openAppLink(redirect)
}
......
......@@ -93,7 +93,8 @@ class AppLinksFeatureTest {
mockContext,
mockSessionManager,
useCases = mockUseCases,
interceptLinkClicks = false
interceptLinkClicks = false,
whitelistedSchemes = setOf()
)
subject.start()
......@@ -118,6 +119,28 @@ class AppLinksFeatureTest {
verifyNoMoreInteractions(mockOpenRedirect)
}
@Test
fun `it tests for whitelisted schemes links when triggered by user clicking on a link`() {
val whitelistedScheme = "whitelisted"
val session = createSession(false)
val subject = AppLinksFeature(
mockContext,
mockSessionManager,
interceptLinkClicks = false,
whitelistedSchemes = setOf(whitelistedScheme),
useCases = mockUseCases
)
val url = "$whitelistedScheme://example.com"
val whitelistedRedirect = AppLinkRedirect(Intent.parseUri(url, 0), url, false)
`when`(mockGetRedirect.invoke(url)).thenReturn(whitelistedRedirect)
subject.handleLoadRequest(session, url, true)
verify(mockGetRedirect).invoke(url)
verify(mockOpenRedirect).invoke(whitelistedRedirect)
}
@Test
fun `when valid sessionId is provided, observe its session`() {
feature = AppLinksFeature(
......@@ -147,6 +170,21 @@ class AppLinksFeatureTest {
verify(mockSessionManager).register(feature.observer)
}
@Test
fun `when a whitelist of schemes is provided, observe the selected session`() {
feature = AppLinksFeature(
mockContext,
sessionManager = mockSessionManager,
useCases = mockUseCases,
interceptLinkClicks = false,
whitelistedSchemes = setOf("whitelisted")
)
feature.start()
verify(mockSessionManager).register(feature.observer)
}
@Test
fun `when start is called must register SessionManager observers`() {
feature.start()
......
......@@ -18,6 +18,9 @@ permalink: /changelog/
* **feature-downloads**
* Added custom notification icon for `FetchDownloadManager`.
* **feature-app-links**
* Added whitelist for schemes of URLs to open with an external app. This defaults to `mailto`, `market`, `sms` and `tel`.
* **feature-accounts**
* ⚠️ **This is a breaking change**: Public API for interacting with `FxaAccountManager` and sync changes
* `FxaAccountManager` now has a new, simplified public API.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment