Files
openchamber-android-native/third_party/mygo/internal/darwin/cookies.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

186 lines
6.0 KiB
Go

//go:build darwin && !ios
package darwin
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/ebitengine/purego/objc"
"github.com/egoist/mygo/internal/platform"
)
// cookieStore returns the WKHTTPCookieStore of store, nil for the
// default one.
func cookieStore(s platform.DataStore) (id, error) {
store := websiteDataStore(s)
if !sendBool(store, "respondsToSelector:", uintptr(sel("httpCookieStore"))) || !sendBool(class("NSHTTPCookie"), "instancesRespondToSelector:", uintptr(sel("sameSitePolicy"))) {
return 0, fmt.Errorf("mygo: cookies: requires macOS 10.15 or newer: %w", platform.ErrUnsupported)
}
return send(store, "httpCookieStore"), nil
}
func readCookie(c id) platform.Cookie {
out := platform.Cookie{Name: goString(send(c, "name")), Value: goString(send(c, "value")), Domain: strings.ToLower(goString(send(c, "domain"))), Path: goString(send(c, "path")), Secure: sendBool(c, "isSecure"), HTTPOnly: sendBool(c, "isHTTPOnly")}
// Foundation names only Lax and Strict, and macOS 26 gives a cookie
// set with SameSite=None no policy, which WebKit sends as None
// (coreSameSitePolicy in CookieCocoa.mm).
out.SameSite = strings.ToLower(goString(send(c, "sameSitePolicy")))
if out.SameSite == "" {
out.SameSite = "none"
}
date := send(c, "expiresDate")
if date != 0 && !sendBool(c, "isSessionOnly") {
out.Expires = time.Unix(int64(msgFloat(date, sel("timeIntervalSince1970"))), 0).UTC()
}
return out
}
// ListCookies lists every cookie, and matches them with the URL in Go:
// WKHTTPCookieStore cannot.
func (a appController) ListCookies(s platform.DataStore, rawURL string, done func([]platform.Cookie, error)) {
var u *url.URL
if rawURL != "" {
var err error
if u, err = url.Parse(rawURL); err != nil {
done(nil, err)
return
}
}
withPool(func() {
store, err := cookieStore(s)
if err != nil {
done(nil, err)
return
}
block := newBlock(func(_ objc.Block, array id) {
var result []platform.Cookie
withPool(func() {
for _, c := range arrayItems(array) {
if c := readCookie(c); u == nil || platform.CookieMatchesURL(c, u) {
result = append(result, c)
}
}
})
a.b.runOnMain(func() { done(result, nil) })
})
send(store, "getAllCookies:", uintptr(block))
block.Release()
})
}
func (a appController) SetCookie(s platform.DataStore, c platform.Cookie, done func(error)) {
withPool(func() {
store, err := cookieStore(s)
if err != nil {
done(err)
return
}
host := strings.TrimPrefix(c.Domain, ".")
if strings.Contains(host, ":") {
host = "[" + host + "]"
}
scheme := "http"
if c.Secure {
scheme = "https"
}
// Foundation parses the attributes, with a stand-in name and value:
// it would split a value with ", " in it as two cookies.
header := "n=v; Path=" + c.Path + "; SameSite=" + c.SameSite
if strings.HasPrefix(c.Domain, ".") {
header += "; Domain=" + c.Domain
}
if !c.Expires.IsZero() {
header += "; Expires=" + c.Expires.Format(http.TimeFormat)
}
if c.Secure {
header += "; Secure"
}
if c.HTTPOnly {
header += "; HttpOnly"
}
headers := send(class("NSDictionary"), "dictionaryWithObject:forKey:", uintptr(nsString(header)), uintptr(nsString("Set-Cookie")))
array := send(class("NSHTTPCookie"), "cookiesWithResponseHeaderFields:forURL:", uintptr(headers), uintptr(nsURL(scheme+"://"+host+"/")))
if sendInt(array, "count") != 1 {
done(fmt.Errorf("mygo: cookies: Set could not construct NSHTTPCookie"))
return
}
props := send(send(send(array, "objectAtIndex:", 0), "properties"), "mutableCopy")
send(props, "setObject:forKey:", uintptr(nsString(c.Name)), uintptr(constString(libFoundation, "NSHTTPCookieName")))
send(props, "setObject:forKey:", uintptr(nsString(c.Value)), uintptr(constString(libFoundation, "NSHTTPCookieValue")))
native := send(class("NSHTTPCookie"), "cookieWithProperties:", uintptr(props))
release(props)
if native == 0 {
done(fmt.Errorf("mygo: cookies: Set could not construct NSHTTPCookie"))
return
}
parsed := readCookie(native)
if parsed.Name != c.Name || parsed.Domain != c.Domain || parsed.Path != c.Path || parsed.Value != c.Value || parsed.Secure != c.Secure || parsed.HTTPOnly != c.HTTPOnly || parsed.SameSite != c.SameSite {
// Say what differs, without the value.
parsed.Value, c.Value = "", ""
done(fmt.Errorf("mygo: cookies: Set made NSHTTPCookie %+v of %+v", parsed, c))
return
}
block := newBlock(func(objc.Block) { a.b.runOnMain(func() { done(nil) }) })
send(store, "setCookie:completionHandler:", uintptr(native), uintptr(block))
block.Release()
})
}
func (a appController) DeleteCookie(s platform.DataStore, key platform.CookieKey, done func(error)) {
withPool(func() {
store, err := cookieStore(s)
if err != nil {
done(err)
return
}
retain(store)
block := newBlock(func(_ objc.Block, array id) {
// The borrowed array must survive dispatch to main.
retain(array)
a.b.runOnMain(func() {
withPool(func() {
defer release(array)
var match id
for _, c := range arrayItems(array) {
if goString(send(c, "name")) == key.Name && strings.ToLower(goString(send(c, "domain"))) == key.Domain && goString(send(c, "path")) == key.Path {
if match != 0 {
release(store)
done(fmt.Errorf("mygo: cookies: ambiguous cookie key"))
return
}
match = c
}
}
if match == 0 {
release(store)
done(nil)
return
}
retain(match)
completion := newBlock(func(objc.Block) { a.b.runOnMain(func() { release(match); release(store); done(nil) }) })
send(store, "deleteCookie:completionHandler:", uintptr(match), uintptr(completion))
completion.Release()
})
})
})
send(store, "getAllCookies:", uintptr(block))
block.Release()
})
}
// ClearCookies removes the cookies of the data store, its other data
// kept.
func (a appController) ClearCookies(s platform.DataStore, done func(error)) {
var err error
withPool(func() { _, err = cookieStore(s) })
if err != nil {
done(err)
return
}
a.removeData(s, platform.SiteDataCookies, done)
}