From ce974c65b30a2ec98a943f3a9888be11e7209ea8 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Fri, 12 Apr 2024 11:06:07 -0700 Subject: [PATCH] Migrate FlutterDartVMServicePublisher to ARC --- shell/platform/darwin/ios/BUILD.gn | 9 +++-- .../Source/FlutterDartVMServicePublisher.h | 2 +- .../Source/FlutterDartVMServicePublisher.mm | 34 ++++++++----------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index daf4f5db585d5..07d783e64ee9d 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -59,6 +59,8 @@ source_set("flutter_framework_source_arc") { public_configs = [ "//flutter:config" ] sources = [ + "framework/Source/FlutterDartVMServicePublisher.h", + "framework/Source/FlutterDartVMServicePublisher.mm", "framework/Source/FlutterEmbedderKeyResponder.h", "framework/Source/FlutterEmbedderKeyResponder.mm", "framework/Source/FlutterKeyPrimaryResponder.h", @@ -83,7 +85,10 @@ source_set("flutter_framework_source_arc") { "IOSurface.framework", ] - deps += [ "//flutter/shell/platform/embedder:embedder_as_internal_library" ] + deps += [ + "//flutter/runtime", + "//flutter/shell/platform/embedder:embedder_as_internal_library", + ] } source_set("flutter_framework_source") { @@ -104,8 +109,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", diff --git a/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h b/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h index 4feda093f5edc..a147bba9b82f9 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h @@ -14,7 +14,7 @@ - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; -@property(nonatomic, retain, readonly) NSURL* url; +@property(nonatomic, strong, readonly) NSURL* url; @end diff --git a/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.mm b/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.mm index 38288a1f247e2..1153de20e1e11 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.mm @@ -40,10 +40,12 @@ - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublicat #include #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 @@ @interface FlutterDartVMServicePublisher () + (NSData*)createTxtData:(NSURL*)url; @property(readonly, class) NSString* serviceName; -@property(readonly) fml::scoped_nsobject> delegate; +@property(readonly) NSObject* delegate; @property(nonatomic, readwrite) NSURL* url; @property(readonly) BOOL enableVMServicePublication; @@ -139,32 +141,30 @@ static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef, @implementation FlutterDartVMServicePublisher { flutter::DartServiceIsolate::CallbackHandle _callbackHandle; - std::unique_ptr> _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>(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]() { // 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 (weakSelf) { + NSURL* url = + [[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]]; + weakSelf.url = url; + if (weakSelf.enableVMServicePublication) { + [[weakSelf delegate] publishServiceProtocolPort:url]; } } }); @@ -190,15 +190,9 @@ + (NSData*)createTxtData:(NSURL*)url { } - (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