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

[web] Use platform detection from Flutter web engine. #147346

Merged
merged 3 commits into from May 7, 2024

Conversation

ditman
Copy link
Member

@ditman ditman commented Apr 25, 2024

Important

Requires the following engine PR:


This PR refactors Flutter foundation's library platform for the web with the same code we use to detect platforms in the engine.

Issues

Testing

Demo app deployed here:

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@ditman ditman requested a review from mdebbar April 25, 2024 00:26
@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Apr 25, 2024
@github-actions github-actions bot added the engine flutter/engine repository. See also e: labels. label Apr 26, 2024
auto-submit bot pushed a commit to flutter/engine that referenced this pull request May 2, 2024
This PR moves the core of `browser_detection.dart` to a location in `dart:ui_web` so it can be used by apps and plugins.

In order for the code to be a little bit tidier in ui_web, it's encapsulated in a singleton instance that can be accessed through `BrowserDetection.instance` or a top level global `browser` in `dart:ui_web`.

## Issues

* Needed to fix: flutter/flutter#128943
* Needed to land: flutter/flutter#147346

## Tests

Updated affected tests. Mostly the update was to call the methods from `web_ui.browser.methodName` rather than a global scope. Also split the tests for this module in two files:

* `engine_browser_detect_test.dart` - with the tests specific to the engine (capability detection, etc...)
* `browser_detect_test.dart` - only the tests pertaining to the "core" of the library.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
@ditman ditman force-pushed the use-engine-browser-detection branch from 7964ddb to b279670 Compare May 2, 2024 18:38
@github-actions github-actions bot removed the engine flutter/engine repository. See also e: labels. label May 2, 2024
@ditman
Copy link
Member Author

ditman commented May 2, 2024

Rebased with latest master, marking as ready for review!

@ditman ditman marked this pull request as ready for review May 2, 2024 18:39
@ditman
Copy link
Member Author

ditman commented May 2, 2024

Rebasing again to give FROB a chance to pick up the engine-related changes as well.

@flutter-dashboard

This comment was marked as resolved.

@Hixie
Copy link
Contributor

Hixie commented May 2, 2024

is it possible to write a test that just spot checks that it's actually using the engine values and not just coincidentally implementing the same algorithm?

@ditman
Copy link
Member Author

ditman commented May 3, 2024

is it possible to write a test that just spot checks that it's actually using the engine values and not just coincidentally implementing the same algorithm?

@Hixie hmm... not sure I can inspect directly that the code I'm using is from the engine, but I think I can mock something wacky in the engine, then ensure that the framework returns the same.

Thanks!

@ditman
Copy link
Member Author

ditman commented May 3, 2024

Tweaked the _testPlatform variable (so it may be overridden by tests), and made it respect the ui_web override. The test now tests the code of the function that translates between OperatingSystem and TargetPlatform.

Comment on lines 22 to 25
final platform.TargetPlatform? _testPlatform = () {
platform.TargetPlatform? result;
// The TargetPlatform used ON WEB TESTS, unless overridden.
//
// Respects the `ui_web.browser.debugOperatingSystemOverride` value (when set).
platform.TargetPlatform? get _testPlatform {
platform.TargetPlatform? testPlatform;
Copy link
Member Author

@ditman ditman May 3, 2024

Choose a reason for hiding this comment

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

Note that _testPlatform used to be a final variable computed from an IIFE, but it needs to be a getter so it can be overridden as needed from tests. The getter ends up as an empty function when asserts are disabled, so I hope this doesn't cause a huge performance hit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe @pragma('dart2js:tryInline') could help?

Copy link
Member Author

Choose a reason for hiding this comment

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

Brought this up with the dart2js people, and they confirmed that the annotation is not needed; @biggs0125 said:

Dart2js should do a pretty good job of inlining that code when assertions are disabled, even without the annotation. We should be able to tell that (1) the code has no side effects and (2) the code always returns null. So it should just inline a null into the callsite.

(I hope it doesn't make debug mode too miserable! In that case, I'd walk back the changes that I added just for testing, and find another way)

ditman added 3 commits May 7, 2024 13:12
* Extract the OperatingSystem -> TargetPlatform conversion to a
  function.
  * Make the switch expression exhaustive.
* Add a special section to the _testPlatform getter so it takes into
  account the web_ui overrides.
* Added browser-only unit-test.
@ditman ditman force-pushed the use-engine-browser-detection branch from 4df122d to 506e055 Compare May 7, 2024 20:36
@ditman ditman added the autosubmit Merge PR when tree becomes green via auto submit App label May 7, 2024
@auto-submit auto-submit bot merged commit d7656f2 into flutter:master May 7, 2024
73 checks passed
@ditman ditman deleted the use-engine-browser-detection branch May 8, 2024 00:36
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 8, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 8, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Theme.of(context).platform running on web returns macOS instead of iOS when accessed from Safari on iPad
3 participants