Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion shell/platform/darwin/ios/platform_view_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ class PlatformViewIOS final : public PlatformView {
void SetSemanticsEnabled(bool enabled) override;

private:
/// Smart pointer for use with objective-c observers.
/// This guarentees we remove the observer.
class ScopedObserver {
public:
ScopedObserver();
~ScopedObserver();
void reset(id<NSObject> observer);
ScopedObserver(const ScopedObserver&) = delete;
ScopedObserver& operator=(const ScopedObserver&) = delete;

private:
id<NSObject> observer_;
};

fml::WeakPtr<FlutterViewController> owner_controller_;
// Since the `ios_surface_` is created on the platform thread but
// used on the GPU thread we need to protect it with a mutex.
Expand All @@ -58,7 +72,7 @@ class PlatformViewIOS final : public PlatformView {
std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
fml::scoped_nsprotocol<FlutterTextInputPlugin*> text_input_plugin_;
fml::closure firstFrameCallback_;
fml::scoped_nsprotocol<NSObject*> dealloc_view_controller_observer_;
ScopedObserver dealloc_view_controller_observer_;

// |PlatformView|
void HandlePlatformMessage(fml::RefPtr<flutter::PlatformMessage> message) override;
Expand Down
37 changes: 28 additions & 9 deletions shell/platform/darwin/ios/platform_view_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@

// Add an observer that will clear out the owner_controller_ ivar and
// the accessibility_bridge_ in case the view controller is deleted.
dealloc_view_controller_observer_.reset([[NSNotificationCenter defaultCenter]
addObserverForName:FlutterViewControllerWillDealloc
object:owner_controller_.get()
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* note) {
// Implicit copy of 'this' is fine.
accessibility_bridge_.reset();
owner_controller_.reset();
}]);
dealloc_view_controller_observer_.reset(
[[[NSNotificationCenter defaultCenter] addObserverForName:FlutterViewControllerWillDealloc
object:owner_controller_.get()
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* note) {
// Implicit copy of 'this' is fine.
accessibility_bridge_.reset();
owner_controller_.reset();
}] retain]);

if (owner_controller_) {
ios_surface_ =
Expand Down Expand Up @@ -174,4 +174,23 @@ new AccessibilityBridge(static_cast<FlutterView*>(owner_controller_.get().view),
text_input_plugin_ = plugin;
}

PlatformViewIOS::ScopedObserver::ScopedObserver() : observer_(nil) {}

PlatformViewIOS::ScopedObserver::~ScopedObserver() {
if (observer_) {
[[NSNotificationCenter defaultCenter] removeObserver:observer_];
[observer_ release];
}
}

void PlatformViewIOS::ScopedObserver::reset(id<NSObject> observer) {
if (observer != observer_) {
if (observer_) {
[[NSNotificationCenter defaultCenter] removeObserver:observer_];
[observer_ release];
}
observer_ = observer;
}
}

} // namespace flutter