Skip to content

Commit 01876c3

Browse files
authored
Merge pull request #11 from OpacityLabs/nero/SDK-199-ios-android-cookie-behavior-unification
unify cookie fetch behavior between ios and android
2 parents b8a11c8 + a95e20d commit 01876c3

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class InAppBrowserActivity : AppCompatActivity() {
228228
try {
229229
val domain = java.net.URL(currentUrl).host
230230
event["cookies"] = cookies[domain]
231-
} catch(e: Exception) {
231+
} catch (e: Exception) {
232232
// If the URL is malformed (usually when it is a URI like "uberlogin://blabla")
233233
// we don't set any cookies
234234
}
@@ -266,5 +266,6 @@ class InAppBrowserActivity : AppCompatActivity() {
266266
LocalBroadcastManager.getInstance(this)
267267
.unregisterReceiver(cookiesForCurrentURLRequestReceiver)
268268
geckoSession.close()
269+
OpacityCore.onBrowserDestroyed()
269270
}
270271
}

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ object OpacityCore {
2626
private lateinit var _url: String
2727
private var headers: Bundle = Bundle()
2828
private lateinit var sRuntime: GeckoRuntime
29+
private var isBrowserActive = false
2930

3031
init {
3132
System.loadLibrary("OpacityCore")
@@ -73,40 +74,49 @@ object OpacityCore {
7374
}
7475

7576
fun presentBrowser() {
76-
val intent = Intent()
77-
intent.setClassName(
78-
appContext.packageName,
79-
"com.opacitylabs.opacitycore.InAppBrowserActivity"
80-
)
77+
val intent = Intent(appContext, InAppBrowserActivity::class.java)
8178
intent.putExtra("url", _url)
8279
intent.putExtra("headers", headers)
8380
appContext.startActivity(intent)
81+
isBrowserActive = true
8482
}
8583

86-
fun getBrowserCookiesForCurrentUrl(): String {
84+
fun getBrowserCookiesForCurrentUrl(): String? {
85+
if (!isBrowserActive) {
86+
return null
87+
}
88+
8789
val cookiesIntent = Intent("com.opacitylabs.opacitycore.GET_COOKIES_FOR_CURRENT_URL")
8890
val resultReceiver = CookieResultReceiver()
8991
cookiesIntent.putExtra("receiver", resultReceiver)
9092
LocalBroadcastManager.getInstance(appContext).sendBroadcast(cookiesIntent)
9193
val json = resultReceiver.waitForResult(1000) // Wait up to 1 second for the result
92-
return json.toString()
94+
return json?.toString()
9395
}
9496

95-
fun getBrowserCookiesForDomain(domain: String): String {
97+
fun getBrowserCookiesForDomain(domain: String): String? {
98+
if (!isBrowserActive) {
99+
return null
100+
}
101+
96102
val cookiesIntent = Intent("com.opacitylabs.opacitycore.GET_COOKIES_FOR_DOMAIN")
97103
val resultsReceiver = CookieResultReceiver()
98104
cookiesIntent.putExtra("receiver", resultsReceiver)
99105
cookiesIntent.putExtra("domain", domain)
100106
LocalBroadcastManager.getInstance(appContext).sendBroadcast(cookiesIntent)
101107
val json = resultsReceiver.waitForResult(1000)
102-
return json.toString()
108+
return json?.toString()
103109
}
104110

105111
fun closeBrowser() {
106112
val closeIntent = Intent("com.opacitylabs.opacitycore.CLOSE_BROWSER")
107113
LocalBroadcastManager.getInstance(appContext).sendBroadcast(closeIntent)
108114
}
109115

116+
fun onBrowserDestroyed() {
117+
isBrowserActive = false
118+
}
119+
110120
private fun parseOpacityError(error: String?): OpacityError {
111121
if (error == null) {
112122
return OpacityError("UnknownError", "No Message")

0 commit comments

Comments
 (0)