-
Notifications
You must be signed in to change notification settings - Fork 134
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
RUMM-1955 Multiple calls to trackDatadogEvents handled #739
RUMM-1955 Multiple calls to trackDatadogEvents handled #739
Conversation
This method removes already-added components before adding them
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.com"], hostsSanitizer: mockSanitizer) | ||
(0..<5).forEach { _ in | ||
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.com"], hostsSanitizer: mockSanitizer) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very unclear why we add this handler 5 times. If we want to test that "given handler already registered, when registering new handler, it removes the previous one", this should be a separate test case.
let userScriptsWithoutDatadog = userScripts.filter { script in | ||
return !script.source.starts(with: jsPrefix) | ||
} | ||
if userScriptsWithoutDatadog.count != userScripts.count { | ||
removeAllUserScripts() | ||
userScriptsWithoutDatadog.forEach { addUserScript($0) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem we're solving is that calling trackDatadogEvents(in:)
more than once results with a crash:
controller.trackDatadogEvents(in hosts: ["foo.com"])
controller.trackDatadogEvents(in hosts: ["foo.com"]) // 🔥 crash
But this solution looks very odd and unobvious: each time the API is called we're removing all user scripts and then re-adding all but Datadog one.
Can't we just early return in this API if it was already called?
func trackDatadogEvents(in hosts: Set<String>) {
guard !isAlreadyTracked(self) else {
userLogger.warn("`trackDatadogEvents(in:)` was called more than once for the same WebView. Second call will be ignored. Make sure you call it only once.")
return
}
// ...
}
f5ba8bf
to
c0359a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
Tests/DatadogTests/Datadog/RUM/WebView/WKUserContentController+DatadogTests.swift
Outdated
Show resolved
Hide resolved
Co-authored-by: Maciek Grzybowski <maciek.grzybowski@datadoghq.com>
What and why?
If
trackDatadogEvents
method is called multiple times, it crashes the app.The reason is that calling
WKUserContentController.add(messageHandler:, name:)
multiple times with the samename
crashes the app.How?
trackDatadogEvents
method removes already-added components before adding them.Review checklist