Skip to content

Commit b8a11c8

Browse files
authored
Merge pull request #10 from OpacityLabs/nero/SDK-194-ios-and-android-behavior-unification
unify behavior with iOS
2 parents 7192176 + 23c0849 commit b8a11c8

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

OpacityCore/src/main/kotlin/com/opacitylabs/opacitycore/InAppBrowserActivity.kt

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ class InAppBrowserActivity : AppCompatActivity() {
4646
override fun onReceive(context: Context?, intent: Intent?) {
4747
if (intent?.action == "com.opacitylabs.opacitycore.GET_COOKIES_FOR_DOMAIN") {
4848
val receiver = intent.getParcelableExtra<CookieResultReceiver>("receiver")
49-
val domain = intent.getStringExtra("domain")
49+
var domain = intent.getStringExtra("domain")
50+
if (domain?.startsWith(".") == true) {
51+
// If the domain starts with a dot, we have to remove it as per rfc 6265
52+
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3
53+
domain = domain.substring(1)
54+
}
5055
val browserCookies = cookies[domain] ?: JSONObject()
5156
receiver?.onReceiveResult(browserCookies)
5257
}
@@ -118,6 +123,9 @@ class InAppBrowserActivity : AppCompatActivity() {
118123
"html_body" -> {
119124
htmlBody = jsonMessage.getString("html")
120125
emitNavigationEvent()
126+
127+
// clear the html_body, needed so we stay consistent with iOS
128+
htmlBody = ""
121129
}
122130

123131
"cookies" -> {
@@ -168,6 +176,9 @@ class InAppBrowserActivity : AppCompatActivity() {
168176
): GeckoResult<AllowOrDeny>? {
169177
currentUrl = request.uri
170178
addToVisitedUrls(request.uri)
179+
180+
emitNavigationEvent()
181+
171182
return super.onLoadRequest(session, request)
172183
}
173184

@@ -206,16 +217,26 @@ class InAppBrowserActivity : AppCompatActivity() {
206217
}
207218

208219
private fun emitNavigationEvent() {
209-
val domain = java.net.URL(currentUrl).host
210-
val event: Map<String, Any?> =
211-
mapOf(
220+
val event: MutableMap<String, Any?> =
221+
mutableMapOf(
212222
"event" to "navigation",
213223
"url" to currentUrl,
214-
"html_body" to htmlBody,
215-
"cookies" to cookies[domain],
216224
"visited_urls" to visitedUrls,
217225
"id" to System.currentTimeMillis().toString()
218226
)
227+
228+
try {
229+
val domain = java.net.URL(currentUrl).host
230+
event["cookies"] = cookies[domain]
231+
} catch(e: Exception) {
232+
// If the URL is malformed (usually when it is a URI like "uberlogin://blabla")
233+
// we don't set any cookies
234+
}
235+
236+
if (htmlBody != "") {
237+
event["html_body"] = htmlBody
238+
}
239+
219240
OpacityCore.emitWebviewEvent(JSONObject(event).toString())
220241
clearVisitedUrls()
221242
}

0 commit comments

Comments
 (0)