Skip to content

Commit 06221b2

Browse files
Chris Yangloic-sharma
Chris Yang
authored andcommitted
Revert "Revert "reland "Migrate darwin common "framework_shared" target to ARC flutter#37049" (flutter#37219)" (flutter#37320)" (flutter#37883)
This reverts commit cf22fc2.
1 parent da89fc5 commit 06221b2

File tree

8 files changed

+154
-132
lines changed

8 files changed

+154
-132
lines changed

shell/platform/darwin/BUILD.gn

+20-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ source_set("flutter_channels") {
2626
sources = [
2727
"common/buffer_conversions.h",
2828
"common/buffer_conversions.mm",
29+
]
30+
31+
deps = [ "//flutter/fml" ]
32+
33+
public_deps = [ ":flutter_channels_arc" ]
34+
35+
public_configs = [ "//flutter:config" ]
36+
}
37+
38+
source_set("flutter_channels_arc") {
39+
cflags_objc = flutter_cflags_objc_arc
40+
cflags_objcc = flutter_cflags_objcc_arc
41+
42+
sources = [
2943
"common/framework/Headers/FlutterBinaryMessenger.h",
3044
"common/framework/Headers/FlutterChannels.h",
3145
"common/framework/Headers/FlutterCodecs.h",
@@ -36,13 +50,12 @@ source_set("flutter_channels") {
3650
"common/framework/Source/FlutterStandardCodec_Internal.h",
3751
]
3852

39-
deps = [
40-
"//flutter/common",
41-
"//flutter/flow",
42-
"//flutter/fml",
43-
"//flutter/runtime",
44-
"//flutter/shell/common",
45-
"//third_party/skia",
53+
public = [
54+
"common/framework/Headers/FlutterBinaryMessenger.h",
55+
"common/framework/Headers/FlutterChannels.h",
56+
"common/framework/Headers/FlutterCodecs.h",
57+
"common/framework/Headers/FlutterMacros.h",
58+
"common/framework/Source/FlutterStandardCodec_Internal.h",
4659
]
4760

4861
public_configs = [ "//flutter:config" ]

shell/platform/darwin/common/BUILD.gn

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ source_set("common") {
1616
"command_line.mm",
1717
]
1818

19-
deps = [
20-
"//flutter/common",
21-
"//flutter/flow",
22-
"//flutter/fml",
23-
"//flutter/runtime",
24-
"//flutter/shell/common",
25-
"//third_party/dart/runtime:dart_api",
26-
"//third_party/skia",
27-
]
19+
deps = [ "//flutter/fml" ]
2820

2921
public_configs = [ "//flutter:config" ]
3022
}
@@ -38,8 +30,8 @@ config("framework_relative_headers") {
3830

3931
# Framework code shared between iOS and macOS.
4032
source_set("framework_shared") {
41-
cflags_objc = flutter_cflags_objc
42-
cflags_objcc = flutter_cflags_objcc
33+
cflags_objc = flutter_cflags_objc_arc
34+
cflags_objcc = flutter_cflags_objcc_arc
4335

4436
sources = [
4537
"framework/Source/FlutterChannels.mm",

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,17 @@ FLUTTER_DARWIN_EXPORT
344344
/**
345345
* The type of the encoded values.
346346
*/
347-
@property(readonly, nonatomic) FlutterStandardDataType type;
347+
@property(readonly, nonatomic, assign) FlutterStandardDataType type;
348348

349349
/**
350350
* The number of value items encoded.
351351
*/
352-
@property(readonly, nonatomic) UInt32 elementCount;
352+
@property(readonly, nonatomic, assign) UInt32 elementCount;
353353

354354
/**
355355
* The number of bytes used by the encoding of a single value item.
356356
*/
357-
@property(readonly, nonatomic) UInt8 elementSize;
357+
@property(readonly, nonatomic, assign) UInt8 elementSize;
358358
@end
359359

360360
/**

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

+31-63
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
66

7+
FLUTTER_ASSERT_ARC
8+
79
#pragma mark - Basic message channel
810

911
static NSString* const kFlutterChannelBuffersChannel = @"dev.flutter/channel-buffers";
@@ -51,9 +53,9 @@ + (instancetype)messageChannelWithName:(NSString*)name
5153
+ (instancetype)messageChannelWithName:(NSString*)name
5254
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
5355
codec:(NSObject<FlutterMessageCodec>*)codec {
54-
return [[[FlutterBasicMessageChannel alloc] initWithName:name
55-
binaryMessenger:messenger
56-
codec:codec] autorelease];
56+
return [[FlutterBasicMessageChannel alloc] initWithName:name
57+
binaryMessenger:messenger
58+
codec:codec];
5759
}
5860

5961
- (instancetype)initWithName:(NSString*)name
@@ -69,21 +71,13 @@ - (instancetype)initWithName:(NSString*)name
6971
taskQueue:(NSObject<FlutterTaskQueue>*)taskQueue {
7072
self = [super init];
7173
NSAssert(self, @"Super init cannot be nil");
72-
_name = [name retain];
73-
_messenger = [messenger retain];
74-
_codec = [codec retain];
75-
_taskQueue = [taskQueue retain];
74+
_name = [name copy];
75+
_messenger = messenger;
76+
_codec = codec;
77+
_taskQueue = taskQueue;
7678
return self;
7779
}
7880

79-
- (void)dealloc {
80-
[_name release];
81-
[_messenger release];
82-
[_codec release];
83-
[_taskQueue release];
84-
[super dealloc];
85-
}
86-
8781
- (void)sendMessage:(id)message {
8882
[_messenger sendOnChannel:_name message:[_codec encode:message]];
8983
}
@@ -107,7 +101,10 @@ - (void)setMessageHandler:(FlutterMessageHandler)handler {
107101
}
108102
return;
109103
}
104+
110105
// Grab reference to avoid retain on self.
106+
// `self` might be released before the block, so the block needs to retain the codec to
107+
// make sure it is not released with `self`
111108
NSObject<FlutterMessageCodec>* codec = _codec;
112109
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
113110
handler([codec decode:message], ^(id reply) {
@@ -128,26 +125,19 @@ - (void)resizeChannelBuffer:(NSInteger)newSize {
128125
////////////////////////////////////////////////////////////////////////////////
129126
@implementation FlutterError
130127
+ (instancetype)errorWithCode:(NSString*)code message:(NSString*)message details:(id)details {
131-
return [[[FlutterError alloc] initWithCode:code message:message details:details] autorelease];
128+
return [[FlutterError alloc] initWithCode:code message:message details:details];
132129
}
133130

134131
- (instancetype)initWithCode:(NSString*)code message:(NSString*)message details:(id)details {
135132
NSAssert(code, @"Code cannot be nil");
136133
self = [super init];
137134
NSAssert(self, @"Super init cannot be nil");
138-
_code = [code retain];
139-
_message = [message retain];
140-
_details = [details retain];
135+
_code = [code copy];
136+
_message = [message copy];
137+
_details = details;
141138
return self;
142139
}
143140

144-
- (void)dealloc {
145-
[_code release];
146-
[_message release];
147-
[_details release];
148-
[super dealloc];
149-
}
150-
151141
- (BOOL)isEqual:(id)object {
152142
if (self == object) {
153143
return YES;
@@ -169,24 +159,18 @@ - (NSUInteger)hash {
169159
////////////////////////////////////////////////////////////////////////////////
170160
@implementation FlutterMethodCall
171161
+ (instancetype)methodCallWithMethodName:(NSString*)method arguments:(id)arguments {
172-
return [[[FlutterMethodCall alloc] initWithMethodName:method arguments:arguments] autorelease];
162+
return [[FlutterMethodCall alloc] initWithMethodName:method arguments:arguments];
173163
}
174164

175165
- (instancetype)initWithMethodName:(NSString*)method arguments:(id)arguments {
176166
NSAssert(method, @"Method name cannot be nil");
177167
self = [super init];
178168
NSAssert(self, @"Super init cannot be nil");
179-
_method = [method retain];
180-
_arguments = [arguments retain];
169+
_method = [method copy];
170+
_arguments = arguments;
181171
return self;
182172
}
183173

184-
- (void)dealloc {
185-
[_method release];
186-
[_arguments release];
187-
[super dealloc];
188-
}
189-
190174
- (BOOL)isEqual:(id)object {
191175
if (self == object) {
192176
return YES;
@@ -224,8 +208,7 @@ + (instancetype)methodChannelWithName:(NSString*)name
224208
+ (instancetype)methodChannelWithName:(NSString*)name
225209
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
226210
codec:(NSObject<FlutterMethodCodec>*)codec {
227-
return [[[FlutterMethodChannel alloc] initWithName:name binaryMessenger:messenger
228-
codec:codec] autorelease];
211+
return [[FlutterMethodChannel alloc] initWithName:name binaryMessenger:messenger codec:codec];
229212
}
230213

231214
- (instancetype)initWithName:(NSString*)name
@@ -240,21 +223,13 @@ - (instancetype)initWithName:(NSString*)name
240223
taskQueue:(NSObject<FlutterTaskQueue>*)taskQueue {
241224
self = [super init];
242225
NSAssert(self, @"Super init cannot be nil");
243-
_name = [name retain];
244-
_messenger = [messenger retain];
245-
_codec = [codec retain];
246-
_taskQueue = [taskQueue retain];
226+
_name = [name copy];
227+
_messenger = messenger;
228+
_codec = codec;
229+
_taskQueue = taskQueue;
247230
return self;
248231
}
249232

250-
- (void)dealloc {
251-
[_name release];
252-
[_messenger release];
253-
[_codec release];
254-
[_taskQueue release];
255-
[super dealloc];
256-
}
257-
258233
- (void)invokeMethod:(NSString*)method arguments:(id)arguments {
259234
FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:method
260235
arguments:arguments];
@@ -285,6 +260,8 @@ - (void)setMethodCallHandler:(FlutterMethodCallHandler)handler {
285260
return;
286261
}
287262
// Make sure the block captures the codec, not self.
263+
// `self` might be released before the block, so the block needs to retain the codec to
264+
// make sure it is not released with `self`
288265
NSObject<FlutterMethodCodec>* codec = _codec;
289266
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
290267
FlutterMethodCall* call = [codec decodeMethodCall:message];
@@ -328,8 +305,7 @@ + (instancetype)eventChannelWithName:(NSString*)name
328305
+ (instancetype)eventChannelWithName:(NSString*)name
329306
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
330307
codec:(NSObject<FlutterMethodCodec>*)codec {
331-
return [[[FlutterEventChannel alloc] initWithName:name binaryMessenger:messenger
332-
codec:codec] autorelease];
308+
return [[FlutterEventChannel alloc] initWithName:name binaryMessenger:messenger codec:codec];
333309
}
334310

335311
- (instancetype)initWithName:(NSString*)name
@@ -344,21 +320,13 @@ - (instancetype)initWithName:(NSString*)name
344320
taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue {
345321
self = [super init];
346322
NSAssert(self, @"Super init cannot be nil");
347-
_name = [name retain];
348-
_messenger = [messenger retain];
349-
_codec = [codec retain];
350-
_taskQueue = [taskQueue retain];
323+
_name = [name copy];
324+
_messenger = messenger;
325+
_codec = codec;
326+
_taskQueue = taskQueue;
351327
return self;
352328
}
353329

354-
- (void)dealloc {
355-
[_name release];
356-
[_codec release];
357-
[_messenger release];
358-
[_taskQueue release];
359-
[super dealloc];
360-
}
361-
362330
static FlutterBinaryMessengerConnection SetStreamHandlerMessageHandlerOnChannel(
363331
NSObject<FlutterStreamHandler>* handler,
364332
NSString* name,

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

+72
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,78 @@ - (void)testBasicMessageChannelTaskQueue {
234234
OCMVerify([binaryMessenger cleanUpConnection:connection]);
235235
}
236236

237+
- (void)testBasicMessageChannelInvokeHandlerAfterChannelReleased {
238+
NSString* channelName = @"foo";
239+
__block NSString* handlerMessage;
240+
__block FlutterBinaryMessageHandler messageHandler;
241+
@autoreleasepool {
242+
FlutterBinaryMessengerConnection connection = 123;
243+
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
244+
id codec = OCMProtocolMock(@protocol(FlutterMessageCodec));
245+
id taskQueue = OCMClassMock([NSObject class]);
246+
FlutterBasicMessageChannel* channel =
247+
[[FlutterBasicMessageChannel alloc] initWithName:channelName
248+
binaryMessenger:binaryMessenger
249+
codec:codec
250+
taskQueue:taskQueue];
251+
252+
FlutterMessageHandler handler = ^(id _Nullable message, FlutterReply callback) {
253+
handlerMessage = message;
254+
};
255+
OCMStub([binaryMessenger
256+
setMessageHandlerOnChannel:channelName
257+
binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
258+
FlutterBinaryMessageHandler handler) {
259+
messageHandler = handler;
260+
return YES;
261+
}]
262+
taskQueue:taskQueue])
263+
.andReturn(connection);
264+
OCMStub([codec decode:[OCMArg any]]).andReturn(@"decoded message");
265+
[channel setMessageHandler:handler];
266+
}
267+
// Channel is released, messageHandler should still retain the codec. The codec
268+
// internally makes a `decode` call and updates the `handlerMessage` to "decoded message".
269+
messageHandler([NSData data], ^(NSData* data){
270+
});
271+
XCTAssertEqualObjects(handlerMessage, @"decoded message");
272+
}
273+
274+
- (void)testMethodChannelInvokeHandlerAfterChannelReleased {
275+
NSString* channelName = @"foo";
276+
FlutterBinaryMessengerConnection connection = 123;
277+
__block FlutterMethodCall* decodedMethodCall;
278+
__block FlutterBinaryMessageHandler messageHandler;
279+
@autoreleasepool {
280+
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
281+
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
282+
id taskQueue = OCMClassMock([NSObject class]);
283+
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
284+
binaryMessenger:binaryMessenger
285+
codec:codec
286+
taskQueue:taskQueue];
287+
FlutterMethodCallHandler handler = ^(FlutterMethodCall* call, FlutterResult result) {
288+
decodedMethodCall = call;
289+
};
290+
OCMStub([binaryMessenger
291+
setMessageHandlerOnChannel:channelName
292+
binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
293+
FlutterBinaryMessageHandler handler) {
294+
messageHandler = handler;
295+
return YES;
296+
}]
297+
taskQueue:taskQueue])
298+
.andReturn(connection);
299+
OCMStub([codec decodeMethodCall:[OCMArg any]])
300+
.andReturn([FlutterMethodCall methodCallWithMethodName:@"decoded method call"
301+
arguments:nil]);
302+
[channel setMethodCallHandler:handler];
303+
}
304+
messageHandler([NSData data], ^(NSData* data){
305+
});
306+
XCTAssertEqualObjects(decodedMethodCall.method, @"decoded method call");
307+
}
308+
237309
- (void)testMethodChannelTaskQueue {
238310
NSString* channelName = @"foo";
239311
FlutterBinaryMessengerConnection connection = 123;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <cstring>
88

9+
FLUTTER_ASSERT_ARC
10+
911
@implementation FlutterBinaryCodec
1012
+ (instancetype)sharedInstance {
1113
static id _sharedInstance = nil;
@@ -48,7 +50,7 @@ - (NSString*)decode:(NSData*)message {
4850
if (message == nil) {
4951
return nil;
5052
}
51-
return [[[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding] autorelease];
53+
return [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding];
5254
}
5355
@end
5456

0 commit comments

Comments
 (0)