Migrate FlutterEmbedderKeyResponder to ARC (flutter/engine#52048)

Smart pointers support ARC as of https://github.com/flutter/engine/pull/47612, and the unit tests were migrated in https://github.com/flutter/engine/pull/48162.

Migrate `FlutterEmbedderKeyResponder` from MRC to ARC.  Clean up some header imports which made this seem like it depended on not-yet-migrated MRC files.

Part of https://github.com/flutter/flutter/issues/137801.
This commit is contained in:
Jenn Magder 2024-04-12 13:09:04 -07:00 committed by GitHub
parent ad5a851612
commit 30569dc07a
4 changed files with 22 additions and 38 deletions

View File

@ -59,6 +59,10 @@ source_set("flutter_framework_source_arc") {
public_configs = [ "//flutter:config" ]
sources = [
"framework/Source/FlutterEmbedderKeyResponder.h",
"framework/Source/FlutterEmbedderKeyResponder.mm",
"framework/Source/FlutterKeyPrimaryResponder.h",
"framework/Source/FlutterKeySecondaryResponder.h",
"framework/Source/FlutterMetalLayer.h",
"framework/Source/FlutterMetalLayer.mm",
"framework/Source/FlutterRestorationPlugin.h",
@ -78,6 +82,8 @@ source_set("flutter_framework_source_arc") {
"UIKit.framework",
"IOSurface.framework",
]
deps += [ "//flutter/shell/platform/embedder:embedder_as_internal_library" ]
}
source_set("flutter_framework_source") {
@ -100,14 +106,10 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterDartProject_Internal.h",
"framework/Source/FlutterDartVMServicePublisher.h",
"framework/Source/FlutterDartVMServicePublisher.mm",
"framework/Source/FlutterEmbedderKeyResponder.h",
"framework/Source/FlutterEmbedderKeyResponder.mm",
"framework/Source/FlutterEngine.mm",
"framework/Source/FlutterEngineGroup.mm",
"framework/Source/FlutterEngine_Internal.h",
"framework/Source/FlutterHeadlessDartRunner.mm",
"framework/Source/FlutterKeyPrimaryResponder.h",
"framework/Source/FlutterKeySecondaryResponder.h",
"framework/Source/FlutterKeyboardManager.h",
"framework/Source/FlutterKeyboardManager.mm",
"framework/Source/FlutterOverlayView.h",

View File

@ -5,12 +5,8 @@
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTEREMBEDDERKEYRESPONDER_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTEREMBEDDERKEYRESPONDER_H_
#import <Foundation/NSObject.h>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#include "fml/memory/weak_ptr.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterKeyPrimaryResponder.h"
#import "flutter/shell/platform/embedder/embedder.h"

View File

@ -11,6 +11,9 @@
#import "KeyCodeMap_Internal.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
FLUTTER_ASSERT_ARC
namespace {
@ -165,7 +168,7 @@ static uint64_t GetLogicalKeyForEvent(FlutterUIPressProxy* press, NSNumber* mayb
const char* characters =
getEventCharacters(press.key.charactersIgnoringModifiers, press.key.keyCode);
NSString* keyLabel =
characters == nullptr ? nil : [[[NSString alloc] initWithUTF8String:characters] autorelease];
characters == nullptr ? nil : [[NSString alloc] initWithUTF8String:characters];
NSUInteger keyLabelLength = [keyLabel length];
// If this key is printable, generate the logical key from its Unicode
// value. Control keys such as ESC, CTRL, and SHIFT are not printable. HOME,
@ -252,7 +255,7 @@ void HandleResponse(bool handled, void* user_data);
*/
@interface FlutterKeyPendingResponse : NSObject
@property(readonly) FlutterEmbedderKeyResponder* responder;
@property(readonly, weak) FlutterEmbedderKeyResponder* responder;
@property(nonatomic) uint64_t responseId;
@ -302,7 +305,7 @@ void HandleResponse(bool handled, void* user_data);
* Only set in debug mode. Nil in release mode, or if the callback has not been
* handled.
*/
@property(readonly) NSString* debugHandleSource;
@property(readonly, copy) NSString* debugHandleSource;
@end
@implementation FlutterKeyCallbackGuard {
@ -319,11 +322,6 @@ void HandleResponse(bool handled, void* user_data);
return self;
}
- (void)dealloc {
[_callback release];
[super dealloc];
}
- (void)pendTo:(nonnull NSMutableDictionary<NSNumber*, FlutterAsyncKeyCallback>*)pendingResponses
withId:(uint64_t)responseId {
NSAssert(!_handled, @"This callback has been handled by %@.", _debugHandleSource);
@ -364,7 +362,7 @@ void HandleResponse(bool handled, void* user_data);
* The keys of the dictionary are physical keys, while the values are the logical keys
* of the key down event.
*/
@property(nonatomic, retain, readonly) NSMutableDictionary<NSNumber*, NSNumber*>* pressingRecords;
@property(nonatomic, copy, readonly) NSMutableDictionary<NSNumber*, NSNumber*>* pressingRecords;
/**
* A constant mask for NSEvent.modifierFlags that Flutter synchronizes with.
@ -403,7 +401,7 @@ void HandleResponse(bool handled, void* user_data);
* Its values are |responseId|s, and keys are the callback that was received
* along with the event.
*/
@property(nonatomic, retain, readonly)
@property(nonatomic, copy, readonly)
NSMutableDictionary<NSNumber*, FlutterAsyncKeyCallback>* pendingResponses;
/**
@ -502,13 +500,6 @@ void HandleResponse(bool handled, void* user_data);
return self;
}
- (void)dealloc {
[_sendEvent release];
[_pressingRecords release];
[_pendingResponses release];
[super dealloc];
}
- (void)handlePress:(nonnull FlutterUIPressProxy*)press
callback:(FlutterAsyncKeyCallback)callback API_AVAILABLE(ios(13.4)) {
if (@available(iOS 13.4, *)) {
@ -522,11 +513,11 @@ void HandleResponse(bool handled, void* user_data);
FlutterKeyCallbackGuard* guardedCallback = nil;
switch (press.phase) {
case UIPressPhaseBegan:
guardedCallback = [[[FlutterKeyCallbackGuard alloc] initWithCallback:callback] autorelease];
guardedCallback = [[FlutterKeyCallbackGuard alloc] initWithCallback:callback];
[self handlePressBegin:press callback:guardedCallback];
break;
case UIPressPhaseEnded:
guardedCallback = [[[FlutterKeyCallbackGuard alloc] initWithCallback:callback] autorelease];
guardedCallback = [[FlutterKeyCallbackGuard alloc] initWithCallback:callback];
[self handlePressEnd:press callback:guardedCallback];
break;
case UIPressPhaseChanged:
@ -632,9 +623,9 @@ void HandleResponse(bool handled, void* user_data);
_responseId += 1;
uint64_t responseId = _responseId;
FlutterKeyPendingResponse* pending =
[[[FlutterKeyPendingResponse alloc] initWithHandler:self responseId:responseId] autorelease];
[[FlutterKeyPendingResponse alloc] initWithHandler:self responseId:responseId];
[callback pendTo:_pendingResponses withId:responseId];
_sendEvent(event, HandleResponse, pending);
_sendEvent(event, HandleResponse, (__bridge_retained void* _Nullable)pending);
}
- (void)sendEmptyEvent {
@ -883,7 +874,7 @@ void HandleResponse(bool handled, void* user_data);
namespace {
void HandleResponse(bool handled, void* user_data) {
FlutterKeyPendingResponse* pending = reinterpret_cast<FlutterKeyPendingResponse*>(user_data);
FlutterKeyPendingResponse* pending = (__bridge_transfer FlutterKeyPendingResponse*)user_data;
[pending.responder handleResponse:handled forId:pending.responseId];
}
} // namespace

View File

@ -27,18 +27,13 @@ FLUTTER_ASSERT_ARC;
@interface TestKeyEvent : NSObject
@property(nonatomic) FlutterKeyEvent* data;
@property(nonatomic) FlutterKeyEventCallback callback;
@property(nonatomic) void* _Nullable userData;
- (nonnull instancetype)initWithEvent:(const FlutterKeyEvent*)event
callback:(nullable FlutterKeyEventCallback)callback
userData:(void* _Nullable)userData;
- (BOOL)hasCallback;
- (void)respond:(BOOL)handled;
@property(nonatomic, nullable) void* userData;
@end
@implementation TestKeyEvent
- (instancetype)initWithEvent:(const FlutterKeyEvent*)event
callback:(nullable FlutterKeyEventCallback)callback
userData:(void* _Nullable)userData {
userData:(nullable void*)userData {
self = [super init];
_data = new FlutterKeyEvent(*event);
if (event->character != nullptr) {