Migrate FlutterDartVMServicePublisher to ARC (flutter/engine#52081)

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 `FlutterDartVMServicePublisher` from MRC to ARC.  I validated `flutter attach` works on this engine PR, so the VM service URL is being advertised and the tool is discovering it.

Part of https://github.com/flutter/flutter/issues/137801.
This commit is contained in:
Jenn Magder
2024-04-15 16:55:27 -07:00
committed by GitHub
parent b4bad7f256
commit 2d3cce14a1
3 changed files with 19 additions and 23 deletions

View File

@@ -61,6 +61,8 @@ source_set("flutter_framework_source_arc") {
sources = [
"framework/Source/FlutterCallbackCache.mm",
"framework/Source/FlutterCallbackCache_Internal.h",
"framework/Source/FlutterDartVMServicePublisher.h",
"framework/Source/FlutterDartVMServicePublisher.mm",
"framework/Source/FlutterEmbedderKeyResponder.h",
"framework/Source/FlutterEmbedderKeyResponder.mm",
"framework/Source/FlutterKeyPrimaryResponder.h",
@@ -89,6 +91,7 @@ source_set("flutter_framework_source_arc") {
deps += [
"//flutter/lib/ui",
"//flutter/runtime",
"//flutter/shell/platform/embedder:embedder_as_internal_library",
]
}
@@ -109,8 +112,6 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterChannelKeyResponder.mm",
"framework/Source/FlutterDartProject.mm",
"framework/Source/FlutterDartProject_Internal.h",
"framework/Source/FlutterDartVMServicePublisher.h",
"framework/Source/FlutterDartVMServicePublisher.mm",
"framework/Source/FlutterEngine.mm",
"framework/Source/FlutterEngineGroup.mm",
"framework/Source/FlutterEngine_Internal.h",

View File

@@ -14,7 +14,7 @@
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@property(nonatomic, retain, readonly) NSURL* url;
@property(nonatomic, readonly) NSURL* url;
@end

View File

@@ -40,10 +40,12 @@
#include <net/if.h>
#include "flutter/fml/logging.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/runtime/dart_service_isolate.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
FLUTTER_ASSERT_ARC
@protocol FlutterDartVMServicePublisherDelegate
- (void)publishServiceProtocolPort:(NSURL*)uri;
@@ -54,7 +56,7 @@
+ (NSData*)createTxtData:(NSURL*)url;
@property(readonly, class) NSString* serviceName;
@property(readonly) fml::scoped_nsobject<NSObject<FlutterDartVMServicePublisherDelegate>> delegate;
@property(readonly) NSObject<FlutterDartVMServicePublisherDelegate>* delegate;
@property(nonatomic, readwrite) NSURL* url;
@property(readonly) BOOL enableVMServicePublication;
@@ -139,32 +141,31 @@ static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
@implementation FlutterDartVMServicePublisher {
flutter::DartServiceIsolate::CallbackHandle _callbackHandle;
std::unique_ptr<fml::WeakPtrFactory<FlutterDartVMServicePublisher>> _weakFactory;
}
- (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
self = [super init];
NSAssert(self, @"Super must not return null on init.");
_delegate.reset([[DartVMServiceDNSServiceDelegate alloc] init]);
_delegate = [[DartVMServiceDNSServiceDelegate alloc] init];
_enableVMServicePublication = enableVMServicePublication;
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterDartVMServicePublisher>>(self);
__weak __typeof(self) weakSelf = self;
fml::MessageLoop::EnsureInitializedForCurrentThread();
_callbackHandle = flutter::DartServiceIsolate::AddServerStatusCallback(
[weak = _weakFactory->GetWeakPtr(),
runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) {
[weakSelf, runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) {
if (!uri.empty()) {
runner->PostTask([weak, uri]() {
runner->PostTask([weakSelf, uri]() {
FlutterDartVMServicePublisher* strongSelf = weakSelf;
// uri comes in as something like 'http://127.0.0.1:XXXXX/' where XXXXX is the port
// number.
if (weak) {
NSURL* url = [[[NSURL alloc]
initWithString:[NSString stringWithUTF8String:uri.c_str()]] autorelease];
weak.get().url = url;
if (weak.get().enableVMServicePublication) {
[[weak.get() delegate] publishServiceProtocolPort:url];
if (strongSelf) {
NSURL* url =
[[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]];
strongSelf.url = url;
if (strongSelf.enableVMServicePublication) {
[[strongSelf delegate] publishServiceProtocolPort:url];
}
}
});
@@ -190,15 +191,9 @@ static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
}
- (void)dealloc {
// It will be destroyed and invalidate its weak pointers
// before any other members are destroyed.
_weakFactory.reset();
[_delegate stopService];
[_url release];
flutter::DartServiceIsolate::RemoveServerStatusCallback(_callbackHandle);
[super dealloc];
}
@end