Skip to content

Commit 130944e

Browse files
Alert engine upon registering ServiceBinding (#126075)
Send a platform message to the engine when the `ServiceBinding` is registered. Framework side of flutter/engine#41733 Addresses flutter/flutter#126033 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --------- Co-authored-by: Greg Spencer <[email protected]>
1 parent cc44359 commit 130944e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

packages/flutter/lib/src/services/binding.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
4545
SystemChannels.platform.setMethodCallHandler(_handlePlatformMessage);
4646
TextInput.ensureInitialized();
4747
readInitialLifecycleStateFromNativeWindow();
48+
initializationComplete();
4849
}
4950

5051
/// The current [ServicesBinding], if one has been created.
@@ -415,6 +416,14 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
415416
void setSystemUiChangeCallback(SystemUiChangeCallback? callback) {
416417
_systemUiChangeCallback = callback;
417418
}
419+
420+
/// Alert the engine that the binding is registered. This instructs the engine to
421+
/// register its top level window handler on Windows. This signals that the app
422+
/// is able to process "System.requestAppExit" signals from the engine.
423+
@protected
424+
Future<void> initializationComplete() async {
425+
await SystemChannels.platform.invokeMethod('System.initializationComplete');
426+
}
418427
}
419428

420429
/// Signature for listening to changes in the [SystemUiMode].

packages/flutter/lib/src/services/system_channels.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ abstract final class SystemChannels {
133133
/// * `System.requestAppExit`: The application has requested that it be
134134
/// terminated. See [ServicesBinding.exitApplication].
135135
///
136+
/// * `System.initializationComplete`: Indicate to the engine the
137+
/// initialization of a binding that may, among other tasks, register a
138+
/// handler for application exit attempts.
139+
///
136140
/// Calls to methods that are not implemented on the shell side are ignored
137141
/// (so it is safe to call methods when the relevant plugin might be missing).
138142
static const MethodChannel platform = OptionalMethodChannel(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/scheduler.dart';
7+
import 'package:flutter/services.dart';
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
class _TestBinding extends BindingBase with SchedulerBinding, ServicesBinding {
11+
@override
12+
Future<void> initializationComplete() async {
13+
return super.initializationComplete();
14+
}
15+
16+
@override
17+
TestDefaultBinaryMessenger get defaultBinaryMessenger => super.defaultBinaryMessenger as TestDefaultBinaryMessenger;
18+
19+
@override
20+
TestDefaultBinaryMessenger createBinaryMessenger() {
21+
Future<ByteData?> keyboardHandler(ByteData? message) async {
22+
return const StandardMethodCodec().encodeSuccessEnvelope(<int, int>{1:1});
23+
}
24+
return TestDefaultBinaryMessenger(
25+
super.createBinaryMessenger(),
26+
outboundHandlers: <String, MessageHandler>{'flutter/keyboard': keyboardHandler},
27+
);
28+
}
29+
}
30+
31+
void main() {
32+
final _TestBinding binding = _TestBinding();
33+
34+
test('can send message on completion of binding initialization', () async {
35+
bool called = false;
36+
binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (MethodCall method) async {
37+
if (method.method == 'System.initializationComplete') {
38+
called = true;
39+
}
40+
return null;
41+
});
42+
await binding.initializationComplete();
43+
expect(called, isTrue);
44+
});
45+
}

0 commit comments

Comments
 (0)