Skip to content

Commit

Permalink
Revert "Listen to WM_CLOSE message on Windows to allow framework to…
Browse files Browse the repository at this point in the history
… cancel exit (flutter#40493)" (flutter#40739)

This caused regressions for two internal customers.

Issue: flutter/flutter#123654
Internal issue: b/275565297.

This reverts commit ebc8b04.
  • Loading branch information
cbracken authored Mar 29, 2023
1 parent 510a10b commit 22f27ad
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 427 deletions.
4 changes: 0 additions & 4 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -3118,8 +3118,6 @@ ORIGIN: ../../../flutter/shell/platform/windows/window_binding_handler_delegate.
ORIGIN: ../../../flutter/shell/platform/windows/window_proc_delegate_manager.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/window_proc_delegate_manager.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/window_state.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/windows_lifecycle_manager.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/windows_lifecycle_manager.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/windows_proc_table.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/windows_proc_table.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/windows_registry.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -5710,8 +5708,6 @@ FILE: ../../../flutter/shell/platform/windows/window_binding_handler_delegate.h
FILE: ../../../flutter/shell/platform/windows/window_proc_delegate_manager.cc
FILE: ../../../flutter/shell/platform/windows/window_proc_delegate_manager.h
FILE: ../../../flutter/shell/platform/windows/window_state.h
FILE: ../../../flutter/shell/platform/windows/windows_lifecycle_manager.cc
FILE: ../../../flutter/shell/platform/windows/windows_lifecycle_manager.h
FILE: ../../../flutter/shell/platform/windows/windows_proc_table.cc
FILE: ../../../flutter/shell/platform/windows/windows_proc_table.h
FILE: ../../../flutter/shell/platform/windows/windows_registry.cc
Expand Down
2 changes: 0 additions & 2 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ source_set("flutter_windows_source") {
"window_proc_delegate_manager.cc",
"window_proc_delegate_manager.h",
"window_state.h",
"windows_lifecycle_manager.cc",
"windows_lifecycle_manager.h",
"windows_proc_table.cc",
"windows_proc_table.h",
"windows_registry.cc",
Expand Down
61 changes: 0 additions & 61 deletions shell/platform/windows/fixtures/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data' show ByteData, Uint8List;
import 'dart:ui' as ui;
import 'dart:convert';

// Signals a waiting latch in the native test.
@pragma('vm:external-name', 'Signal')
Expand Down Expand Up @@ -87,66 +86,6 @@ void alertPlatformChannel() async {
ui.PlatformDispatcher.instance.sendPlatformMessage('flutter/accessibility', byteData, (ByteData? _){});
}

@pragma('vm:entry-point')
void exitTestExit() async {
final Completer<ByteData?> closed = Completer<ByteData?>();
ui.channelBuffers.setListener('flutter/platform', (ByteData? data, ui.PlatformMessageResponseCallback callback) async {
final String jsonString = json.encode(<Map<String, String>>[{'response': 'exit'}]);
final ByteData responseData = ByteData.sublistView(Uint8List.fromList(utf8.encode(jsonString)));
callback(responseData);
closed.complete(data);
});
await closed.future;

// From here down, nothing should be called, because the application will have already been closed.
final Completer<ByteData?> exited = Completer<ByteData?>();
final String jsonString = json.encode(<String, dynamic>{
'method': 'System.exitApplication',
'args': <String, dynamic>{
'type': 'required', 'exitCode': 0
}
});
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/platform',
ByteData.sublistView(
Uint8List.fromList(utf8.encode(jsonString))
),
(ByteData? reply) {
exited.complete(reply);
});
await exited.future;
}

@pragma('vm:entry-point')
void exitTestCancel() async {
final Completer<ByteData?> closed = Completer<ByteData?>();
ui.channelBuffers.setListener('flutter/platform', (ByteData? data, ui.PlatformMessageResponseCallback callback) async {
final String jsonString = json.encode(<Map<String, String>>[{'response': 'cancel'}]);
final ByteData responseData = ByteData.sublistView(Uint8List.fromList(utf8.encode(jsonString)));
callback(responseData);
closed.complete(data);
});
await closed.future;

// Because the request was canceled, the below shall execute.
final Completer<ByteData?> exited = Completer<ByteData?>();
final String jsonString = json.encode(<String, dynamic>{
'method': 'System.exitApplication',
'args': <String, dynamic>{
'type': 'required', 'exitCode': 0
}
});
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/platform',
ByteData.sublistView(
Uint8List.fromList(utf8.encode(jsonString))
),
(ByteData? reply) {
exited.complete(reply);
});
await exited.future;
}

@pragma('vm:entry-point')
void customEntrypoint() {}

Expand Down
23 changes: 1 addition & 22 deletions shell/platform/windows/flutter_windows_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ FlutterWindowsEngine::FlutterWindowsEngine(
std::unique_ptr<WindowsRegistry> registry)
: project_(std::make_unique<FlutterProjectBundle>(project)),
aot_data_(nullptr, nullptr),
windows_registry_(std::move(registry)),
lifecycle_manager_(std::make_unique<WindowsLifecycleManager>(this)) {
windows_registry_(std::move(registry)) {
embedder_api_.struct_size = sizeof(FlutterEngineProcTable);
FlutterEngineGetProcAddresses(&embedder_api_);

Expand Down Expand Up @@ -204,17 +203,6 @@ FlutterWindowsEngine::FlutterWindowsEngine(
std::make_unique<FlutterWindowsTextureRegistrar>(this, gl_procs_);
surface_manager_ = AngleSurfaceManager::Create();
window_proc_delegate_manager_ = std::make_unique<WindowProcDelegateManager>();
window_proc_delegate_manager_->RegisterTopLevelWindowProcDelegate(
[](HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar, void* user_data,
LRESULT* result) {
BASE_DCHECK(user_data);
FlutterWindowsEngine* that =
static_cast<FlutterWindowsEngine*>(user_data);
BASE_DCHECK(that->lifecycle_manager_);
return that->lifecycle_manager_->WindowProc(hwnd, msg, wpar, lpar,
result);
},
static_cast<void*>(this));

// Set up internal channels.
// TODO: Replace this with an embedder.h API. See
Expand Down Expand Up @@ -762,13 +750,4 @@ void FlutterWindowsEngine::HandleAccessibilityMessage(
reinterpret_cast<const uint8_t*>(""), 0);
}

void FlutterWindowsEngine::RequestApplicationQuit(ExitType exit_type,
UINT exit_code) {
platform_handler_->RequestAppExit(exit_type, exit_code);
}

void FlutterWindowsEngine::OnQuit(UINT exit_code) {
lifecycle_manager_->Quit(exit_code);
}

} // namespace flutter
10 changes: 0 additions & 10 deletions shell/platform/windows/flutter_windows_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "flutter/shell/platform/windows/text_input_plugin.h"
#include "flutter/shell/platform/windows/window_proc_delegate_manager.h"
#include "flutter/shell/platform/windows/window_state.h"
#include "flutter/shell/platform/windows/windows_lifecycle_manager.h"
#include "flutter/shell/platform/windows/windows_registry.h"
#include "third_party/rapidjson/include/rapidjson/document.h"

Expand Down Expand Up @@ -255,12 +254,6 @@ class FlutterWindowsEngine {
// Updates accessibility, e.g. switch to high contrast mode
void UpdateAccessibilityFeatures(FlutterAccessibilityFeature flags);

// Called when the application quits in response to a quit request.
void OnQuit(UINT exit_code);

// Called when a WM_CLOSE message is received.
void RequestApplicationQuit(ExitType exit_type, UINT exit_code);

protected:
// Creates the keyboard key handler.
//
Expand Down Expand Up @@ -402,9 +395,6 @@ class FlutterWindowsEngine {
// Wrapper providing Windows registry access.
std::unique_ptr<WindowsRegistry> windows_registry_;

// Handler for top level window messages.
std::unique_ptr<WindowsLifecycleManager> lifecycle_manager_;

FML_DISALLOW_COPY_AND_ASSIGN(FlutterWindowsEngine);
};

Expand Down
87 changes: 0 additions & 87 deletions shell/platform/windows/flutter_windows_engine_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,92 +619,5 @@ TEST_F(FlutterWindowsEngineTest, AlertPlatformMessage) {
}
}

class MockWindowsLifecycleManager : public WindowsLifecycleManager {
public:
MockWindowsLifecycleManager(FlutterWindowsEngine* engine)
: WindowsLifecycleManager(engine) {}
virtual ~MockWindowsLifecycleManager() {}

MOCK_CONST_METHOD1(Quit, void(UINT));
};

TEST_F(FlutterWindowsEngineTest, TestExit) {
FlutterWindowsEngineBuilder builder{GetContext()};
builder.SetDartEntrypoint("exitTestExit");
bool finished = false;
bool did_call = false;

std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();

EngineModifier modifier(engine.get());
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
auto handler = std::make_unique<MockWindowsLifecycleManager>(engine.get());
ON_CALL(*handler, Quit).WillByDefault([&finished](UINT exit_code) {
finished = exit_code == 0;
});
EXPECT_CALL(*handler, Quit).Times(1);
modifier.SetLifecycleManager(std::move(handler));

auto binary_messenger =
std::make_unique<BinaryMessengerImpl>(engine->messenger());
binary_messenger->SetMessageHandler(
"flutter/platform", [&did_call](const uint8_t* message,
size_t message_size, BinaryReply reply) {
did_call = true;
char response[] = "";
reply(reinterpret_cast<uint8_t*>(response), 0);
});

engine->Run();

engine->window_proc_delegate_manager()->OnTopLevelWindowProc(0, WM_CLOSE, 0,
0);

while (!finished) {
engine->task_runner()->ProcessTasks();
}

EXPECT_FALSE(did_call);
}

TEST_F(FlutterWindowsEngineTest, TestExitCancel) {
FlutterWindowsEngineBuilder builder{GetContext()};
builder.SetDartEntrypoint("exitTestCancel");
bool finished = false;
bool did_call = false;

std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();

EngineModifier modifier(engine.get());
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
auto handler = std::make_unique<MockWindowsLifecycleManager>(engine.get());
ON_CALL(*handler, Quit).WillByDefault([&finished](int64_t exit_code) {
finished = true;
});
EXPECT_CALL(*handler, Quit).Times(0);
modifier.SetLifecycleManager(std::move(handler));

auto binary_messenger =
std::make_unique<BinaryMessengerImpl>(engine->messenger());
binary_messenger->SetMessageHandler(
"flutter/platform", [&did_call](const uint8_t* message,
size_t message_size, BinaryReply reply) {
did_call = true;
char response[] = "";
reply(reinterpret_cast<uint8_t*>(response), 0);
});

engine->Run();

engine->window_proc_delegate_manager()->OnTopLevelWindowProc(0, WM_CLOSE, 0,
0);

while (!did_call) {
engine->task_runner()->ProcessTasks();
}

EXPECT_FALSE(finished);
}

} // namespace testing
} // namespace flutter
Loading

0 comments on commit 22f27ad

Please sign in to comment.