Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 79ad78a

Browse files
committed
iOS: Migrate platform_view_ios to ARC
Migrates PlatformViewIOS from manual reference counting to ARC. Eliminates use of scoped_nsobject, scoped_nsprotocol, and WeakNSObject. Since this is the last non-ARC file in `flutter_framework_source`, this also eliminates the `flutter_framework_source` target, then also renames the `flutter_framework_source_arc` target to `flutter_framework_source` since... it's ALL ARC. No semantic changes, therefore no changes to tests. Issue: flutter/flutter#137801
1 parent e1603de commit 79ad78a

File tree

8 files changed

+58
-142
lines changed

8 files changed

+58
-142
lines changed

shell/platform/darwin/ios/BUILD.gn

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ _flutter_framework_headers = [
3939

4040
_flutter_framework_headers_copy_dir = "$_flutter_framework_dir/Headers"
4141

42-
source_set("flutter_framework_source_arc") {
42+
source_set("flutter_framework_source") {
4343
visibility = [ ":*" ]
4444
cflags_objc = flutter_cflags_objc_arc
4545
cflags_objcc = flutter_cflags_objcc_arc
@@ -48,15 +48,10 @@ source_set("flutter_framework_source_arc") {
4848
if (darwin_extension_safe) {
4949
defines += [ "APPLICATION_EXTENSION_API_ONLY=1" ]
5050
}
51-
allow_circular_includes_from = [ ":flutter_framework_source" ]
52-
deps = [
53-
":flutter_framework_source",
54-
"//flutter/fml",
55-
"//flutter/shell/platform/common:common_cpp_input",
56-
"//flutter/shell/platform/darwin/common:framework_common",
57-
"//flutter/third_party/icu",
51+
public_configs = [
52+
":ios_gpu_configuration_config",
53+
"//flutter:config",
5854
]
59-
public_configs = [ "//flutter:config" ]
6055

6156
sources = [
6257
"framework/Source/FlutterAppDelegate.mm",
@@ -156,81 +151,46 @@ source_set("flutter_framework_source_arc") {
156151
"ios_surface_software.mm",
157152
"platform_message_handler_ios.h",
158153
"platform_message_handler_ios.mm",
154+
"platform_view_ios.h",
155+
"platform_view_ios.mm",
159156
"rendering_api_selection.h",
160157
"rendering_api_selection.mm",
161158
]
159+
sources += _flutter_framework_headers
162160

163161
frameworks = [
164-
"UIKit.framework",
162+
"AudioToolbox.framework",
163+
"CoreMedia.framework",
164+
"CoreVideo.framework",
165165
"IOSurface.framework",
166+
"QuartzCore.framework",
167+
"UIKit.framework",
166168
]
169+
if (flutter_runtime_mode == "profile" || flutter_runtime_mode == "debug") {
170+
# This is required by the profiler_metrics_ios.mm to get GPU statistics.
171+
# Usage in release builds will cause rejection from the App Store.
172+
frameworks += [ "IOKit.framework" ]
173+
}
167174

168-
deps += [
175+
deps = [
169176
":ios_gpu_configuration",
170177
"//flutter/common:common",
171178
"//flutter/common/graphics",
179+
"//flutter/fml",
172180
"//flutter/lib/ui",
173181
"//flutter/runtime",
174182
"//flutter/shell/common",
183+
"//flutter/shell/platform/common:common_cpp_input",
175184
"//flutter/shell/platform/darwin/common",
185+
"//flutter/shell/platform/darwin/common:framework_common",
176186
"//flutter/shell/platform/darwin/graphics",
177187
"//flutter/shell/platform/embedder:embedder_as_internal_library",
178188
"//flutter/shell/profiling:profiling",
189+
"//flutter/third_party/icu",
179190
"//flutter/third_party/spring_animation",
180191
]
181192
}
182193

183-
source_set("flutter_framework_source") {
184-
visibility = [ ":*" ]
185-
cflags_objc = flutter_cflags_objc
186-
cflags_objcc = flutter_cflags_objcc
187-
188-
deps = []
189-
190-
sources = [
191-
# iOS embedder is migrating to ARC.
192-
# New files are highly encouraged to be in ARC.
193-
# To add new files in ARC, add them to the `flutter_framework_source_arc` target.
194-
"platform_view_ios.h",
195-
"platform_view_ios.mm",
196-
]
197-
198-
sources += _flutter_framework_headers
199-
200-
defines = [ "FLUTTER_FRAMEWORK=1" ]
201-
if (darwin_extension_safe) {
202-
defines += [ "APPLICATION_EXTENSION_API_ONLY=1" ]
203-
}
204-
205-
deps += [
206-
"//flutter/fml",
207-
"//flutter/runtime",
208-
"//flutter/shell/common",
209-
"//flutter/shell/platform/darwin/common",
210-
"//flutter/shell/platform/darwin/common:framework_common",
211-
"//flutter/shell/platform/embedder:embedder_as_internal_library",
212-
"//flutter/shell/profiling:profiling",
213-
]
214-
215-
public_configs = [
216-
":ios_gpu_configuration_config",
217-
"//flutter:config",
218-
]
219-
220-
frameworks = [
221-
"AudioToolbox.framework",
222-
"CoreMedia.framework",
223-
"CoreVideo.framework",
224-
"QuartzCore.framework",
225-
"UIKit.framework",
226-
]
227-
if (flutter_runtime_mode == "profile" || flutter_runtime_mode == "debug") {
228-
# This is required by the profiler_metrics_ios.mm to get GPU statistics.
229-
# Usage in release builds will cause rejection from the App Store.
230-
frameworks += [ "IOKit.framework" ]
231-
}
232-
}
233-
234194
platform_frameworks_path =
235195
rebase_path("$ios_sdk_path/../../Library/Frameworks/")
236196

@@ -295,7 +255,6 @@ shared_library("ios_test_flutter") {
295255
deps = [
296256
":flutter_framework",
297257
":flutter_framework_source",
298-
":flutter_framework_source_arc",
299258
":ios_gpu_configuration",
300259
"//flutter/common:common",
301260
"//flutter/lib/ui:ui",
@@ -332,10 +291,7 @@ shared_library("create_flutter_framework_dylib") {
332291

333292
public = _flutter_framework_headers
334293

335-
deps = [
336-
":flutter_framework_source",
337-
":flutter_framework_source_arc",
338-
]
294+
deps = [ ":flutter_framework_source" ]
339295

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

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "flutter/common/constants.h"
1313
#include "flutter/fml/message_loop.h"
1414
#include "flutter/fml/platform/darwin/platform_version.h"
15-
#include "flutter/fml/platform/darwin/weak_nsobject.h"
1615
#include "flutter/fml/trace_event.h"
1716
#include "flutter/runtime/ptrace_check.h"
1817
#include "flutter/shell/common/engine.h"
@@ -152,10 +151,6 @@ @implementation FlutterEngine {
152151
std::shared_ptr<flutter::ThreadHost> _threadHost;
153152
std::unique_ptr<flutter::Shell> _shell;
154153

155-
// TODO(cbracken): https://github.com/flutter/flutter/issues/155943
156-
// Migrate to @property(nonatomic, weak).
157-
fml::WeakNSObject<FlutterViewController> _viewController;
158-
159154
std::shared_ptr<flutter::PlatformViewsController> _platformViewsController;
160155
flutter::IOSRenderingAPI _renderingApi;
161156
std::shared_ptr<flutter::ProfilerMetricsIOS> _profiler_metrics;
@@ -407,8 +402,7 @@ - (void)ensureSemanticsEnabled {
407402

408403
- (void)setViewController:(FlutterViewController*)viewController {
409404
FML_DCHECK(self.iosPlatformView);
410-
_viewController = viewController ? [viewController getWeakNSObject]
411-
: fml::WeakNSObject<FlutterViewController>();
405+
_viewController = viewController;
412406
self.iosPlatformView->SetOwnerViewController(_viewController);
413407
[self maybeSetupPlatformViewChannels];
414408
[self updateDisplays];
@@ -455,7 +449,7 @@ - (void)notifyViewControllerDeallocated {
455449
}
456450
}
457451
[self.textInputPlugin resetViewResponder];
458-
_viewController.reset();
452+
_viewController = nil;
459453
}
460454

461455
- (void)destroyContext {
@@ -467,13 +461,6 @@ - (void)destroyContext {
467461
_platformViewsController.reset();
468462
}
469463

470-
- (FlutterViewController*)viewController {
471-
if (!_viewController) {
472-
return nil;
473-
}
474-
return _viewController.get();
475-
}
476-
477464
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController {
478465
return _platformViewsController;
479466
}

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ - (void)deregisterNotifications;
149149
@end
150150

151151
@implementation FlutterViewController {
152-
// TODO(cbracken): https://github.com/flutter/flutter/issues/137801
153-
// Eliminate once we can use weak pointers in platform_view_ios.h.
154-
std::unique_ptr<fml::WeakNSObjectFactory<FlutterViewController>> _weakFactory;
155-
156152
flutter::ViewportMetrics _viewportMetrics;
157153
MouseState _mouseState;
158154
}
@@ -184,7 +180,6 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
184180
_flutterView = [[FlutterView alloc] initWithDelegate:_engine
185181
opaque:self.isViewOpaque
186182
enableWideGamut:engine.project.isWideGamutEnabled];
187-
_weakFactory = std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(self);
188183
_ongoingTouches = [[NSMutableSet alloc] init];
189184

190185
[self performCommonViewControllerInitialization];
@@ -245,7 +240,6 @@ - (void)sharedSetupWithProject:(nullable FlutterDartProject*)project
245240
project = [[FlutterDartProject alloc] init];
246241
}
247242
FlutterView.forceSoftwareRendering = project.settings.enable_software_rendering;
248-
_weakFactory = std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(self);
249243
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"io.flutter"
250244
project:project
251245
allowHeadlessExecution:self.engineAllowHeadlessExecution
@@ -292,10 +286,6 @@ - (void)performCommonViewControllerInitialization {
292286
[self setUpNotificationCenterObservers];
293287
}
294288

295-
- (fml::WeakNSObject<FlutterViewController>)getWeakNSObject {
296-
return _weakFactory->GetWeakNSObject();
297-
}
298-
299289
- (void)setUpNotificationCenterObservers {
300290
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
301291
[center addObserver:self
@@ -956,10 +946,6 @@ - (void)deregisterNotifications {
956946
}
957947

958948
- (void)dealloc {
959-
// It will be destroyed and invalidate its weak pointers
960-
// before any other members are destroyed.
961-
_weakFactory.reset();
962-
963949
[self removeInternalPlugins];
964950
[self deregisterNotifications];
965951

shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEWCONTROLLER_INTERNAL_H_
66
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEWCONTROLLER_INTERNAL_H_
77

8-
#include "flutter/fml/platform/darwin/weak_nsobject.h"
98
#include "flutter/fml/time/time_point.h"
109

1110
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
@@ -57,7 +56,6 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
5756
*/
5857
@property(nonatomic, assign, readwrite) BOOL prefersStatusBarHidden;
5958

60-
- (fml::WeakNSObject<FlutterViewController>)getWeakNSObject;
6159
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController;
6260
- (FlutterRestorationPlugin*)restorationPlugin;
6361

shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
5555
weak_factory_(this) {
5656
accessibility_channel_.reset([[FlutterBasicMessageChannel alloc]
5757
initWithName:@"flutter/accessibility"
58-
binaryMessenger:platform_view->GetOwnerViewController().get().engine.binaryMessenger
58+
binaryMessenger:platform_view->GetOwnerViewController().engine.binaryMessenger
5959
codec:[FlutterStandardMessageCodec sharedInstance]]);
6060
[accessibility_channel_.get() setMessageHandler:^(id message, FlutterReply reply) {
6161
HandleEvent((NSDictionary*)message);
@@ -69,7 +69,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
6969
}
7070

7171
UIView<UITextInput>* AccessibilityBridge::textInputView() {
72-
return [[platform_view_->GetOwnerViewController().get().engine textInputPlugin] textInputView];
72+
return [[platform_view_->GetOwnerViewController().engine textInputPlugin] textInputView];
7373
}
7474

7575
void AccessibilityBridge::AccessibilityObjectDidBecomeFocused(int32_t id) {

shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,7 @@ - (void)testAccessibilityObjectDidBecomeFocused {
14021402
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
14031403
fml::AutoResetWaitableEvent latch;
14041404
thread_task_runner->PostTask([&] {
1405-
auto weakFactory =
1406-
std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(flutterViewController);
1407-
platform_view->SetOwnerViewController(weakFactory->GetWeakNSObject());
1405+
platform_view->SetOwnerViewController(flutterViewController);
14081406
auto bridge =
14091407
std::make_unique<flutter::AccessibilityBridge>(/*view=*/nil,
14101408
/*platform_view=*/platform_view.get(),
@@ -2091,9 +2089,7 @@ - (void)testAccessibilityMessageAfterDeletion {
20912089
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
20922090
fml::AutoResetWaitableEvent latch;
20932091
thread_task_runner->PostTask([&] {
2094-
auto weakFactory =
2095-
std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(flutterViewController);
2096-
platform_view->SetOwnerViewController(weakFactory->GetWeakNSObject());
2092+
platform_view->SetOwnerViewController(flutterViewController);
20972093
auto bridge =
20982094
std::make_unique<flutter::AccessibilityBridge>(/*view=*/nil,
20992095
/*platform_view=*/platform_view.get(),
@@ -2184,9 +2180,7 @@ - (void)testPlatformViewDestructorDoesNotCallSemanticsAPIs {
21842180

21852181
OCMStub([mockFlutterViewController platformViewsController])
21862182
.andReturn(flutterPlatformViewsController.get());
2187-
auto weakFactory = std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(
2188-
mockFlutterViewController);
2189-
platform_view->SetOwnerViewController(weakFactory->GetWeakNSObject());
2183+
platform_view->SetOwnerViewController(mockFlutterViewController);
21902184

21912185
platform_view->SetSemanticsEnabled(true);
21922186
XCTAssertNotEqual(test_delegate.set_semantics_enabled_calls, 0);

shell/platform/darwin/ios/platform_view_ios.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include "flutter/fml/closure.h"
1111
#include "flutter/fml/macros.h"
12-
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
13-
#include "flutter/fml/platform/darwin/weak_nsobject.h"
1412
#include "flutter/shell/common/platform_view.h"
1513
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
1614
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
@@ -59,14 +57,14 @@ class PlatformViewIOS final : public PlatformView {
5957
* Returns the `FlutterViewController` currently attached to the `FlutterEngine` owning
6058
* this PlatformViewIOS.
6159
*/
62-
fml::WeakNSObject<FlutterViewController> GetOwnerViewController() const;
60+
FlutterViewController* GetOwnerViewController() const;
6361

6462
/**
6563
* Updates the `FlutterViewController` currently attached to the `FlutterEngine` owning
6664
* this PlatformViewIOS. This should be updated when the `FlutterEngine`
6765
* is given a new `FlutterViewController`.
6866
*/
69-
void SetOwnerViewController(const fml::WeakNSObject<FlutterViewController>& owner_controller);
67+
void SetOwnerViewController(__weak FlutterViewController* owner_controller);
7068

7169
/**
7270
* Called one time per `FlutterViewController` when the `FlutterViewController`'s
@@ -133,15 +131,14 @@ class PlatformViewIOS final : public PlatformView {
133131
std::function<void(bool)> set_semantics_enabled_;
134132
};
135133

136-
fml::WeakNSObject<FlutterViewController> owner_controller_;
134+
__weak FlutterViewController* owner_controller_;
137135
// Since the `ios_surface_` is created on the platform thread but
138136
// used on the raster thread we need to protect it with a mutex.
139137
std::mutex ios_surface_mutex_;
140138
std::unique_ptr<IOSSurface> ios_surface_;
141139
std::shared_ptr<IOSContext> ios_context_;
142140
const std::shared_ptr<PlatformViewsController>& platform_views_controller_;
143141
AccessibilityBridgeManager accessibility_bridge_;
144-
fml::scoped_nsprotocol<FlutterTextInputPlugin*> text_input_plugin_;
145142
ScopedObserver dealloc_view_controller_observer_;
146143
std::vector<std::string> platform_resolved_locale_;
147144
std::shared_ptr<PlatformMessageHandlerIos> platform_message_handler_;

0 commit comments

Comments
 (0)