fml: Delete weak_nsobject.h,mm (flutter/engine#56289)

These classes are no longer necessary now that the entire codebase has been migrated to ARC.

Issue: https://github.com/flutter/flutter/issues/137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Bracken
2024-11-01 05:05:11 -07:00
committed by GitHub
parent 518d528ca4
commit 0be1987e1a
9 changed files with 2 additions and 583 deletions

View File

@@ -109,8 +109,6 @@
../../../flutter/fml/platform/darwin/scoped_nsobject_arc_unittests.mm
../../../flutter/fml/platform/darwin/scoped_nsobject_unittests.mm
../../../flutter/fml/platform/darwin/string_range_sanitization_unittests.mm
../../../flutter/fml/platform/darwin/weak_nsobject_arc_unittests.mm
../../../flutter/fml/platform/darwin/weak_nsobject_unittests.mm
../../../flutter/fml/platform/fuchsia/log_interest_listener_unittests.cc
../../../flutter/fml/platform/win/file_win_unittests.cc
../../../flutter/fml/platform/win/wstring_conversion_unittests.cc

View File

@@ -42739,8 +42739,6 @@ ORIGIN: ../../../flutter/fml/platform/darwin/scoped_policy.h + ../../../flutter/
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_typeref.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/string_range_sanitization.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/string_range_sanitization.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/weak_nsobject.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/weak_nsobject.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/fuchsia/log_interest_listener.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/fuchsia/log_interest_listener.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/fuchsia/log_state.cc + ../../../flutter/LICENSE
@@ -45601,8 +45599,6 @@ FILE: ../../../flutter/fml/platform/darwin/scoped_policy.h
FILE: ../../../flutter/fml/platform/darwin/scoped_typeref.h
FILE: ../../../flutter/fml/platform/darwin/string_range_sanitization.h
FILE: ../../../flutter/fml/platform/darwin/string_range_sanitization.mm
FILE: ../../../flutter/fml/platform/darwin/weak_nsobject.h
FILE: ../../../flutter/fml/platform/darwin/weak_nsobject.mm
FILE: ../../../flutter/fml/platform/fuchsia/log_interest_listener.cc
FILE: ../../../flutter/fml/platform/fuchsia/log_interest_listener.h
FILE: ../../../flutter/fml/platform/fuchsia/log_state.cc

View File

@@ -162,8 +162,6 @@ source_set("fml") {
"platform/darwin/scoped_typeref.h",
"platform/darwin/string_range_sanitization.h",
"platform/darwin/string_range_sanitization.mm",
"platform/darwin/weak_nsobject.h",
"platform/darwin/weak_nsobject.mm",
]
frameworks = [ "Foundation.framework" ]
@@ -377,10 +375,7 @@ if (enable_unittests) {
}
if (is_mac || is_ios) {
sources += [
"platform/darwin/scoped_nsobject_unittests.mm",
"platform/darwin/weak_nsobject_unittests.mm",
]
sources += [ "platform/darwin/scoped_nsobject_unittests.mm" ]
}
if (is_fuchsia) {
@@ -418,10 +413,7 @@ if (enable_unittests) {
testonly = true
if (is_mac || is_ios) {
cflags_objcc = flutter_cflags_objc_arc
sources = [
"platform/darwin/scoped_nsobject_arc_unittests.mm",
"platform/darwin/weak_nsobject_arc_unittests.mm",
]
sources = [ "platform/darwin/scoped_nsobject_arc_unittests.mm" ]
}
deps = [

View File

@@ -1,282 +0,0 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FML_PLATFORM_DARWIN_WEAK_NSOBJECT_H_
#define FLUTTER_FML_PLATFORM_DARWIN_WEAK_NSOBJECT_H_
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#include <stdlib.h>
#include <utility>
#include "flutter/fml/logging.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/fml/memory/thread_checker.h"
namespace debug {
struct DebugThreadChecker {
FML_DECLARE_THREAD_CHECKER(checker);
};
} // namespace debug
// WeakNSObject<> is patterned after scoped_nsobject<>, but instead of
// maintaining ownership of an NSObject subclass object, it will nil itself out
// when the object is deallocated.
//
// WeakNSProtocol<> has the same behavior as WeakNSObject, but can be used
// with protocols.
//
// Example usage (fml::WeakNSObject<T>):
// WeakNSObjectFactory factory([[Foo alloc] init]);
// WeakNSObject<Foo> weak_foo; // No pointer
// weak_foo = factory.GetWeakNSObject() // Now a weak reference is kept.
// [weak_foo description]; // Returns [foo description].
// foo.reset(); // The reference is released.
// [weak_foo description]; // Returns nil, as weak_foo is pointing to nil.
//
//
// Implementation wise a WeakNSObject keeps a reference to a refcounted
// WeakContainer. There is one unique instance of a WeakContainer per watched
// NSObject, this relationship is maintained via the ObjectiveC associated
// object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class.
//
// Threading restrictions:
// - Several WeakNSObject pointing to the same underlying object must all be
// created and dereferenced on the same thread;
// - thread safety is enforced by the implementation, except:
// - it is allowed to destroy a WeakNSObject on any thread;
// - the implementation assumes that the tracked object will be released on the
// same thread that the WeakNSObject is created on.
//
// fml specifics:
// WeakNSObjects can only originate from a |WeakNSObjectFactory| (see below), though WeakNSObjects
// are copyable and movable.
//
// WeakNSObjects are not in general thread-safe. They may only be *used* on
// a single thread, namely the same thread as the "originating"
// |WeakNSObjectFactory| (which can invalidate the WeakNSObjects that it
// generates).
//
// However, WeakNSObject may be passed to other threads, reset on other
// threads, or destroyed on other threads. They may also be reassigned on
// other threads (in which case they should then only be used on the thread
// corresponding to the new "originating" |WeakNSObjectFactory|).
namespace fml {
// Forward declaration, so |WeakNSObject<NST>| can friend it.
template <typename NST>
class WeakNSObjectFactory;
// WeakContainer keeps a weak pointer to an object and clears it when it
// receives nullify() from the object's sentinel.
class WeakContainer : public fml::RefCountedThreadSafe<WeakContainer> {
public:
explicit WeakContainer(id object, const debug::DebugThreadChecker& checker);
id object() {
CheckThreadSafety();
return object_;
}
void nullify() { object_ = nil; }
private:
friend fml::RefCountedThreadSafe<WeakContainer>;
~WeakContainer();
__unsafe_unretained id object_;
void CheckThreadSafety() const { FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); }
// checker_ is unused in non-unopt mode.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
debug::DebugThreadChecker checker_;
#pragma clang diagnostic pop
};
} // namespace fml
// Sentinel for observing the object contained in the weak pointer. The object
// will be deleted when the weak object is deleted and will notify its
// container.
@interface CRBWeakNSProtocolSentinel : NSObject
// Return the only associated container for this object. There can be only one.
// Will return null if object is nil .
+ (fml::RefPtr<fml::WeakContainer>)containerForObject:(id)object
threadChecker:(debug::DebugThreadChecker)checker;
@end
namespace fml {
// Base class for all WeakNSObject derivatives.
template <typename NST>
class WeakNSProtocol {
public:
WeakNSProtocol() = default;
// A WeakNSProtocol object can be copied on one thread and used on
// another.
WeakNSProtocol(const WeakNSProtocol<NST>& that)
: container_(that.container_), checker_(that.checker_) {}
~WeakNSProtocol() = default;
void reset() {
container_ = [CRBWeakNSProtocolSentinel containerForObject:nil threadChecker:checker_];
}
NST get() const {
CheckThreadSafety();
if (!container_.get()) {
return nil;
}
return container_->object();
}
WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
// A WeakNSProtocol object can be copied on one thread and used on
// another.
container_ = that.container_;
checker_ = that.checker_;
return *this;
}
bool operator==(NST that) const {
CheckThreadSafety();
return get() == that;
}
bool operator!=(NST that) const {
CheckThreadSafety();
return get() != that;
}
// This appears to be intentional to allow truthiness?
// NOLINTNEXTLINE(google-explicit-constructor)
operator NST() const {
CheckThreadSafety();
return get();
}
protected:
friend class WeakNSObjectFactory<NST>;
explicit WeakNSProtocol(RefPtr<fml::WeakContainer> container,
const debug::DebugThreadChecker& checker)
: container_(std::move(container)), checker_(checker) {}
// Refecounted reference to the container tracking the ObjectiveC object this
// class encapsulates.
RefPtr<fml::WeakContainer> container_;
void CheckThreadSafety() const { FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); }
debug::DebugThreadChecker checker_;
};
// Free functions
template <class NST>
bool operator==(NST p1, const WeakNSProtocol<NST>& p2) {
return p1 == p2.get();
}
template <class NST>
bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) {
return p1 != p2.get();
}
template <typename NST>
class WeakNSObject : public WeakNSProtocol<NST*> {
public:
WeakNSObject() = default;
WeakNSObject(const WeakNSObject<NST>& that) : WeakNSProtocol<NST*>(that) {}
WeakNSObject& operator=(const WeakNSObject<NST>& that) {
WeakNSProtocol<NST*>::operator=(that);
return *this;
}
private:
friend class WeakNSObjectFactory<NST>;
explicit WeakNSObject(RefPtr<fml::WeakContainer> container, debug::DebugThreadChecker checker)
: WeakNSProtocol<NST*>(container, checker) {}
};
// Specialization to make WeakNSObject<id> work.
template <>
class WeakNSObject<id> : public WeakNSProtocol<id> {
public:
WeakNSObject() = default;
WeakNSObject(const WeakNSObject<id>& that) : WeakNSProtocol<id>(that) {}
WeakNSObject& operator=(const WeakNSObject<id>& that) {
WeakNSProtocol<id>::operator=(that);
return *this;
}
private:
friend class WeakNSObjectFactory<id>;
explicit WeakNSObject(const RefPtr<fml::WeakContainer>& container,
const debug::DebugThreadChecker& checker)
: WeakNSProtocol<id>(container, checker) {}
};
// Class that produces (valid) |WeakNSObject<NST>|s. Typically, this is used as a
// member variable of |NST| (preferably the last one -- see below), and |NST|'s
// methods control how WeakNSObjects to it are vended. This class is not
// thread-safe, and should only be created, destroyed and used on a single
// thread.
//
// Example:
//
// ```objc
// @implementation Controller {
// std::unique_ptr<fml::WeakNSObjectFactory<Controller>> _weakFactory;
// }
//
// - (instancetype)init {
// self = [super init];
// _weakFactory = std::make_unique<fml::WeakNSObjectFactory<Controller>>(self)
// }
// - (fml::WeakNSObject<Controller>) {
// return _weakFactory->GetWeakNSObject()
// }
//
// @end
// ```
template <typename NST>
class WeakNSObjectFactory {
public:
explicit WeakNSObjectFactory(NST* object) {
FML_DCHECK(object);
container_ = [CRBWeakNSProtocolSentinel containerForObject:object threadChecker:checker_];
}
~WeakNSObjectFactory() { CheckThreadSafety(); }
// Gets a new weak pointer, which will be valid until this object is
// destroyed.e
WeakNSObject<NST> GetWeakNSObject() const { return WeakNSObject<NST>(container_, checker_); }
private:
// Refecounted reference to the container tracking the ObjectiveC object this
// class encapsulates.
RefPtr<fml::WeakContainer> container_;
void CheckThreadSafety() const { FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); }
debug::DebugThreadChecker checker_;
FML_DISALLOW_COPY_AND_ASSIGN(WeakNSObjectFactory);
};
} // namespace fml
#endif // FLUTTER_FML_PLATFORM_DARWIN_WEAK_NSOBJECT_H_

View File

@@ -1,67 +0,0 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/platform/darwin/weak_nsobject.h"
#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
namespace {
// The key needed by objc_setAssociatedObject.
char sentinelObserverKey_;
} // namespace
namespace fml {
WeakContainer::WeakContainer(id object, const debug::DebugThreadChecker& checker)
: object_(object), checker_(checker) {}
WeakContainer::~WeakContainer() {}
} // namespace fml
@interface CRBWeakNSProtocolSentinel ()
// Container to notify on dealloc.
@property(readonly, assign) fml::RefPtr<fml::WeakContainer> container;
// Designed initializer.
- (id)initWithContainer:(fml::RefPtr<fml::WeakContainer>)container;
@end
@implementation CRBWeakNSProtocolSentinel
+ (fml::RefPtr<fml::WeakContainer>)containerForObject:(id)object
threadChecker:(debug::DebugThreadChecker)checker {
if (object == nil) {
return nullptr;
}
// The autoreleasePool is needed here as the call to objc_getAssociatedObject
// returns an autoreleased object which is better released sooner than later.
fml::ScopedNSAutoreleasePool pool;
CRBWeakNSProtocolSentinel* sentinel = objc_getAssociatedObject(object, &sentinelObserverKey_);
if (!sentinel) {
fml::scoped_nsobject<CRBWeakNSProtocolSentinel> newSentinel([[CRBWeakNSProtocolSentinel alloc]
initWithContainer:AdoptRef(new fml::WeakContainer(object, checker))]);
sentinel = newSentinel;
objc_setAssociatedObject(object, &sentinelObserverKey_, sentinel, OBJC_ASSOCIATION_RETAIN);
// The retain count is 2. One retain is due to the alloc, the other to the
// association with the weak object.
FML_DCHECK(2u == [sentinel retainCount]);
}
return [sentinel container];
}
- (id)initWithContainer:(fml::RefPtr<fml::WeakContainer>)container {
FML_DCHECK(container.get());
self = [super init];
if (self) {
_container = container;
}
return self;
}
- (void)dealloc {
_container->nullify();
[super dealloc];
}
@end

View File

@@ -1,121 +0,0 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/message_loop.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/fml/platform/darwin/weak_nsobject.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/thread.h"
#include "gtest/gtest.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace fml {
namespace {
TEST(WeakNSObjectTestARC, WeakNSObject) {
scoped_nsobject<NSObject> p1;
WeakNSObject<NSObject> w1;
@autoreleasepool {
p1.reset(([[NSObject alloc] init]));
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
p1.reset();
}
EXPECT_FALSE(w1);
}
TEST(WeakNSObjectTestARC, MultipleWeakNSObject) {
scoped_nsobject<NSObject> p1;
WeakNSObject<NSObject> w1;
WeakNSObject<NSObject> w2;
@autoreleasepool {
p1.reset([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
w2 = w1;
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
EXPECT_TRUE(w1.get() == w2.get());
p1.reset();
}
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
TEST(WeakNSObjectTestARC, WeakNSObjectDies) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
{
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
}
}
TEST(WeakNSObjectTestARC, WeakNSObjectReset) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
w1.reset();
EXPECT_FALSE(w1);
EXPECT_TRUE(p1);
EXPECT_TRUE([p1 description]);
}
TEST(WeakNSObjectTestARC, WeakNSObjectEmpty) {
scoped_nsobject<NSObject> p1;
WeakNSObject<NSObject> w1;
@autoreleasepool {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
EXPECT_FALSE(w1);
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
p1.reset();
}
EXPECT_FALSE(w1);
}
TEST(WeakNSObjectTestARC, WeakNSObjectCopy) {
scoped_nsobject<NSObject> p1;
WeakNSObject<NSObject> w1;
WeakNSObject<NSObject> w2;
@autoreleasepool {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
w2 = w1;
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
p1.reset();
}
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
TEST(WeakNSObjectTestARC, WeakNSObjectAssignment) {
scoped_nsobject<NSObject> p1;
WeakNSObject<NSObject> w1;
WeakNSObject<NSObject> w2;
@autoreleasepool {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
w2 = w1;
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
p1.reset();
}
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
} // namespace
} // namespace fml

View File

@@ -1,95 +0,0 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/message_loop.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/fml/platform/darwin/weak_nsobject.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/thread.h"
#include "gtest/gtest.h"
namespace fml {
namespace {
TEST(WeakNSObjectTest, WeakNSObject) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
p1.reset();
EXPECT_FALSE(w1);
}
TEST(WeakNSObjectTest, MultipleWeakNSObject) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
WeakNSObject<NSObject> w2(w1);
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
EXPECT_TRUE(w1.get() == w2.get());
p1.reset();
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
TEST(WeakNSObjectTest, WeakNSObjectDies) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
{
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
}
}
TEST(WeakNSObjectTest, WeakNSObjectReset) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
w1.reset();
EXPECT_FALSE(w1);
EXPECT_TRUE(p1);
EXPECT_TRUE([p1 description]);
}
TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1;
EXPECT_FALSE(w1);
WeakNSObjectFactory factory(p1.get());
w1 = factory.GetWeakNSObject();
EXPECT_TRUE(w1);
p1.reset();
EXPECT_FALSE(w1);
}
TEST(WeakNSObjectTest, WeakNSObjectCopy) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
WeakNSObject<NSObject> w2(w1);
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
p1.reset();
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObjectFactory factory(p1.get());
WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
WeakNSObject<NSObject> w2 = w1;
EXPECT_TRUE(w1);
EXPECT_TRUE(w2);
p1.reset();
EXPECT_FALSE(w1);
EXPECT_FALSE(w2);
}
} // namespace
} // namespace fml

View File

@@ -5,7 +5,6 @@
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMPLUGIN_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMPLUGIN_H_
#include "flutter/fml/platform/darwin/weak_nsobject.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h"

View File

@@ -5,7 +5,6 @@
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "flutter/fml/platform/darwin/weak_nsobject.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"