From d270a882cdba0af99c9337ee5fd52d484f3bffe1 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 16 Jun 2023 09:14:50 -0700 Subject: [PATCH] Revert "Add deprecations to PlatformMessage stuff (#42580)" This reverts commit cfbd7c7e8241d4a1c6a57a07501f3969004a08de. (poke) --- lib/ui/channel_buffers.dart | 14 +------ lib/ui/platform_dispatcher.dart | 19 ++------- lib/ui/window.dart | 11 +---- testing/dart/channel_buffers_test.dart | 18 --------- .../observatory/vmservice_methods_test.dart | 40 ++++++++++++++----- testing/dart/text_test.dart | 19 ++++----- testing/scenario_app/lib/main.dart | 26 ++++++++---- .../lib/src/platform_echo_mixin.dart | 21 ++++++++++ .../scenario_app/lib/src/platform_view.dart | 14 +++---- .../scenario_app/lib/src/poppable_screen.dart | 18 ++------- testing/scenario_app/lib/src/scenario.dart | 11 +++++ 11 files changed, 105 insertions(+), 106 deletions(-) create mode 100644 testing/scenario_app/lib/src/platform_echo_mixin.dart diff --git a/lib/ui/channel_buffers.dart b/lib/ui/channel_buffers.dart index a8616698553a4..ae9fbd78c9d7f 100644 --- a/lib/ui/channel_buffers.dart +++ b/lib/ui/channel_buffers.dart @@ -6,18 +6,13 @@ // KEEP THIS SYNCHRONIZED WITH ../web_ui/lib/channel_buffers.dart part of dart.ui; -/// Deprecated. Migrate to [ChannelCallback] instead. -/// /// Signature for [ChannelBuffers.drain]'s `callback` argument. /// /// The first argument is the data sent by the plugin. /// /// The second argument is a closure that, when called, will send messages /// back to the plugin. -@Deprecated( - 'Migrate to ChannelCallback instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', -) +// TODO(ianh): deprecate this once the framework is migrated to [ChannelCallback]. typedef DrainChannelCallback = Future Function(ByteData? data, PlatformMessageResponseCallback callback); /// Signature for [ChannelBuffers.setListener]'s `callback` argument. @@ -382,8 +377,6 @@ class ChannelBuffers { } } - /// Deprecated. Migrate to [setListener] instead. - /// /// Remove and process all stored messages for a given channel. /// /// This should be called once a channel is prepared to handle messages @@ -391,10 +384,7 @@ class ChannelBuffers { /// /// The messages are processed by calling the given `callback`. Each message /// is processed in its own microtask. - @Deprecated( - 'Migrate to setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', - ) + // TODO(ianh): deprecate once framework uses [setListener]. Future drain(String name, DrainChannelCallback callback) async { final _Channel? channel = _channels[name]; while (channel != null && !channel._queue.isEmpty) { diff --git a/lib/ui/platform_dispatcher.dart b/lib/ui/platform_dispatcher.dart index 47eb65cab3363..1d479391984b3 100644 --- a/lib/ui/platform_dispatcher.dart +++ b/lib/ui/platform_dispatcher.dart @@ -41,13 +41,8 @@ typedef SemanticsActionEventCallback = void Function(SemanticsActionEvent action /// [PlatformDispatcher.onPlatformMessage]. typedef PlatformMessageResponseCallback = void Function(ByteData? data); -/// Deprecated. Migrate to [ChannelBuffers.setListener] instead. -/// /// Signature for [PlatformDispatcher.onPlatformMessage]. -@Deprecated( - 'Migrate to ChannelBuffers.setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', -) +// TODO(ianh): deprecate once framework uses [ChannelBuffers.setListener]. typedef PlatformMessageCallback = void Function(String name, ByteData? data, PlatformMessageResponseCallback? callback); // Signature for _setNeedsReportTimings. @@ -656,8 +651,6 @@ class PlatformDispatcher { @Native(symbol: 'PlatformConfigurationNativeApi::RegisterBackgroundIsolate') external static void __registerBackgroundIsolate(int rootIsolateId); - /// Deprecated. Migrate to [ChannelBuffers.setListener] instead. - /// /// Called whenever this platform dispatcher receives a message from a /// platform-specific plugin. /// @@ -671,17 +664,11 @@ class PlatformDispatcher { /// /// The framework invokes this callback in the same zone in which the callback /// was set. - @Deprecated( - 'Migrate to ChannelBuffers.setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', - ) + // TODO(ianh): Deprecate onPlatformMessage once the framework is moved over + // to using channel buffers exclusively. PlatformMessageCallback? get onPlatformMessage => _onPlatformMessage; PlatformMessageCallback? _onPlatformMessage; Zone _onPlatformMessageZone = Zone.root; - @Deprecated( - 'Migrate to ChannelBuffers.setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', - ) set onPlatformMessage(PlatformMessageCallback? callback) { _onPlatformMessage = callback; _onPlatformMessageZone = Zone.current; diff --git a/lib/ui/window.dart b/lib/ui/window.dart index 689ebdd8f5c38..0b0e43d570334 100644 --- a/lib/ui/window.dart +++ b/lib/ui/window.dart @@ -787,8 +787,6 @@ class SingletonFlutterWindow extends FlutterView { platformDispatcher.sendPlatformMessage(name, data, callback); } - /// Deprecated. Migrate to [ChannelBuffers.setListener] instead. - /// /// Called whenever this window receives a message from a platform-specific /// plugin. /// @@ -804,15 +802,8 @@ class SingletonFlutterWindow extends FlutterView { /// /// The framework invokes this callback in the same zone in which the /// callback was set. - @Deprecated( - 'Migrate to ChannelBuffers.setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', - ) + // TODO(ianh): deprecate once framework uses [ChannelBuffers.setListener]. PlatformMessageCallback? get onPlatformMessage => platformDispatcher.onPlatformMessage; - @Deprecated( - 'Migrate to ChannelBuffers.setListener instead. ' - 'This feature was deprecated after v3.11.0-20.0.pre.', - ) set onPlatformMessage(PlatformMessageCallback? callback) { platformDispatcher.onPlatformMessage = callback; } diff --git a/testing/dart/channel_buffers_test.dart b/testing/dart/channel_buffers_test.dart index 4058719d5f996..4a17705b74c31 100644 --- a/testing/dart/channel_buffers_test.dart +++ b/testing/dart/channel_buffers_test.dart @@ -31,9 +31,6 @@ void main() { called = true; } buffers.push(channel, data, callback); - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { expect(drainedData, equals(data)); assert(!called); @@ -55,9 +52,6 @@ void main() { // Ignoring the returned future because the completion of the drain is // communicated using the `completer`. - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { log.add('callback'); completer.complete(); @@ -83,9 +77,6 @@ void main() { _resize(buffers, channel, 0); buffers.push(channel, data, callback); bool didCall = false; - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { didCall = true; }); @@ -96,9 +87,6 @@ void main() { const String channel = 'foo'; final ui.ChannelBuffers buffers = ui.ChannelBuffers(); bool didCall = false; - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { didCall = true; }); @@ -119,9 +107,6 @@ void main() { buffers.push(channel, three, callback); buffers.push(channel, four, callback); int counter = 0; - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { switch (counter) { case 0: @@ -147,9 +132,6 @@ void main() { buffers.push(channel, two, callback); _resize(buffers, channel, 1); int counter = 0; - // Ignoring the deprecated member use because we're specifically testing - // deprecated API. - // ignore: deprecated_member_use await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async { switch (counter) { case 0: diff --git a/testing/dart/observatory/vmservice_methods_test.dart b/testing/dart/observatory/vmservice_methods_test.dart index fa522b751ed09..7ba1565d0a8d0 100644 --- a/testing/dart/observatory/vmservice_methods_test.dart +++ b/testing/dart/observatory/vmservice_methods_test.dart @@ -74,15 +74,12 @@ void main() { fail('This test must not be run with --disable-vm-service.'); } - final Completer completer = Completer(); - ui.channelBuffers.setListener( - 'flutter/system', - (ByteData? data, ui.PlatformMessageResponseCallback callback) { - final ByteBuffer buffer = data!.buffer; - final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); - completer.complete(utf8.decode(list)); - }, - ); + final Completer completer = Completer(); + ui.PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, ui.PlatformMessageResponseCallback? callback) { + final ByteBuffer buffer = data!.buffer; + final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); + completer.complete(PlatformResponse(name: name, contents: utf8.decode(list))); + }; vmService = await vmServiceConnectUri( 'ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws', @@ -97,11 +94,13 @@ void main() { expect(fontChangeResponse.type, 'Success'); expect( await completer.future, - '{"type":"fontsChange"}', + const PlatformResponse( + name: 'flutter/system', + contents: '{"type":"fontsChange"}', + ), ); } finally { await vmService?.dispose(); - ui.channelBuffers.clearListener('flutter/system'); } }); } @@ -122,3 +121,22 @@ Future getIsolateId(vms.VmService vmService) async { } return null; } + +class PlatformResponse { + const PlatformResponse({ + required this.name, + required this.contents, + }); + + final String name; + final String contents; + + @override + bool operator ==(Object other) => + other is PlatformResponse && + other.name == name && + other.contents == contents; + + @override + int get hashCode => Object.hash(name, contents); +} diff --git a/testing/dart/text_test.dart b/testing/dart/text_test.dart index 23fd1bdba8936..db23069ce0b87 100644 --- a/testing/dart/text_test.dart +++ b/testing/dart/text_test.dart @@ -195,19 +195,20 @@ void testTextRange() { void testLoadFontFromList() { test('loadFontFromList will send platform message after font is loaded', () async { + final PlatformMessageCallback? oldHandler = PlatformDispatcher.instance.onPlatformMessage; + late String actualName; late String message; - channelBuffers.setListener( - 'flutter/system', - (ByteData? data, PlatformMessageResponseCallback? callback) { - assert(data != null); - final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); - message = utf8.decode(list); - }, - ); + PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, PlatformMessageResponseCallback? callback) { + assert(data != null); + actualName = name; + final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); + message = utf8.decode(list); + }; final Uint8List fontData = Uint8List(0); await loadFontFromList(fontData, fontFamily: 'fake'); + PlatformDispatcher.instance.onPlatformMessage = oldHandler; + expect(actualName, 'flutter/system'); expect(message, '{"type":"fontsChange"}'); - channelBuffers.clearListener('flutter/system'); }); } diff --git a/testing/scenario_app/lib/main.dart b/testing/scenario_app/lib/main.dart index b7e69ad119292..06567dba20894 100644 --- a/testing/scenario_app/lib/main.dart +++ b/testing/scenario_app/lib/main.dart @@ -17,13 +17,12 @@ void main() { // FlutterView to the _view property. assert(PlatformDispatcher.instance.implicitView != null); PlatformDispatcher.instance + ..onPlatformMessage = _handlePlatformMessage ..onBeginFrame = _onBeginFrame ..onDrawFrame = _onDrawFrame ..onMetricsChanged = _onMetricsChanged ..onPointerDataPacket = _onPointerDataPacket ..scheduleFrame(); - channelBuffers.setListener('driver', _handleDriverMessage); - channelBuffers.setListener('write_timeline', _handleWriteTimelineMessage); final FlutterView view = PlatformDispatcher.instance.implicitView!; // Asserting that this is greater than zero since this app runs on different @@ -42,8 +41,7 @@ void main() { /// The FlutterView into which the [Scenario]s will be rendered. FlutterView get _view => PlatformDispatcher.instance.implicitView!; -void _handleDriverMessage(ByteData? data, PlatformMessageResponseCallback? callback) { - final Map call = json.decode(utf8.decode(data!.buffer.asUint8List())) as Map; +void _handleDriverMessage(Map call) { final String? methodName = call['method'] as String?; switch (methodName) { case 'set_scenario': @@ -54,9 +52,23 @@ void _handleDriverMessage(ByteData? data, PlatformMessageResponseCallback? callb } } -Future _handleWriteTimelineMessage(ByteData? data, PlatformMessageResponseCallback? callback) async { - final String timelineData = await _getTimelineData(); - callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData()); +Future _handlePlatformMessage( + String name, ByteData? data, PlatformMessageResponseCallback? callback) async { + if (data != null) { + print('$name = ${utf8.decode(data.buffer.asUint8List())}'); + } else { + print(name); + } + + switch (name) { + case 'driver': + _handleDriverMessage(json.decode(utf8.decode(data!.buffer.asUint8List())) as Map); + case 'write_timeline': + final String timelineData = await _getTimelineData(); + callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData()); + default: + currentScenario?.onPlatformMessage(name, data, callback); + } } Future _getTimelineData() async { diff --git a/testing/scenario_app/lib/src/platform_echo_mixin.dart b/testing/scenario_app/lib/src/platform_echo_mixin.dart new file mode 100644 index 0000000000000..9e80929012113 --- /dev/null +++ b/testing/scenario_app/lib/src/platform_echo_mixin.dart @@ -0,0 +1,21 @@ +// 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. + +import 'dart:typed_data'; +import 'dart:ui'; + +import 'scenario.dart'; + +/// Echo platform messages back to the sender. +mixin PlatformEchoMixin on Scenario { + /// Handle a platform message. + @override + void onPlatformMessage( + String name, + ByteData? data, + PlatformMessageResponseCallback? callback, + ) { + view.platformDispatcher.sendPlatformMessage(name, data, null); + } +} diff --git a/testing/scenario_app/lib/src/platform_view.dart b/testing/scenario_app/lib/src/platform_view.dart index 9e3c559130224..aeb9f7c7d05a0 100644 --- a/testing/scenario_app/lib/src/platform_view.dart +++ b/testing/scenario_app/lib/src/platform_view.dart @@ -422,7 +422,6 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario required this.secondId, }) { _nextFrame = _firstFrame; - channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage); } /// The platform view identifier to use for the first platform view. @@ -505,10 +504,15 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario String _lastLifecycleState = ''; - void _onPlatformMessage( + @override + void onPlatformMessage( + String name, ByteData? data, PlatformMessageResponseCallback? callback, ) { + if (name != 'flutter/lifecycle') { + return; + } final String message = utf8.decode(data!.buffer.asUint8List()); if (_lastLifecycleState == 'AppLifecycleState.inactive' && message == 'AppLifecycleState.resumed') { @@ -518,12 +522,6 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario _lastLifecycleState = message; } - - @override - void unmount() { - channelBuffers.clearListener('flutter/lifecycle'); - super.unmount(); - } } /// Platform view with clip rect. diff --git a/testing/scenario_app/lib/src/poppable_screen.dart b/testing/scenario_app/lib/src/poppable_screen.dart index 8f633bffbd9b7..394aaaa247059 100644 --- a/testing/scenario_app/lib/src/poppable_screen.dart +++ b/testing/scenario_app/lib/src/poppable_screen.dart @@ -2,19 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:typed_data'; import 'dart:ui'; import 'channel_util.dart'; +import 'platform_echo_mixin.dart'; import 'scenario.dart'; /// A blank page with a button that pops the page when tapped. -class PoppableScreenScenario extends Scenario { +class PoppableScreenScenario extends Scenario with PlatformEchoMixin { /// Creates the PoppableScreenScenario. - PoppableScreenScenario(super.view) { - channelBuffers.setListener('flutter/platform', _onHandlePlatformMessage); - } + PoppableScreenScenario(super.view); // Rect for the pop button. Only defined once onMetricsChanged is called. Rect? _buttonRect; @@ -81,14 +79,4 @@ class PoppableScreenScenario extends Scenario { // will fail. ); } - - void _onHandlePlatformMessage(ByteData? data, PlatformMessageResponseCallback callback) { - view.platformDispatcher.sendPlatformMessage('flutter/platform', data, null); - } - - @override - void unmount() { - channelBuffers.clearListener('flutter/platform'); - super.unmount(); - } } diff --git a/testing/scenario_app/lib/src/scenario.dart b/testing/scenario_app/lib/src/scenario.dart index f44eeccac6f3f..6c3611b6f4e54 100644 --- a/testing/scenario_app/lib/src/scenario.dart +++ b/testing/scenario_app/lib/src/scenario.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:typed_data'; import 'dart:ui'; /// A scenario to run for testing. @@ -50,4 +51,14 @@ abstract class Scenario { /// /// See [PlatformDispatcher.onPointerDataPacket]. void onPointerDataPacket(PointerDataPacket packet) {} + + /// Called by the program when an engine side platform channel message is + /// received. + /// + /// See [PlatformDispatcher.onPlatformMessage]. + void onPlatformMessage( + String name, + ByteData? data, + PlatformMessageResponseCallback? callback, + ) {} }