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

Commit 7946f64

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 3162aaa commit 7946f64

8 files changed

+58
-144
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",
@@ -157,81 +152,46 @@ source_set("flutter_framework_source_arc") {
157152
"ios_surface_software.mm",
158153
"platform_message_handler_ios.h",
159154
"platform_message_handler_ios.mm",
155+
"platform_view_ios.h",
156+
"platform_view_ios.mm",
160157
"rendering_api_selection.h",
161158
"rendering_api_selection.mm",
162159
]
160+
sources += _flutter_framework_headers
163161

164162
frameworks = [
165-
"UIKit.framework",
163+
"AudioToolbox.framework",
164+
"CoreMedia.framework",
165+
"CoreVideo.framework",
166166
"IOSurface.framework",
167+
"QuartzCore.framework",
168+
"UIKit.framework",
167169
]
170+
if (flutter_runtime_mode == "profile" || flutter_runtime_mode == "debug") {
171+
# This is required by the profiler_metrics_ios.mm to get GPU statistics.
172+
# Usage in release builds will cause rejection from the App Store.
173+
frameworks += [ "IOKit.framework" ]
174+
}
168175

169-
deps += [
176+
deps = [
170177
":ios_gpu_configuration",
171178
"//flutter/common:common",
172179
"//flutter/common/graphics",
180+
"//flutter/fml",
173181
"//flutter/lib/ui",
174182
"//flutter/runtime",
175183
"//flutter/shell/common",
184+
"//flutter/shell/platform/common:common_cpp_input",
176185
"//flutter/shell/platform/darwin/common",
186+
"//flutter/shell/platform/darwin/common:framework_common",
177187
"//flutter/shell/platform/darwin/graphics",
178188
"//flutter/shell/platform/embedder:embedder_as_internal_library",
179189
"//flutter/shell/profiling:profiling",
190+
"//flutter/third_party/icu",
180191
"//flutter/third_party/spring_animation",
181192
]
182193
}
183194

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

@@ -296,7 +256,6 @@ shared_library("ios_test_flutter") {
296256
deps = [
297257
":flutter_framework",
298258
":flutter_framework_source",
299-
":flutter_framework_source_arc",
300259
":ios_gpu_configuration",
301260
"//flutter/common:common",
302261
"//flutter/lib/ui:ui",
@@ -333,10 +292,7 @@ shared_library("create_flutter_framework_dylib") {
333292

334293
public = _flutter_framework_headers
335294

336-
deps = [
337-
":flutter_framework_source",
338-
":flutter_framework_source_arc",
339-
]
295+
deps = [ ":flutter_framework_source" ]
340296

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

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::SamplingProfiler> _profiler;
@@ -406,8 +401,7 @@ - (void)ensureSemanticsEnabled {
406401

407402
- (void)setViewController:(FlutterViewController*)viewController {
408403
FML_DCHECK(self.iosPlatformView);
409-
_viewController = viewController ? [viewController getWeakNSObject]
410-
: fml::WeakNSObject<FlutterViewController>();
404+
_viewController = viewController;
411405
self.iosPlatformView->SetOwnerViewController(_viewController);
412406
[self maybeSetupPlatformViewChannels];
413407
[self updateDisplays];
@@ -454,7 +448,7 @@ - (void)notifyViewControllerDeallocated {
454448
}
455449
}
456450
[self.textInputPlugin resetViewResponder];
457-
_viewController.reset();
451+
_viewController = nil;
458452
}
459453

460454
- (void)destroyContext {
@@ -466,13 +460,6 @@ - (void)destroyContext {
466460
_platformViewsController.reset();
467461
}
468462

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

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,7 @@ - (void)handleKeyboardAnimationCallbackWithTargetTime:(fml::TimePoint)targetTime
153153
@end
154154

155155
@implementation FlutterViewController {
156-
// TODO(cbracken): https://github.com/flutter/flutter/issues/137801
157-
// Eliminate once we can use weak pointers in platform_view_ios.h.
158-
std::unique_ptr<fml::WeakNSObjectFactory<FlutterViewController>> _weakFactory;
159156
FlutterEngine* _engine;
160-
161157
flutter::ViewportMetrics _viewportMetrics;
162158
MouseState _mouseState;
163159
}
@@ -189,7 +185,6 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
189185
_flutterView = [[FlutterView alloc] initWithDelegate:_engine
190186
opaque:self.isViewOpaque
191187
enableWideGamut:engine.project.isWideGamutEnabled];
192-
_weakFactory = std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(self);
193188
_ongoingTouches = [[NSMutableSet alloc] init];
194189

195190
// TODO(cbracken): https://github.com/flutter/flutter/issues/157140
@@ -256,7 +251,6 @@ - (void)sharedSetupWithProject:(nullable FlutterDartProject*)project
256251
project = [[FlutterDartProject alloc] init];
257252
}
258253
FlutterView.forceSoftwareRendering = project.settings.enable_software_rendering;
259-
_weakFactory = std::make_unique<fml::WeakNSObjectFactory<FlutterViewController>>(self);
260254
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"io.flutter"
261255
project:project
262256
allowHeadlessExecution:self.engineAllowHeadlessExecution
@@ -312,10 +306,6 @@ - (FlutterEngine*)engine {
312306
return _engine;
313307
}
314308

315-
- (fml::WeakNSObject<FlutterViewController>)getWeakNSObject {
316-
return _weakFactory->GetWeakNSObject();
317-
}
318-
319309
- (void)setUpNotificationCenterObservers {
320310
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
321311
[center addObserver:self
@@ -980,12 +970,6 @@ - (void)deregisterNotifications {
980970
}
981971

982972
- (void)dealloc {
983-
// It will be destroyed and invalidate its weak pointers
984-
// before any other members are destroyed.
985-
_weakFactory.reset();
986-
987-
// TODO(cbracken): https://github.com/flutter/flutter/issues/157140
988-
// Eliminate method calls in initializers and dealloc.
989973
[self removeInternalPlugins];
990974
[self deregisterNotifications];
991975

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)