-
Notifications
You must be signed in to change notification settings - Fork 6
MOBILE-269: Webview cache #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5174816
MOBILE-269: Webview cache
justSmK 6ad4445
MOBILE-269: Remove profiling. Rename InAppWebViewPrewService.
enotniy 9f0b6b9
MOBILE-269: Add feature toggles for webview cache
enotniy 1e48ba4
MOBILE-269: Add tests
enotniy 52a982e
MOBILE-269: Add InAppWebViewCachePolicy
enotniy 7c31a18
MOBILE-269: Follow code review
enotniy 654fb67
MOBILE-269: Follow code review
enotniy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [submodule "kmp-common-sdk"] | ||
| path = kmp-common-sdk | ||
| url = https://github.com/mindbox-cloud/kmp-common-sdk.git | ||
| branch = develop | ||
| branch = develop |
Submodule kmp-common-sdk
updated
24 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...c/main/java/cloud/mindbox/mobile_sdk/inapp/data/managers/InAppWebViewLearnedHostsStore.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.data.managers | ||
|
|
||
| import cloud.mindbox.mobile_sdk.managers.SharedPreferencesManager | ||
| import cloud.mindbox.mobile_sdk.utils.loggingRunCatching | ||
| import org.json.JSONArray | ||
|
|
||
| /** | ||
| * Persists https hosts actually observed during webview in-app shows (per endpoint). | ||
| * The mobile config only reveals the bootstrap hosts; the heavy ones (image CDNs) | ||
| * are discovered at show time and fed to the next launch's preconnect prewarm. | ||
| */ | ||
| internal class InAppWebViewLearnedHostsStore { | ||
|
|
||
| companion object { | ||
| private const val KEY_PREFIX = "MBInAppWebViewLearnedHosts." | ||
| private const val MAX_HOSTS = 12 | ||
| } | ||
|
|
||
| fun hosts(endpointId: String): List<String> = loggingRunCatching(defaultValue = emptyList()) { | ||
| val raw = SharedPreferencesManager.getString(key(endpointId)) | ||
| ?.takeIf { it.isNotBlank() } | ||
| ?: return@loggingRunCatching emptyList() | ||
| val array = JSONArray(raw) | ||
| (0 until array.length()).mapNotNull { index -> | ||
| array.optString(index).takeIf { host -> host.isNotBlank() } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Newest-first merge capped at [MAX_HOSTS] so one weird show can't flood the list. | ||
| * Synchronized: merges arrive from evaluate callbacks/coroutines of concurrent closes, | ||
| * and an unsynchronized read-modify-write would silently drop one close's hosts. | ||
| */ | ||
| @Synchronized | ||
| fun merge(endpointId: String, observedHosts: List<String>): Unit = loggingRunCatching { | ||
| if (endpointId.isBlank()) return@loggingRunCatching | ||
| val incoming = observedHosts.map(String::trim).filter(String::isNotBlank) | ||
| if (incoming.isEmpty()) return@loggingRunCatching | ||
| val merged = (incoming + hosts(endpointId)).distinct().take(MAX_HOSTS) | ||
| SharedPreferencesManager.put(key(endpointId), JSONArray(merged).toString()) | ||
| } | ||
|
|
||
| private fun key(endpointId: String): String = KEY_PREFIX + endpointId | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/presentation/InAppWebViewCachePolicy.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.presentation | ||
|
|
||
| import cloud.mindbox.mobile_sdk.inapp.data.managers.CACHE_INAPP_WEBVIEW_FEATURE | ||
| import cloud.mindbox.mobile_sdk.inapp.data.managers.FEATURE_TOGGLE_DEFAULT | ||
| import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.MobileConfigSerializationManager | ||
| import cloud.mindbox.mobile_sdk.models.operation.response.InAppConfigResponseBlank | ||
| import cloud.mindbox.mobile_sdk.repository.MindboxPreferences | ||
|
|
||
| /** | ||
| * The cache half of the WebView feature toggles (`MobileSdkShouldCacheInAppWebView`), | ||
| * latched once per process from the cached config on disk — mirrors iOS's | ||
| * `InAppWebViewDataStore.isCacheFeatureEnabled`. | ||
| * | ||
| * Read from the cache, not [cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.FeatureToggleManager]: | ||
| * the prewarm's first WebView is created at SDK init, before any fresh config can populate | ||
| * the manager — reading the live toggle there always sees the default (enabled), even when | ||
| * the cached config already says otherwise. The decision is latched for the whole launch by | ||
| * design: prewarm and every later show must agree, or the cache would be split between two | ||
| * behaviors for the same session. This is also why the value can't just live in | ||
| * [cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.FeatureToggleManager]'s toggle | ||
| * map: that map is cleared and repopulated from the fresh config on every fetch, which would | ||
| * silently un-latch this decision the first time a fresh config disagreed with the cache. | ||
| */ | ||
| internal class InAppWebViewCachePolicy( | ||
| private val mobileConfigSerializationManager: MobileConfigSerializationManager | ||
| ) { | ||
|
|
||
| @Volatile | ||
| private var latched: Boolean? = null | ||
|
|
||
| val isCacheEnabled: Boolean | ||
| @Synchronized get() = latched ?: extract(parseCachedConfigBlank()).also { latched = it } | ||
|
enotniy marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Lets a caller that already parsed the cached config blank for its own purposes (the | ||
| * prewarm manager, which needs the same blank for its layers and prewarm-toggle checks) | ||
| * hand it over instead of this class deserializing the same JSON a second time. | ||
| * | ||
| * First value wins: a call after the toggle has already latched — from here or from | ||
| * [isCacheEnabled] itself — is a no-op. | ||
| */ | ||
| @Synchronized | ||
| fun prime(configBlank: InAppConfigResponseBlank?) { | ||
| if (latched == null) latched = extract(configBlank) | ||
| } | ||
|
|
||
| private fun extract(configBlank: InAppConfigResponseBlank?): Boolean = | ||
| configBlank?.settings?.featureToggles?.toggles?.get(CACHE_INAPP_WEBVIEW_FEATURE) ?: FEATURE_TOGGLE_DEFAULT | ||
|
|
||
| private fun parseCachedConfigBlank(): InAppConfigResponseBlank? { | ||
| val configString = MindboxPreferences.inAppConfig | ||
| if (configString.isBlank()) return null | ||
| return mobileConfigSerializationManager.deserializeToConfigDtoBlank(configString) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.