Skip to content

Commit

Permalink
Revert "reland "Migrate darwin common "framework_shared" target to ARC
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Yang authored Nov 4, 2022
1 parent 6950689 commit cf22fc2
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 154 deletions.
27 changes: 7 additions & 20 deletions shell/platform/darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ source_set("flutter_channels") {
sources = [
"common/buffer_conversions.h",
"common/buffer_conversions.mm",
]

deps = [ "//flutter/fml" ]

public_deps = [ ":flutter_channels_arc" ]

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

source_set("flutter_channels_arc") {
cflags_objc = flutter_cflags_objc_arc
cflags_objcc = flutter_cflags_objcc_arc

sources = [
"common/framework/Headers/FlutterBinaryMessenger.h",
"common/framework/Headers/FlutterChannels.h",
"common/framework/Headers/FlutterCodecs.h",
Expand All @@ -50,12 +36,13 @@ source_set("flutter_channels_arc") {
"common/framework/Source/FlutterStandardCodec_Internal.h",
]

public = [
"common/framework/Headers/FlutterBinaryMessenger.h",
"common/framework/Headers/FlutterChannels.h",
"common/framework/Headers/FlutterCodecs.h",
"common/framework/Headers/FlutterMacros.h",
"common/framework/Source/FlutterStandardCodec_Internal.h",
deps = [
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/runtime",
"//flutter/shell/common",
"//third_party/skia",
]

public_configs = [ "//flutter:config" ]
Expand Down
14 changes: 11 additions & 3 deletions shell/platform/darwin/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ source_set("common") {
"command_line.mm",
]

deps = [ "//flutter/fml" ]
deps = [
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/runtime",
"//flutter/shell/common",
"//third_party/dart/runtime:dart_api",
"//third_party/skia",
]

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

# Framework code shared between iOS and macOS.
source_set("framework_shared") {
cflags_objc = flutter_cflags_objc_arc
cflags_objcc = flutter_cflags_objcc_arc
cflags_objc = flutter_cflags_objc
cflags_objcc = flutter_cflags_objcc

sources = [
"framework/Source/FlutterChannels.mm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ FLUTTER_DARWIN_EXPORT
/**
* The type of the encoded values.
*/
@property(readonly, nonatomic, assign) FlutterStandardDataType type;
@property(readonly, nonatomic) FlutterStandardDataType type;

/**
* The number of value items encoded.
*/
@property(readonly, nonatomic, assign) UInt32 elementCount;
@property(readonly, nonatomic) UInt32 elementCount;

/**
* The number of bytes used by the encoding of a single value item.
*/
@property(readonly, nonatomic, assign) UInt8 elementSize;
@property(readonly, nonatomic) UInt8 elementSize;
@end

/**
Expand Down
94 changes: 63 additions & 31 deletions shell/platform/darwin/common/framework/Source/FlutterChannels.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

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

FLUTTER_ASSERT_ARC

#pragma mark - Basic message channel

static NSString* const kFlutterChannelBuffersChannel = @"dev.flutter/channel-buffers";
Expand Down Expand Up @@ -53,9 +51,9 @@ + (instancetype)messageChannelWithName:(NSString*)name
+ (instancetype)messageChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
codec:(NSObject<FlutterMessageCodec>*)codec {
return [[FlutterBasicMessageChannel alloc] initWithName:name
binaryMessenger:messenger
codec:codec];
return [[[FlutterBasicMessageChannel alloc] initWithName:name
binaryMessenger:messenger
codec:codec] autorelease];
}

- (instancetype)initWithName:(NSString*)name
Expand All @@ -71,13 +69,21 @@ - (instancetype)initWithName:(NSString*)name
taskQueue:(NSObject<FlutterTaskQueue>*)taskQueue {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_name = [name copy];
_messenger = messenger;
_codec = codec;
_taskQueue = taskQueue;
_name = [name retain];
_messenger = [messenger retain];
_codec = [codec retain];
_taskQueue = [taskQueue retain];
return self;
}

- (void)dealloc {
[_name release];
[_messenger release];
[_codec release];
[_taskQueue release];
[super dealloc];
}

- (void)sendMessage:(id)message {
[_messenger sendOnChannel:_name message:[_codec encode:message]];
}
Expand All @@ -101,10 +107,7 @@ - (void)setMessageHandler:(FlutterMessageHandler)handler {
}
return;
}

// Grab reference to avoid retain on self.
// `self` might be released before the block, so the block needs to retain the codec to
// make sure it is not released with `self`
NSObject<FlutterMessageCodec>* codec = _codec;
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
handler([codec decode:message], ^(id reply) {
Expand All @@ -125,19 +128,26 @@ - (void)resizeChannelBuffer:(NSInteger)newSize {
////////////////////////////////////////////////////////////////////////////////
@implementation FlutterError
+ (instancetype)errorWithCode:(NSString*)code message:(NSString*)message details:(id)details {
return [[FlutterError alloc] initWithCode:code message:message details:details];
return [[[FlutterError alloc] initWithCode:code message:message details:details] autorelease];
}

- (instancetype)initWithCode:(NSString*)code message:(NSString*)message details:(id)details {
NSAssert(code, @"Code cannot be nil");
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_code = [code copy];
_message = [message copy];
_details = details;
_code = [code retain];
_message = [message retain];
_details = [details retain];
return self;
}

- (void)dealloc {
[_code release];
[_message release];
[_details release];
[super dealloc];
}

- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
Expand All @@ -159,18 +169,24 @@ - (NSUInteger)hash {
////////////////////////////////////////////////////////////////////////////////
@implementation FlutterMethodCall
+ (instancetype)methodCallWithMethodName:(NSString*)method arguments:(id)arguments {
return [[FlutterMethodCall alloc] initWithMethodName:method arguments:arguments];
return [[[FlutterMethodCall alloc] initWithMethodName:method arguments:arguments] autorelease];
}

- (instancetype)initWithMethodName:(NSString*)method arguments:(id)arguments {
NSAssert(method, @"Method name cannot be nil");
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_method = [method copy];
_arguments = arguments;
_method = [method retain];
_arguments = [arguments retain];
return self;
}

- (void)dealloc {
[_method release];
[_arguments release];
[super dealloc];
}

- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
Expand Down Expand Up @@ -208,7 +224,8 @@ + (instancetype)methodChannelWithName:(NSString*)name
+ (instancetype)methodChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
codec:(NSObject<FlutterMethodCodec>*)codec {
return [[FlutterMethodChannel alloc] initWithName:name binaryMessenger:messenger codec:codec];
return [[[FlutterMethodChannel alloc] initWithName:name binaryMessenger:messenger
codec:codec] autorelease];
}

- (instancetype)initWithName:(NSString*)name
Expand All @@ -223,13 +240,21 @@ - (instancetype)initWithName:(NSString*)name
taskQueue:(NSObject<FlutterTaskQueue>*)taskQueue {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_name = [name copy];
_messenger = messenger;
_codec = codec;
_taskQueue = taskQueue;
_name = [name retain];
_messenger = [messenger retain];
_codec = [codec retain];
_taskQueue = [taskQueue retain];
return self;
}

- (void)dealloc {
[_name release];
[_messenger release];
[_codec release];
[_taskQueue release];
[super dealloc];
}

- (void)invokeMethod:(NSString*)method arguments:(id)arguments {
FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:method
arguments:arguments];
Expand Down Expand Up @@ -260,8 +285,6 @@ - (void)setMethodCallHandler:(FlutterMethodCallHandler)handler {
return;
}
// Make sure the block captures the codec, not self.
// `self` might be released before the block, so the block needs to retain the codec to
// make sure it is not released with `self`
NSObject<FlutterMethodCodec>* codec = _codec;
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
FlutterMethodCall* call = [codec decodeMethodCall:message];
Expand Down Expand Up @@ -305,7 +328,8 @@ + (instancetype)eventChannelWithName:(NSString*)name
+ (instancetype)eventChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
codec:(NSObject<FlutterMethodCodec>*)codec {
return [[FlutterEventChannel alloc] initWithName:name binaryMessenger:messenger codec:codec];
return [[[FlutterEventChannel alloc] initWithName:name binaryMessenger:messenger
codec:codec] autorelease];
}

- (instancetype)initWithName:(NSString*)name
Expand All @@ -320,13 +344,21 @@ - (instancetype)initWithName:(NSString*)name
taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_name = [name copy];
_messenger = messenger;
_codec = codec;
_taskQueue = taskQueue;
_name = [name retain];
_messenger = [messenger retain];
_codec = [codec retain];
_taskQueue = [taskQueue retain];
return self;
}

- (void)dealloc {
[_name release];
[_codec release];
[_messenger release];
[_taskQueue release];
[super dealloc];
}

static FlutterBinaryMessengerConnection SetStreamHandlerMessageHandlerOnChannel(
NSObject<FlutterStreamHandler>* handler,
NSString* name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,78 +234,6 @@ - (void)testBasicMessageChannelTaskQueue {
OCMVerify([binaryMessenger cleanUpConnection:connection]);
}

- (void)testBasicMessageChannelInvokeHandlerAfterChannelReleased {
NSString* channelName = @"foo";
__block NSString* handlerMessage;
__block FlutterBinaryMessageHandler messageHandler;
@autoreleasepool {
FlutterBinaryMessengerConnection connection = 123;
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
id codec = OCMProtocolMock(@protocol(FlutterMessageCodec));
id taskQueue = OCMClassMock([NSObject class]);
FlutterBasicMessageChannel* channel =
[[FlutterBasicMessageChannel alloc] initWithName:channelName
binaryMessenger:binaryMessenger
codec:codec
taskQueue:taskQueue];

FlutterMessageHandler handler = ^(id _Nullable message, FlutterReply callback) {
handlerMessage = message;
};
OCMStub([binaryMessenger
setMessageHandlerOnChannel:channelName
binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
FlutterBinaryMessageHandler handler) {
messageHandler = handler;
return YES;
}]
taskQueue:taskQueue])
.andReturn(connection);
OCMStub([codec decode:[OCMArg any]]).andReturn(@"decoded message");
[channel setMessageHandler:handler];
}
// Channel is released, messageHandler should still retain the codec. The codec
// internally makes a `decode` call and updates the `handlerMessage` to "decoded message".
messageHandler([NSData data], ^(NSData* data){
});
XCTAssertEqualObjects(handlerMessage, @"decoded message");
}

- (void)testMethodChannelInvokeHandlerAfterChannelReleased {
NSString* channelName = @"foo";
FlutterBinaryMessengerConnection connection = 123;
__block FlutterMethodCall* decodedMethodCall;
__block FlutterBinaryMessageHandler messageHandler;
@autoreleasepool {
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
id taskQueue = OCMClassMock([NSObject class]);
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
binaryMessenger:binaryMessenger
codec:codec
taskQueue:taskQueue];
FlutterMethodCallHandler handler = ^(FlutterMethodCall* call, FlutterResult result) {
decodedMethodCall = call;
};
OCMStub([binaryMessenger
setMessageHandlerOnChannel:channelName
binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
FlutterBinaryMessageHandler handler) {
messageHandler = handler;
return YES;
}]
taskQueue:taskQueue])
.andReturn(connection);
OCMStub([codec decodeMethodCall:[OCMArg any]])
.andReturn([FlutterMethodCall methodCallWithMethodName:@"decoded method call"
arguments:nil]);
[channel setMethodCallHandler:handler];
}
messageHandler([NSData data], ^(NSData* data){
});
XCTAssertEqualObjects(decodedMethodCall.method, @"decoded method call");
}

- (void)testMethodChannelTaskQueue {
NSString* channelName = @"foo";
FlutterBinaryMessengerConnection connection = 123;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <cstring>

FLUTTER_ASSERT_ARC

@implementation FlutterBinaryCodec
+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
Expand Down Expand Up @@ -50,7 +48,7 @@ - (NSString*)decode:(NSData*)message {
if (message == nil) {
return nil;
}
return [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding];
return [[[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding] autorelease];
}
@end

Expand Down
Loading

0 comments on commit cf22fc2

Please sign in to comment.