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

Commit 9782c71

Browse files
committed
Move control channel logic to a private namespace
1 parent 82b13b0 commit 9782c71

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,15 @@ class BasicMessageChannel {
106106
// to channels that aren't fully set up yet. For example, the engine isn't
107107
// running yet or the channel's message handler isn't set up on the Dart side
108108
// yet.
109-
void Resize(int new_size) {
110-
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
111-
messenger_, kControlChannelName, &StandardMethodCodec::GetInstance());
112-
113-
// The deserialization logic handles only 32 bits values, see
114-
// https://github.com/flutter/engine/blob/93e8901490e78c7ba7e319cce4470d9c6478c6dc/lib/ui/channel_buffers.dart#L495.
115-
control_channel->InvokeMethod(
116-
kResizeMethod, std::make_unique<EncodableValue>(EncodableList{
117-
EncodableValue(name_),
118-
EncodableValue(static_cast<int32_t>(new_size)),
119-
}));
120-
}
109+
void Resize(int new_size) { ResizeChannel(messenger_, name_, new_size); }
121110

122111
// Defines whether the channel should show warning messages when discarding
123112
// messages due to overflow.
124113
//
125114
// When |warns| is false, the channel is expected to overflow and warning
126115
// messages will not be shown.
127116
void SetWarnsOnOverflow(bool warns) {
128-
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
129-
messenger_, kControlChannelName, &StandardMethodCodec::GetInstance());
130-
131-
control_channel->InvokeMethod(
132-
kOverflowMethod, std::make_unique<EncodableValue>(EncodableList{
133-
EncodableValue(name_),
134-
EncodableValue(!warns),
135-
}));
117+
SetChannelWarnsOnOverflow(messenger_, name_, warns);
136118
}
137119

138120
private:

shell/platform/common/client_wrapper/include/flutter/method_channel.h

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
namespace flutter {
1919

20-
static constexpr char kControlChannelName[] = "dev.flutter/channel-buffers";
21-
static constexpr char kResizeMethod[] = "resize";
22-
static constexpr char kOverflowMethod[] = "overflow";
23-
2420
class EncodableValue;
2521

2622
// A handler for receiving a method call from the Flutter engine.
@@ -131,35 +127,15 @@ class MethodChannel {
131127
// to channels that aren't fully set up yet. For example, the engine isn't
132128
// running yet or the channel's message handler isn't set up on the Dart side
133129
// yet.
134-
//
135-
// |new_size] is an int because the deserialization logic handles only 32 bits
136-
// values, see
137-
// https://github.com/flutter/engine/blob/93e8901490e78c7ba7e319cce4470d9c6478c6dc/lib/ui/channel_buffers.dart#L495.
138-
void Resize(int new_size) {
139-
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
140-
messenger_, kControlChannelName, &StandardMethodCodec::GetInstance());
141-
142-
control_channel->InvokeMethod(
143-
kResizeMethod, std::make_unique<EncodableValue>(EncodableList{
144-
EncodableValue(name_),
145-
EncodableValue(new_size),
146-
}));
147-
}
130+
void Resize(int new_size) { ResizeChannel(messenger_, name_, new_size); }
148131

149132
// Defines whether the channel should show warning messages when discarding
150133
// messages due to overflow.
151134
//
152135
// When |warns| is false, the channel is expected to overflow and warning
153136
// messages will not be shown.
154137
void SetWarnsOnOverflow(bool warns) {
155-
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
156-
messenger_, kControlChannelName, &StandardMethodCodec::GetInstance());
157-
158-
control_channel->InvokeMethod(
159-
kOverflowMethod, std::make_unique<EncodableValue>(EncodableList{
160-
EncodableValue(name_),
161-
EncodableValue(!warns),
162-
}));
138+
SetChannelWarnsOnOverflow(messenger_, name_, warns);
163139
}
164140

165141
private:
@@ -168,6 +144,49 @@ class MethodChannel {
168144
const MethodCodec<T>* codec_;
169145
};
170146

147+
namespace {
148+
149+
static constexpr char kControlChannelName[] = "dev.flutter/channel-buffers";
150+
static constexpr char kResizeMethod[] = "resize";
151+
static constexpr char kOverflowMethod[] = "overflow";
152+
153+
// Adjusts the number of messages that will get buffered when sending messages
154+
// to channels that aren't fully set up yet. For example, the engine isn't
155+
// running yet or the channel's message handler isn't set up on the Dart side
156+
// yet.
157+
void ResizeChannel(BinaryMessenger* messenger, std::string name, int new_size) {
158+
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
159+
messenger, kControlChannelName, &StandardMethodCodec::GetInstance());
160+
161+
// The deserialization logic handles only 32 bits values, see
162+
// https://github.com/flutter/engine/blob/93e8901490e78c7ba7e319cce4470d9c6478c6dc/lib/ui/channel_buffers.dart#L495.
163+
control_channel->InvokeMethod(
164+
kResizeMethod, std::make_unique<EncodableValue>(EncodableList{
165+
EncodableValue(name),
166+
EncodableValue(static_cast<int32_t>(new_size)),
167+
}));
168+
}
169+
170+
// Defines whether the channel should show warning messages when discarding
171+
// messages due to overflow.
172+
//
173+
// When |warns| is false, the channel is expected to overflow and warning
174+
// messages will not be shown.
175+
void SetChannelWarnsOnOverflow(BinaryMessenger* messenger,
176+
std::string name,
177+
bool warns) {
178+
auto control_channel = std::make_unique<MethodChannel<EncodableValue>>(
179+
messenger, kControlChannelName, &StandardMethodCodec::GetInstance());
180+
181+
control_channel->InvokeMethod(kOverflowMethod,
182+
std::make_unique<EncodableValue>(EncodableList{
183+
EncodableValue(name),
184+
EncodableValue(!warns),
185+
}));
186+
}
187+
188+
} // namespace
189+
171190
} // namespace flutter
172191

173192
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_CHANNEL_H_

shell/platform/common/client_wrapper/method_channel_unittests.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ TEST(MethodChannelTest, Resize) {
172172

173173
channel.Resize(3);
174174

175+
// Because the Dart implementation for the control channel implements its own
176+
// custom deserialization logic, this test compares the generated bytes array
177+
// to the expected one (for instance, the deserialization logic expects the
178+
// size parameter of the resize method call to be an uint32).
179+
//
175180
// The expected content was created from the following Dart code:
176181
// MethodCall call = MethodCall('resize', ['flutter/test',3]);
177182
// StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();

0 commit comments

Comments
 (0)