Skip to content
Merged
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
15 changes: 12 additions & 3 deletions src/main/platform/macos/cocoa-website-data.ts
Original file line number Diff line number Diff line change
@@ -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<void> =>
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), []),
);
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/macos/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('<html><body>x</body></html>', '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();
}
});
});
}
2 changes: 1 addition & 1 deletion website/src/content/docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
Loading