-
Notifications
You must be signed in to change notification settings - Fork 4
fix: separate local storage between WebView processes #54
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
Open
ItsChaceD
wants to merge
7
commits into
main
Choose a base branch
from
fix/RMET-4918/isolate-local-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9a3b3c
fix: separate local storage between WebView processes
ItsChaceD 60c0758
chore: update pom.xml version and changelog
ItsChaceD 0d1632e
fix: broadcast events correctly across processes
ItsChaceD e607a29
fix tests and code cleanup
ItsChaceD b811afa
fix catch block
ItsChaceD 4801756
add isIsolated config value
ItsChaceD 863c4b8
increment version to 2.0.0 and fix merge conflicts
ItsChaceD 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
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
100 changes: 95 additions & 5 deletions
100
src/main/java/com.outsystems.plugins.inappbrowser/osinappbrowserlib/OSIABEvents.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 |
|---|---|---|
| @@ -1,36 +1,126 @@ | ||
| package com.outsystems.plugins.inappbrowser.osinappbrowserlib | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.IntentFilter | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.core.content.IntentCompat | ||
| import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABWebViewActivity | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
| import java.io.Serializable | ||
|
|
||
| sealed class OSIABEvents { | ||
| @RequiresOptIn( | ||
| level = RequiresOptIn.Level.WARNING, | ||
| message = "This API requires a prior call to OSIABEvents.registerReceiver(context) to work correctly with process isolation on Android 9+." | ||
| ) | ||
| @Retention(AnnotationRetention.BINARY) | ||
| @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) | ||
| annotation class RequiresEventBridgeRegistration | ||
|
|
||
| sealed class OSIABEvents : Serializable { | ||
|
OS-pedrogustavobilro marked this conversation as resolved.
|
||
| abstract val browserId: String | ||
|
|
||
| data class BrowserPageLoaded(override val browserId: String) : OSIABEvents() | ||
| data class BrowserFinished(override val browserId: String) : OSIABEvents() | ||
| data class BrowserPageNavigationCompleted(override val browserId: String, val url: String?) : OSIABEvents() | ||
|
|
||
| data class OSIABCustomTabsEvent( | ||
| override val browserId: String, | ||
| val action: String, | ||
| val context: Context | ||
| @Transient val context: Context? = null | ||
| ) : OSIABEvents() | ||
|
|
||
| data class OSIABWebViewEvent( | ||
| override val browserId: String, | ||
| val activity: OSIABWebViewActivity | ||
| @Transient val activity: OSIABWebViewActivity? = null | ||
|
ItsChaceD marked this conversation as resolved.
Outdated
|
||
| ) : OSIABEvents() | ||
|
|
||
| companion object { | ||
| const val EXTRA_BROWSER_ID = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_BROWSER_ID" | ||
| const val ACTION_IAB_EVENT = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.ACTION_IAB_EVENT" | ||
| const val ACTION_CLOSE_WEBVIEW = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.ACTION_CLOSE_WEBVIEW" | ||
| const val EXTRA_EVENT_DATA = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_EVENT_DATA" | ||
|
|
||
| private val _events = MutableSharedFlow<OSIABEvents>() | ||
| // Buffer capacity is required because BroadcastReceiver.onReceive() is synchronous. | ||
| // We must use tryEmit() which would drop events without buffer space. | ||
| private val _events = MutableSharedFlow<OSIABEvents>(extraBufferCapacity = 64) | ||
|
ItsChaceD marked this conversation as resolved.
|
||
| val events = _events.asSharedFlow() | ||
|
|
||
| private var receiver: BroadcastReceiver? = null | ||
| private var receiverRefCount = 0 | ||
|
|
||
| /** | ||
| * Registers a BroadcastReceiver to listen for events from the isolated WebView process. | ||
| * This must be called before opening a WebView on Android 9+ to ensure events are received. | ||
| */ | ||
| @Synchronized | ||
| fun registerReceiver(context: Context) { | ||
|
ItsChaceD marked this conversation as resolved.
ItsChaceD marked this conversation as resolved.
|
||
| receiverRefCount++ | ||
| if (receiver != null) return | ||
|
|
||
| val appContext = context.applicationContext | ||
| receiver = object : BroadcastReceiver() { | ||
| override fun onReceive(context: Context?, intent: Intent?) { | ||
| if (intent?.action == ACTION_IAB_EVENT) { | ||
| val event = IntentCompat.getSerializableExtra( | ||
| intent, | ||
| EXTRA_EVENT_DATA, | ||
| OSIABEvents::class.java | ||
| ) | ||
| event?.let { | ||
| _events.tryEmit(it) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val filter = IntentFilter(ACTION_IAB_EVENT) | ||
| ContextCompat.registerReceiver( | ||
| appContext, | ||
| receiver, | ||
| filter, | ||
| ContextCompat.RECEIVER_NOT_EXPORTED | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Unregisters the BroadcastReceiver. Should be called when the browser is closed. | ||
| * The receiver is only truly unregistered when all registered 'users' have unregistered. | ||
| */ | ||
| @Synchronized | ||
| fun unregisterReceiver(context: Context) { | ||
| if (receiverRefCount > 0) { | ||
| receiverRefCount-- | ||
| } | ||
|
|
||
| if (receiverRefCount == 0) { | ||
| receiver?.let { | ||
| try { | ||
| context.applicationContext.unregisterReceiver(it) | ||
| } catch (e: Exception) { | ||
| // Receiver may not be registered, ignore | ||
| } | ||
| receiver = null | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun postEvent(event: OSIABEvents) { | ||
| _events.emit(event) | ||
| } | ||
|
|
||
| /** | ||
| * Broadcasts an event from the isolated WebView process to the main process. | ||
| * Only data-only events should be broadcast (BrowserPageLoaded, BrowserFinished, etc.). | ||
| */ | ||
| fun broadcastEvent(context: Context, event: OSIABEvents) { | ||
| val intent = Intent(ACTION_IAB_EVENT).apply { | ||
| setPackage(context.packageName) | ||
| putExtra(EXTRA_EVENT_DATA, event) | ||
| } | ||
| context.sendBroadcast(intent) | ||
| } | ||
| } | ||
|
|
||
| } | ||
2 changes: 2 additions & 0 deletions
2
...outsystems.plugins.inappbrowser/osinappbrowserlib/helpers/OSIABCustomTabsSessionHelper.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
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
Oops, something went wrong.
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.