Skip to content

Commit d1de551

Browse files
committed
Add Android cookie support
1 parent 2d9783a commit d1de551

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

android/Gutenberg/src/main/java/org/wordpress/gutenberg/EditorConfiguration.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ open class EditorConfiguration constructor(
6161
val authHeader: String,
6262
val webViewGlobals: List<WebViewGlobal>,
6363
val editorSettings: String?,
64-
val locale: String?
65-
) : Parcelable {
64+
val locale: String?,
65+
val cookies: Map<String, String>
66+
): Parcelable {
6667
companion object {
6768
@JvmStatic
6869
fun builder(): Builder = Builder()
@@ -84,6 +85,7 @@ open class EditorConfiguration constructor(
8485
private var webViewGlobals: List<WebViewGlobal> = emptyList()
8586
private var editorSettings: String? = null
8687
private var locale: String? = "en"
88+
private var cookies: Map<String, String> = mapOf()
8789

8890
fun setTitle(title: String) = apply { this.title = title }
8991
fun setContent(content: String) = apply { this.content = content }
@@ -100,6 +102,7 @@ open class EditorConfiguration constructor(
100102
fun setWebViewGlobals(webViewGlobals: List<WebViewGlobal>) = apply { this.webViewGlobals = webViewGlobals }
101103
fun setEditorSettings(editorSettings: String?) = apply { this.editorSettings = editorSettings }
102104
fun setLocale(locale: String?) = apply { this.locale = locale }
105+
fun setCookies(cookies: Map<String, String>) = apply { this.cookies = cookies }
103106

104107
fun build(): EditorConfiguration = EditorConfiguration(
105108
title = title,
@@ -116,7 +119,8 @@ open class EditorConfiguration constructor(
116119
authHeader = authHeader,
117120
webViewGlobals = webViewGlobals,
118121
editorSettings = editorSettings,
119-
locale = locale
122+
locale = locale,
123+
cookies = cookies
120124
)
121125
}
122126

@@ -141,6 +145,7 @@ open class EditorConfiguration constructor(
141145
if (webViewGlobals != other.webViewGlobals) return false
142146
if (editorSettings != other.editorSettings) return false
143147
if (locale != other.locale) return false
148+
if (cookies != other.cookies) return false
144149

145150
return true
146151
}
@@ -161,6 +166,7 @@ open class EditorConfiguration constructor(
161166
result = 31 * result + webViewGlobals.hashCode()
162167
result = 31 * result + (editorSettings?.hashCode() ?: 0)
163168
result = 31 * result + (locale?.hashCode() ?: 0)
169+
result = 31 * result + cookies.hashCode()
164170
return result
165171
}
166172
}

android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import android.util.AttributeSet
1111
import android.util.Log
1212
import android.view.View
1313
import android.webkit.ConsoleMessage
14+
import android.webkit.CookieManager
1415
import android.webkit.JavascriptInterface
1516
import android.webkit.ValueCallback
1617
import android.webkit.WebChromeClient
1718
import android.webkit.WebResourceError
1819
import android.webkit.WebResourceRequest
1920
import android.webkit.WebResourceResponse
21+
import android.webkit.WebStorage
2022
import android.webkit.WebView
2123
import android.webkit.WebViewClient
2224
import androidx.webkit.WebViewAssetLoader
@@ -245,9 +247,24 @@ class GutenbergView : WebView {
245247
} else {
246248
ASSET_URL
247249
}
248-
this.loadUrl(editorUrl)
249250

250-
Log.i("GutenbergView", "Startup Complete")
251+
WebStorage.getInstance().deleteAllData()
252+
this.clearCache(true)
253+
// All cookies are third-party cookies because the root of this document
254+
// lives under `https://appassets.androidplatform.net`
255+
CookieManager.getInstance().setAcceptThirdPartyCookies(this, true);
256+
257+
// Erase all local cookies before loading the URL – we don't want to persist
258+
// anything between uses – otherwise we might send the wrong cookies
259+
CookieManager.getInstance().removeAllCookies {
260+
CookieManager.getInstance().flush()
261+
for(cookie in configuration.cookies) {
262+
CookieManager.getInstance().setCookie(cookie.key, cookie.value)
263+
}
264+
this.loadUrl(editorUrl)
265+
266+
Log.i("GutenbergView", "Startup Complete")
267+
}
251268
}
252269

253270
private fun setGlobalJavaScriptVariables() {

android/app/src/main/java/com/example/gutenbergkit/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example.gutenbergkit
22

33
import android.os.Bundle
4-
import android.util.Log
54
import android.webkit.WebView
65
import androidx.activity.enableEdgeToEdge
76
import androidx.appcompat.app.AppCompatActivity
@@ -39,6 +38,7 @@ class MainActivity : AppCompatActivity() {
3938
.setNamespaceExcludedPaths(arrayOf())
4039
.setAuthHeader("")
4140
.setWebViewGlobals(emptyList())
41+
.setCookies(emptyMap())
4242
.build()
4343

4444
gbView.start(config)

0 commit comments

Comments
 (0)