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

[url_launcher] When not fully loaded, clicking close causes the callback to not be triggered correctly. #8582

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

StanleyCocos
Copy link
Contributor

@StanleyCocos StanleyCocos commented Feb 7, 2025

When SFSafariViewController is loading a webpage, if the close button is clicked during the loading process, it can prevent the callback from being triggered correctly.

List which issues are fixed by this PR. You must list at least one issue.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I [linked to at least one issue that this PR fixes] in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes].
  • I updated CHANGELOG.md to add a description of the change, [following repository CHANGELOG style], or this PR is [exempt from CHANGELOG changes].
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@StanleyCocos

This comment was marked as resolved.

@stuartmorgan
Copy link
Contributor

This should be testable with native unit tests that directly drive calls to the URLLaunchSession.

@@ -12,6 +12,7 @@ final class URLLaunchSession: NSObject, SFSafariViewControllerDelegate {

private let completion: OpenInSafariCompletionHandler
private let url: URL
private var isLoadCompleted: Bool?
Copy link
Contributor

Choose a reason for hiding this comment

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

A nullable boolean should only be used when there are three conceptually different states. The code in this PR only actually recognizes two.

@StanleyCocos

This comment was marked as resolved.

@stuartmorgan stuartmorgan changed the title fix(url_launcher): When not fully loaded, clicking close causes the callback to not be triggered correctly. [url_launcher] When not fully loaded, clicking close causes the callback to not be triggered correctly. Feb 8, 2025
@stuartmorgan
Copy link
Contributor

That's a Dart unit test, which does not run the code you changed; native unit tests are here, as described in our contributor docs.

@stuartmorgan stuartmorgan marked this pull request as draft February 8, 2025 22:12
@stuartmorgan
Copy link
Contributor

(Marking as Draft pending the test, so it doesn't show up in review queues.)

@StanleyCocos StanleyCocos marked this pull request as ready for review February 10, 2025 03:17
@StanleyCocos
Copy link
Contributor Author

Oh, my God. I forgot. Thanks for the reminder. I’ve added the test. If you have time, you can continue review

@stuartmorgan stuartmorgan added the triage-ios Should be looked at in iOS triage label Feb 10, 2025
@jmagman jmagman requested review from loic-sharma and removed request for jmagman February 12, 2025 22:25

* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
* Fixes When not fully loaded, clicking close causes the callback to not be triggered correctly.
Copy link
Member

@loic-sharma loic-sharma Feb 12, 2025

Choose a reason for hiding this comment

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

What do you think of this?

Suggested change
* Fixes When not fully loaded, clicking close causes the callback to not be triggered correctly.
* Ensures the completion callback is invoked if the user dismisses the Safari view before the initial URL load completes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has been adjusted.

}

/// Called when the user finishes using the Safari view controller.
///
/// - Parameter controller: The Safari view controller.
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
if !isLoadCompleted {
completion(.success(.failedToLoad))
Copy link
Member

Choose a reason for hiding this comment

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

Is .failedToLoad the correct result here? Or we should add a new InAppLoadResult.dismissed result?

If this is the correct result, should we update this comment to indicate this case can happen if the user dismisses the Safari view before the initial load completes?

/// The URL did not load successfully.
failedToLoad,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first, I did consider adding a new InAppLoadResult to describe the close action, but it seems it could lead to confusion among developers. This is because, currently, after a successful load, clicking the close button does not trigger any callback.

Additionally, when the user clicks close before the loading is complete, triggering the failedToLoad callback seems reasonable in a broader sense, as the interruption of the loading process can be considered a loading failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this is the correct result, should we update this comment to indicate this case can happen if the user dismisses the Safari view before the initial load completes?

Do you think it’s possible to modify the comment like this:

// The URL did not load successfully (or close the SFSafariViewController earlier)

expectation.fulfill()
}
plugin.closeSafariViewController()
wait(for: [expectation], timeout: 30)
Copy link
Member

@loic-sharma loic-sharma Feb 13, 2025

Choose a reason for hiding this comment

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

Is this timeout: 30 necessary or is timeout: 1 sufficient?

From my understanding plugin.closeSafariViewController() calls the completion callback, which in turn calls expectation.fulfill(). The expectation should be already be fulfilled at this point.

I apologize if this is a silly question, I'm not very familiar with XCTest.

Copy link
Contributor

Choose a reason for hiding this comment

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

Our standard for expectation timeouts in the repo is 30 (older code still uses lower timeouts). Flutter style is not to have timeouts at all but that's awkward with XCTest APIs, so we just use a value that is absurdly long for unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Under normal circumstances, wait(for: [expectation], timeout: 30) won’t actually wait, because the closure has already been triggered before it. The 30-second timeout is set to prevent accidentally removing this PR functionality. If this PR functionality is deleted, setting the wait time to 1 might not make sense, as the callback may never be reached for verification.

let plugin = createPlugin(launcher: launcher)

let expectation = XCTestExpectation(description: "completion called")
plugin.openUrlInSafariViewController(url: "https://flutter.dev") { result in
Copy link
Member

Choose a reason for hiding this comment

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

I worry that this test would flake if https://flutter.dev loads before the test calls closeSafariViewController. Is there some URL we could test that is known to hang forever? Or am I overthinking this? cc @stuartmorgan

Copy link
Contributor

@stuartmorgan stuartmorgan Feb 13, 2025

Choose a reason for hiding this comment

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

I would not expect it to be possible for that to happen. Unless SFSafariViewController's didCompleteInitialLoad callback happens on a non-main thread (which I wouldn't expect since it passes the view controller as a parameter), it shouldn't be possible for the callback to trigger until the runloop runs again, which I would think could not happen before the wait call.

There are some assumptions in there, so I wouldn't be confident in saying that it's impossible, but I'd be fine with waiting to see if it does actually happen before we add a bunch of extra logic (since the reliable way to avoid that would be dependency injection of a fake SFSafariViewController that we know won't load anything) to avoid I case that I strongly suspect is in fact impossible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: url_launcher platform-ios triage-ios Should be looked at in iOS triage
Projects
None yet
3 participants