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

Commit a620b61

Browse files
committed
[windows] Use FML_DCHECK in place of C assert
Now that the embedders depend on FML, migrate assertions to FML_DCHECK, which allows for an explanatory log message to be emitted along with the assertion. No tests since no semantic changes are made.
1 parent 35cfe91 commit a620b61

13 files changed

+58
-48
lines changed

shell/platform/windows/accessibility_bridge_windows.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/shell/platform/windows/accessibility_bridge_windows.h"
66

7+
#include "flutter/fml/logging.h"
78
#include "flutter/shell/platform/windows/flutter_platform_node_delegate_windows.h"
89
#include "flutter/third_party/accessibility/ax/platform/ax_platform_node_delegate_base.h"
910

@@ -13,8 +14,8 @@ AccessibilityBridgeWindows::AccessibilityBridgeWindows(
1314
FlutterWindowsEngine* engine,
1415
FlutterWindowsView* view)
1516
: engine_(engine), view_(view) {
16-
assert(engine_);
17-
assert(view_);
17+
FML_DCHECK(engine_);
18+
FML_DCHECK(view_);
1819
}
1920

2021
void AccessibilityBridgeWindows::OnAccessibilityEvent(
@@ -24,7 +25,8 @@ void AccessibilityBridgeWindows::OnAccessibilityEvent(
2425

2526
auto node_delegate =
2627
GetFlutterPlatformNodeDelegateFromID(ax_node->id()).lock();
27-
assert(node_delegate);
28+
FML_DCHECK(node_delegate)
29+
<< "No FlutterPlatformNodeDelegate found for node ID " << ax_node->id();
2830
std::shared_ptr<FlutterPlatformNodeDelegateWindows> win_delegate =
2931
std::static_pointer_cast<FlutterPlatformNodeDelegateWindows>(
3032
node_delegate);

shell/platform/windows/flutter_platform_node_delegate_windows.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/shell/platform/windows/flutter_platform_node_delegate_windows.h"
88

9+
#include "flutter/fml/logging.h"
910
#include "flutter/shell/platform/windows/accessibility_bridge_windows.h"
1011
#include "flutter/shell/platform/windows/flutter_windows_view.h"
1112
#include "flutter/third_party/accessibility/ax/ax_clipping_behavior.h"
@@ -18,8 +19,9 @@ FlutterPlatformNodeDelegateWindows::FlutterPlatformNodeDelegateWindows(
1819
std::weak_ptr<AccessibilityBridge> bridge,
1920
FlutterWindowsView* view)
2021
: bridge_(bridge), view_(view) {
21-
assert(!bridge_.expired());
22-
assert(view_);
22+
FML_DCHECK(!bridge_.expired())
23+
<< "Expired AccessibilityBridge passed to node delegate";
24+
FML_DCHECK(view_);
2325
}
2426

2527
FlutterPlatformNodeDelegateWindows::~FlutterPlatformNodeDelegateWindows() {
@@ -33,14 +35,15 @@ void FlutterPlatformNodeDelegateWindows::Init(std::weak_ptr<OwnerBridge> bridge,
3335
ui::AXNode* node) {
3436
FlutterPlatformNodeDelegate::Init(bridge, node);
3537
ax_platform_node_ = ui::AXPlatformNode::Create(this);
36-
assert(ax_platform_node_);
38+
FML_DCHECK(ax_platform_node_) << "Failed to create AXPlatformNode";
3739
}
3840

3941
// |ui::AXPlatformNodeDelegate|
4042
gfx::NativeViewAccessible
4143
FlutterPlatformNodeDelegateWindows::GetNativeViewAccessible() {
42-
assert(ax_platform_node_);
43-
return ax_platform_node_->GetNativeViewAccessible();
44+
FML_DCHECK(ax_platform_node_)
45+
<< "AXPlatformNode hasn't been created;
46+
return ax_platform_node_->GetNativeViewAccessible();
4447
}
4548
4649
// |ui::AXPlatformNodeDelegate|
@@ -58,12 +61,13 @@ gfx::NativeViewAccessible FlutterPlatformNodeDelegateWindows::HitTestSync(
5861
5962
// If any child in this node's subtree contains the point, return that child.
6063
auto bridge = bridge_.lock();
61-
assert(bridge);
64+
FML_DCHECK(bridge);
6265
for (const ui::AXNode* child : GetAXNode()->children()) {
6366
std::shared_ptr<FlutterPlatformNodeDelegateWindows> win_delegate =
6467
std::static_pointer_cast<FlutterPlatformNodeDelegateWindows>(
6568
bridge->GetFlutterPlatformNodeDelegateFromID(child->id()).lock());
66-
assert(win_delegate);
69+
FML_DCHECK(win_delegate)
70+
<< "No FlutterPlatformNodeDelegate found for node " << child->id();
6771
auto hit_view = win_delegate->HitTestSync(screen_physical_pixel_x,
6872
screen_physical_pixel_y);
6973
if (hit_view) {

shell/platform/windows/flutter_windows.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <io.h>
88

99
#include <algorithm>
10-
#include <cassert>
1110
#include <chrono>
1211
#include <cstdlib>
1312
#include <filesystem>

shell/platform/windows/keyboard_key_embedder_handler.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
#include "flutter/shell/platform/windows/keyboard_key_embedder_handler.h"
66

7-
#include <assert.h>
87
#include <windows.h>
98

109
#include <chrono>
1110
#include <iostream>
1211
#include <string>
1312

13+
#include "flutter/fml/logging.h"
1414
#include "flutter/shell/platform/windows/keyboard_utils.h"
1515

1616
namespace flutter {
@@ -38,7 +38,7 @@ char _GetBit(char32_t ch, size_t start, size_t end) {
3838

3939
std::string ConvertChar32ToUtf8(char32_t ch) {
4040
std::string result;
41-
assert(0 <= ch && ch <= 0x10FFFF);
41+
FML_DCHECK(0 <= ch && ch <= 0x10FFFF) << "Character out of range";
4242
if (ch <= 0x007F) {
4343
result.push_back(ch);
4444
} else if (ch <= 0x07FF) {
@@ -160,8 +160,8 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
160160
std::function<void(bool)> callback) {
161161
const uint64_t physical_key = GetPhysicalKey(scancode, extended);
162162
const uint64_t logical_key = GetLogicalKey(key, extended, scancode);
163-
assert(action == WM_KEYDOWN || action == WM_KEYUP ||
164-
action == WM_SYSKEYDOWN || action == WM_SYSKEYUP);
163+
FML_DCHECK(action == WM_KEYDOWN || action == WM_KEYUP ||
164+
action == WM_SYSKEYDOWN || action == WM_SYSKEYUP);
165165

166166
auto last_logical_record_iter = pressingRecords_.find(physical_key);
167167
bool had_record = last_logical_record_iter != pressingRecords_.end();
@@ -230,7 +230,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
230230
if (was_down) {
231231
// A normal repeated key.
232232
type = kFlutterKeyEventTypeRepeat;
233-
assert(had_record);
233+
FML_DCHECK(had_record);
234234
ConvertUtf32ToUtf8_(character_bytes, character);
235235
eventual_logical_record = last_logical_record;
236236
result_logical_key = last_logical_record;
@@ -245,7 +245,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
245245
} else {
246246
// A normal down event (whether the system event is a repeat or not).
247247
type = kFlutterKeyEventTypeDown;
248-
assert(!had_record);
248+
FML_DCHECK(!had_record);
249249
ConvertUtf32ToUtf8_(character_bytes, character);
250250
eventual_logical_record = logical_key;
251251
result_logical_key = logical_key;
@@ -260,7 +260,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
260260
} else {
261261
// A normal up event.
262262
type = kFlutterKeyEventTypeUp;
263-
assert(had_record);
263+
FML_DCHECK(had_record);
264264
// Up events never have character.
265265
character_bytes[0] = '\0';
266266
eventual_logical_record = 0;
@@ -278,7 +278,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
278278
if (record_iter != pressingRecords_.end()) {
279279
pressingRecords_.erase(record_iter);
280280
} else {
281-
assert(false);
281+
FML_DCHECK(false);
282282
}
283283
}
284284

@@ -375,7 +375,7 @@ void KeyboardKeyEmbedderHandler::SynchronizeCriticalToggledStates(
375375
// Never seen this key.
376376
continue;
377377
}
378-
assert(key_info.logical_key != 0);
378+
FML_DCHECK(key_info.logical_key != 0);
379379

380380
// Check toggling state first, because it might alter pressing state.
381381
if (key_info.check_toggled) {
@@ -440,7 +440,7 @@ void KeyboardKeyEmbedderHandler::SynchronizeCriticalPressedStates(
440440
// Never seen this key.
441441
continue;
442442
}
443-
assert(key_info.logical_key != 0);
443+
FML_DCHECK(key_info.logical_key != 0);
444444
if (key_info.check_pressed) {
445445
SHORT true_pressed = get_key_state_(virtual_key) & kStateMaskPressed;
446446
auto pressing_record_iter = pressingRecords_.find(key_info.physical_key);

shell/platform/windows/keyboard_key_handler.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void KeyboardKeyHandler::ResolvePendingEvent(uint64_t sequence_id,
8383
PendingEvent& event = **iter;
8484
event.any_handled = event.any_handled || handled;
8585
event.unreplied -= 1;
86-
assert(event.unreplied >= 0);
86+
FML_DCHECK(event.unreplied >= 0)
87+
<< "Pending events must have unreplied count > 0";
8788
// If all delegates have replied, report if any of them handled the event.
8889
if (event.unreplied == 0) {
8990
std::unique_ptr<PendingEvent> event_ptr = std::move(*iter);
@@ -95,7 +96,7 @@ void KeyboardKeyHandler::ResolvePendingEvent(uint64_t sequence_id,
9596
}
9697
}
9798
// The pending event should always be found.
98-
assert(false);
99+
FML_LOG(FATAL) << "No unreplied pending events found";
99100
}
100101

101102
} // namespace flutter

shell/platform/windows/keyboard_manager.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include <assert.h>
65
#include <memory>
76
#include <string>
87

@@ -323,7 +322,8 @@ bool KeyboardManager::HandleMessage(UINT const action,
323322
return !IsSysAction(action);
324323
}
325324
default:
326-
assert(false);
325+
FML_LOG(FATAL) << "No event handler for keyboard event with action "
326+
<< action;
327327
}
328328
return false;
329329
}
@@ -336,7 +336,7 @@ void KeyboardManager::ProcessNextEvent() {
336336
auto pending_event = std::move(pending_events_.front());
337337
pending_events_.pop_front();
338338
PerformProcessEvent(std::move(pending_event), [this] {
339-
assert(processing_event_);
339+
FML_DCHECK(processing_event_);
340340
processing_event_ = false;
341341
ProcessNextEvent();
342342
});
@@ -392,7 +392,7 @@ void KeyboardManager::DispatchText(const PendingEvent& event) {
392392
// keys defined by `IsPrintable` are certain characters at lower ASCII range.
393393
// These ASCII control characters are sent as WM_CHAR events for all control
394394
// key shortcuts.
395-
assert(!event.session.empty());
395+
FML_DCHECK(!event.session.empty());
396396
bool is_printable = IsPrintable(event.session.back().wparam);
397397
bool valid = event.character != 0 && is_printable;
398398
if (valid) {

shell/platform/windows/keyboard_unittests.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include "flutter/fml/logging.h"
56
#include "flutter/shell/platform/common/json_message_codec.h"
67
#include "flutter/shell/platform/embedder/embedder.h"
78
#include "flutter/shell/platform/embedder/test_utils/key_codes.g.h"
@@ -103,7 +104,8 @@ class TestKeyboardManager : public KeyboardManager {
103104

104105
protected:
105106
void RedispatchEvent(std::unique_ptr<PendingEvent> event) override {
106-
assert(!during_redispatch_);
107+
FML_DCHECK(!during_redispatch_)
108+
<< "RedispatchEvent called while already redispatching an event";
107109
during_redispatch_ = true;
108110
KeyboardManager::RedispatchEvent(std::move(event));
109111
during_redispatch_ = false;
@@ -235,7 +237,7 @@ class MockKeyboardManagerDelegate : public KeyboardManager::WindowDelegate,
235237
break;
236238
}
237239
default:
238-
assert(false);
240+
FML_LOG(FATAL) << "Unhandled KeyboardChange type " << change.type;
239241
}
240242
}
241243
}
@@ -337,7 +339,7 @@ class TestFlutterWindowsView : public FlutterWindowsView {
337339
const char* args) {
338340
rapidjson::Document args_doc;
339341
args_doc.Parse(args);
340-
assert(!args_doc.HasParseError());
342+
FML_DCHECK(!args_doc.HasParseError());
341343

342344
rapidjson::Document message_doc(rapidjson::kObjectType);
343345
auto& allocator = message_doc.GetAllocator();
@@ -473,7 +475,7 @@ class KeyboardTester {
473475
}
474476

475477
void InjectKeyboardChanges(std::vector<KeyboardChange> changes) {
476-
assert(window_ != nullptr);
478+
FML_DCHECK(window_ != nullptr);
477479
window_->InjectKeyboardChanges(changes);
478480
}
479481

shell/platform/windows/keyboard_utils.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
#include "keyboard_utils.h"
66

7+
#include "flutter/fml/logging.h"
8+
79
namespace flutter {
810

911
std::u16string EncodeUtf16(char32_t character) {
1012
// Algorithm: https://en.wikipedia.org/wiki/UTF-16#Description
1113
std::u16string result;
1214
// Invalid value.
13-
assert(!(character >= 0xD800 && character <= 0xDFFF) &&
14-
!(character > 0x10FFFF));
15+
FML_DCHECK(!(character >= 0xD800 && character <= 0xDFFF) &&
16+
!(character > 0x10FFFF));
1517
if ((character >= 0xD800 && character <= 0xDFFF) || (character > 0x10FFFF)) {
1618
return result;
1719
}

shell/platform/windows/keyboard_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_WIN32_COMMON_H_
66
#define FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_WIN32_COMMON_H_
77

8-
#include <assert.h>
98
#include <stdint.h>
9+
1010
#include <string>
1111

1212
namespace flutter {

shell/platform/windows/platform_handler.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool ScopedClipboard::HasString() {
159159
}
160160

161161
std::variant<std::wstring, int> ScopedClipboard::GetString() {
162-
assert(opened_);
162+
FML_DCHECK(opened_) << "Called GetString when clipboard is closed";
163163

164164
HANDLE data = ::GetClipboardData(CF_UNICODETEXT);
165165
if (data == nullptr) {
@@ -174,7 +174,7 @@ std::variant<std::wstring, int> ScopedClipboard::GetString() {
174174
}
175175

176176
int ScopedClipboard::SetString(const std::wstring string) {
177-
assert(opened_);
177+
FML_DCHECK(opened_) << "Called GetString when clipboard is closed";
178178
if (!::EmptyClipboard()) {
179179
return ::GetLastError();
180180
}

shell/platform/windows/testing/test_binary_messenger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
66
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
77

8-
#include <cassert>
98
#include <functional>
109
#include <map>
1110
#include <string>
1211

12+
#include "flutter/fml/logging.h"
1313
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
1414

1515
namespace flutter {
@@ -53,7 +53,7 @@ class TestBinaryMessenger : public BinaryMessenger {
5353
size_t message_size,
5454
BinaryReply reply) const override {
5555
// If something under test sends a message, the test should be handling it.
56-
assert(send_handler_);
56+
FML_DCHECK(send_handler_);
5757
send_handler_(channel, message, message_size, reply);
5858
}
5959

shell/platform/windows/testing/test_keyboard.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
// found in the LICENSE file.
44

55
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
6-
#include "flutter/shell/platform/common/json_message_codec.h"
7-
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
86

97
#include <rapidjson/document.h>
108

9+
#include "flutter/fml/logging.h"
10+
#include "flutter/shell/platform/common/json_message_codec.h"
11+
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
12+
1113
namespace flutter {
1214
namespace testing {
1315

@@ -205,7 +207,8 @@ void MockMessageQueue::PushBack(const Win32Message* message) {
205207
}
206208

207209
LRESULT MockMessageQueue::DispatchFront() {
208-
assert(!_pending_messages.empty());
210+
FML_DCHECK(!_pending_messages.empty())
211+
<< "Called DispatchFront while pending message queue is empty";
209212
Win32Message message = _pending_messages.front();
210213
_pending_messages.pop_front();
211214
_sent_messages.push_back(message);

0 commit comments

Comments
 (0)