Skip to content

Commit

Permalink
Merge a2a3309 into 489fbb8
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich authored Nov 11, 2024
2 parents 489fbb8 + a2a3309 commit 9de41a6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Sources/Sentry/PrivateSentrySDKOnly.mm
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ + (void)addReplayRedactClasses:(NSArray<Class> *_Nonnull)classes
{
[[PrivateSentrySDKOnly getReplayIntegration].viewPhotographer addRedactClasses:classes];
}

+ (void)setIgnoreWrapperClass:(Class _Nonnull)ignoreClass
{
[[PrivateSentrySDKOnly getReplayIntegration].viewPhotographer
setIgnoreWrapperClass:ignoreClass];
}
#endif

@end
1 change: 1 addition & 0 deletions Sources/Sentry/include/HybridPublic/PrivateSentrySDKOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ typedef void (^SentryOnAppStartMeasurementAvailable)(
+ (NSString *__nullable)getReplayId;
+ (void)addReplayIgnoreClasses:(NSArray<Class> *_Nonnull)classes;
+ (void)addReplayRedactClasses:(NSArray<Class> *_Nonnull)classes;
+ (void)setIgnoreWrapperClass:(Class _Nonnull)ignoreClass;

#endif
+ (nullable NSDictionary<NSString *, id> *)appStartMeasurementWithSpans;
Expand Down
7 changes: 6 additions & 1 deletion Sources/Swift/Tools/SentryViewPhotographer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ class SentryViewPhotographer: NSObject, SentryViewScreenshotProvider {
func addRedactClasses(classes: [AnyClass]) {
redactBuilder.addRedactClasses(classes)
}


@objc(setIgnoreWrapperClass:)
func setIgnoreWrapperClass(wrapperClass: AnyClass) {
redactBuilder.setIgnoreWrapperClass(wrapperClass)
}

#if TEST || TESTCI
func getRedactBuild() -> UIRedactBuilder {
redactBuilder
Expand Down
34 changes: 31 additions & 3 deletions Sources/Swift/Tools/UIRedactBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct RedactRegion {
}

class UIRedactBuilder {

///This is a wrapper which marks it's direct children to be redacted
private var ignoreWrapperClassIdentifier: ObjectIdentifier?
///This is a list of UIView subclasses that will be ignored during redact process
private var ignoreClassesIdentifiers: Set<ObjectIdentifier>
///This is a list of UIView subclasses that need to be redacted from screenshot
Expand Down Expand Up @@ -135,7 +136,17 @@ class UIRedactBuilder {
func addRedactClasses(_ redactClasses: [AnyClass]) {
redactClasses.forEach(addRedactClass(_:))
}


func setIgnoreWrapperClass(_ ignoreClasss: AnyClass) {
ignoreWrapperClassIdentifier = ObjectIdentifier(ignoreClasss)
}

#if TEST || TESTCI
func isIgnoreWrapperClassTestOnly(_ unmaskWrapperClass: AnyClass) -> Bool {
return isIgnoreWrapperClass(unmaskWrapperClass)
}
#endif

/**
This function identifies and returns the regions within a given UIView that need to be redacted, based on the specified redaction options.
Expand Down Expand Up @@ -178,7 +189,24 @@ class UIRedactBuilder {
}

private func shouldIgnore(view: UIView) -> Bool {
return SentryRedactViewHelper.shouldUnmask(view) || containsIgnoreClass(type(of: view))

return SentryRedactViewHelper.shouldUnmask(view) || containsIgnoreClass(type(of: view)) || isParentIgnoreWrapper(view)
}

private func isParentIgnoreWrapper(_ view: UIView) -> Bool {
let parent = view.superview
if let parent = parent {
return isIgnoreWrapperClass(type(of: parent))
}
return false
}

private func isIgnoreWrapperClass(_ unmaskWrapperClass: AnyClass) -> Bool {
if ignoreWrapperClassIdentifier == nil {
return false
}

return ObjectIdentifier(unmaskWrapperClass) == ignoreWrapperClassIdentifier
}

private func shouldRedact(view: UIView) -> Bool {
Expand Down
21 changes: 21 additions & 0 deletions Tests/SentryTests/PrivateSentrySDKOnlyTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@testable import Sentry
import SentryTestUtils
import XCTest

Expand Down Expand Up @@ -369,6 +370,26 @@ class PrivateSentrySDKOnlyTests: XCTestCase {
PrivateSentrySDKOnly.addReplayRedactClasses([UILabel.self])
}

func testAddIgnoreWrapper() throws {
class IgnoreWrapper: UIView {}

SentrySDK.start {
$0.experimental.sessionReplay = SentryReplayOptions(sessionSampleRate: 1, onErrorSampleRate: 1)
$0.setIntegrations([SentrySessionReplayIntegration.self])
}

PrivateSentrySDKOnly.setIgnoreWrapperClass(IgnoreWrapper.self)

let replayIntegration = try getReplayIntegration()

let redactBuilder = replayIntegration.viewPhotographer.getRedactBuild()
XCTAssertTrue(redactBuilder.isIgnoreWrapperClassTestOnly(IgnoreWrapper.self))
}

private func getReplayIntegration() throws -> SentrySessionReplayIntegration {
return try XCTUnwrap(SentrySDK.currentHub().installedIntegrations().first as? SentrySessionReplayIntegration)
}

let VALID_REPLAY_ID = "0eac7ab503354dd5819b03e263627a29"

private class TestSentrySessionReplayIntegration: SentrySessionReplayIntegration {
Expand Down
36 changes: 35 additions & 1 deletion Tests/SentryTests/UIRedactBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,41 @@ class UIRedactBuilderTests: XCTestCase {
let result = sut.redactRegionsFor(view: rootView)
XCTAssertEqual(result.count, 1)
}


func testIgnoreWrappedChildView() {
class IgnoreWrapper: UIView {}
class AnotherLabel: UILabel {}

let sut = getSut()
sut.setIgnoreWrapperClass(IgnoreWrapper.self)

let ignoreWrapper = IgnoreWrapper(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
let wrappedLabel = AnotherLabel(frame: CGRect(x: 20, y: 20, width: 40, height: 40))
ignoreWrapper.addSubview(wrappedLabel)
rootView.addSubview(ignoreWrapper)

let result = sut.redactRegionsFor(view: rootView)
XCTAssertEqual(result.count, 0)
}

func testIgnoreWrappedDirectChildView() {
class IgnoreWrapper: UIView {}
class AnotherLabel: UILabel {}

let sut = getSut()
sut.setIgnoreWrapperClass(IgnoreWrapper.self)

let ignoreWrapper = IgnoreWrapper(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
let wrappedLabel = AnotherLabel(frame: CGRect(x: 20, y: 20, width: 40, height: 40))
let redactedLabel = AnotherLabel(frame: CGRect(x: 10, y: 10, width: 10, height: 10))
wrappedLabel.addSubview(redactedLabel)
ignoreWrapper.addSubview(wrappedLabel)
rootView.addSubview(ignoreWrapper)

let result = sut.redactRegionsFor(view: rootView)
XCTAssertEqual(result.count, 1)
}

func testIgnoreView() {
class AnotherLabel: UILabel {
}
Expand Down

0 comments on commit 9de41a6

Please sign in to comment.