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

Commit 4248e61

Browse files
committed
[macOS,iOS] Expose channel buffers 'resize' and 'overflow' control commands
1 parent 2f9a4ef commit 4248e61

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

shell/platform/darwin/common/framework/Headers/FlutterChannels.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,46 @@ FLUTTER_DARWIN_EXPORT
143143
* Adjusts the number of messages that will get buffered when sending messages to
144144
* channels that aren't fully set up yet. For example, the engine isn't running
145145
* yet or the channel's message handler isn't set up on the Dart side yet.
146+
*
147+
* @param name The channel name.
148+
* @param messenger The binary messenger.
149+
* @param newSize The new size of the buffer.
150+
*/
151+
+ (void)resizeChannelWithName:(NSString*)name
152+
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
153+
newSize:(NSInteger)newSize;
154+
155+
/**
156+
* Adjusts the number of messages that will get buffered when sending messages to
157+
* channels that aren't fully set up yet. For example, the engine isn't running
158+
* yet or the channel's message handler isn't set up on the Dart side yet.
159+
*
160+
* @param newSize The new size of the buffer.
146161
*/
147162
- (void)resizeChannelBuffer:(NSInteger)newSize;
148163

164+
/**
165+
* Toggles whether the channel should show warning messages when discarding messages
166+
* due to overflow.
167+
*
168+
* @param name The channel name.
169+
* @param messenger The binary messenger.
170+
* @param allowed When true, the channel is expected to overflow and warning messages
171+
* will not be shown.
172+
*/
173+
+ (void)setAllowOverflowChannelWithName:(NSString*)name
174+
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
175+
allowed:(BOOL)allowed;
176+
177+
/**
178+
* Toggles whether the channel should show warning messages when discarding messages
179+
* due to overflow.
180+
*
181+
* @param allowed When true, the channel is expected to overflow and warning messages
182+
* will not be shown.
183+
*/
184+
- (void)setAllowOverflow:(BOOL)allowed;
185+
149186
@end
150187

151188
/**

shell/platform/darwin/common/framework/Source/FlutterChannels.mm

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@
99
#pragma mark - Basic message channel
1010

1111
static NSString* const kFlutterChannelBuffersChannel = @"dev.flutter/channel-buffers";
12+
static NSString* const kResizeMethod = @"resize";
13+
static NSString* const kOverflowMethod = @"overflow";
1214

1315
static void ResizeChannelBuffer(NSObject<FlutterBinaryMessenger>* binaryMessenger,
1416
NSString* channel,
1517
NSInteger newSize) {
16-
NSString* messageString = [NSString stringWithFormat:@"resize\r%@\r%@", channel, @(newSize)];
17-
NSData* message = [messageString dataUsingEncoding:NSUTF8StringEncoding];
18+
NSArray* args = @[ channel, [NSNumber numberWithInt:newSize] ];
19+
FlutterMethodCall* resizeMethodCall = [FlutterMethodCall methodCallWithMethodName:kResizeMethod
20+
arguments:args];
21+
NSObject<FlutterMethodCodec>* codec = [FlutterStandardMethodCodec sharedInstance];
22+
NSData* message = [codec encodeMethodCall:resizeMethodCall];
23+
[binaryMessenger sendOnChannel:kFlutterChannelBuffersChannel message:message];
24+
}
25+
26+
static void SetAllowChannelOverflow(NSObject<FlutterBinaryMessenger>* binaryMessenger,
27+
NSString* channel,
28+
BOOL allowed) {
29+
NSArray* args = @[ channel, [NSNumber numberWithBool:allowed] ];
30+
FlutterMethodCall* overflowMethodCall =
31+
[FlutterMethodCall methodCallWithMethodName:kOverflowMethod arguments:args];
32+
NSObject<FlutterMethodCodec>* codec = [FlutterStandardMethodCodec sharedInstance];
33+
NSData* message = [codec encodeMethodCall:overflowMethodCall];
1834
[binaryMessenger sendOnChannel:kFlutterChannelBuffersChannel message:message];
1935
}
2036

@@ -114,10 +130,26 @@ - (void)setMessageHandler:(FlutterMessageHandler)handler {
114130
_connection = SetMessageHandler(_messenger, _name, messageHandler, _taskQueue);
115131
}
116132

133+
+ (void)resizeChannelWithName:(NSString*)name
134+
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
135+
newSize:(NSInteger)newSize {
136+
ResizeChannelBuffer(messenger, name, newSize);
137+
}
138+
117139
- (void)resizeChannelBuffer:(NSInteger)newSize {
118140
ResizeChannelBuffer(_messenger, _name, newSize);
119141
}
120142

143+
+ (void)setAllowOverflowChannelWithName:(NSString*)name
144+
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
145+
allowed:(BOOL)allowed {
146+
SetAllowChannelOverflow(messenger, name, allowed);
147+
}
148+
149+
- (void)setAllowOverflow:(BOOL)allowed {
150+
SetAllowChannelOverflow(_messenger, _name, allowed);
151+
}
152+
121153
@end
122154

123155
#pragma mark - Method channel

shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,31 @@ - (void)testCallMethodHandler {
147147
}
148148

149149
- (void)testResize {
150-
NSString* channelName = @"foo";
150+
NSString* channelName = @"flutter/test";
151+
id binaryMessenger = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
152+
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
153+
FlutterBasicMessageChannel* channel =
154+
[[FlutterBasicMessageChannel alloc] initWithName:channelName
155+
binaryMessenger:binaryMessenger
156+
codec:codec];
157+
XCTAssertNotNil(channel);
158+
159+
// The expected content was created from the following Dart code:
160+
// MethodCall call = MethodCall('resize', ['flutter/test',3]);
161+
// StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
162+
const unsigned char bytes[] = {7, 6, 114, 101, 115, 105, 122, 101, 12, 2,
163+
7, 12, 102, 108, 117, 116, 116, 101, 114, 47,
164+
116, 101, 115, 116, 3, 3, 0, 0, 0};
165+
NSData* expectedMessage = [NSData dataWithBytes:bytes length:sizeof(bytes)];
166+
167+
OCMExpect([binaryMessenger sendOnChannel:@"dev.flutter/channel-buffers" message:expectedMessage]);
168+
[channel resizeChannelBuffer:3];
169+
OCMVerifyAll(binaryMessenger);
170+
[binaryMessenger stopMocking];
171+
}
172+
173+
- (bool)testetAllowChannelOverflow {
174+
NSString* channelName = @"flutter/test";
151175
id binaryMessenger = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
152176
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
153177
FlutterBasicMessageChannel* channel =
@@ -156,11 +180,15 @@ - (void)testResize {
156180
codec:codec];
157181
XCTAssertNotNil(channel);
158182

159-
NSString* expectedMessageString =
160-
[NSString stringWithFormat:@"resize\r%@\r%@", channelName, @100];
161-
NSData* expectedMessage = [expectedMessageString dataUsingEncoding:NSUTF8StringEncoding];
183+
// The expected content was created from the following Dart code:
184+
// MethodCall call = MethodCall('overflow',['flutter/test', true]);
185+
// StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
186+
const unsigned char bytes[] = {7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
187+
102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
188+
NSData* expectedMessage = [NSData dataWithBytes:bytes length:sizeof(bytes)];
189+
162190
OCMExpect([binaryMessenger sendOnChannel:@"dev.flutter/channel-buffers" message:expectedMessage]);
163-
[channel resizeChannelBuffer:100];
191+
[channel setAllowOverflow:YES];
164192
OCMVerifyAll(binaryMessenger);
165193
[binaryMessenger stopMocking];
166194
}

0 commit comments

Comments
 (0)