Skip to content

Commit

Permalink
Fix leftover merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
simond-stripe committed Nov 19, 2024
1 parent cbfb412 commit 484a84b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 62 deletions.

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.stripe.android.connect.webview

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.webkit.JavascriptInterface
import android.webkit.PermissionRequest
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import androidx.annotation.RestrictTo
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.core.view.doOnAttach
import androidx.core.view.doOnDetach
import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.stripe.android.connect.BuildConfig
Expand All @@ -28,9 +35,12 @@ import com.stripe.android.connect.webview.serialization.SetterMessage
import com.stripe.android.connect.webview.serialization.toJs
import com.stripe.android.core.Logger
import com.stripe.android.core.version.StripeSdkVersion
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.encodeToJsonElement
Expand Down Expand Up @@ -79,6 +89,16 @@ internal class StripeConnectWebViewContainerImpl(
@VisibleForTesting
internal val stripeWebViewClient = StripeConnectWebViewClient()

@VisibleForTesting
internal val stripeWebChromeContainer = StripeConnectWebChromeClient(
context = webView?.context ?: throw IllegalStateException("WebView is not initialized"),
embeddedComponentManager = embeddedComponentManager
?: throw IllegalStateException("EmbeddedComponentManager is not initialized"),
viewScope = {
webView?.findViewTreeLifecycleOwner()?.lifecycleScope ?: throw IllegalStateException("View is not attached")
}
)

private var controller: StripeConnectWebViewContainerController? = null

init {
Expand All @@ -105,6 +125,7 @@ internal class StripeConnectWebViewContainerImpl(
internal fun initializeWebView(webView: WebView) {
with(webView) {
webViewClient = stripeWebViewClient
webChromeClient = stripeWebChromeContainer
settings.apply {
@SuppressLint("SetJavaScriptEnabled")
javaScriptEnabled = true
Expand Down Expand Up @@ -210,6 +231,48 @@ internal class StripeConnectWebViewContainerImpl(
}
}

internal class StripeConnectWebChromeClient(
private val context: Context,
private val embeddedComponentManager: EmbeddedComponentManager,
private val viewScope: () -> LifecycleCoroutineScope,
) : WebChromeClient() {

private val inProgressRequests: MutableMap<PermissionRequest, Job> = mutableMapOf()

override fun onPermissionRequest(request: PermissionRequest) {
// we only care about camera permissions at this time (video/audio)
val permissionsRequested = request.resources.filter {
it in listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOURCE_AUDIO_CAPTURE)
}.toTypedArray()
if (permissionsRequested.isEmpty()) {
request.deny() // no supported permissions were requested, so reject the request
return
}

if (checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
request.grant(permissionsRequested)
} else {
val job = viewScope().launch {
val isGranted = embeddedComponentManager.requestCameraPermission()
withContext(Dispatchers.Main) {
if (isGranted) {
request.grant(permissionsRequested)
} else {
request.deny()
}
}
inProgressRequests.remove(request)
}
inProgressRequests[request] = job
}
}

override fun onPermissionRequestCanceled(request: PermissionRequest?) {
if (request == null) return
inProgressRequests.remove(request)?.also { it.cancel() }
}
}

private inner class StripeJsInterface {
@JavascriptInterface
fun debug(message: String) {
Expand Down

0 comments on commit 484a84b

Please sign in to comment.