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
1 change: 0 additions & 1 deletion .changeset/v4-remove-replay-wireframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
- Masking covers text, inputs, images and web views. Custom-drawn content (custom `draw(_:)` views, maps, video, charts) is now visible in recordings: wrap anything sensitive in `postHogMask()` or tag it `ph-no-capture`.
- Apps whose window root is a SwiftUI view, which previously recorded nothing, now record sessions.
- `PostHogSessionReplayConfig.screenshotMode` is deprecated and has no effect: delete the assignment. Setting it to `false` no longer prevents screenshots; use the masking options instead.
- `screenshotModeBackgroundCapture` now applies without setting `screenshotMode`.
- The replay screen name now names the visible screen (SwiftUI screens report their view name), and is also sent when `screenshotMode` was enabled. It comes from the view controller's type name only: a plain `UIViewController`'s `title` is no longer sent, since it can contain user data.
- Secure-entry text fields now stay masked inside a `postHogNoMask()` or `ph-no-mask` subtree.
5 changes: 5 additions & 0 deletions .changeset/v4-remove-screenshot-background-capture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-ios': major
---

**Breaking:** remove the experimental `PostHogSessionReplayConfig.screenshotModeBackgroundCapture` option. Delete the assignment. Screenshots are now always captured on the main thread, where rendering is fast enough that the off-main workaround is no longer needed.
31 changes: 0 additions & 31 deletions PostHog/Replay/PostHogMaskSettleAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,6 @@
return oldRects
}

/// Per-owner union of two samples taken either side of a render: covers wherever the
/// content sat while the render ran. An owner present in only one sample was still on
/// screen for part of that window, so its rect is emitted as collected rather than
/// discarding the frame β€” a scroll that recycles cells changes the owner set constantly,
/// and dropping every such frame freezes the recording across the whole interaction.
/// Unlike `pairedOldRects` the result is a mask list, not positionally tied to `after`.
/// nil only for a repeated owner within one sample, where the pairing is genuinely
/// ambiguous and there is no safe rect to emit.
static func sweptRects(before: [MaskedRegion]?, after: [MaskedRegion]?) -> [CGRect]? {
guard let before, let after, let beforeByOwner = rectsByOwner(before) else {
return nil
}

var rects: [CGRect] = []
rects.reserveCapacity(max(before.count, after.count))
var seenAfterOwners: Set<ObjectIdentifier> = []
seenAfterOwners.reserveCapacity(after.count)
for region in after {
guard seenAfterOwners.insert(region.owner).inserted else {
return nil
}
// No counterpart means the owner appeared mid-render; cover where it landed.
rects.append(beforeByOwner[region.owner].map { $0.union(region.rect) } ?? region.rect)
}
// Owners that went away mid-render were still displayed for part of it.
for region in before where !seenAfterOwners.contains(region.owner) {
rects.append(region.rect)
}
return rects
}

/// Banded by measured drift:
/// - still (≀ tolerance): rects as collected.
/// - drift (≀ budget): each mask inflated to the swept region (both sampled positions
Expand Down
58 changes: 3 additions & 55 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,6 @@
/// All regions to redact in `window`: heuristic widgets from the hierarchy walk plus the
/// live regions of `postHogMask()` reporters. Returns nil when a reporter hasn't laid out
/// yet β€” the caller must skip the frame rather than capture it under-masked.
/// Pre-existing limitation with `screenshotModeBackgroundCapture` (off by default): pixels
/// render after this collection, so any rect source can go stale for content committed in
/// between.
private func collectMaskedRegions(in window: UIWindow) -> [MaskedRegion]? {
guard !window.hasCameraForReplay() else {
return nil
Expand Down Expand Up @@ -1212,8 +1209,8 @@
}

// The settled path renders here so the pixels come from the same main-thread tick that
// measured the mask rects β€” any later and the presentation tree has moved on. Callers
// that render themselves (background capture, the bridge's first frame) pass false.
// measured the mask rects β€” any later and the presentation tree has moved on. The
// bridge's first frame renders itself and passes false.
let image = renderImage ? window.toImage(preferFidelityRenderer: preferFidelityRenderer) : nil

return ScreenshotCapture(wireframe: wireframe, windowSize: window.bounds.size, timestampDate: Date(), image: image)
Expand Down Expand Up @@ -1441,49 +1438,6 @@
)
}

/// The render sits between two mask samples instead of one: measure geometry, render
/// off-main, measure again, mask the per-owner union β€” provably covering wherever the
/// content sat while the render ran, so no threshold is needed. Always uses
/// `drawHierarchy`, never the presentation-tree renderer: that reads `layer.presentation()`,
/// which is main-only, and this path's other reads already sit inside `main.sync`.
@discardableResult
private func performBracketedBackgroundCapture(window: UIWindow, screenName: String?, postHog: PostHogSDK) -> Bool {
defer { finishScreenshotRender() }

guard let before = DispatchQueue.main.sync(execute: { self.collectMaskedRegions(in: window) }) else {
return false
}
// Off-main on purpose, and the reason this mode exists: drawHierarchy on main was too
// slow to keep up. UIKit documents it as main-thread-only, so it stays experimental
// behind `screenshotModeBackgroundCapture` β€” the bracketing above is what keeps masks
// aligned with pixels despite the render happening on this thread.
let image = window.toImage(preferFidelityRenderer: true)
let capture = DispatchQueue.main.sync { () -> ScreenshotCapture? in
let after = self.collectMaskedRegions(in: window)
guard let rects = Self.sweptRects(before: before, after: after) else {
hedgeLog("[Session Replay] Skipping snapshot: mask samples could not be paired")
return nil
}
// No renderer preference: `renderImage: false` means this call never renders β€” the
// image was already taken off-main above.
return self.collectScreenshotMetadata(window, overrideMaskRects: rects, renderImage: false)
}

guard let capture, let image, postHog.isSessionReplayActive() else {
return false
}

return renderAndEnqueueScreenshot(
capture.wireframe,
window: window,
windowSize: capture.windowSize,
screenName: screenName,
postHog: postHog,
timestampDate: capture.timestampDate,
image: image
)
}

/// Settle-then-shoot: after one display-pipeline depth, unchanged mask geometry proves the
/// displayed frame identical to the current tree, so full-fidelity drawHierarchy is safe
/// (blur/video/Metal intact); drift within budget keeps drawHierarchy with masks swept to
Expand Down Expand Up @@ -1533,13 +1487,7 @@
.flatMap(UIViewController.getViewControllerTypeName)
.flatMap { PostHogScreenNameSanitizer.sanitize(rawScreenName: $0) }

if postHog.config.sessionReplayConfig.screenshotModeBackgroundCapture {
PostHogReplayIntegration.dispatchQueue.async { [weak self] in
self?.performBracketedBackgroundCapture(window: window, screenName: screenName, postHog: postHog)
}
} else {
scheduleSettledCapture(window: window, screenName: screenName, postHog: postHog)
}
scheduleSettledCapture(window: window, screenName: screenName, postHog: postHog)
}

private func handleEventCaptured(event: String) {
Expand Down
10 changes: 0 additions & 10 deletions PostHog/Replay/PostHogSessionReplayConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@
set {} // swiftlint:disable:this unused_setter_value
}

/// Schedule screenshot image capture on a background queue.
///
/// Warning: Enabling this option will trigger Main Thread Checker warnings and may
/// briefly freeze the app the first time a screenshot is captured. Consider disabling
/// Main Thread Checker in your scheme's run diagnostics when enabling this.
///
/// Experimental.
/// Default: false
@objc public var screenshotModeBackgroundCapture: Bool = false

/// Throttle delay used to reduce the number of snapshots captured and reduce performance impact
/// This is used for capturing the screenshot
/// The lower the number more snapshots will be captured but higher the performance impact
Expand Down
2 changes: 1 addition & 1 deletion PostHog/Replay/UIView+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
}

func toImage(afterScreenUpdates: Bool = false, preferFidelityRenderer: Bool = true) -> UIImage? {
// Background capture also enters here; controller traversal belongs on main.
// Replay callers render on main; the sync is defensive, since controller traversal belongs on main.
let hasCamera = {
(self as? UIWindow ?? self.window)?.hasCameraForReplay() ?? false
}
Expand Down
111 changes: 0 additions & 111 deletions PostHogTests/PostHogMaskScenarioTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,116 +497,5 @@
#expect(inflated[0].minY <= 95)
#expect(inflated[0].maxY >= 125.31)
}

@Test("swept union covers both sample positions, in after order")
func sweptRectsCoversBothSamplesInAfterOrder() throws {
let a = NSObject()
let b = NSObject()
let before = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(b, CGRect(x: 0, y: 100, width: 100, height: 20)),
]
// b moves; a is stationary. after is in swapped order vs before, so
// ordering must be recovered from `owner`, not index.
let after = [
region(b, CGRect(x: 0, y: 130, width: 100, height: 20)),
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
]
let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after))
#expect(swept.count == 2)

// swept[0] pairs with after[0] (b): union of y=100..120 and y=130..150.
#expect(swept[0] == CGRect(x: 0, y: 100, width: 100, height: 50))

// swept[1] pairs with after[1] (a): stationary, so it collapses to a's rect.
#expect(swept[1] == CGRect(x: 0, y: 0, width: 100, height: 20))
}

@Test("nil before makes sweptRects nil")
func sweptRectsNilBefore() {
let a = NSObject()
let after = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))]
#expect(PostHogReplayIntegration.sweptRects(before: nil, after: after) == nil)
}

@Test("nil after makes sweptRects nil")
func sweptRectsNilAfter() {
let a = NSObject()
let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))]
#expect(PostHogReplayIntegration.sweptRects(before: before, after: nil) == nil)
}

@Test("an owner that appears mid-render is covered, not dropped")
func sweptRectsCountMismatch() throws {
let a = NSObject()
let b = NSObject()
let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))]
let after = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(b, CGRect(x: 0, y: 100, width: 100, height: 20)),
]
// b appeared mid-render, so it was displayed for part of the window and is covered
// rather than costing the whole frame.
let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after))
#expect(swept == [
CGRect(x: 0, y: 0, width: 100, height: 20),
CGRect(x: 0, y: 100, width: 100, height: 20),
])
}

@Test("duplicate owner in before makes sweptRects nil")
func sweptRectsDuplicateOwnerInBefore() {
let a = NSObject()
let b = NSObject()
let before = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
]
let after = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(b, CGRect(x: 0, y: 100, width: 100, height: 20)),
]
#expect(PostHogReplayIntegration.sweptRects(before: before, after: after) == nil)
}

@Test("duplicate owner in after makes sweptRects nil")
func sweptRectsDuplicateOwnerInAfter() {
let a = NSObject()
let b = NSObject()
let before = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(b, CGRect(x: 0, y: 100, width: 100, height: 20)),
]
let after = [
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
region(a, CGRect(x: 0, y: 0, width: 100, height: 20)),
]
#expect(PostHogReplayIntegration.sweptRects(before: before, after: after) == nil)
}

@Test("a fully recycled owner set covers both samples, not nil")
func sweptRectsDisjointOwnerSet() throws {
let a = NSObject()
let b = NSObject()
let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))]
let after = [region(b, CGRect(x: 0, y: 200, width: 100, height: 20))]
// Neither owner can be paired, but both were on screen during the render window:
// `after` entries come first, then the ones that went away.
let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after))
#expect(swept == [
CGRect(x: 0, y: 200, width: 100, height: 20),
CGRect(x: 0, y: 0, width: 100, height: 20),
])
}

@Test("a stationary owner's swept rect equals its rect unchanged")
func sweptRectsStationaryOwnerUnchanged() throws {
let a = NSObject()
let rect = CGRect(x: 10, y: 20, width: 100, height: 20)
let before = [region(a, rect)]
let after = [region(a, rect)]
let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after))
#expect(swept == [rect])
}
}
#endif
1 change: 0 additions & 1 deletion api/posthog-ios.public-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ PostHog | PostHogSessionReplayConfig.maskAllSandboxedViews | property | @objc va
PostHog | PostHogSessionReplayConfig.maskAllTextInputs | property | @objc var maskAllTextInputs: Bool | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)maskAllTextInputs
PostHog | PostHogSessionReplayConfig.sampleRate | property | @objc var sampleRate: NSNumber? { get set } | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)sampleRate
PostHog | PostHogSessionReplayConfig.screenshotMode | property | @objc var screenshotMode: Bool { get set } | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)screenshotMode
PostHog | PostHogSessionReplayConfig.screenshotModeBackgroundCapture | property | @objc var screenshotModeBackgroundCapture: Bool | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)screenshotModeBackgroundCapture
PostHog | PostHogSessionReplayConfig.throttleDelay | property | @objc var throttleDelay: TimeInterval | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)throttleDelay
PostHog | PostHogSessionReplayConsoleLogConfig | class | @objc class PostHogSessionReplayConsoleLogConfig | c:@M@PostHog@objc(cs)PostHogSessionReplayConsoleLogConfig
PostHog | PostHogSessionReplayConsoleLogConfig.logSanitizer | property | @objc var logSanitizer: (String) -> PostHogConsoleLogEntry? | c:@M@PostHog@objc(cs)PostHogSessionReplayConsoleLogConfig(py)logSanitizer
Expand Down
8 changes: 0 additions & 8 deletions test-audit/replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,6 @@ Production owner: presentation-aware mask collection. The harness requires visib
| 450 | `emptyArraysStill` | R β€” legitimate no-mask geometry settles. |
| 457 | `driftInflationCoversSweepInAfterOrder` | R β€” swept coverage and owner ordering; catches wrong pairing/under-inflation. |
| 486 | `driftInflationCoversTrailBehindBefore` | R β€” displayed-pixel lag requires trailing inflation. |
| 502 | `sweptRectsCoversBothSamplesInAfterOrder` | R β€” union and owner ordering independently specified. |
| 526 | `sweptRectsNilBefore` | R β€” missing sample cannot manufacture a safe union. |
| 533 | `sweptRectsNilAfter` | R β€” same later-sample guard. |
| 540 | `sweptRectsCountMismatch` | R β€” newly appearing owner remains covered. |
| 558 | `sweptRectsDuplicateOwnerInBefore` | R β€” ambiguous duplicate pairing returns nil. |
| 573 | `sweptRectsDuplicateOwnerInAfter` | R β€” symmetric ambiguity guard. |
| 588 | `sweptRectsDisjointOwnerSet` | R β€” recycled owners cover both samples instead of losing disappeared content. |
| 603 | `sweptRectsStationaryOwnerUnchanged` | R β€” stationary union does not distort mask. |

### 5. `PostHogMaskSnapshotTest.swift`

Expand Down
Loading