-
Notifications
You must be signed in to change notification settings - Fork 6
MOBILEWEBVIEW-6: JS Mobile bridge #671
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
5 commits
Select commit
Hold shift + click to select a range
3a50ab0
MOBILEWEBVIEW-6: Add js brige
enotniy 014d236
MOBILEWEBVIEW-6: Add message validator
enotniy 2ca8c4b
MOBILEWEBVIEW-6: Add tests
enotniy 7406683
MOBILEWEBVIEW-6: Update common sdk
enotniy 0473969
MOBILEWEBVIEW-6: 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
49 changes: 49 additions & 0 deletions
49
sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/data/validators/BridgeMessageValidator.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,49 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.data.validators | ||
|
|
||
| import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi | ||
| import cloud.mindbox.mobile_sdk.inapp.presentation.view.BridgeMessage | ||
| import cloud.mindbox.mobile_sdk.logger.mindboxLogW | ||
|
|
||
| @OptIn(InternalMindboxApi::class) | ||
| internal class BridgeMessageValidator : Validator<BridgeMessage?> { | ||
| override fun isValid(item: BridgeMessage?): Boolean { | ||
| item ?: return false | ||
|
|
||
| runCatching { | ||
| if (item.id.isBlank()) { | ||
| mindboxLogW("BridgeMessage id is empty") | ||
| return false | ||
| } | ||
|
|
||
| if (item.type !in listOf( | ||
| BridgeMessage.TYPE_REQUEST, | ||
| BridgeMessage.TYPE_RESPONSE, | ||
| BridgeMessage.TYPE_ERROR | ||
| ) | ||
| ) { | ||
| mindboxLogW("BridgeMessage type ${item.type} is not supported") | ||
| return false | ||
| } | ||
|
|
||
| if (item.action.name.isEmpty()) { | ||
| mindboxLogW("BridgeMessage action is empty") | ||
| return false | ||
| } | ||
|
|
||
| if (item.timestamp <= 0L) { | ||
| mindboxLogW("BridgeMessage timestamp must be positive") | ||
| return false | ||
| } | ||
|
|
||
| if (item.version > BridgeMessage.VERSION) { | ||
| mindboxLogW("BridgeMessage version ${item.version} is not supported") | ||
| return false | ||
| } | ||
| }.onFailure { error -> | ||
| mindboxLogW("BridgeMessage validation error: $error") | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
| } | ||
151 changes: 151 additions & 0 deletions
151
sdk/src/main/java/cloud/mindbox/mobile_sdk/inapp/presentation/view/WebViewAction.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,151 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.presentation.view | ||
|
|
||
| import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi | ||
| import cloud.mindbox.mobile_sdk.logger.mindboxLogW | ||
| import com.google.gson.annotations.SerializedName | ||
| import java.util.UUID | ||
|
|
||
| @InternalMindboxApi | ||
| public enum class WebViewAction { | ||
| @SerializedName("init") | ||
| INIT, | ||
|
|
||
| @SerializedName("ready") | ||
| READY, | ||
|
|
||
| @SerializedName("click") | ||
| CLICK, | ||
|
|
||
| @SerializedName("close") | ||
| CLOSE, | ||
|
|
||
| @SerializedName("hide") | ||
| HIDE, | ||
|
|
||
| @SerializedName("log") | ||
| LOG, | ||
|
|
||
| @SerializedName("alert") | ||
| ALERT, | ||
|
|
||
| @SerializedName("toast") | ||
| TOAST, | ||
| } | ||
|
|
||
| @InternalMindboxApi | ||
| public sealed class BridgeMessage { | ||
| public abstract val version: Int | ||
| public abstract val type: String | ||
| public abstract val action: WebViewAction | ||
| public abstract val payload: String? | ||
| public abstract val id: String | ||
| public abstract val timestamp: Long | ||
|
|
||
| public data class Request( | ||
| override val version: Int, | ||
| override val action: WebViewAction, | ||
| override val payload: String?, | ||
| override val id: String, | ||
| override val timestamp: Long, | ||
| override val type: String = TYPE_REQUEST, | ||
| ) : BridgeMessage() | ||
|
|
||
| public data class Response( | ||
| override val version: Int, | ||
| override val action: WebViewAction, | ||
| override val payload: String?, | ||
| override val id: String, | ||
| override val timestamp: Long, | ||
| override val type: String = TYPE_RESPONSE, | ||
| ) : BridgeMessage() | ||
|
|
||
| public data class Error( | ||
| override val version: Int, | ||
| override val action: WebViewAction, | ||
| override val payload: String?, | ||
| override val id: String, | ||
| override val timestamp: Long, | ||
| override val type: String = TYPE_ERROR, | ||
| ) : BridgeMessage() | ||
|
|
||
| public companion object { | ||
| public const val VERSION: Int = 1 | ||
| public const val EMPTY_PAYLOAD: String = "{}" | ||
| public const val TYPE_FIELD_NAME: String = "type" | ||
| public const val TYPE_REQUEST: String = "request" | ||
| public const val TYPE_RESPONSE: String = "response" | ||
| public const val TYPE_ERROR: String = "error" | ||
|
|
||
| public fun createAction(action: WebViewAction, payload: String): Request = | ||
| Request( | ||
| id = UUID.randomUUID().toString(), | ||
| version = VERSION, | ||
| action = action, | ||
| payload = payload, | ||
| timestamp = System.currentTimeMillis(), | ||
| ) | ||
|
|
||
| public fun createResponseAction(message: Request, payload: String?): Response = | ||
| Response( | ||
| id = message.id, | ||
| version = message.version, | ||
| action = message.action, | ||
| payload = payload, | ||
| timestamp = System.currentTimeMillis(), | ||
| ) | ||
|
|
||
| public fun createErrorAction(message: Request, payload: String?): Error = | ||
| Error( | ||
| id = message.id, | ||
| version = message.version, | ||
| action = message.action, | ||
| payload = payload, | ||
| timestamp = System.currentTimeMillis(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @InternalMindboxApi | ||
| internal typealias BridgeMessageHandler = (BridgeMessage.Request) -> String | ||
|
|
||
| @InternalMindboxApi | ||
| internal typealias BridgeSuspendMessageHandler = suspend (BridgeMessage.Request) -> String | ||
|
|
||
| @InternalMindboxApi | ||
| internal class WebViewActionHandlers { | ||
|
|
||
| private val handlersByActionValue: MutableMap<WebViewAction, BridgeMessageHandler> = mutableMapOf() | ||
| private val suspendHandlersByActionValue: MutableMap<WebViewAction, BridgeSuspendMessageHandler> = mutableMapOf() | ||
|
|
||
| fun register(actionValue: WebViewAction, handler: BridgeMessageHandler) { | ||
| if (handlersByActionValue.containsKey(actionValue)) { | ||
| mindboxLogW("Handler for action $actionValue already registered") | ||
| } | ||
| handlersByActionValue[actionValue] = handler | ||
| } | ||
|
|
||
| fun registerSuspend(actionValue: WebViewAction, handler: BridgeSuspendMessageHandler) { | ||
| if (suspendHandlersByActionValue.containsKey(actionValue)) { | ||
| mindboxLogW("Suspend handler for action $actionValue already registered") | ||
| } | ||
| suspendHandlersByActionValue[actionValue] = handler | ||
| } | ||
|
|
||
| fun hasSuspendHandler(actionValue: WebViewAction): Boolean { | ||
| return suspendHandlersByActionValue.containsKey(actionValue) | ||
| } | ||
|
|
||
| fun handleRequest(message: BridgeMessage.Request): Result<String> { | ||
| return runCatching { | ||
enotniy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| handlersByActionValue[message.action]?.invoke(message) | ||
| ?: throw IllegalArgumentException("No handler for action ${message.action}") | ||
| } | ||
| } | ||
|
|
||
| suspend fun handleRequestSuspend(message: BridgeMessage.Request): Result<String> { | ||
| return runCatching { | ||
| suspendHandlersByActionValue[message.action]?.invoke(message) | ||
| ?: throw IllegalArgumentException("No suspend handler for action ${message.action}") | ||
| } | ||
| } | ||
| } | ||
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.