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

Commit 1feb930

Browse files
Add callback to Embedder API to respond to new channel listeners, and use for Windows lifecycle (#44827)
Supplant the temporary solution #44238 with a more elegant solution with am embedder API callback. The windows engine provides a callback that enables graceful exit and app lifecycle when the platform and lifecycle channels are listened to, respectively. flutter/flutter#131616 ## 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] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [x] 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 [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [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: Chris Bracken <[email protected]>
1 parent 01a1579 commit 1feb930

31 files changed

+269
-24
lines changed

lib/ui/channel_buffers.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ class ChannelBuffers {
380380
assert(!name.contains('\u0000'), 'Channel names must not contain U+0000 NULL characters.');
381381
final _Channel channel = _channels.putIfAbsent(name, () => _Channel());
382382
channel.setListener(callback);
383+
sendChannelUpdate(name, listening: true);
383384
}
384385

385386
/// Clears the listener for the specified channel.
@@ -392,9 +393,15 @@ class ChannelBuffers {
392393
final _Channel? channel = _channels[name];
393394
if (channel != null) {
394395
channel.clearListener();
396+
sendChannelUpdate(name, listening: false);
395397
}
396398
}
397399

400+
@Native<Void Function(Handle, Bool)>(symbol: 'PlatformConfigurationNativeApi::SendChannelUpdate')
401+
external static void _sendChannelUpdate(String name, bool listening);
402+
403+
void sendChannelUpdate(String name, {required bool listening}) => _sendChannelUpdate(name, listening);
404+
398405
/// Deprecated. Migrate to [setListener] instead.
399406
///
400407
/// Remove and process all stored messages for a given channel.

lib/ui/dart_ui.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ typedef CanvasPath Path;
110110
V(PlatformConfigurationNativeApi::GetRootIsolateToken, 0) \
111111
V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate, 1) \
112112
V(PlatformConfigurationNativeApi::SendPortPlatformMessage, 4) \
113+
V(PlatformConfigurationNativeApi::SendChannelUpdate, 2) \
113114
V(PlatformConfigurationNativeApi::GetScaledFontSize, 2) \
114115
V(DartRuntimeHooks::Logger_PrintDebugString, 1) \
115116
V(DartRuntimeHooks::Logger_PrintString, 1) \

lib/ui/window/platform_configuration.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,12 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate(
631631
dart_state->SetPlatformMessageHandler(weak_platform_message_handler);
632632
}
633633

634+
void PlatformConfigurationNativeApi::SendChannelUpdate(const std::string& name,
635+
bool listening) {
636+
UIDartState::Current()->platform_configuration()->client()->SendChannelUpdate(
637+
name, listening);
638+
}
639+
634640
double PlatformConfigurationNativeApi::GetScaledFontSize(
635641
double unscaled_font_size,
636642
int configuration_id) {

lib/ui/window/platform_configuration.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ class PlatformConfigurationClient {
207207
///
208208
virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id) = 0;
209209

210+
//--------------------------------------------------------------------------
211+
/// @brief Invoked when a listener is registered on a platform channel.
212+
///
213+
/// @param[in] name The name of the platform channel to which a
214+
/// listener has been registered or cleared.
215+
///
216+
/// @param[in] listening Whether the listener has been set (true) or
217+
/// cleared (false).
218+
///
219+
virtual void SendChannelUpdate(std::string name, bool listening) = 0;
220+
210221
//--------------------------------------------------------------------------
211222
/// @brief Synchronously invokes platform-specific APIs to apply the
212223
/// system text scaling on the given unscaled font size.
@@ -571,6 +582,8 @@ class PlatformConfigurationNativeApi {
571582
static void RespondToPlatformMessage(int response_id,
572583
const tonic::DartByteData& data);
573584

585+
static void SendChannelUpdate(const std::string& name, bool listening);
586+
574587
//--------------------------------------------------------------------------
575588
/// @brief Requests the Dart VM to adjusts the GC heuristics based on
576589
/// the requested `performance_mode`. Returns the old performance

lib/web_ui/lib/channel_buffers.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ class ChannelBuffers {
257257
return true;
258258
}());
259259
}
260+
261+
void sendChannelUpdate(String name, {required bool listening}) {}
260262
}
261263

262264
final ChannelBuffers channelBuffers = ChannelBuffers();

runtime/runtime_controller.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ RuntimeController::ComputePlatformResolvedLocale(
401401
return client_.ComputePlatformResolvedLocale(supported_locale_data);
402402
}
403403

404+
// |PlatformConfigurationClient|
405+
void RuntimeController::SendChannelUpdate(std::string name, bool listening) {
406+
client_.SendChannelUpdate(std::move(name), listening);
407+
}
408+
404409
Dart_Port RuntimeController::GetMainPort() {
405410
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
406411
return root_isolate ? root_isolate->main_port() : ILLEGAL_PORT;

runtime/runtime_controller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ class RuntimeController : public PlatformConfigurationClient {
683683
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale(
684684
const std::vector<std::string>& supported_locale_data) override;
685685

686+
// |PlatformConfigurationClient|
687+
void SendChannelUpdate(std::string name, bool listening) override;
688+
686689
// |PlatformConfigurationClient|
687690
double GetScaledFontSize(double unscaled_font_size,
688691
int configuration_id) const override;

runtime/runtime_delegate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class RuntimeDelegate {
5454
virtual std::weak_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
5555
const = 0;
5656

57+
virtual void SendChannelUpdate(std::string name, bool listening) = 0;
58+
5759
virtual double GetScaledFontSize(double unscaled_font_size,
5860
int configuration_id) const = 0;
5961

shell/common/engine.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ std::weak_ptr<PlatformMessageHandler> Engine::GetPlatformMessageHandler()
570570
return delegate_.GetPlatformMessageHandler();
571571
}
572572

573+
void Engine::SendChannelUpdate(std::string name, bool listening) {
574+
delegate_.OnEngineChannelUpdate(std::move(name), listening);
575+
}
576+
573577
void Engine::LoadDartDeferredLibrary(
574578
intptr_t loading_unit_id,
575579
std::unique_ptr<const fml::Mapping> snapshot_data,

shell/common/engine.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
294294
virtual const std::shared_ptr<PlatformMessageHandler>&
295295
GetPlatformMessageHandler() const = 0;
296296

297+
//--------------------------------------------------------------------------
298+
/// @brief Invoked when a listener is registered on a platform channel.
299+
///
300+
/// @param[in] name The name of the platform channel to which a
301+
/// listener has been registered or cleared.
302+
///
303+
/// @param[in] listening Whether the listener has been set (true) or
304+
/// cleared (false).
305+
///
306+
virtual void OnEngineChannelUpdate(std::string name, bool listening) = 0;
307+
297308
//--------------------------------------------------------------------------
298309
/// @brief Synchronously invokes platform-specific APIs to apply the
299310
/// system text scaling on the given unscaled font size.
@@ -977,6 +988,9 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
977988
std::weak_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
978989
const override;
979990

991+
// |RuntimeDelegate|
992+
void SendChannelUpdate(std::string name, bool listening) override;
993+
980994
// |RuntimeDelegate|
981995
double GetScaledFontSize(double unscaled_font_size,
982996
int configuration_id) const override;

0 commit comments

Comments
 (0)