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

RUMM-1955 Multiple calls to trackDatadogEvents handled #739

Merged

Conversation

buranmert
Copy link
Contributor

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 same name crashes the app.

How?

trackDatadogEvents method removes already-added components before adding them.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference

This method removes already-added components before adding them
@buranmert buranmert self-assigned this Jan 27, 2022
@buranmert buranmert requested a review from a team as a code owner January 27, 2022 16:41
Comment on lines 53 to 61
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.com"], hostsSanitizer: mockSanitizer)
(0..<5).forEach { _ in
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.com"], hostsSanitizer: mockSanitizer)
}
Copy link
Member

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.

Comment on lines 85 to 91
let userScriptsWithoutDatadog = userScripts.filter { script in
return !script.source.starts(with: jsPrefix)
}
if userScriptsWithoutDatadog.count != userScripts.count {
removeAllUserScripts()
userScriptsWithoutDatadog.forEach { addUserScript($0) }
}
Copy link
Member

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
   }

   // ...
}

@buranmert buranmert requested a review from ncreated January 31, 2022 14:21
@buranmert buranmert force-pushed the buranmert/RUMM-1955-multiple-calls-to-trackDatadogEvents branch from f5ba8bf to c0359a9 Compare January 31, 2022 14:35
Copy link
Member

@ncreated ncreated left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Co-authored-by: Maciek Grzybowski <maciek.grzybowski@datadoghq.com>
@buranmert buranmert merged commit 5500e7a into master Feb 1, 2022
@buranmert buranmert deleted the buranmert/RUMM-1955-multiple-calls-to-trackDatadogEvents branch February 1, 2022 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants