Skip to content

Commit 13a1446

Browse files
committed
expose gecko fetch api
1 parent 73a3234 commit 13a1446

File tree

7 files changed

+938
-174
lines changed

7 files changed

+938
-174
lines changed

packages/flutter_mozilla_components/android/src/main/kotlin/eu/weblibre/flutter_mozilla_components/api/GeckoBrowserApiImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoCookieApi
2727
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoDeleteBrowsingDataController
2828
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoDownloadsApi
2929
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoEngineSettingsApi
30+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchApi
3031
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFindApi
3132
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoHistoryApi
3233
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoIconsApi
@@ -231,6 +232,7 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
231232
GeckoDownloadsApi.setUp(_flutterPluginBinding.binaryMessenger, GeckoDownloadsApiImpl())
232233
GeckoBrowserExtensionApi.setUp(_flutterPluginBinding.binaryMessenger, GeckoBrowserExtensionApiImpl())
233234
GeckoHistoryApi.setUp(_flutterPluginBinding.binaryMessenger, GeckoHistoryApiImpl())
235+
GeckoFetchApi.setUp(_flutterPluginBinding.binaryMessenger, GeckoFetchApiImpl())
234236

235237
ReaderViewEvents.setUp(
236238
_flutterPluginBinding.binaryMessenger,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package eu.weblibre.flutter_mozilla_components.api
2+
3+
import android.os.Build
4+
import androidx.annotation.RequiresApi
5+
import eu.weblibre.flutter_mozilla_components.GlobalComponents
6+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchApi
7+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchCookiePolicy
8+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchMethod
9+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchRedircet
10+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchRequest
11+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchResponse
12+
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoHeader
13+
import kotlinx.coroutines.CoroutineScope
14+
import kotlinx.coroutines.Dispatchers
15+
import kotlinx.coroutines.SupervisorJob
16+
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.withContext
18+
import mozilla.components.concept.fetch.MutableHeaders
19+
import mozilla.components.concept.fetch.Request
20+
import java.util.concurrent.TimeUnit
21+
22+
class GeckoFetchApiImpl : GeckoFetchApi {
23+
private val components by lazy {
24+
requireNotNull(GlobalComponents.components) { "Components not initialized" }
25+
}
26+
27+
companion object {
28+
private val coroutineScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
29+
}
30+
31+
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
32+
override fun fetch(
33+
request: GeckoFetchRequest,
34+
callback: (Result<GeckoFetchResponse>) -> Unit
35+
) {
36+
val headers = MutableHeaders()
37+
for (header in request.headers) {
38+
headers.append(header.key, header.value)
39+
}
40+
41+
val request = Request(
42+
url = request.url,
43+
method = when (request.method) {
44+
GeckoFetchMethod.GET -> Request.Method.GET
45+
GeckoFetchMethod.HEAD -> Request.Method.HEAD
46+
GeckoFetchMethod.POST -> Request.Method.POST
47+
GeckoFetchMethod.PUT -> Request.Method.PUT
48+
GeckoFetchMethod.DELETE -> Request.Method.DELETE
49+
GeckoFetchMethod.CONNECT -> Request.Method.CONNECT
50+
GeckoFetchMethod.OPTIONS -> Request.Method.OPTIONS
51+
GeckoFetchMethod.TRACE -> Request.Method.TRACE
52+
},
53+
headers = headers,
54+
connectTimeout = if (request.connectTimeoutMillis != null) Pair(
55+
request.connectTimeoutMillis,
56+
TimeUnit.MILLISECONDS
57+
) else null,
58+
readTimeout = if (request.readTimeoutMillis != null) Pair(
59+
request.readTimeoutMillis,
60+
TimeUnit.MILLISECONDS
61+
) else null,
62+
body = if (request.body != null) Request.Body.fromString(request.body) else null,
63+
redirect = when (request.redirect) {
64+
GeckoFetchRedircet.FOLLOW -> Request.Redirect.FOLLOW
65+
GeckoFetchRedircet.MANUAL -> Request.Redirect.MANUAL
66+
},
67+
cookiePolicy = when (request.cookiePolicy) {
68+
GeckoFetchCookiePolicy.INCLUDE -> Request.CookiePolicy.INCLUDE
69+
GeckoFetchCookiePolicy.OMIT -> Request.CookiePolicy.OMIT
70+
},
71+
useCaches = request.useCaches,
72+
private = request.private,
73+
useOhttp = request.useOhttp,
74+
referrerUrl = request.referrerUrl,
75+
conservative = request.conservative
76+
)
77+
78+
coroutineScope.launch {
79+
withContext(Dispatchers.Main) {
80+
val response = components.core.client.fetch(request)
81+
callback(
82+
Result.success(
83+
GeckoFetchResponse(
84+
url = response.url,
85+
status = response.status.toLong(),
86+
body = response.body.useStream { stream -> stream.readAllBytes() },
87+
headers = response.headers.map { it -> GeckoHeader(it.name, it.value) }
88+
)))
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)