Add callback to Embedder API to respond to new channel listeners, and use for Windows lifecycle (flutter/engine#44827)
Supplant the temporary solution https://github.com/flutter/engine/pull/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. https://github.com/flutter/flutter/issues/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 <chris@bracken.jp>
This commit is contained in:
@@ -380,6 +380,7 @@ class ChannelBuffers {
|
||||
assert(!name.contains('\u0000'), 'Channel names must not contain U+0000 NULL characters.');
|
||||
final _Channel channel = _channels.putIfAbsent(name, () => _Channel());
|
||||
channel.setListener(callback);
|
||||
sendChannelUpdate(name, listening: true);
|
||||
}
|
||||
|
||||
/// Clears the listener for the specified channel.
|
||||
@@ -392,9 +393,15 @@ class ChannelBuffers {
|
||||
final _Channel? channel = _channels[name];
|
||||
if (channel != null) {
|
||||
channel.clearListener();
|
||||
sendChannelUpdate(name, listening: false);
|
||||
}
|
||||
}
|
||||
|
||||
@Native<Void Function(Handle, Bool)>(symbol: 'PlatformConfigurationNativeApi::SendChannelUpdate')
|
||||
external static void _sendChannelUpdate(String name, bool listening);
|
||||
|
||||
void sendChannelUpdate(String name, {required bool listening}) => _sendChannelUpdate(name, listening);
|
||||
|
||||
/// Deprecated. Migrate to [setListener] instead.
|
||||
///
|
||||
/// Remove and process all stored messages for a given channel.
|
||||
|
||||
@@ -110,6 +110,7 @@ typedef CanvasPath Path;
|
||||
V(PlatformConfigurationNativeApi::GetRootIsolateToken, 0) \
|
||||
V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate, 1) \
|
||||
V(PlatformConfigurationNativeApi::SendPortPlatformMessage, 4) \
|
||||
V(PlatformConfigurationNativeApi::SendChannelUpdate, 2) \
|
||||
V(PlatformConfigurationNativeApi::GetScaledFontSize, 2) \
|
||||
V(DartRuntimeHooks::Logger_PrintDebugString, 1) \
|
||||
V(DartRuntimeHooks::Logger_PrintString, 1) \
|
||||
|
||||
@@ -631,6 +631,12 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate(
|
||||
dart_state->SetPlatformMessageHandler(weak_platform_message_handler);
|
||||
}
|
||||
|
||||
void PlatformConfigurationNativeApi::SendChannelUpdate(const std::string& name,
|
||||
bool listening) {
|
||||
UIDartState::Current()->platform_configuration()->client()->SendChannelUpdate(
|
||||
name, listening);
|
||||
}
|
||||
|
||||
double PlatformConfigurationNativeApi::GetScaledFontSize(
|
||||
double unscaled_font_size,
|
||||
int configuration_id) {
|
||||
|
||||
@@ -207,6 +207,17 @@ class PlatformConfigurationClient {
|
||||
///
|
||||
virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id) = 0;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/// @brief Invoked when a listener is registered on a platform channel.
|
||||
///
|
||||
/// @param[in] name The name of the platform channel to which a
|
||||
/// listener has been registered or cleared.
|
||||
///
|
||||
/// @param[in] listening Whether the listener has been set (true) or
|
||||
/// cleared (false).
|
||||
///
|
||||
virtual void SendChannelUpdate(std::string name, bool listening) = 0;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/// @brief Synchronously invokes platform-specific APIs to apply the
|
||||
/// system text scaling on the given unscaled font size.
|
||||
@@ -571,6 +582,8 @@ class PlatformConfigurationNativeApi {
|
||||
static void RespondToPlatformMessage(int response_id,
|
||||
const tonic::DartByteData& data);
|
||||
|
||||
static void SendChannelUpdate(const std::string& name, bool listening);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/// @brief Requests the Dart VM to adjusts the GC heuristics based on
|
||||
/// the requested `performance_mode`. Returns the old performance
|
||||
|
||||
@@ -257,6 +257,8 @@ class ChannelBuffers {
|
||||
return true;
|
||||
}());
|
||||
}
|
||||
|
||||
void sendChannelUpdate(String name, {required bool listening}) {}
|
||||
}
|
||||
|
||||
final ChannelBuffers channelBuffers = ChannelBuffers();
|
||||
|
||||
@@ -401,6 +401,11 @@ RuntimeController::ComputePlatformResolvedLocale(
|
||||
return client_.ComputePlatformResolvedLocale(supported_locale_data);
|
||||
}
|
||||
|
||||
// |PlatformConfigurationClient|
|
||||
void RuntimeController::SendChannelUpdate(std::string name, bool listening) {
|
||||
client_.SendChannelUpdate(std::move(name), listening);
|
||||
}
|
||||
|
||||
Dart_Port RuntimeController::GetMainPort() {
|
||||
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
|
||||
return root_isolate ? root_isolate->main_port() : ILLEGAL_PORT;
|
||||
|
||||
@@ -683,6 +683,9 @@ class RuntimeController : public PlatformConfigurationClient {
|
||||
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale(
|
||||
const std::vector<std::string>& supported_locale_data) override;
|
||||
|
||||
// |PlatformConfigurationClient|
|
||||
void SendChannelUpdate(std::string name, bool listening) override;
|
||||
|
||||
// |PlatformConfigurationClient|
|
||||
double GetScaledFontSize(double unscaled_font_size,
|
||||
int configuration_id) const override;
|
||||
|
||||
@@ -54,6 +54,8 @@ class RuntimeDelegate {
|
||||
virtual std::weak_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
|
||||
const = 0;
|
||||
|
||||
virtual void SendChannelUpdate(std::string name, bool listening) = 0;
|
||||
|
||||
virtual double GetScaledFontSize(double unscaled_font_size,
|
||||
int configuration_id) const = 0;
|
||||
|
||||
|
||||
@@ -570,6 +570,10 @@ std::weak_ptr<PlatformMessageHandler> Engine::GetPlatformMessageHandler()
|
||||
return delegate_.GetPlatformMessageHandler();
|
||||
}
|
||||
|
||||
void Engine::SendChannelUpdate(std::string name, bool listening) {
|
||||
delegate_.OnEngineChannelUpdate(std::move(name), listening);
|
||||
}
|
||||
|
||||
void Engine::LoadDartDeferredLibrary(
|
||||
intptr_t loading_unit_id,
|
||||
std::unique_ptr<const fml::Mapping> snapshot_data,
|
||||
|
||||
@@ -294,6 +294,17 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
|
||||
virtual const std::shared_ptr<PlatformMessageHandler>&
|
||||
GetPlatformMessageHandler() const = 0;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/// @brief Invoked when a listener is registered on a platform channel.
|
||||
///
|
||||
/// @param[in] name The name of the platform channel to which a
|
||||
/// listener has been registered or cleared.
|
||||
///
|
||||
/// @param[in] listening Whether the listener has been set (true) or
|
||||
/// cleared (false).
|
||||
///
|
||||
virtual void OnEngineChannelUpdate(std::string name, bool listening) = 0;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/// @brief Synchronously invokes platform-specific APIs to apply the
|
||||
/// system text scaling on the given unscaled font size.
|
||||
@@ -977,6 +988,9 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
|
||||
std::weak_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
|
||||
const override;
|
||||
|
||||
// |RuntimeDelegate|
|
||||
void SendChannelUpdate(std::string name, bool listening) override;
|
||||
|
||||
// |RuntimeDelegate|
|
||||
double GetScaledFontSize(double unscaled_font_size,
|
||||
int configuration_id) const override;
|
||||
|
||||
@@ -39,6 +39,7 @@ class MockDelegate : public Engine::Delegate {
|
||||
MOCK_METHOD0(GetCurrentTimePoint, fml::TimePoint());
|
||||
MOCK_CONST_METHOD0(GetPlatformMessageHandler,
|
||||
const std::shared_ptr<PlatformMessageHandler>&());
|
||||
MOCK_METHOD2(OnEngineChannelUpdate, void(std::string, bool));
|
||||
MOCK_CONST_METHOD2(GetScaledFontSize,
|
||||
double(double font_size, int configuration_id));
|
||||
};
|
||||
@@ -69,6 +70,7 @@ class MockRuntimeDelegate : public RuntimeDelegate {
|
||||
MOCK_METHOD1(RequestDartDeferredLibrary, void(intptr_t));
|
||||
MOCK_CONST_METHOD0(GetPlatformMessageHandler,
|
||||
std::weak_ptr<PlatformMessageHandler>());
|
||||
MOCK_METHOD2(SendChannelUpdate, void(std::string, bool));
|
||||
MOCK_CONST_METHOD2(GetScaledFontSize,
|
||||
double(double font_size, int configuration_id));
|
||||
};
|
||||
|
||||
@@ -114,6 +114,8 @@ void PlatformView::UpdateSemantics(
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
CustomAccessibilityActionUpdates actions) {}
|
||||
|
||||
void PlatformView::SendChannelUpdate(const std::string& name, bool listening) {}
|
||||
|
||||
void PlatformView::HandlePlatformMessage(
|
||||
std::unique_ptr<PlatformMessage> message) {
|
||||
if (auto response = message->response()) {
|
||||
|
||||
@@ -465,6 +465,17 @@ class PlatformView {
|
||||
virtual void UpdateSemantics(SemanticsNodeUpdates updates,
|
||||
CustomAccessibilityActionUpdates actions);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Used by the framework to tell the embedder that it has
|
||||
/// registered a listener on a given channel.
|
||||
///
|
||||
/// @param[in] name The name of the channel on which the listener has
|
||||
/// set or cleared a listener.
|
||||
/// @param[in] listening True if a listener has been set, false if it has
|
||||
/// been cleared.
|
||||
///
|
||||
virtual void SendChannelUpdate(const std::string& name, bool listening);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Used by embedders to specify the updated viewport metrics for
|
||||
/// a view. In response to this call, on the raster thread, the
|
||||
|
||||
@@ -1328,6 +1328,18 @@ void Shell::OnEngineHandlePlatformMessage(
|
||||
}
|
||||
}
|
||||
|
||||
void Shell::OnEngineChannelUpdate(std::string name, bool listening) {
|
||||
FML_DCHECK(is_set_up_);
|
||||
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
|
||||
|
||||
task_runners_.GetPlatformTaskRunner()->PostTask(
|
||||
[view = platform_view_->GetWeakPtr(), name = std::move(name), listening] {
|
||||
if (view) {
|
||||
view->SendChannelUpdate(name, listening);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Shell::HandleEngineSkiaMessage(std::unique_ptr<PlatformMessage> message) {
|
||||
const auto& data = message->data();
|
||||
|
||||
|
||||
@@ -680,6 +680,9 @@ class Shell final : public PlatformView::Delegate,
|
||||
// |Engine::Delegate|
|
||||
fml::TimePoint GetCurrentTimePoint() override;
|
||||
|
||||
// |Engine::Delegate|
|
||||
void OnEngineChannelUpdate(std::string name, bool listening) override;
|
||||
|
||||
// |Engine::Delegate|
|
||||
double GetScaledFontSize(double unscaled_font_size,
|
||||
int configuration_id) const override;
|
||||
|
||||
@@ -1881,6 +1881,17 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
|
||||
user_data]() { return ptr(user_data); };
|
||||
}
|
||||
|
||||
flutter::PlatformViewEmbedder::ChanneUpdateCallback channel_update_callback =
|
||||
nullptr;
|
||||
if (SAFE_ACCESS(args, channel_update_callback, nullptr) != nullptr) {
|
||||
channel_update_callback = [ptr = args->channel_update_callback, user_data](
|
||||
const std::string& name, bool listening) {
|
||||
FlutterChannelUpdate update{sizeof(FlutterChannelUpdate), name.c_str(),
|
||||
listening};
|
||||
ptr(&update, user_data);
|
||||
};
|
||||
}
|
||||
|
||||
auto external_view_embedder_result = InferExternalViewEmbedderFromArgs(
|
||||
SAFE_ACCESS(args, compositor, nullptr), settings.enable_impeller);
|
||||
if (external_view_embedder_result.second) {
|
||||
@@ -1895,6 +1906,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
|
||||
vsync_callback, //
|
||||
compute_platform_resolved_locale_callback, //
|
||||
on_pre_engine_restart_callback, //
|
||||
channel_update_callback, //
|
||||
};
|
||||
|
||||
auto on_create_platform_view = InferPlatformViewCreationCallback(
|
||||
|
||||
@@ -1402,6 +1402,20 @@ typedef void (*FlutterUpdateSemanticsCallback2)(
|
||||
const FlutterSemanticsUpdate2* /* semantics update */,
|
||||
void* /* user data*/);
|
||||
|
||||
/// An update to whether a message channel has a listener set or not.
|
||||
typedef struct {
|
||||
// The size of the struct. Must be sizeof(FlutterChannelUpdate).
|
||||
size_t struct_size;
|
||||
/// The name of the channel.
|
||||
const char* channel;
|
||||
/// True if a listener has been set, false if one has been cleared.
|
||||
bool listening;
|
||||
} FlutterChannelUpdate;
|
||||
|
||||
typedef void (*FlutterChannelUpdateCallback)(
|
||||
const FlutterChannelUpdate* /* channel update */,
|
||||
void* /* user data */);
|
||||
|
||||
typedef struct _FlutterTaskRunner* FlutterTaskRunner;
|
||||
|
||||
typedef struct {
|
||||
@@ -2194,6 +2208,11 @@ typedef struct {
|
||||
/// and `update_semantics_callback2` may be provided; the others must be set
|
||||
/// to null.
|
||||
FlutterUpdateSemanticsCallback2 update_semantics_callback2;
|
||||
|
||||
/// The callback invoked by the engine in response to a channel listener
|
||||
/// being registered on the framework side. The callback is invoked from
|
||||
/// a task posted to the UI task runner.
|
||||
FlutterChannelUpdateCallback channel_update_callback;
|
||||
} FlutterProjectArgs;
|
||||
|
||||
#ifndef FLUTTER_ENGINE_NO_PROTOTYPES
|
||||
|
||||
@@ -1289,3 +1289,12 @@ void pointer_data_packet() {
|
||||
|
||||
signalNativeTest();
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void channel_listener_response() async {
|
||||
channelBuffers.setListener('test/listen',
|
||||
(ByteData? data, PlatformMessageResponseCallback callback) {
|
||||
callback(null);
|
||||
});
|
||||
signalNativeTest();
|
||||
}
|
||||
|
||||
@@ -199,6 +199,14 @@ void PlatformViewEmbedder::OnPreEngineRestart() const {
|
||||
}
|
||||
}
|
||||
|
||||
// |PlatformView|
|
||||
void PlatformViewEmbedder::SendChannelUpdate(const std::string& name,
|
||||
bool listening) {
|
||||
if (platform_dispatch_table_.on_channel_update != nullptr) {
|
||||
platform_dispatch_table_.on_channel_update(name, listening);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<PlatformMessageHandler>
|
||||
PlatformViewEmbedder::GetPlatformMessageHandler() const {
|
||||
return platform_message_handler_;
|
||||
|
||||
@@ -41,6 +41,7 @@ class PlatformViewEmbedder final : public PlatformView {
|
||||
std::function<std::unique_ptr<std::vector<std::string>>(
|
||||
const std::vector<std::string>& supported_locale_data)>;
|
||||
using OnPreEngineRestartCallback = std::function<void()>;
|
||||
using ChanneUpdateCallback = std::function<void(const std::string&, bool)>;
|
||||
|
||||
struct PlatformDispatchTable {
|
||||
UpdateSemanticsCallback update_semantics_callback; // optional
|
||||
@@ -50,6 +51,7 @@ class PlatformViewEmbedder final : public PlatformView {
|
||||
ComputePlatformResolvedLocaleCallback
|
||||
compute_platform_resolved_locale_callback;
|
||||
OnPreEngineRestartCallback on_pre_engine_restart_callback; // optional
|
||||
ChanneUpdateCallback on_channel_update; // optional
|
||||
};
|
||||
|
||||
// Create a platform view that sets up a software rasterizer.
|
||||
@@ -134,6 +136,9 @@ class PlatformViewEmbedder final : public PlatformView {
|
||||
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
|
||||
const std::vector<std::string>& supported_locale_data) override;
|
||||
|
||||
// |PlatformView|
|
||||
void SendChannelUpdate(const std::string& name, bool listening) override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder);
|
||||
};
|
||||
|
||||
|
||||
@@ -114,6 +114,7 @@ EmbedderConfigBuilder::EmbedderConfigBuilder(
|
||||
SetSemanticsCallbackHooks();
|
||||
SetLogMessageCallbackHook();
|
||||
SetLocalizationCallbackHooks();
|
||||
SetChannelUpdateCallbackHook();
|
||||
AddCommandLineArgument("--disable-vm-service");
|
||||
|
||||
if (preference == InitializationPreference::kSnapshotsInitialize ||
|
||||
@@ -263,6 +264,11 @@ void EmbedderConfigBuilder::SetLogMessageCallbackHook() {
|
||||
EmbedderTestContext::GetLogMessageCallbackHook();
|
||||
}
|
||||
|
||||
void EmbedderConfigBuilder::SetChannelUpdateCallbackHook() {
|
||||
project_args_.channel_update_callback =
|
||||
context_.GetChannelUpdateCallbackHook();
|
||||
}
|
||||
|
||||
void EmbedderConfigBuilder::SetLogTag(std::string tag) {
|
||||
log_tag_ = std::move(tag);
|
||||
project_args_.log_tag = log_tag_.c_str();
|
||||
|
||||
@@ -80,6 +80,8 @@ class EmbedderConfigBuilder {
|
||||
// Used to set a custom log message handler.
|
||||
void SetLogMessageCallbackHook();
|
||||
|
||||
void SetChannelUpdateCallbackHook();
|
||||
|
||||
// Used to set a custom log tag.
|
||||
void SetLogTag(std::string tag);
|
||||
|
||||
|
||||
@@ -147,6 +147,11 @@ void EmbedderTestContext::SetPlatformMessageCallback(
|
||||
platform_message_callback_ = callback;
|
||||
}
|
||||
|
||||
void EmbedderTestContext::SetChannelUpdateCallback(
|
||||
const ChannelUpdateCallback& callback) {
|
||||
channel_update_callback_ = callback;
|
||||
}
|
||||
|
||||
void EmbedderTestContext::PlatformMessageCallback(
|
||||
const FlutterPlatformMessage* message) {
|
||||
if (platform_message_callback_) {
|
||||
@@ -232,6 +237,20 @@ EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook() {
|
||||
};
|
||||
}
|
||||
|
||||
FlutterChannelUpdateCallback
|
||||
EmbedderTestContext::GetChannelUpdateCallbackHook() {
|
||||
if (channel_update_callback_ == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return [](const FlutterChannelUpdate* update, void* user_data) {
|
||||
auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
|
||||
if (context->channel_update_callback_) {
|
||||
context->channel_update_callback_(update);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
FlutterTransformation EmbedderTestContext::GetRootSurfaceTransformation() {
|
||||
return FlutterTransformationMake(root_surface_transformation_);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ using SemanticsActionCallback =
|
||||
std::function<void(const FlutterSemanticsCustomAction*)>;
|
||||
using LogMessageCallback =
|
||||
std::function<void(const char* tag, const char* message)>;
|
||||
using ChannelUpdateCallback = std::function<void(const FlutterChannelUpdate*)>;
|
||||
|
||||
struct AOTDataDeleter {
|
||||
void operator()(FlutterEngineAOTData aot_data) {
|
||||
@@ -89,6 +90,8 @@ class EmbedderTestContext {
|
||||
|
||||
void SetLogMessageCallback(const LogMessageCallback& log_message_callback);
|
||||
|
||||
void SetChannelUpdateCallback(const ChannelUpdateCallback& callback);
|
||||
|
||||
std::future<sk_sp<SkImage>> GetNextSceneImage();
|
||||
|
||||
EmbedderTestCompositor& GetCompositor();
|
||||
@@ -132,6 +135,7 @@ class EmbedderTestContext {
|
||||
SemanticsUpdateCallback update_semantics_callback_;
|
||||
SemanticsNodeCallback update_semantics_node_callback_;
|
||||
SemanticsActionCallback update_semantics_custom_action_callback_;
|
||||
ChannelUpdateCallback channel_update_callback_;
|
||||
std::function<void(const FlutterPlatformMessage*)> platform_message_callback_;
|
||||
LogMessageCallback log_message_callback_;
|
||||
std::unique_ptr<EmbedderTestCompositor> compositor_;
|
||||
@@ -155,6 +159,8 @@ class EmbedderTestContext {
|
||||
static FlutterComputePlatformResolvedLocaleCallback
|
||||
GetComputePlatformResolvedLocaleCallbackHook();
|
||||
|
||||
FlutterChannelUpdateCallback GetChannelUpdateCallbackHook();
|
||||
|
||||
void SetupAOTMappingsIfNecessary();
|
||||
|
||||
void SetupAOTDataIfNecessary();
|
||||
|
||||
@@ -2562,6 +2562,37 @@ TEST_F(EmbedderTest, CanSendPointer) {
|
||||
message_latch.Wait();
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest, RegisterChannelListener) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
fml::AutoResetWaitableEvent latch2;
|
||||
bool listening = false;
|
||||
context.AddNativeCallback(
|
||||
"SignalNativeTest",
|
||||
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) { latch.Signal(); }));
|
||||
context.SetChannelUpdateCallback([&](const FlutterChannelUpdate* update) {
|
||||
EXPECT_STREQ(update->channel, "test/listen");
|
||||
EXPECT_TRUE(update->listening);
|
||||
listening = true;
|
||||
latch2.Signal();
|
||||
});
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetSoftwareRendererConfig();
|
||||
builder.SetDartEntrypoint("channel_listener_response");
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
latch.Wait();
|
||||
// Drain tasks posted to platform thread task runner.
|
||||
fml::MessageLoop::GetCurrent().RunExpiredTasksNow();
|
||||
latch2.Wait();
|
||||
|
||||
ASSERT_TRUE(listening);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h"
|
||||
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
|
||||
#include "flutter/shell/platform/common/path_utils.h"
|
||||
#include "flutter/shell/platform/embedder/embedder_struct_macros.h"
|
||||
#include "flutter/shell/platform/windows/accessibility_bridge_windows.h"
|
||||
#include "flutter/shell/platform/windows/flutter_windows_view.h"
|
||||
#include "flutter/shell/platform/windows/keyboard_key_channel_handler.h"
|
||||
@@ -373,6 +374,15 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) {
|
||||
host->root_isolate_create_callback_();
|
||||
}
|
||||
};
|
||||
args.channel_update_callback = [](const FlutterChannelUpdate* update,
|
||||
void* user_data) {
|
||||
auto host = static_cast<FlutterWindowsEngine*>(user_data);
|
||||
if (SAFE_ACCESS(update, channel, nullptr) != nullptr) {
|
||||
std::string channel_name(update->channel);
|
||||
host->OnChannelUpdate(std::move(channel_name),
|
||||
SAFE_ACCESS(update, listening, false));
|
||||
}
|
||||
};
|
||||
|
||||
args.custom_task_runners = &custom_task_runners;
|
||||
|
||||
@@ -792,16 +802,6 @@ void FlutterWindowsEngine::OnDwmCompositionChanged() {
|
||||
view_->OnDwmCompositionChanged();
|
||||
}
|
||||
|
||||
// TODO(yaakovschectman): This enables the flutter/lifecycle channel
|
||||
// once the System.initializationComplete message is received on
|
||||
// the flutter/system channel. This is a short-term workaround to
|
||||
// ensure the framework is initialized and ready to accept lifecycle
|
||||
// messages. This cross-channel dependency should be removed.
|
||||
// See: https://github.com/flutter/flutter/issues/132514
|
||||
void FlutterWindowsEngine::OnApplicationLifecycleEnabled() {
|
||||
lifecycle_manager_->BeginProcessingLifecycle();
|
||||
}
|
||||
|
||||
void FlutterWindowsEngine::OnWindowStateEvent(HWND hwnd,
|
||||
WindowStateEvent event) {
|
||||
lifecycle_manager_->OnWindowStateEvent(hwnd, event);
|
||||
@@ -819,4 +819,12 @@ std::optional<LRESULT> FlutterWindowsEngine::ProcessExternalWindowMessage(
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void FlutterWindowsEngine::OnChannelUpdate(std::string name, bool listening) {
|
||||
if (name == "flutter/platform" && listening) {
|
||||
lifecycle_manager_->BeginProcessingExit();
|
||||
} else if (name == "flutter/lifecycle" && listening) {
|
||||
lifecycle_manager_->BeginProcessingLifecycle();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -258,10 +258,6 @@ class FlutterWindowsEngine {
|
||||
// Called when a WM_DWMCOMPOSITIONCHANGED message is received.
|
||||
void OnDwmCompositionChanged();
|
||||
|
||||
// Called in response to the framework registering a ServiceBindings.
|
||||
// Registers the top level handler for the WM_CLOSE window message.
|
||||
void OnApplicationLifecycleEnabled();
|
||||
|
||||
// Called when a Window receives an event that may alter the application
|
||||
// lifecycle state.
|
||||
void OnWindowStateEvent(HWND hwnd, WindowStateEvent event);
|
||||
@@ -301,6 +297,10 @@ class FlutterWindowsEngine {
|
||||
// created. This is typically caused by a hot restart (Shift-R in CLI.)
|
||||
void OnPreEngineRestart();
|
||||
|
||||
// Invoked by the engine when a listener is set or cleared on a platform
|
||||
// channel.
|
||||
virtual void OnChannelUpdate(std::string name, bool listening);
|
||||
|
||||
private:
|
||||
// Allows swapping out embedder_api_ calls in tests.
|
||||
friend class EngineModifier;
|
||||
|
||||
@@ -703,7 +703,8 @@ TEST_F(FlutterWindowsEngineTest, TestExit) {
|
||||
ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; });
|
||||
EXPECT_CALL(*handler, Quit).Times(1);
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
|
||||
engine->lifecycle_manager()->BeginProcessingExit();
|
||||
|
||||
engine->Run();
|
||||
|
||||
@@ -740,7 +741,7 @@ TEST_F(FlutterWindowsEngineTest, TestExitCancel) {
|
||||
ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; });
|
||||
EXPECT_CALL(*handler, Quit).Times(0);
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
engine->lifecycle_manager()->BeginProcessingExit();
|
||||
|
||||
auto binary_messenger =
|
||||
std::make_unique<BinaryMessengerImpl>(engine->messenger());
|
||||
@@ -801,7 +802,7 @@ TEST_F(FlutterWindowsEngineTest, TestExitSecondCloseMessage) {
|
||||
hwnd, msg, wparam, lparam);
|
||||
});
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
engine->lifecycle_manager()->BeginProcessingExit();
|
||||
|
||||
engine->Run();
|
||||
|
||||
@@ -852,7 +853,7 @@ TEST_F(FlutterWindowsEngineTest, TestExitCloseMultiWindow) {
|
||||
// Quit should not be called when there is more than one window.
|
||||
EXPECT_CALL(*handler, Quit).Times(0);
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
engine->lifecycle_manager()->BeginProcessingExit();
|
||||
|
||||
engine->Run();
|
||||
|
||||
@@ -900,7 +901,7 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) {
|
||||
});
|
||||
EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1);
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
engine->lifecycle_manager()->BeginProcessingExit();
|
||||
|
||||
engine->window_proc_delegate_manager()->OnTopLevelWindowProc(0, WM_CLOSE, 0,
|
||||
0);
|
||||
@@ -1054,7 +1055,8 @@ TEST_F(FlutterWindowsEngineTest, EnableLifecycleState) {
|
||||
EXPECT_FALSE(finished);
|
||||
|
||||
// Test that we can set the state afterwards.
|
||||
engine->OnApplicationLifecycleEnabled();
|
||||
|
||||
engine->lifecycle_manager()->BeginProcessingLifecycle();
|
||||
view.OnWindowStateEvent(hwnd, WindowStateEvent::kShow);
|
||||
|
||||
while (!finished) {
|
||||
@@ -1111,5 +1113,29 @@ TEST_F(FlutterWindowsEngineTest, LifecycleStateToFrom) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) {
|
||||
FlutterWindowsEngineBuilder builder{GetContext()};
|
||||
builder.SetDartEntrypoint("enableLifecycleToFrom");
|
||||
|
||||
auto window_binding_handler =
|
||||
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
|
||||
MockFlutterWindowsView view(std::move(window_binding_handler));
|
||||
view.SetEngine(builder.Build());
|
||||
FlutterWindowsEngine* engine = view.GetEngine();
|
||||
EngineModifier modifier(engine);
|
||||
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
|
||||
|
||||
bool lifecycle_began = false;
|
||||
auto handler = std::make_unique<MockWindowsLifecycleManager>(engine);
|
||||
handler->begin_processing_callback = [&]() { lifecycle_began = true; };
|
||||
modifier.SetLifecycleManager(std::move(handler));
|
||||
|
||||
engine->Run();
|
||||
|
||||
while (!lifecycle_began) {
|
||||
engine->task_runner()->ProcessTasks();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -498,7 +498,7 @@ void PlatformHandler::HandleMethodCall(
|
||||
|
||||
SystemSoundPlay(sound_type.GetString(), std::move(result));
|
||||
} else if (method.compare(kInitializationCompleteMethod) == 0) {
|
||||
engine_->OnApplicationLifecycleEnabled();
|
||||
// Deprecated but should not cause an error.
|
||||
result->Success();
|
||||
} else {
|
||||
result->NotImplemented();
|
||||
|
||||
@@ -49,7 +49,7 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd,
|
||||
// is, we re-dispatch a new WM_CLOSE message. In order to allow the new
|
||||
// message to reach other delegates, we ignore it here.
|
||||
case WM_CLOSE: {
|
||||
if (!process_lifecycle_) {
|
||||
if (!process_exit_) {
|
||||
return false;
|
||||
}
|
||||
auto key = std::make_tuple(hwnd, wpar, lpar);
|
||||
@@ -183,6 +183,10 @@ void WindowsLifecycleManager::BeginProcessingLifecycle() {
|
||||
process_lifecycle_ = true;
|
||||
}
|
||||
|
||||
void WindowsLifecycleManager::BeginProcessingExit() {
|
||||
process_exit_ = true;
|
||||
}
|
||||
|
||||
// TODO(schectman): Wait until the platform channel is registered to send
|
||||
// the platform message.
|
||||
// https://github.com/flutter/flutter/issues/131616
|
||||
|
||||
@@ -52,10 +52,12 @@ class WindowsLifecycleManager {
|
||||
// update the application lifecycle.
|
||||
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT* result);
|
||||
|
||||
// Signal to start consuming WM_CLOSE messages and sending lifecycle state
|
||||
// update messages.
|
||||
// Signal to start sending lifecycle state update messages.
|
||||
virtual void BeginProcessingLifecycle();
|
||||
|
||||
// Signal to start consuming WM_CLOSE messages.
|
||||
virtual void BeginProcessingExit();
|
||||
|
||||
// Update the app lifecycle state in response to a change in window state.
|
||||
// When the app lifecycle state actually changes, this sends a platform
|
||||
// message to the framework notifying it of the state change.
|
||||
@@ -102,6 +104,7 @@ class WindowsLifecycleManager {
|
||||
std::map<std::tuple<HWND, WPARAM, LPARAM>, int> sent_close_messages_;
|
||||
|
||||
bool process_lifecycle_ = false;
|
||||
bool process_exit_ = false;
|
||||
|
||||
std::set<HWND> visible_windows_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user