Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cfbd7c7

Browse files
authored
Add deprecations to PlatformMessage stuff (#42580)
Fixes old TODOs originally added in #22181. The framework appears to be fully migrated off these.
1 parent d4717bd commit cfbd7c7

File tree

11 files changed

+106
-105
lines changed

11 files changed

+106
-105
lines changed

lib/ui/channel_buffers.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
// KEEP THIS SYNCHRONIZED WITH ../web_ui/lib/channel_buffers.dart
77
part of dart.ui;
88

9+
/// Deprecated. Migrate to [ChannelCallback] instead.
10+
///
911
/// Signature for [ChannelBuffers.drain]'s `callback` argument.
1012
///
1113
/// The first argument is the data sent by the plugin.
1214
///
1315
/// The second argument is a closure that, when called, will send messages
1416
/// back to the plugin.
15-
// TODO(ianh): deprecate this once the framework is migrated to [ChannelCallback].
17+
@Deprecated(
18+
'Migrate to ChannelCallback instead. '
19+
'This feature was deprecated after v3.11.0-20.0.pre.',
20+
)
1621
typedef DrainChannelCallback = Future<void> Function(ByteData? data, PlatformMessageResponseCallback callback);
1722

1823
/// Signature for [ChannelBuffers.setListener]'s `callback` argument.
@@ -377,14 +382,19 @@ class ChannelBuffers {
377382
}
378383
}
379384

385+
/// Deprecated. Migrate to [setListener] instead.
386+
///
380387
/// Remove and process all stored messages for a given channel.
381388
///
382389
/// This should be called once a channel is prepared to handle messages
383390
/// (i.e. when a message handler is set up in the framework).
384391
///
385392
/// The messages are processed by calling the given `callback`. Each message
386393
/// is processed in its own microtask.
387-
// TODO(ianh): deprecate once framework uses [setListener].
394+
@Deprecated(
395+
'Migrate to setListener instead. '
396+
'This feature was deprecated after v3.11.0-20.0.pre.',
397+
)
388398
Future<void> drain(String name, DrainChannelCallback callback) async {
389399
final _Channel? channel = _channels[name];
390400
while (channel != null && !channel._queue.isEmpty) {

lib/ui/platform_dispatcher.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ typedef SemanticsActionEventCallback = void Function(SemanticsActionEvent action
4141
/// [PlatformDispatcher.onPlatformMessage].
4242
typedef PlatformMessageResponseCallback = void Function(ByteData? data);
4343

44+
/// Deprecated. Migrate to [ChannelBuffers.setListener] instead.
45+
///
4446
/// Signature for [PlatformDispatcher.onPlatformMessage].
45-
// TODO(ianh): deprecate once framework uses [ChannelBuffers.setListener].
47+
@Deprecated(
48+
'Migrate to ChannelBuffers.setListener instead. '
49+
'This feature was deprecated after v3.11.0-20.0.pre.',
50+
)
4651
typedef PlatformMessageCallback = void Function(String name, ByteData? data, PlatformMessageResponseCallback? callback);
4752

4853
// Signature for _setNeedsReportTimings.
@@ -651,6 +656,8 @@ class PlatformDispatcher {
651656
@Native<Void Function(Int64)>(symbol: 'PlatformConfigurationNativeApi::RegisterBackgroundIsolate')
652657
external static void __registerBackgroundIsolate(int rootIsolateId);
653658

659+
/// Deprecated. Migrate to [ChannelBuffers.setListener] instead.
660+
///
654661
/// Called whenever this platform dispatcher receives a message from a
655662
/// platform-specific plugin.
656663
///
@@ -664,11 +671,17 @@ class PlatformDispatcher {
664671
///
665672
/// The framework invokes this callback in the same zone in which the callback
666673
/// was set.
667-
// TODO(ianh): Deprecate onPlatformMessage once the framework is moved over
668-
// to using channel buffers exclusively.
674+
@Deprecated(
675+
'Migrate to ChannelBuffers.setListener instead. '
676+
'This feature was deprecated after v3.11.0-20.0.pre.',
677+
)
669678
PlatformMessageCallback? get onPlatformMessage => _onPlatformMessage;
670679
PlatformMessageCallback? _onPlatformMessage;
671680
Zone _onPlatformMessageZone = Zone.root;
681+
@Deprecated(
682+
'Migrate to ChannelBuffers.setListener instead. '
683+
'This feature was deprecated after v3.11.0-20.0.pre.',
684+
)
672685
set onPlatformMessage(PlatformMessageCallback? callback) {
673686
_onPlatformMessage = callback;
674687
_onPlatformMessageZone = Zone.current;

lib/ui/window.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ class SingletonFlutterWindow extends FlutterView {
787787
platformDispatcher.sendPlatformMessage(name, data, callback);
788788
}
789789

790+
/// Deprecated. Migrate to [ChannelBuffers.setListener] instead.
791+
///
790792
/// Called whenever this window receives a message from a platform-specific
791793
/// plugin.
792794
///
@@ -802,8 +804,15 @@ class SingletonFlutterWindow extends FlutterView {
802804
///
803805
/// The framework invokes this callback in the same zone in which the
804806
/// callback was set.
805-
// TODO(ianh): deprecate once framework uses [ChannelBuffers.setListener].
807+
@Deprecated(
808+
'Migrate to ChannelBuffers.setListener instead. '
809+
'This feature was deprecated after v3.11.0-20.0.pre.',
810+
)
806811
PlatformMessageCallback? get onPlatformMessage => platformDispatcher.onPlatformMessage;
812+
@Deprecated(
813+
'Migrate to ChannelBuffers.setListener instead. '
814+
'This feature was deprecated after v3.11.0-20.0.pre.',
815+
)
807816
set onPlatformMessage(PlatformMessageCallback? callback) {
808817
platformDispatcher.onPlatformMessage = callback;
809818
}

testing/dart/channel_buffers_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ void main() {
3131
called = true;
3232
}
3333
buffers.push(channel, data, callback);
34+
// Ignoring the deprecated member use because we're specifically testing
35+
// deprecated API.
36+
// ignore: deprecated_member_use
3437
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
3538
expect(drainedData, equals(data));
3639
assert(!called);
@@ -52,6 +55,9 @@ void main() {
5255

5356
// Ignoring the returned future because the completion of the drain is
5457
// communicated using the `completer`.
58+
// Ignoring the deprecated member use because we're specifically testing
59+
// deprecated API.
60+
// ignore: deprecated_member_use
5561
buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
5662
log.add('callback');
5763
completer.complete();
@@ -77,6 +83,9 @@ void main() {
7783
_resize(buffers, channel, 0);
7884
buffers.push(channel, data, callback);
7985
bool didCall = false;
86+
// Ignoring the deprecated member use because we're specifically testing
87+
// deprecated API.
88+
// ignore: deprecated_member_use
8089
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
8190
didCall = true;
8291
});
@@ -87,6 +96,9 @@ void main() {
8796
const String channel = 'foo';
8897
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
8998
bool didCall = false;
99+
// Ignoring the deprecated member use because we're specifically testing
100+
// deprecated API.
101+
// ignore: deprecated_member_use
90102
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
91103
didCall = true;
92104
});
@@ -107,6 +119,9 @@ void main() {
107119
buffers.push(channel, three, callback);
108120
buffers.push(channel, four, callback);
109121
int counter = 0;
122+
// Ignoring the deprecated member use because we're specifically testing
123+
// deprecated API.
124+
// ignore: deprecated_member_use
110125
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
111126
switch (counter) {
112127
case 0:
@@ -132,6 +147,9 @@ void main() {
132147
buffers.push(channel, two, callback);
133148
_resize(buffers, channel, 1);
134149
int counter = 0;
150+
// Ignoring the deprecated member use because we're specifically testing
151+
// deprecated API.
152+
// ignore: deprecated_member_use
135153
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
136154
switch (counter) {
137155
case 0:

testing/dart/observatory/vmservice_methods_test.dart

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ void main() {
7474
fail('This test must not be run with --disable-vm-service.');
7575
}
7676

77-
final Completer<PlatformResponse> completer = Completer<PlatformResponse>();
78-
ui.PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, ui.PlatformMessageResponseCallback? callback) {
79-
final ByteBuffer buffer = data!.buffer;
80-
final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
81-
completer.complete(PlatformResponse(name: name, contents: utf8.decode(list)));
82-
};
77+
final Completer<String> completer = Completer<String>();
78+
ui.channelBuffers.setListener(
79+
'flutter/system',
80+
(ByteData? data, ui.PlatformMessageResponseCallback callback) {
81+
final ByteBuffer buffer = data!.buffer;
82+
final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
83+
completer.complete(utf8.decode(list));
84+
},
85+
);
8386

8487
vmService = await vmServiceConnectUri(
8588
'ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws',
@@ -94,13 +97,11 @@ void main() {
9497
expect(fontChangeResponse.type, 'Success');
9598
expect(
9699
await completer.future,
97-
const PlatformResponse(
98-
name: 'flutter/system',
99-
contents: '{"type":"fontsChange"}',
100-
),
100+
'{"type":"fontsChange"}',
101101
);
102102
} finally {
103103
await vmService?.dispose();
104+
ui.channelBuffers.clearListener('flutter/system');
104105
}
105106
});
106107
}
@@ -121,22 +122,3 @@ Future<String?> getIsolateId(vms.VmService vmService) async {
121122
}
122123
return null;
123124
}
124-
125-
class PlatformResponse {
126-
const PlatformResponse({
127-
required this.name,
128-
required this.contents,
129-
});
130-
131-
final String name;
132-
final String contents;
133-
134-
@override
135-
bool operator ==(Object other) =>
136-
other is PlatformResponse &&
137-
other.name == name &&
138-
other.contents == contents;
139-
140-
@override
141-
int get hashCode => Object.hash(name, contents);
142-
}

testing/dart/text_test.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,19 @@ void testTextRange() {
195195

196196
void testLoadFontFromList() {
197197
test('loadFontFromList will send platform message after font is loaded', () async {
198-
final PlatformMessageCallback? oldHandler = PlatformDispatcher.instance.onPlatformMessage;
199-
late String actualName;
200198
late String message;
201-
PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, PlatformMessageResponseCallback? callback) {
202-
assert(data != null);
203-
actualName = name;
204-
final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
205-
message = utf8.decode(list);
206-
};
199+
channelBuffers.setListener(
200+
'flutter/system',
201+
(ByteData? data, PlatformMessageResponseCallback? callback) {
202+
assert(data != null);
203+
final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
204+
message = utf8.decode(list);
205+
},
206+
);
207207
final Uint8List fontData = Uint8List(0);
208208
await loadFontFromList(fontData, fontFamily: 'fake');
209-
PlatformDispatcher.instance.onPlatformMessage = oldHandler;
210-
expect(actualName, 'flutter/system');
211209
expect(message, '{"type":"fontsChange"}');
210+
channelBuffers.clearListener('flutter/system');
212211
});
213212
}
214213

testing/scenario_app/lib/main.dart

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ void main() {
1717
// FlutterView to the _view property.
1818
assert(PlatformDispatcher.instance.implicitView != null);
1919
PlatformDispatcher.instance
20-
..onPlatformMessage = _handlePlatformMessage
2120
..onBeginFrame = _onBeginFrame
2221
..onDrawFrame = _onDrawFrame
2322
..onMetricsChanged = _onMetricsChanged
2423
..onPointerDataPacket = _onPointerDataPacket
2524
..scheduleFrame();
25+
channelBuffers.setListener('driver', _handleDriverMessage);
26+
channelBuffers.setListener('write_timeline', _handleWriteTimelineMessage);
2627

2728
final FlutterView view = PlatformDispatcher.instance.implicitView!;
2829
// Asserting that this is greater than zero since this app runs on different
@@ -41,7 +42,8 @@ void main() {
4142
/// The FlutterView into which the [Scenario]s will be rendered.
4243
FlutterView get _view => PlatformDispatcher.instance.implicitView!;
4344

44-
void _handleDriverMessage(Map<String, dynamic> call) {
45+
void _handleDriverMessage(ByteData? data, PlatformMessageResponseCallback? callback) {
46+
final Map<String, dynamic> call = json.decode(utf8.decode(data!.buffer.asUint8List())) as Map<String, dynamic>;
4547
final String? methodName = call['method'] as String?;
4648
switch (methodName) {
4749
case 'set_scenario':
@@ -52,23 +54,9 @@ void _handleDriverMessage(Map<String, dynamic> call) {
5254
}
5355
}
5456

55-
Future<void> _handlePlatformMessage(
56-
String name, ByteData? data, PlatformMessageResponseCallback? callback) async {
57-
if (data != null) {
58-
print('$name = ${utf8.decode(data.buffer.asUint8List())}');
59-
} else {
60-
print(name);
61-
}
62-
63-
switch (name) {
64-
case 'driver':
65-
_handleDriverMessage(json.decode(utf8.decode(data!.buffer.asUint8List())) as Map<String, dynamic>);
66-
case 'write_timeline':
67-
final String timelineData = await _getTimelineData();
68-
callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData());
69-
default:
70-
currentScenario?.onPlatformMessage(name, data, callback);
71-
}
57+
Future<void> _handleWriteTimelineMessage(ByteData? data, PlatformMessageResponseCallback? callback) async {
58+
final String timelineData = await _getTimelineData();
59+
callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData());
7260
}
7361

7462
Future<String> _getTimelineData() async {

testing/scenario_app/lib/src/platform_echo_mixin.dart

Lines changed: 0 additions & 21 deletions
This file was deleted.

testing/scenario_app/lib/src/platform_view.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
422422
required this.secondId,
423423
}) {
424424
_nextFrame = _firstFrame;
425+
channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage);
425426
}
426427

427428
/// The platform view identifier to use for the first platform view.
@@ -504,15 +505,10 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
504505

505506
String _lastLifecycleState = '';
506507

507-
@override
508-
void onPlatformMessage(
509-
String name,
508+
void _onPlatformMessage(
510509
ByteData? data,
511510
PlatformMessageResponseCallback? callback,
512511
) {
513-
if (name != 'flutter/lifecycle') {
514-
return;
515-
}
516512
final String message = utf8.decode(data!.buffer.asUint8List());
517513
if (_lastLifecycleState == 'AppLifecycleState.inactive' &&
518514
message == 'AppLifecycleState.resumed') {
@@ -522,6 +518,12 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
522518

523519
_lastLifecycleState = message;
524520
}
521+
522+
@override
523+
void unmount() {
524+
channelBuffers.clearListener('flutter/lifecycle');
525+
super.unmount();
526+
}
525527
}
526528

527529
/// Platform view with clip rect.

0 commit comments

Comments
 (0)