Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Session replay masking not working inside scroll view #4498

Merged
merged 4 commits into from
Nov 7, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- Expose `SentrySessionReplayIntegration-Hybrid.h` as `private` (#4486)
- Add `maskedViewClasses` and `unmaskedViewClasses` to SentryReplayOptions init via dict (#4492)

### Fixes

- Session replay masking not working inside scroll view (#4498)

## 8.39.0

### Removal of Experimental API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TableViewController: UITableViewController {

let w = 1.0 - (Double(indexPath.row) / 99)
cell.backgroundColor = UIColor(white: CGFloat(w), alpha: 1)
cell.textLabel?.text = "Row #\(indexPath.row)"

return cell
}
Expand Down
25 changes: 13 additions & 12 deletions Sources/Swift/Tools/UIRedactBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ class UIRedactBuilder {
var redactingRegions = [RedactRegion]()

self.mapRedactRegion(fromView: view,
relativeTo: nil,
redacting: &redactingRegions,
rootFrame: view.frame,
transform: CGAffineTransform.identity)
rootFrame: view.frame)

var swiftUIRedact = [RedactRegion]()
var otherRegions = [RedactRegion]()
Expand Down Expand Up @@ -198,12 +198,12 @@ class UIRedactBuilder {
return image.imageAsset?.value(forKey: "_containingBundle") == nil
}

private func mapRedactRegion(fromView view: UIView, redacting: inout [RedactRegion], rootFrame: CGRect, transform: CGAffineTransform, forceRedact: Bool = false) {
private func mapRedactRegion(fromView view: UIView, relativeTo parentLayer: CALayer?, redacting: inout [RedactRegion], rootFrame: CGRect, forceRedact: Bool = false) {
guard !redactClassesIdentifiers.isEmpty && !view.isHidden && view.alpha != 0 else { return }

let layer = view.layer.presentation() ?? view.layer

let newTransform = concatenateTranform(transform, with: layer)
let newTransform = getTranform(from: layer, withParent: parentLayer)

let ignore = !forceRedact && shouldIgnore(view: view)
let swiftUI = SentryRedactViewHelper.shouldRedactSwiftUI(view)
Expand Down Expand Up @@ -233,23 +233,24 @@ class UIRedactBuilder {
redacting.append(RedactRegion(size: layer.bounds.size, transform: newTransform, type: .clipEnd))
}
for subview in view.subviews.sorted(by: { $0.layer.zPosition < $1.layer.zPosition }) {
mapRedactRegion(fromView: subview, redacting: &redacting, rootFrame: rootFrame, transform: newTransform, forceRedact: enforceRedact)
mapRedactRegion(fromView: subview, relativeTo: layer, redacting: &redacting, rootFrame: rootFrame, forceRedact: enforceRedact)
}
if view.clipsToBounds {
redacting.append(RedactRegion(size: layer.bounds.size, transform: newTransform, type: .clipBegin))
}
}

/**
Apply the layer transformation and position to given transformation.
Gets a transform that represents the layer global position.
*/
private func concatenateTranform(_ transform: CGAffineTransform, with layer: CALayer) -> CGAffineTransform {
private func getTranform(from layer: CALayer, withParent parentLayer: CALayer?) -> CGAffineTransform {
let size = layer.bounds.size
let layerMiddle = CGPoint(x: size.width * layer.anchorPoint.x, y: size.height * layer.anchorPoint.y)

var newTransform = transform.translatedBy(x: layer.position.x, y: layer.position.y)
let anchorPoint = CGPoint(x: size.width * layer.anchorPoint.x, y: size.height * layer.anchorPoint.y)
let position = parentLayer?.convert(layer.position, to: nil) ?? layer.position

var newTransform = CGAffineTransform(translationX: position.x, y: position.y)
newTransform = CATransform3DGetAffineTransform(layer.transform).concatenating(newTransform)
return newTransform.translatedBy(x: -layerMiddle.x, y: -layerMiddle.y)
return newTransform.translatedBy(x: -anchorPoint.x, y: -anchorPoint.y)
}

/**
Expand Down
15 changes: 15 additions & 0 deletions Tests/SentryTests/SentryViewPhotographerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ class SentryViewPhotographerTests: XCTestCase {
assertColor(pixel1, .green)
}

func testLabelInsideScrollView() throws {
let label1 = UILabel(frame: CGRect(x: 0, y: 25, width: 50, height: 25))
label1.text = "Test"
label1.textColor = .green

let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
scrollView.addSubview(label1)
scrollView.contentOffset = CGPoint(x: 0, y: 25)

let image = try XCTUnwrap(prepare(views: [scrollView]))
let pixel1 = color(at: CGPoint(x: 10, y: 10), in: image)

assertColor(pixel1, .green)
}

private func assertColor(_ color1: UIColor, _ color2: UIColor) {
let sRGBColor1 = color1.cgColor.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil)
let sRGBColor2 = color2.cgColor.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil)
Expand Down
Loading