Skip to content

Add cookie support #144

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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ open class EditorConfiguration constructor(
val authHeader: String,
val webViewGlobals: List<WebViewGlobal>,
val editorSettings: String?,
val locale: String?
) : Parcelable {
val locale: String?,
val cookies: Map<String, String>
): Parcelable {
companion object {
@JvmStatic
fun builder(): Builder = Builder()
Expand All @@ -84,6 +85,7 @@ open class EditorConfiguration constructor(
private var webViewGlobals: List<WebViewGlobal> = emptyList()
private var editorSettings: String? = null
private var locale: String? = "en"
private var cookies: Map<String, String> = mapOf()

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

fun build(): EditorConfiguration = EditorConfiguration(
title = title,
Expand All @@ -116,7 +119,8 @@ open class EditorConfiguration constructor(
authHeader = authHeader,
webViewGlobals = webViewGlobals,
editorSettings = editorSettings,
locale = locale
locale = locale,
cookies = cookies
)
}

Expand All @@ -141,6 +145,7 @@ open class EditorConfiguration constructor(
if (webViewGlobals != other.webViewGlobals) return false
if (editorSettings != other.editorSettings) return false
if (locale != other.locale) return false
if (cookies != other.cookies) return false

return true
}
Expand All @@ -161,6 +166,7 @@ open class EditorConfiguration constructor(
result = 31 * result + webViewGlobals.hashCode()
result = 31 * result + (editorSettings?.hashCode() ?: 0)
result = 31 * result + (locale?.hashCode() ?: 0)
result = 31 * result + cookies.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.webkit.ConsoleMessage
import android.webkit.CookieManager
import android.webkit.JavascriptInterface
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebStorage
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.webkit.WebViewAssetLoader
Expand Down Expand Up @@ -245,9 +247,24 @@ class GutenbergView : WebView {
} else {
ASSET_URL
}
this.loadUrl(editorUrl)

Log.i("GutenbergView", "Startup Complete")
WebStorage.getInstance().deleteAllData()
this.clearCache(true)
// All cookies are third-party cookies because the root of this document
// lives under `https://appassets.androidplatform.net`
CookieManager.getInstance().setAcceptThirdPartyCookies(this, true);

// Erase all local cookies before loading the URL – we don't want to persist
// anything between uses – otherwise we might send the wrong cookies
CookieManager.getInstance().removeAllCookies {
CookieManager.getInstance().flush()
for(cookie in configuration.cookies) {
CookieManager.getInstance().setCookie(cookie.key, cookie.value)
}
this.loadUrl(editorUrl)

Log.i("GutenbergView", "Startup Complete")
}
}

private fun setGlobalJavaScriptVariables() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.gutenbergkit

import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -39,6 +38,7 @@ class MainActivity : AppCompatActivity() {
.setNamespaceExcludedPaths(arrayOf())
.setAuthHeader("")
.setWebViewGlobals(emptyList())
.setCookies(emptyMap())
.build()

gbView.start(config)
Expand Down
17 changes: 14 additions & 3 deletions ios/Demo-iOS/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ import GutenbergKit
let editorURL: URL? = ProcessInfo.processInfo.environment["GUTENBERG_EDITOR_URL"].flatMap(URL.init)

struct ContentView: View {
private let configuration: EditorConfiguration

init(configuration: EditorConfiguration = .default) {
self.configuration = configuration
}

var body: some View {
NavigationView {
EditorView(editorURL: editorURL)
}
NavigationSplitView(preferredCompactColumn: .constant(.detail), sidebar: {
EmptyView()
}, detail: {
EditorView(
editorURL: editorURL,
editorConfiguration: configuration
).navigationBarBackButtonHidden()
})
}
}

Expand Down
24 changes: 20 additions & 4 deletions ios/Demo-iOS/Sources/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import SwiftUI
import GutenbergKit

struct EditorView: View {
var editorURL: URL?

private let editorURL: URL?

private let editorConfiguration: EditorConfiguration

init(editorURL: URL? = nil, editorConfiguration: EditorConfiguration = .default) {
self.editorURL = editorURL
self.editorConfiguration = editorConfiguration
}

var body: some View {
_EditorView(editorURL: editorURL)
_EditorView(editorURL: editorURL, configuration: editorConfiguration)
.toolbar {
ToolbarItemGroup(placement: .topBarLeading) {
Button(action: {}, label: {
Expand Down Expand Up @@ -69,10 +77,18 @@ struct EditorView: View {
}

private struct _EditorView: UIViewControllerRepresentable {
var editorURL: URL?

private let editorURL: URL?

private let configuration: EditorConfiguration

init(editorURL: URL? = nil, configuration: EditorConfiguration) {
self.editorURL = editorURL
self.configuration = configuration
}

func makeUIViewController(context: Context) -> EditorViewController {
let viewController = EditorViewController()
let viewController = EditorViewController(configuration: self.configuration)
viewController.editorURL = editorURL
if #available(iOS 16.4, *) {
viewController.webView.isInspectable = true
Expand Down
1 change: 1 addition & 0 deletions ios/Sources/GutenbergKit/Sources/EditorConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct EditorConfiguration {
public var editorSettings: [String: Any]?
/// The locale to use for translations
public var locale = "en"
public var cookies: [HTTPCookie] = []

public init(title: String = "", content: String = "") {
self.title = title
Expand Down
6 changes: 6 additions & 0 deletions ios/Sources/GutenbergKit/Sources/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
config.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
config.setValue(true, forKey: "allowUniversalAccessFromFileURLs")

// The editor shouldn't try to persist cookies – we want complete control over how they're handled
config.websiteDataStore = WKWebsiteDataStore.nonPersistent()
for cookie in configuration.cookies {
config.websiteDataStore.httpCookieStore.setCookie(cookie)
}

// Set-up communications with the editor.
config.userContentController.add(controller, name: "editorDelegate")

Expand Down