Repository navigation
Description
Context
An app with an in-app browser often wants its pages signed in as the user already is in their own browser (an app that downloads videos the user can watch, for example: members-only or age-restricted ones need the sign-in). The app can read the user's browser's cookies itself (yt-dlp's --cookies-from-browser, or a cookie-reading library), but it cannot put them into its web views: NSGo has no way to write to the web views' cookie store.
- Apple: each
WKWebViewusesWKWebsiteDataStore.defaultDataStore(internal/platform/apple/web.go, theWKWebViewConfigurationit builds); nothing reacheshttpCookieStore(setCookie:completionHandler:). - Windows: every web view shares one WebView2 profile (
internal/platform/windows/web.go:120-131);ICoreWebView2CookieManager(CreateCookie,AddOrUpdateCookie) is never queried. - Linux: the WebKitGTK network session's cookie manager is fetched only to set persistent storage (
internal/platform/linux/web.go,webkit_cookie_manager_set_persistent_storage);webkit_cookie_manager_add_cookieis not bound. - Android:
android.webkit.CookieManager.setCookieis not reached. Ref.InjectJavaScript/document.cookiecannot help: the cookies that carry a sign-in areHttpOnly(YouTube'sSID,__Secure-3PSID, Bilibili'sSESSDATA), which page scripts can neither read nor write.
#112 asks for listing and clearing the store; this is the other half, writing to it.
Why it matters
Without it, an in-app browser always starts signed out, and the user has to sign in again inside it, which some sites refuse in an embedded web view (Google's sign-in rejects many of them). Kando (an app built on NSGo) wants its Browse page to carry the sign-in the user has in Chrome, Edge, Firefox or Safari.
Proposal
A function on the web views' shared store, async because every platform's call is:
// WebCookie is a cookie of the web views' store.
type WebCookie struct {
Name, Value, Domain, Path string
Expires time.Time // zero: a session cookie
Secure, HTTPOnly bool
SameSite http.SameSite
}
// SetWebCookies adds or replaces cookies in the store every web view of the app shares (not an Incognito view's), and calls done on the engine's goroutine.
func SetWebCookies(cx *Scope, cookies []WebCookie, done func(error))With #112's listing on the same type, WebCookie would serve both. Apple: WKHTTPCookieStore setCookie:completionHandler: per cookie (NSHTTPCookie from properties, including HttpOnly and SameSite). Windows: ICoreWebView2CookieManager.CreateCookie + AddOrUpdateCookie. Linux: webkit_cookie_manager_add_cookie with a SoupCookie. Android: CookieManager.setCookie with the attribute string, then flush.
Acceptance
- After
SetWebCookieswith anHttpOnly,Securecookie for.example.com, a web view loadinghttps://example.com/sends it (checked by a test server reading the request'sCookieheader) on macOS, iOS, Windows, Linux and Android. - A cookie with a zero
Expireslasts the session; one with anExpiressurvives a restart of the app (the store is persistent). - An
Incognitoweb view does not see the cookies. - Writing works before any web view has been made (the store exists on its own).
Activity
feat(signin): download with the sign-in of the user's own browser (#168)
feat(core): set web view cookies, HttpOnly ones included
feat(core): set web view cookies, HttpOnly ones included
feat(core): set web view cookies, HttpOnly ones included
feat(browse): sign Browse's pages in with the chosen browser's sign-in (