This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Introduce js interop to enable experimental flags on web #17099
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abdd22c
[web] Introduce platform message to enable experiment flags on web
mdebbar d73750f
revert canvaskit changes + use js interop instead of platform messages
mdebbar 23cf2d8
more tests
mdebbar a5cd13e
fix missing license
mdebbar 351bda9
address review comments
mdebbar c2114c4
fix tests
mdebbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| part of engine; | ||
|
|
||
| /// A bag of all experiment flags in the web engine. | ||
| /// | ||
| /// This class also handles platform messages that can be sent to enable/disable | ||
| /// certain experiments at runtime without the need to access engine internals. | ||
| class WebExperiments { | ||
| WebExperiments._() { | ||
| js.context['_flutter_internal_update_experiment'] = updateExperiment; | ||
| registerHotRestartListener(() { | ||
| js.context['_flutter_internal_update_experiment'] = null; | ||
| }); | ||
| } | ||
|
|
||
| static WebExperiments ensureInitialized() { | ||
| if (WebExperiments.instance == null) { | ||
| WebExperiments.instance = WebExperiments._(); | ||
| } | ||
| return WebExperiments.instance; | ||
| } | ||
|
|
||
| static WebExperiments instance; | ||
|
|
||
| /// Experiment flag for using canvas-based text measurement. | ||
| bool get useCanvasText => _useCanvasText ?? false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does allowing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. In tests, we set the flag to a specific value (true or false), then we set it back to |
||
| set useCanvasText(bool enabled) { | ||
| _useCanvasText = enabled; | ||
| } | ||
|
|
||
| bool _useCanvasText = const bool.fromEnvironment( | ||
| 'FLUTTER_WEB_USE_EXPERIMENTAL_CANVAS_TEXT', | ||
| defaultValue: null, | ||
| ); | ||
|
|
||
| /// Reset all experimental flags to their default values. | ||
| void reset() { | ||
| _useCanvasText = null; | ||
| } | ||
|
|
||
| /// Used to enable/disable experimental flags in the web engine. | ||
| void updateExperiment(String name, bool enabled) { | ||
| switch (name) { | ||
| case 'useCanvasText': | ||
| _useCanvasText = enabled; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| import 'dart:html' as html; | ||
| import 'dart:js_util' as js_util; | ||
|
|
||
| import 'package:test/test.dart'; | ||
| import 'package:ui/src/engine.dart'; | ||
|
|
||
| void main() { | ||
| setUp(() { | ||
| WebExperiments.ensureInitialized(); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| WebExperiments.instance.reset(); | ||
| }); | ||
|
|
||
| test('default web experiment values', () { | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| }); | ||
|
|
||
| test('can turn on/off web experiments', () { | ||
| WebExperiments.instance.updateExperiment('useCanvasText', true); | ||
| expect(WebExperiments.instance.useCanvasText, true); | ||
|
|
||
| WebExperiments.instance.updateExperiment('useCanvasText', false); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
|
|
||
| WebExperiments.instance.updateExperiment('useCanvasText', null); | ||
| // Goes back to default value. | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| }); | ||
|
|
||
| test('ignores unknown experiments', () { | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| WebExperiments.instance.updateExperiment('foobarbazqux', true); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| WebExperiments.instance.updateExperiment('foobarbazqux', false); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| }); | ||
|
|
||
| test('can reset web experiments', () { | ||
| WebExperiments.instance.updateExperiment('useCanvasText', true); | ||
| WebExperiments.instance.reset(); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
|
|
||
| WebExperiments.instance.updateExperiment('useCanvasText', true); | ||
| WebExperiments.instance.updateExperiment('foobarbazqux', true); | ||
| WebExperiments.instance.reset(); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| }); | ||
|
|
||
| test('js interop also works', () { | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
|
|
||
| expect(() => jsUpdateExperiment('useCanvasText', true), returnsNormally); | ||
| expect(WebExperiments.instance.useCanvasText, true); | ||
|
|
||
| expect(() => jsUpdateExperiment('useCanvasText', null), returnsNormally); | ||
| expect(WebExperiments.instance.useCanvasText, false); | ||
| }); | ||
|
|
||
| test('js interop throws on wrong type', () { | ||
| expect(() => jsUpdateExperiment(123, true), throwsA(anything)); | ||
| expect(() => jsUpdateExperiment('foo', 123), throwsA(anything)); | ||
| expect(() => jsUpdateExperiment('foo', 'bar'), throwsA(anything)); | ||
| expect(() => jsUpdateExperiment(false, 'foo'), throwsA(anything)); | ||
| }); | ||
| } | ||
|
|
||
| void jsUpdateExperiment(dynamic name, dynamic enabled) { | ||
| js_util.callMethod( | ||
| html.window, | ||
| '_flutter_internal_update_experiment', | ||
| <dynamic>[name, enabled], | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm surprised this works without
jsifyorallowInteropor some such 🤔