diff --git a/src/main/platform/macos/cocoa-website-data.ts b/src/main/platform/macos/cocoa-website-data.ts index e8f54f8..c04eef6 100644 --- a/src/main/platform/macos/cocoa-website-data.ts +++ b/src/main/platform/macos/cocoa-website-data.ts @@ -1,19 +1,28 @@ import { makeOneShotBlock } from './cocoa-block'; import { bounded } from './cocoa-cookies'; -import { msgSendPtr3 } from './cocoa-msgsend-variants'; +import { nsString } from './cocoa-foundation'; +import { msgSendPtr, msgSendPtr3 } from './cocoa-msgsend-variants'; import { cocoa } from './cocoa-runtime'; import { loadWebKit } from './cocoa-webkit'; -/** Remove all website data from the default store; resolves when it completes (D022b). */ +/** Remove the default store's website data, bar the in-process memory cache; resolves when done (D022b). */ export const clearStorageData = (): Promise => bounded('clearStorageData', (resolve) => { loadWebKit(); const rt = cocoa(); const store = rt.classes.get('WKWebsiteDataStore'); + const types = msgSendPtr( + rt.classes.get('NSMutableSet'), + rt.selectors.get('setWithSet:'), + rt.msgSend(store, rt.selectors.get('allWebsiteDataTypes')), + ); + // Only the memory cache waits on every web process, and a hidden view's process + // runs DarwinBG: on a saturated CPU it never replies, so the removal never completes. + msgSendPtr(types, rt.selectors.get('removeObject:'), nsString('WKWebsiteDataTypeMemoryCache')); msgSendPtr3( rt.msgSend(store, rt.selectors.get('defaultDataStore')), rt.selectors.get('removeDataOfTypes:modifiedSince:completionHandler:'), - rt.msgSend(store, rt.selectors.get('allWebsiteDataTypes')), + types, rt.msgSend(rt.classes.get('NSDate'), rt.selectors.get('distantPast')), makeOneShotBlock(() => resolve(undefined), []), ); diff --git a/tests/integration/macos/session.test.ts b/tests/integration/macos/session.test.ts index c0b354d..5bb540f 100644 --- a/tests/integration/macos/session.test.ts +++ b/tests/integration/macos/session.test.ts @@ -2,6 +2,30 @@ import { describe, expect, test } from 'bun:test'; import { currentPlatform } from '../../../src/common/platform'; import { session } from '../../../src/main/api/session'; import { createMacOSApplication } from '../../../src/main/platform/macos/cocoa-backend'; +import { nsStringToString } from '../../../src/main/platform/macos/cocoa-foundation'; +import { + msgSendI64, + msgSendReturnsI64, +} from '../../../src/main/platform/macos/cocoa-msgsend-variants'; +import { cocoa } from '../../../src/main/platform/macos/cocoa-runtime'; + +/** The WebContent pid behind the window titled `title` (its content view is the WKWebView). */ +const webProcessPid = (title: string): number => { + const rt = cocoa(); + const app = rt.msgSend(rt.classes.get('NSApplication'), rt.selectors.get('sharedApplication')); + const windows = rt.msgSend(app, rt.selectors.get('windows')); + const count = Number(rt.msgSend(windows, rt.selectors.get('count'))); + for (let i = 0; i < count; i += 1) { + const win = msgSendI64(windows, rt.selectors.get('objectAtIndex:'), BigInt(i)); + if (nsStringToString(rt.msgSend(win, rt.selectors.get('title'))) === title) { + const view = rt.msgSend(win, rt.selectors.get('contentView')); + return Number( + BigInt.asIntN(32, msgSendReturnsI64(view, rt.selectors.get('_webProcessIdentifier'))), + ); + } + } + return 0; +}; /** * `session.clearStorageData` against a real `WKWebsiteDataStore`. Proves the @@ -24,5 +48,37 @@ if (currentPlatform() === 'macos') { app.quit(); } }); + + // A hidden view's web process runs DarwinBG and gets no CPU on a saturated machine; + // SIGSTOP stands in for that starvation deterministically. + test('does not wait on a web process that gets no CPU', async () => { + const app = createMacOSApplication(); + app.start(); + const win = app.createWindow({ + width: 200, + height: 100, + title: 'session-stopped', + show: true, + }); + let pid = 0; + try { + win.webContents.loadHTML('x', 'about:blank'); + await win.webContents.executeJavaScript('1'); + pid = webProcessPid('session-stopped'); + expect(pid).toBeGreaterThan(0); + process.kill(pid, 'SIGSTOP'); + const outcome = await Promise.race([ + session.defaultSession.clearStorageData().then(() => 'resolved'), + Bun.sleep(5000).then(() => 'pending'), + ]); + expect(outcome).toBe('resolved'); + } finally { + if (pid > 0) { + process.kill(pid, 'SIGCONT'); + } + win.close(); + app.quit(); + } + }); }); } diff --git a/website/src/content/docs/api/session.md b/website/src/content/docs/api/session.md index 625d521..5549ea6 100644 --- a/website/src/content/docs/api/session.md +++ b/website/src/content/docs/api/session.md @@ -137,7 +137,7 @@ This is the all-or-nothing form. Passing Electron's `options` argument (`origin` Platform notes on exactly _what_ gets cleared: -- **macOS** - clears **all** website data: cache, cookies, local and session storage, IndexedDB, and the rest. +- **macOS** - clears **all** website data: cache, cookies, local and session storage, IndexedDB, and the rest. Open pages keep their in-memory resource cache until they reload. - **Windows** - clears cookies and the fetch/HTTP caches. Clearing local storage and IndexedDB is a follow-up, so it is **not** the full wipe macOS performs yet. - **Linux** - not yet wired: `clearStorageData` currently rejects with an `UnsupportedPlatformError` (`WebKitWebsiteDataManager` clearing is a follow-up).