Coverage Summary for Class: EmbeddedBlockContentController (cloud.mindbox.mobile_sdk.embedded)

Class Method, % Branch, % Line, % Instruction, %
EmbeddedBlockContentController 100% (100/100) 83.5% (552/661) 99.6% (472/474) 94.5% (3009/3184)
EmbeddedBlockContentController$1 100% (1/1) 100% (1/1) 100% (5/5)
EmbeddedBlockContentController$1$1 100% (1/1) 100% (2/2) 100% (1/1) 100% (8/8)
EmbeddedBlockContentController$2 100% (1/1) 100% (1/1) 100% (2/2)
EmbeddedBlockContentController$3 100% (1/1) 50% (1/2) 100% (1/1) 50% (4/8)
EmbeddedBlockContentController$4 0% (0/1) 0% (0/2) 0% (0/1) 0% (0/10)
EmbeddedBlockContentController$5 100% (1/1) 100% (1/1) 100% (6/6)
EmbeddedBlockContentController$5$1 100% (1/1) 50% (3/6) 100% (1/1) 63.2% (12/19)
EmbeddedBlockContentController$6 0% (0/1) 0% (0/1) 0% (0/1)
EmbeddedBlockContentController$7 100% (1/1) 100% (1/1) 100% (10/10)
EmbeddedBlockContentController$7$1 100% (1/1) 75% (3/4) 100% (1/1) 88.9% (16/18)
EmbeddedBlockContentController$answerForRebuild$counted$1 100% (1/1) 100% (1/1) 100% (6/6)
EmbeddedBlockContentController$Companion 100% (1/1) 75% (3/4) 100% (6/6) 87% (20/23)
EmbeddedBlockContentController$configBudget$1 100% (1/1) 100% (1/1) 100% (3/3)
EmbeddedBlockContentController$DataPush 100% (1/1) 100% (1/1) 100% (12/12)
EmbeddedBlockContentController$dropProvider$1$1 100% (1/1) 100% (1/1) 100% (3/3)
EmbeddedBlockContentController$notifyContentDropped$1 100% (1/1) 100% (2/2) 100% (1/1) 100% (14/14)
EmbeddedBlockContentController$PageDescriptor 100% (2/2) 75% (3/4) 100% (6/6) 100% (40/40)
EmbeddedBlockContentController$ParkedAnswer
EmbeddedBlockContentController$ParkedAnswer$ConfigUnavailable 100% (1/1) 100% (1/1) 100% (12/12)
EmbeddedBlockContentController$ParkedAnswer$Resolved 100% (1/1) 100% (2/2) 100% (2/2) 100% (24/24)
EmbeddedBlockContentController$pushPageData$onResult$1 100% (2/2) 81.8% (18/22) 100% (21/21) 93.1% (122/131)
EmbeddedBlockContentController$readyBudget$1 100% (1/1) 100% (1/1) 100% (3/3)
EmbeddedBlockContentController$recreateProvider$created$1 100% (1/1) 100% (1/1) 100% (11/11)
EmbeddedBlockContentController$refreshMetricsSnapshot$1 100% (1/1) 100% (1/1) 100% (10/10)
EmbeddedBlockContentController$release$1$1 100% (1/1) 100% (1/1) 100% (3/3)
EmbeddedBlockContentController$report$1$1 100% (1/1) 100% (1/1) 100% (6/6)
EmbeddedBlockContentController$sendFailure$1 100% (1/1) 100% (6/6) 100% (22/22)
EmbeddedBlockContentController$sendWaitBudgetExceeded$1 100% (1/1) 100% (1/1) 100% (9/9)
EmbeddedBlockContentController$stopWaitingForDi$1 100% (1/1) 100% (1/1) 100% (6/6)
EmbeddedBlockContentController$waitForDi$1 100% (1/1) 100% (3/3) 100% (14/14)
EmbeddedBlockContentController$waitForDi$1$1 0% (0/2) 0% (0/12) 0% (0/4) 0% (0/38)
EmbeddedBlockContentController$WhenMappings
Total 97% (129/133) 81.5% (589/723) 98.5% (537/545) 93.2% (3412/3661)


 package cloud.mindbox.mobile_sdk.embedded
 
 import android.app.Activity
 import android.os.Handler
 import android.os.Looper
 import android.os.SystemClock
 import android.view.View
 import cloud.mindbox.mobile_sdk.Mindbox
 import cloud.mindbox.mobile_sdk.di.MindboxDI
 import cloud.mindbox.mobile_sdk.gatedTags
 import cloud.mindbox.mobile_sdk.inapp.data.managers.SEND_INAPP_TAGS_FEATURE
 import cloud.mindbox.mobile_sdk.inapp.domain.extensions.sendBlockFailureWithContext
 import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.interactors.PlaceShowReservation
 import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.InAppFailureTracker
 import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.WaitBudgetPhase
 import cloud.mindbox.mobile_sdk.inapp.domain.models.InAppType
 import cloud.mindbox.mobile_sdk.inapp.domain.models.Layer
 import cloud.mindbox.mobile_sdk.embedded.webview.DataPushOutcome
 import cloud.mindbox.mobile_sdk.embedded.webview.EmbeddedUpdatableContentProvider
 import cloud.mindbox.mobile_sdk.logger.mindboxLogE
 import cloud.mindbox.mobile_sdk.logger.mindboxLogI
 import cloud.mindbox.mobile_sdk.logger.mindboxLogW
 import cloud.mindbox.mobile_sdk.managers.LifecycleManager
 import cloud.mindbox.mobile_sdk.models.Milliseconds
 import cloud.mindbox.mobile_sdk.models.PlaceKey
 import cloud.mindbox.mobile_sdk.models.SessionEpoch
 import cloud.mindbox.mobile_sdk.models.operation.request.FailureReason
 import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
 import cloud.mindbox.mobile_sdk.utils.Constants
 import cloud.mindbox.mobile_sdk.utils.loggingRunCatching
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.flow.launchIn
 import kotlinx.coroutines.flow.onEach
 import java.io.Closeable
 
 internal class EmbeddedBlockContentController(
     placeSystemName: String? = null,
     configTimeout: Milliseconds = Constants.Embedded.defaultConfigTimeout,
     private val readyTimeout: Milliseconds = Constants.WebView.readyTimeout,
     private val providerFactory: (InAppType.Embedded, Milliseconds) -> EmbeddedContentProvider?,
     private val blocksRegistry: () -> EmbeddedBlocksRegistry? = {
         loggingRunCatching(defaultValue = null) {
             if (MindboxDI.isInitialized()) MindboxDI.appModule.embeddedBlocksRegistry else null
         }
     },
     private val monotonicNow: () -> Milliseconds = { Milliseconds(SystemClock.elapsedRealtime()) },
     private val failureTracker: () -> InAppFailureTracker? = {
         if (MindboxDI.isInitialized()) MindboxDI.appModule.inAppFailureTracker else null
     },
     private val isTagsFeatureEnabled: () -> Boolean = {
         MindboxDI.isInitialized() && MindboxDI.appModule.featureToggleManager.isEnabled(SEND_INAPP_TAGS_FEATURE)
     },
     private val hasConfig: () -> Boolean = {
         loggingRunCatching(defaultValue = false) {
             MindboxDI.isInitialized() && MindboxDI.appModule.mobileConfigRepositoryIfCreated?.hasConfig() == true
         }
     },
     private val hostActivity: () -> Activity? = { null },
     private val isAppInForeground: () -> Boolean = {
         loggingRunCatching(defaultValue = true) { LifecycleManager.instance?.isInForegroundBesides(hostActivity()) != false }
     },
 ) : EmbeddedBlockHandle {
 
     private val placeSystemName: PlaceKey? = placeSystemName?.takeIf { it.isNotBlank() }?.let(PlaceKey::of)
 
     var onStateChange: ((EmbeddedBlockState) -> Unit)? = null
 
     val contentView: View?
         get() = provider?.contentView
 
     override val isActive: Boolean
         get() = isStarted && !isReleased && !hasGivenUp
 
     override val isPausedForBackground: Boolean
         get() = isAwayInBackground && !isStarted && !isReleased
 
     override val isLeftBehind: Boolean
         get() = leftBehind && holdsLiveContent && !isStarted && !isReleased
 
     private var provider: EmbeddedContentProvider? = null
     private val updatablePage: EmbeddedUpdatableContentProvider?
         get() = provider as? EmbeddedUpdatableContentProvider
     private var failureTrackerCache: InAppFailureTracker? = null
     private var isStarted = false
     private var isReleased = false
     private var hasGivenUp = false
     private var isAwayInBackground = false
     private var leftBehind = false
 
     var lastReportedState: EmbeddedBlockState? = null
         private set
 
     var isPageCovered = false
         private set
     private var isNewDataSentToCoveredPage = false
     private var wasCoverReported = false
 
     val isRetainable: Boolean
         get() = !isReleased && heldOutcome == null &&
             (lastReportedState == EmbeddedBlockState.Ready || isPageCovered) && provider?.contentView != null
 
     val heldOutcome: EmbeddedBlockState?
         get() = heldCollapse ?: EmbeddedBlockState.Empty.takeIf { isShownPageEmptied && !hasParkedShowableContent }
 
     private val isShownPageEmptied: Boolean
         get() = updatablePage?.rendersNothing == true && lastReportedState?.nothingToShow != true
 
     private var registration: Closeable? = null
     private var configJob: Job? = null
 
     private sealed interface ParkedAnswer {
         val answer: EmbeddedPlaceAnswer
         val selectedInAppId: String?
 
         class Resolved(val content: InAppType.Embedded?, override val answer: EmbeddedPlaceAnswer) : ParkedAnswer {
             override val selectedInAppId: String? = content?.inAppId
         }
 
         class ConfigUnavailable(override val answer: EmbeddedPlaceAnswer, override val selectedInAppId: String?) : ParkedAnswer
     }
 
     private var parkedAnswer: ParkedAnswer? = null
 
     private var deferredParkedMayHold: Boolean? = null
 
     private val hasPendingContent: Boolean
         get() = (parkedAnswer as? ParkedAnswer.Resolved)?.content != null
 
     private val hasParkedShowableContent: Boolean
         get() = (parkedAnswer as? ParkedAnswer.Resolved)?.content.isShowable
 
     private val hasNoContentComing: Boolean
         get() = provider == null && !hasPendingContent
 
     private var heldCollapse: EmbeddedBlockState? = null
 
     private var appliedSessionEpoch: SessionEpoch? = null
 
     private class DataPush(val descriptor: PageDescriptor, val sessionEpoch: SessionEpoch?)
 
     private var dataPush: DataPush? = null
 
     private val liveDataPush: DataPush?
         get() = dataPush?.takeUnless { updatablePage?.isDataPushAbandoned == true }
 
     private val pageSessionEpoch: SessionEpoch?
         get() = liveDataPush?.sessionEpoch ?: appliedSessionEpoch
 
     private var dueSessionRefresh: EmbeddedPlaceAnswer? = null
 
     private var appliedDescriptor: PageDescriptor? = null
 
     private var appliedContent: InAppType.Embedded? = null
 
     private data class PageDescriptor(
         val inAppId: String,
         val baseUrl: String?,
         val contentUrl: String?,
         val params: Map<String, String>,
     ) {
         fun isSamePage(other: PageDescriptor): Boolean =
             inAppId == other.inAppId && baseUrl == other.baseUrl && contentUrl == other.contentUrl
     }
 
     private fun descriptorOf(inAppId: String, layer: Layer.WebViewLayer): PageDescriptor =
         PageDescriptor(
             inAppId = inAppId,
             baseUrl = layer.baseUrl,
             contentUrl = layer.contentUrl,
             params = layer.params,
         )
 
     private var updateEpoch = 0
 
     private var attemptStartTick: Milliseconds? = null
 
     private var pendingDeliverySession: SessionEpoch? = null
 
     private val hasPendingDelivery: Boolean
         get() = pendingDeliverySession != null
 
     private val mainHandler = Handler(Looper.getMainLooper())
 
     private val configWaitDuration: Milliseconds = sanitizedConfigTimeout(configTimeout, this.placeSystemName)
 
     private val configBudget = EmbeddedBlockWaitBudget(configWaitDuration, mainHandler) { onConfigTimeout() }
     private val readyBudget = EmbeddedBlockWaitBudget(readyTimeout, mainHandler) { onReadyTimeout() }
 
     fun start() {
         if (isReleased) return
         isStarted = true
         hasGivenUp = false
         val place = placeSystemName ?: run {
             report(EmbeddedBlockState.Empty)
             return
         }
 
         if (lastReportedState?.nothingToShow == true) {
             mindboxLogI("[EmbeddedBlock] Returning to a block with nothing on it, asking for its content again")
             dropProvider()
             appliedDescriptor = null
         }
 
         val isBackFromBackground = isAwayInBackground
         isAwayInBackground = false
         val isBackAfterBeingLeftBehind = leftBehind
         leftBehind = false
         if (isBackAfterBeingLeftBehind) resumeAfterBeingLeftBehind()
         val hasLandedCollapse = parkedAnswer?.let { parked ->
             if (parked.waitsForReturnCheck && blocksRegistry()?.deferUntilReturnChecked() == true) {
                 mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' is back before the session check of the app's return, its parked answer waits for it")
                 deferredParkedMayHold = isBackFromBackground
                 false
             } else {
                 landParkedAnswer(mayHold = isBackFromBackground)
             }
         } == true
         coverPageOfEndedSession()
         dropDataSentInEndedSession()
 
         provider?.let { current ->
             current.onStateChange = ::onProviderState
             if (!isPageCovered || isNewDataSentToCoveredPage) readyBudget.armIfNeeded()
             armCoverWaitIfNeeded()
             if (!startProvider(current, appliedContent)) return
         }
 
         if (!ensureRegistered()) {
             beginWaitingForContent()
             return
         }
         if (hasLandedCollapse) {
             mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' is back collapsed by the answer parked for it, asking its place again")
         } else if (hasNoContentComing) {
             beginWaitingForContent()
         }
         blocksRegistry()?.onBlockAppeared(place)
     }
 
     fun pause() {
         isStarted = false
         deferredParkedMayHold = null
         isAwayInBackground = !isAppInForeground()
         readyBudget.pause()
         configBudget.pause()
         provider?.pause()
         if (isAwayInBackground) {
             mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' paused by the app going to background, it keeps what it shows")
             return
         }
         collapseHeldAnswer()
     }
 
     fun release() {
         isStarted = false
         isReleased = true
         readyBudget.reset()
         configBudget.reset()
         stopWaitingForDi()
         registration?.let { loggingRunCatching { it.close() } }
         registration = null
         dropProvider()
         notifyContentDropped()
     }
 
     override fun onContentResolved(content: InAppType.Embedded?, answer: EmbeddedPlaceAnswer) {
         if (!acceptAnswer("Content")) return
         if (content != null && content.pageLayer == null) reportInternalFailure(content, winnerWithoutPage(content))
         if (!takesAnswerNow(waitsForReturnCheck = content.isShowable)) {
             if (isActive) {
                 mindboxLogI("[EmbeddedBlock] Content for '$placeSystemName' waits for the session check of the app's return")
             } else {
                 mindboxLogI("[EmbeddedBlock] The answer for '$placeSystemName' arrived off screen, deferring it")
             }
             val earlierOfSameInApp = parkedAnswer?.takeIf { parked -> content != null && parked.selectedInAppId == content.inAppId }
             parkedAnswer = ParkedAnswer.Resolved(content, keepingFirstSelectionOfNewSession(answer, earlierOfSameInApp?.answer))
             if (content.isShowable) supersedeHeldOutcome()
             return
         }
         dropContentWaitingForReturnCheck()
         if (!applyAnswer(content, answer, mayHold = true, isParked = false)) armCoverWaitIfNeeded()
     }
 
     private val ParkedAnswer.waitsForReturnCheck: Boolean
         get() = this is ParkedAnswer.Resolved && content.isShowable
 
     private val InAppType.Embedded?.isShowable: Boolean
         get() = this?.pageLayer != null
 
     private fun takesAnswerNow(waitsForReturnCheck: Boolean): Boolean =
         isActive && (deferredParkedMayHold == null || !waitsForReturnCheck)
 
     private fun dropContentWaitingForReturnCheck() {
         if (deferredParkedMayHold == null) return
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' has nothing to show before the session check of the app's return, dropping the content parked for it")
         deferredParkedMayHold = null
         parkedAnswer = null
     }
 
     override fun onReturnChecked() {
         coverPageOfEndedSession()
         dropDataSentInEndedSession()
         deferredParkedMayHold?.let { mayHold ->
             deferredParkedMayHold = null
             if (isActive && !applyParkedAnswer(mayHold)) {
                 if (hasNoContentComing) beginWaitingForContent() else armCoverWaitIfNeeded()
             }
         }
         sendDueSessionRefresh()
     }
 
     override fun onAppResumedOn(activity: Activity) {
         if (!isPausedForBackground) return
         val host = hostActivity() ?: return
         if (host === activity) return
         mindboxLogI("[EmbeddedBlock] The app came back from background on another screen, block '$placeSystemName' has left the screen")
         leaveScreenPausedForBackground()
     }
 
     fun onLeftScreen() {
         if (!isPausedForBackground) return
         if (hostActivity()?.isChangingConfigurations == true) return
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' left its screen before it was shown again after the app went to background")
         leaveScreenPausedForBackground()
     }
 
     private fun leaveScreenPausedForBackground() {
         isAwayInBackground = false
         applyParkedRefusal()
         collapseHeldAnswer()
         giveBackParkedShow()
         leftBehind = holdsLiveContent
     }
 
     private fun applyParkedRefusal() {
         val parked = parkedAnswer ?: return
         if (parked.waitsForReturnCheck || isOfEndedSession(parked.answer.sessionEpoch)) return
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' has left the screen, applying the answer with nothing to show parked for it")
         applyParkedAnswer(mayHold = false)
     }
 
     private fun giveBackParkedShow() {
         val parked = parkedAnswer as? ParkedAnswer.Resolved ?: return
         val place = placeSystemName ?: return
         if (!parked.answer.isShowReserved || !parked.content.isShowable) return
         parkedAnswer = ParkedAnswer.Resolved(parked.content, parked.answer.copy(isShowReserved = false))
         blocksRegistry()?.giveBackParkedShow(place, parked.answer.sessionEpoch)
     }
 
     override fun onSessionRenewed() {
         if (isReleased) return
         if (pendingDeliverySession?.let(::isOfEndedSession) == true) pendingDeliverySession = null
         if (isStarted) {
             coverPageOfEndedSession()
             dropDataSentInEndedSession()
             armCoverWaitIfNeeded()
             return
         }
         if (isAwayInBackground || !holdsLiveContent) return
         leftBehind = true
     }
 
     private fun coverPageOfEndedSession() {
         if (isPageCovered || !isStarted || updatablePage == null) return
         if (lastReportedState != EmbeddedBlockState.Ready || !isOfEndedSession(pageSessionEpoch)) return
         coverPage()
     }
 
     private fun coverShownPage() {
         if (isPageCovered || !isStarted || lastReportedState != EmbeddedBlockState.Ready) return
         coverPage()
     }
 
     private fun coverPage() {
         mindboxLogI(
             "[EmbeddedBlock] Block '$placeSystemName' shows the page of an ended session, " +
                 "covering it with the placeholder until the new session's content renders"
         )
         isPageCovered = true
         isNewDataSentToCoveredPage = false
         attemptStartTick = attemptStartTick ?: monotonicNow()
         updatablePage?.setCovered(true)
         armCoverWaitIfNeeded()
         report(EmbeddedBlockState.Loading)
     }
 
     private fun armCoverWaitIfNeeded() {
         if (isStarted && isCoveredAwaitingAnswer && !hasPendingDelivery) configBudget.armIfNeeded()
     }
 
     private fun uncoverPage() {
         if (isPageCovered) updatablePage?.setCovered(false)
         isPageCovered = false
         isNewDataSentToCoveredPage = false
     }
 
     private val isCoveredAwaitingAnswer: Boolean
         get() = isPageCovered && !isNewDataSentToCoveredPage
 
     private fun awaitNextAnswerUnderCover() {
         isNewDataSentToCoveredPage = false
         readyBudget.reset()
         armCoverWaitIfNeeded()
     }
 
     private fun dropDataSentInEndedSession() {
         if (!isPageCovered || !isNewDataSentToCoveredPage || !isOfEndedSession(pageSessionEpoch)) return
         mindboxLogI("[EmbeddedBlock] The session whose data was sent to the covered page of '$placeSystemName' has ended, waiting for the new session's answer")
         awaitNextAnswerUnderCover()
     }
 
     private fun applyParkedAnswer(mayHold: Boolean): Boolean {
         val parked = parkedAnswer ?: return false
         parkedAnswer = null
         return applyParked(parked, mayHold)
     }
 
     private fun landParkedAnswer(mayHold: Boolean): Boolean =
         applyParkedAnswer(mayHold) && lastReportedState?.nothingToShow == true
 
     private fun isOfEndedSession(sessionEpoch: SessionEpoch?): Boolean =
         sessionEpoch != null && blocksRegistry()?.isLiveSession(sessionEpoch) == false
 
     private fun resumeAfterBeingLeftBehind() {
         if (parkedAnswer?.let { parked -> isOfEndedSession(parked.answer.sessionEpoch) } == true) {
             parkedAnswer = null
             dropAnswerOfEndedSession()
         }
     }
 
     private fun applyParked(parked: ParkedAnswer, mayHold: Boolean): Boolean {
         if (isOfEndedSession(parked.answer.sessionEpoch)) {
             dropAnswerOfEndedSession()
             return false
         }
         return when (parked) {
             is ParkedAnswer.Resolved -> applyAnswer(parked.content, parked.answer, mayHold, isParked = true)
             is ParkedAnswer.ConfigUnavailable -> {
                 applyConfigUnavailable(parked.answer, mayHold)
                 true
             }
         }
     }
 
     private fun dropAnswerOfEndedSession() {
         if (isHoldingContent) {
             mindboxLogI(
                 "[EmbeddedBlock] The answer parked for '$placeSystemName' was given in a session that has ended, " +
                     "dropping it and keeping the content until the place answers again"
             )
         } else {
             mindboxLogI(
                 "[EmbeddedBlock] The answer parked for '$placeSystemName' was given in a session that has ended, " +
                     "dropping it; the block has nothing to show until the place answers again"
             )
         }
     }
 
     private fun applyAnswer(
         content: InAppType.Embedded?,
         answer: EmbeddedPlaceAnswer,
         mayHold: Boolean,
         isParked: Boolean,
     ): Boolean {
         val place = placeSystemName
         val toReserve = content?.takeIf { winner -> (isParked || !answer.isShowReserved) && winner.pageLayer != null }
         if (toReserve == null || place == null) {
             applyResolved(content, answer, mayHold)
             return true
         }
         when (blocksRegistry()?.reserveShow(place, toReserve, answer) ?: PlaceShowReservation.RESERVED) {
             PlaceShowReservation.RESERVED -> applyResolved(content, answer, mayHold)
             PlaceShowReservation.REFUSED -> applyResolved(null, answer, mayHold)
             PlaceShowReservation.STALE -> {
                 dropAnswerOfEndedSession()
                 return false
             }
         }
         return true
     }
 
     override fun onContentPending(sessionEpoch: SessionEpoch) {
         if (isReleased || hasGivenUp) return
         configBudget.reset()
         pendingDeliverySession = sessionEpoch
     }
 
     override fun onConfigUnavailable(answer: EmbeddedPlaceAnswer) {
         if (!acceptAnswer("The SDK's answer")) return
         val earlierParked = parkedAnswer
         parkedAnswer = null
         mindboxLogW("[EmbeddedBlock] The SDK has no config to answer for '$placeSystemName', sending its wait failure")
         sendWaitBudgetExceeded(
             waited = attemptStartTick?.let(::elapsedSince) ?: Milliseconds(0L),
             phase = WaitBudgetPhase.CONFIG_MISSING,
         )
         if (!takesAnswerNow(waitsForReturnCheck = false)) {
             mindboxLogI("[EmbeddedBlock] The failure for '$placeSystemName' arrived while paused, deferring it")
             val selectedInAppId = earlierParked?.takeIf { parked -> parked.answer.sessionEpoch == answer.sessionEpoch }?.selectedInAppId
             parkedAnswer = ParkedAnswer.ConfigUnavailable(keepingFirstSelectionOfNewSession(answer, earlierParked?.answer), selectedInAppId)
             return
         }
         dropContentWaitingForReturnCheck()
         applyConfigUnavailable(answer, mayHold = true)
     }
 
     private fun applyConfigUnavailable(answer: EmbeddedPlaceAnswer, mayHold: Boolean) {
         collapseOrHold(EmbeddedBlockState.Failed(FailureReason.WAIT_BUDGET_EXCEEDED), answer, mayHold)
     }
 
     private fun collapseOrHold(state: EmbeddedBlockState, answer: EmbeddedPlaceAnswer, mayHold: Boolean) {
         if (mayHold && holdsShownContent(answer)) {
             holdCollapse(state)
             return
         }
         forgetAppliedContent()
         report(state)
     }
 
     private fun holdsShownContent(answer: EmbeddedPlaceAnswer): Boolean =
         !answer.isByOperation && lastReportedState == EmbeddedBlockState.Ready && !isNewSessionFor(answer)
 
     private fun holdCollapse(state: EmbeddedBlockState) {
         mindboxLogI(
             "[EmbeddedBlock] Place '$placeSystemName' has nothing to show while its content is on screen, " +
                 "keeping it until the block leaves the screen"
         )
         heldCollapse = state
         dueSessionRefresh = null
         updatablePage?.withholdShow(true)
     }
 
     private fun releaseHeldCollapse() {
         heldCollapse = null
         updatablePage?.withholdShow(false)
     }
 
     private fun supersedeHeldOutcome() {
         if (heldCollapse == null) {
             if (isShownPageEmptied) keepEmptiedPageForParkedContent()
             return
         }
         mindboxLogI("[EmbeddedBlock] Content parked for '$placeSystemName' replaces the collapse held for it, the page's show waits for that content")
         heldCollapse = null
     }
 
     private fun keepEmptiedPageForParkedContent() {
         mindboxLogI("[EmbeddedBlock] The page of block '$placeSystemName' renders nothing while content is parked for the block, keeping the page; its show waits for that content")
         updatablePage?.withholdShow(true)
     }
 
     private fun collapseHeldAnswer() {
         val held = heldOutcome ?: return
         heldCollapse = null
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName' left the screen, collapsing the content it has nothing to show for")
         forgetAppliedContent()
         report(held)
     }
 
     private fun onPageEmptied() {
         if (isReleased || !isShownPageEmptied) return
         if (hasParkedShowableContent) {
             keepEmptiedPageForParkedContent()
             return
         }
         if (isPageCovered) {
             if (isOfEndedSession(pageSessionEpoch)) {
                 mindboxLogI("[EmbeddedBlock] The covered page of block '$placeSystemName' rendered nothing for a session that has ended, keeping it for the new session's answer")
                 return
             }
             mindboxLogI("[EmbeddedBlock] The covered page of block '$placeSystemName' rendered nothing for the new session, collapsing it")
             forgetAppliedContent()
             report(EmbeddedBlockState.Empty)
             return
         }
         if (isStarted || isAwayInBackground) return
         mindboxLogI("[EmbeddedBlock] The page of block '$placeSystemName' was left with nothing to show after the block left the screen, collapsing it")
         forgetAppliedContent()
         report(EmbeddedBlockState.Empty)
     }
 
     private fun acceptAnswer(what: String): Boolean {
         if (isReleased) return false
         if (hasGivenUp) {
             mindboxLogI(
                 "[EmbeddedBlock] $what for '$placeSystemName' arrived after the block gave up " +
                     "waiting, dropping it; the next appearance on screen asks afresh"
             )
             notifyContentDropped()
             return false
         }
         configBudget.reset()
         pendingDeliverySession = null
         return true
     }
 
     private fun sendWaitBudgetExceeded(waited: Milliseconds, phase: WaitBudgetPhase) {
         val place = placeSystemName ?: return
         val tracker = resolveFailureTracker() ?: return
         loggingRunCatching { tracker.sendPlaceWaitBudgetExceeded(place, waited, phase) }
     }
 
     private fun applyResolved(content: InAppType.Embedded?, answer: EmbeddedPlaceAnswer, mayHold: Boolean) {
         if (content == null) {
             collapseOrHold(EmbeddedBlockState.Empty, answer, mayHold)
             return
         }
         val layer = content.pageLayer ?: run {
             collapseOrHold(EmbeddedBlockState.Failed(FailureReason.UNKNOWN_ERROR), answer, mayHold)
             return
         }
         val descriptor = descriptorOf(content.inAppId, layer)
         val current = provider
         val sentPush = liveDataPush
 
         if (current != null && descriptor == (sentPush?.descriptor ?: appliedDescriptor) && lastReportedState?.nothingToShow != true) {
             refreshMetricsSnapshot(current, content)
             when {
                 current is EmbeddedUpdatableContentProvider && isNewSessionFor(answer) -> {
                     coverShownPage()
                     refreshForNewSession(current, answer, content)
                 }
                 sentPush != null -> mindboxLogI(
                     "[EmbeddedBlock] Same winner ${content.inAppId} for '$placeSystemName' with the params already on their way to its page, sending nothing"
                 )
                 else -> mindboxLogI("[EmbeddedBlock] Same winner ${content.inAppId} for '$placeSystemName', keeping the content")
             }
             releaseHeldCollapse()
             return
         }
         if (lastReportedState?.nothingToShow == true) {
             mindboxLogI("[EmbeddedBlock] Delivery for a collapsed block '$placeSystemName', rebuilding the content")
             recreateProvider(content, layer, answer)
             return
         }
         if (current is EmbeddedUpdatableContentProvider &&
             appliedDescriptor?.isSamePage(descriptor) == true &&
             (lastReportedState == EmbeddedBlockState.Ready || isPageCovered)
         ) {
             mindboxLogI("[EmbeddedBlock] Same winner ${content.inAppId} with new params, updating the content in place")
             refreshMetricsSnapshot(current, content)
             if (isNewSessionFor(answer)) coverShownPage()
             pushPageData(current, layer, content, answer)
             releaseHeldCollapse()
             return
         }
         recreateProvider(content, layer, answer)
     }
 
     private fun winnerWithoutPage(content: InAppType.Embedded): String = "Winner ${content.inAppId} has no webview layer"
 
     private fun isNewSessionFor(answer: EmbeddedPlaceAnswer): Boolean =
         pageSessionEpoch?.let { pageSession -> answer.sessionEpoch > pageSession } == true
 
     private fun keepingFirstSelectionOfNewSession(answer: EmbeddedPlaceAnswer, earlier: EmbeddedPlaceAnswer?): EmbeddedPlaceAnswer {
         val first = earlier?.takeIf { held -> held.sessionEpoch == answer.sessionEpoch && isNewSessionFor(answer) } ?: return answer
         return answer.copy(selectionTime = first.selectionTime)
     }
 
     private fun refreshForNewSession(
         current: EmbeddedUpdatableContentProvider,
         answer: EmbeddedPlaceAnswer,
         content: InAppType.Embedded,
     ) {
         val layer = content.pageLayer ?: return
         if (lastReportedState != EmbeddedBlockState.Ready && !isPageCovered) {
             mindboxLogI(
                 "[EmbeddedBlock] A new session picked ${content.inAppId} for '$placeSystemName' again while " +
                     "its page loads, refreshing the page once it renders"
             )
             val due = keepingFirstSelectionOfNewSession(answer, dueSessionRefresh)
             dueSessionRefresh = due
             current.scheduleSessionRefresh(due.sessionEpoch, due.selectionTime)
             return
         }
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName': same in-app — updating the page in place")
         pushPageData(current, layer, content, answer)
     }
 
     private val isDueSessionRefreshSendable: Boolean
         get() = dueSessionRefresh != null && isActive && (lastReportedState == EmbeddedBlockState.Ready || isPageCovered)
 
     private fun sendDueSessionRefreshOnceReturnChecked() {
         if (!isDueSessionRefreshSendable) return
         if (blocksRegistry()?.deferUntilReturnChecked() == true) {
             mindboxLogI("[EmbeddedBlock] The page of '$placeSystemName' rendered before the session check of the app's return, its new session's data waits for the check")
             return
         }
         sendDueSessionRefresh()
     }
 
     private fun sendDueSessionRefresh() {
         if (!isDueSessionRefreshSendable) return
         val answer = dueSessionRefresh ?: return
         val current = updatablePage ?: return
         dueSessionRefresh = null
         if (isOfEndedSession(answer.sessionEpoch)) {
             mindboxLogI("[EmbeddedBlock] The session that picked the page of '$placeSystemName' again has ended before the page rendered, dropping its refresh")
             return
         }
         refreshForNewSession(current, answer, appliedContent ?: return)
     }
 
     private fun pushPageData(
         current: EmbeddedUpdatableContentProvider,
         layer: Layer.WebViewLayer,
         content: InAppType.Embedded,
         answer: EmbeddedPlaceAnswer,
     ) {
         val descriptor = descriptorOf(content.inAppId, layer)
         val epoch = ++updateEpoch
         val isNewSession = isNewSessionFor(answer)
         val push = DataPush(descriptor, sessionEpoch = if (isNewSession) answer.sessionEpoch else pageSessionEpoch)
         dataPush = push
         if (isPageCovered) {
             isNewDataSentToCoveredPage = true
             configBudget.reset()
             if (isStarted) readyBudget.armIfNeeded()
         }
         val onResult: (DataPushOutcome) -> Unit = { outcome ->
             mainHandler.post {
                 if (isReleased || provider !== current || epoch != updateEpoch) return@post
                 dataPush = null
                 when {
                     outcome == DataPushOutcome.CONFIRMED -> {
                         appliedDescriptor = descriptor
                         appliedContent = content
                         appliedSessionEpoch = push.sessionEpoch
                     }
                     outcome == DataPushOutcome.ABANDONED -> {
                         awaitNextAnswerUnderCover()
                         mindboxLogI(
                             "[EmbeddedBlock] The data push to the page of '$placeSystemName' was abandoned before the page answered, " +
                                 "the place's next answer sends it again"
                         )
                     }
                     heldOutcome != null -> {
                         mindboxLogW("[EmbeddedBlock] In-place update over the bridge failed while the block has nothing to show, not rebuilding")
                     }
                     isOfEndedSession(answer.sessionEpoch) -> {
                         awaitNextAnswerUnderCover()
                         mindboxLogI(
                             "[EmbeddedBlock] The page of '$placeSystemName' did not take the data of a session that has ended, " +
                                 "keeping it until the new session answers"
                         )
                     }
                     isPageCovered -> failPresentation("Page did not confirm the new session's data")
                     else -> {
                         mindboxLogW("[EmbeddedBlock] Block '$placeSystemName': the page never confirmed the data push — rebuilding it")
                         recreateProvider(content, layer, answerForRebuild(current, answer, isNewSession))
                     }
                 }
             }
         }
         runCatching {
             if (isNewSession) {
                 current.refreshForSession(layer.params, answer.sessionEpoch, answer.selectionTime, onResult)
             } else {
                 current.updateParams(layer.params, onResult)
             }
         }.onFailure { error ->
             mindboxLogW("[EmbeddedBlock] In-place update crashed ($error), recreating the content")
             recreateProvider(content, layer, answerForRebuild(current, answer, isNewSession))
         }
     }
 
     private fun answerForRebuild(
         failed: EmbeddedUpdatableContentProvider,
         answer: EmbeddedPlaceAnswer,
         isNewSession: Boolean,
     ): EmbeddedPlaceAnswer {
         if (!isNewSession) return answer
         val counted = loggingRunCatching(defaultValue = null) { failed.timeToDisplaySoFar(answer.sessionEpoch) } ?: return answer
         return answer.copy(selectionTime = counted)
     }
 
     private fun refreshMetricsSnapshot(current: EmbeddedContentProvider, content: InAppType.Embedded) {
         if (current !is EmbeddedUpdatableContentProvider) return
         val applied = appliedContent
         if (applied != null && applied.frequency == content.frequency && applied.tags == content.tags) return
         mindboxLogI("[EmbeddedBlock] Same winner ${content.inAppId}: the config changed its frequency/tags, refreshing the snapshot")
         loggingRunCatching { current.refreshMetricsSnapshot(content.frequency, content.tags) }
         appliedContent = content
     }
 
     private fun recreateProvider(content: InAppType.Embedded, layer: Layer.WebViewLayer, answer: EmbeddedPlaceAnswer) {
         dropProvider()
         if (lastReportedState != EmbeddedBlockState.Loading || attemptStartTick == null) {
             attemptStartTick = monotonicNow()
         }
         val created = loggingRunCatching(defaultValue = null) { providerFactory(content, answer.selectionTime) } ?: run {
             failInternally(content, "Could not build content for ${content.inAppId}")
             return
         }
         provider = created
         appliedDescriptor = descriptorOf(content.inAppId, layer)
         appliedContent = content
         appliedSessionEpoch = answer.sessionEpoch
         (created as? EmbeddedUpdatableContentProvider)?.let { page ->
             page.confirmForSession(answer.sessionEpoch)
             page.onRendersNothing = ::onPageEmptied
         }
         created.onStateChange = ::onProviderState
         if (isStarted) {
             readyBudget.reset()
             readyBudget.armIfNeeded()
             startProvider(created, content)
         }
     }
 
     override val isHoldingContent: Boolean
         get() = lastReportedState is EmbeddedBlockState.Loading ||
             lastReportedState == EmbeddedBlockState.Ready ||
             hasPendingContent
 
     override val holdsLiveContent: Boolean
         get() = isHoldingContent && (hasPendingContent || provider?.isAttemptAlive != false)
 
     private fun onProviderState(state: EmbeddedBlockState) {
         if (state is EmbeddedBlockState.Ready && provider?.contentView == null) {
             dropProvider()
             failInternally(appliedContent, "Ready content for '$placeSystemName' has no view")
             return
         }
         if (state is EmbeddedBlockState.Ready && isPageCovered && !hasCoveredPageRenderedNewData) {
             mindboxLogI("[EmbeddedBlock] The covered page of block '$placeSystemName' reports itself shown before rendering the new session's data, keeping it covered")
             return
         }
         if (state is EmbeddedBlockState.Ready && rendersPageOfEndedSession) {
             readyBudget.reset()
             coverPage()
             sendDueSessionRefreshOnceReturnChecked()
             return
         }
         report(state)
         if (state == EmbeddedBlockState.Ready) sendDueSessionRefreshOnceReturnChecked()
     }
 
     private val hasCoveredPageRenderedNewData: Boolean
         get() = isNewDataSentToCoveredPage && updatablePage?.hasRenderedLastData != false && !isOfEndedSession(pageSessionEpoch)
 
     private val rendersPageOfEndedSession: Boolean
         get() = !isPageCovered && isStarted && updatablePage != null &&
             (dueSessionRefresh != null || isOfEndedSession(pageSessionEpoch))
 
     private fun report(state: EmbeddedBlockState) {
         if (state !is EmbeddedBlockState.Loading) {
             readyBudget.reset()
             attemptStartTick = null
             uncoverPage()
         }
         if (state.nothingToShow) {
             heldCollapse = null
             dueSessionRefresh = null
         }
         if (state == lastReportedState && isPageCovered == wasCoverReported) return
         lastReportedState = state
         wasCoverReported = isPageCovered
         onStateChange?.let { listener -> loggingRunCatching { listener(state) } }
         if (state.nothingToShow) notifyContentDropped()
     }
 
     private fun startProvider(current: EmbeddedContentProvider, content: InAppType.Embedded?): Boolean =
         runCatching { current.start() }
             .onFailure { error ->
                 dropProvider()
                 failInternally(content, "Starting content for '$placeSystemName' crashed", error)
             }
             .isSuccess
 
     private fun failInternally(content: InAppType.Embedded?, description: String, error: Throwable? = null) {
         reportInternalFailure(content, description, error)
         report(EmbeddedBlockState.Failed(FailureReason.UNKNOWN_ERROR))
     }
 
     private fun reportInternalFailure(content: InAppType.Embedded?, description: String, error: Throwable? = null) {
         if (!sendFailure(content, FailureReason.UNKNOWN_ERROR, "[EmbeddedBlock] $description", error)) {
             mindboxLogE("[EmbeddedBlock] $description, reporting failure", error)
         }
     }
 
     private fun sendFailure(
         content: InAppType.Embedded?,
         code: FailureReason,
         description: String,
         error: Throwable? = null,
     ): Boolean {
         val failed = content ?: return false
         val tracker = resolveFailureTracker() ?: return false
         loggingRunCatching {
             tracker.sendBlockFailureWithContext(
                 inAppId = failed.inAppId,
                 failureReason = code,
                 errorDescription = description,
                 throwable = error,
                 tags = failed.tags.gatedTags(isTagsFeatureEnabled()),
             )
         }
         return true
     }
 
     private fun resolveFailureTracker(): InAppFailureTracker? =
         failureTrackerCache ?: failureTracker()?.also { tracker -> failureTrackerCache = tracker }
 
     private fun notifyContentDropped() {
         val place = placeSystemName ?: return
         loggingRunCatching { blocksRegistry()?.onBlockContentDropped(place) }
     }
 
     private fun ensureRegistered(): Boolean {
         if (registration != null) return true
         val place = placeSystemName ?: return false
         val registry = blocksRegistry() ?: run {
             waitForDi()
             return false
         }
         stopWaitingForDi()
         registration = registry.register(place, this)
         return true
     }
 
     private fun waitForDi() {
         if (configJob != null) return
         report(EmbeddedBlockState.Loading)
         configJob = loggingRunCatching(defaultValue = null) {
             MindboxPreferences.inAppConfigFlow
                 .onEach {
                     mainHandler.post {
                         if (isReleased || registration != null) return@post
                         if (ensureRegistered() && isStarted) {
                             placeSystemName?.let { place -> blocksRegistry()?.onBlockAppeared(place) }
                         }
                     }
                 }
                 .launchIn(Mindbox.mindboxScope)
         }
     }
 
     private fun stopWaitingForDi() {
         val job = configJob ?: return
         configJob = null
         loggingRunCatching { job.cancel() }
     }
 
     private fun beginWaitingForContent() {
         report(EmbeddedBlockState.Loading)
         attemptStartTick = attemptStartTick ?: monotonicNow()
         if (!hasEverResolved()) {
             configBudget.armIfNeeded()
         }
     }
 
     private fun onConfigTimeout() {
         if (isReleased || (hasEverResolved() && (!isCoveredAwaitingAnswer || hasPendingDelivery))) {
             configBudget.reset()
             return
         }
         mindboxLogW(
             "[EmbeddedBlock] No answer within ${configWaitDuration.interval}ms of waiting for " +
                 "'$placeSystemName', failing; a later answer is dropped, the next appearance " +
                 "on screen asks afresh"
         )
         hasGivenUp = true
         configBudget.reset()
         sendWaitBudgetExceeded(
             waited = configWaitDuration,
             phase = if (hasConfig()) WaitBudgetPhase.RESOLVE_PENDING else WaitBudgetPhase.CONFIG_MISSING,
         )
         if (isPageCovered) forgetAppliedContent()
         report(EmbeddedBlockState.Failed(FailureReason.WAIT_BUDGET_EXCEEDED))
     }
 
     private fun hasEverResolved(): Boolean =
         provider != null || parkedAnswer != null || appliedDescriptor != null || hasPendingDelivery
 
     private fun onReadyTimeout() {
         if (!isStarted || provider == null) return
         failPresentation("Page did not send contentRendered within ${readyTimeout.interval / 1_000}s")
     }
 
     private fun failPresentation(description: String) {
         mindboxLogI("[EmbeddedBlock] Block '$placeSystemName': reporting presentation_failed for in-app ${appliedContent?.inAppId}")
         sendFailure(content = appliedContent, code = FailureReason.PRESENTATION_FAILED, description = description)
         hasGivenUp = true
         dropProvider()
         report(EmbeddedBlockState.Failed(FailureReason.PRESENTATION_FAILED))
     }
 
     private fun forgetAppliedContent() {
         dropProvider()
         appliedDescriptor = null
         appliedContent = null
     }
 
     private fun elapsedSince(start: Milliseconds): Milliseconds = Milliseconds(monotonicNow().interval - start.interval)
 
     private fun dropProvider() {
         readyBudget.reset()
         provider?.let { current -> loggingRunCatching { current.release() } }
         provider = null
         uncoverPage()
         appliedSessionEpoch = null
         dataPush = null
         dueSessionRefresh = null
         heldCollapse = null
     }
 
     private companion object {
         fun sanitizedConfigTimeout(requested: Milliseconds, place: PlaceKey?): Milliseconds {
             if (requested.interval > 0) return requested
             mindboxLogE(
                 "[EmbeddedBlock] Block for place '$place' was given timeout " +
                     "${requested.interval}ms: it must be positive, using the default " +
                     "${Constants.Embedded.defaultConfigTimeout.interval}ms"
             )
             return Constants.Embedded.defaultConfigTimeout
         }
     }
 }