Files
openchamber-android-native/third_party/mygo/internal/darwin/session.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

106 lines
3.3 KiB
Go

//go:build darwin && !ios
package darwin
import (
"log"
"sync"
"github.com/ebitengine/purego/objc"
"github.com/egoist/mygo/internal/platform"
)
// Sessions (mygo.Session): a store is a WKWebsiteDataStore, which the
// configuration of each web view of the session names. window.open hands
// the opener's configuration over, and its store with it.
// dataStore holds a WKWebsiteDataStore, retained until Close.
type dataStore struct {
store id
closed bool
}
var inMemoryOnce sync.Once
// NewDataStore makes the store of a session: a persistent one is
// +dataStoreForIdentifier: of the session's UUID (macOS 14), kept in
// memory until the app quits on older systems, an ephemeral one
// +nonPersistentDataStore.
func (a appController) NewDataStore(opts platform.DataStoreOptions) (platform.DataStore, error) {
s := &dataStore{}
withPool(func() {
cls := class("WKWebsiteDataStore")
if !opts.Ephemeral && sendBool(cls, "respondsToSelector:", uintptr(sel("dataStoreForIdentifier:"))) {
uuid := send(send(class("NSUUID"), "alloc"), "initWithUUIDString:", uintptr(nsString(opts.ID)))
s.store = retain(send(cls, "dataStoreForIdentifier:", uintptr(uuid)))
release(uuid)
return
}
if !opts.Ephemeral {
inMemoryOnce.Do(func() {
log.Print("mygo: sessions keep their data in memory until the app quits before macOS 14")
})
}
s.store = retain(send(cls, "nonPersistentDataStore"))
})
return s, nil
}
// Close releases the store, which its web views keep while they live.
func (s *dataStore) Close() {
if !s.closed {
s.closed = true
release(s.store)
}
}
// websiteDataStore returns the WKWebsiteDataStore of store, the default
// one for nil.
func websiteDataStore(store platform.DataStore) id {
if s, ok := store.(*dataStore); ok {
return s.store
}
return send(class("WKWebsiteDataStore"), "defaultDataStore")
}
// websiteDataTypes returns the set of WKWebsiteDataTypes of types, those
// of the system's WebKit.
func websiteDataTypes(types platform.SiteData) id {
if types.All() {
return send(class("WKWebsiteDataStore"), "allWebsiteDataTypes")
}
set := send(class("NSMutableSet"), "set")
add := func(names ...string) {
for _, name := range names {
if t := constString(libWebKit, name); t != 0 {
send(set, "addObject:", uintptr(t))
}
}
}
if types&platform.SiteDataCookies != 0 {
add("WKWebsiteDataTypeCookies")
}
if types&platform.SiteDataLocalStorage != 0 {
add("WKWebsiteDataTypeLocalStorage", "WKWebsiteDataTypeSessionStorage")
}
if types&platform.SiteDataIndexedDB != 0 {
add("WKWebsiteDataTypeIndexedDBDatabases", "WKWebsiteDataTypeWebSQLDatabases")
}
if types&platform.SiteDataCaches != 0 {
add("WKWebsiteDataTypeDiskCache", "WKWebsiteDataTypeMemoryCache", "WKWebsiteDataTypeOfflineWebApplicationCache", "WKWebsiteDataTypeFetchCache")
}
if types&platform.SiteDataServiceWorkers != 0 {
add("WKWebsiteDataTypeServiceWorkerRegistrations")
}
return set
}
// removeData removes the data of types from store, whatever its age.
func (a appController) removeData(store platform.DataStore, types platform.SiteData, done func(error)) {
withPool(func() {
blk := newBlock(func(_ objc.Block) { a.b.runOnMain(func() { done(nil) }) })
send(websiteDataStore(store), "removeDataOfTypes:modifiedSince:completionHandler:", uintptr(websiteDataTypes(types)), uintptr(send(class("NSDate"), "distantPast")), uintptr(blk))
blk.Release()
})
}