Skip to content

Refactor ForEachAppDelegateClass for iOS into a new function that swizzles [UIApplication setDelegate:] to obtain App Delegate classes. #1737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions Jules.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ API documentation.
as mentioned in `CONTRIBUTING.md`.
* **Formatting**: Use `python3 scripts/format_code.py -git_diff -verbose` to
format your code before committing.
* **Naming Precision for Dynamic Systems**: Function names should precisely reflect their behavior, especially in systems with dynamic or asynchronous interactions. For example, a function that processes a list of items should be named differently from one that operates on a single, specific item captured asynchronously. Regularly re-evaluate function names as requirements evolve to maintain clarity.

## Comments

Expand Down Expand Up @@ -362,6 +363,7 @@ API documentation.
otherwise ensuring the `Future` completes its course, particularly for
operations with side effects or those that allocate significant backend
resources.
* **Lifecycle of Queued Callbacks/Blocks**: If blocks or callbacks are queued to be run upon an asynchronous event (e.g., an App Delegate class being set or a Future completing), clearly define and document their lifecycle. Determine if they are one-shot (cleared after first execution) or persistent (intended to run for multiple or future events). This impacts how associated data and the blocks themselves are stored and cleared, preventing memory leaks or unexpected multiple executions.

## Immutability

Expand Down Expand Up @@ -397,6 +399,10 @@ API documentation.
integration, it can occasionally be a factor to consider when debugging app
delegate behavior or integrating with other libraries that also perform
swizzling.
When implementing or interacting with swizzling, especially for App Delegate methods like `[UIApplication setDelegate:]`:
* Be highly aware that `setDelegate:` can be called multiple times with different delegate class instances, including proxy classes from other libraries (e.g., GUL - Google Utilities). Swizzling logic must be robust against being invoked multiple times for the same effective method on the same class or on classes in a hierarchy. An idempotency check (i.e., if the method's current IMP is already the target swizzled IMP, do nothing more for that specific swizzle attempt) in any swizzling utility can prevent issues like recursion.
* When tracking unique App Delegate classes (e.g., for applying hooks or callbacks via swizzling), consider the class hierarchy. If a superclass has already been processed, processing a subclass for the same inherited methods might be redundant or problematic. A strategy to check if a newly set delegate is a subclass of an already processed delegate can prevent such issues.
* For code that runs very early in the application lifecycle on iOS/macOS (e.g., `+load` methods, static initializers involved in swizzling), prefer using `NSLog` directly over custom logging frameworks if there's any uncertainty about whether the custom framework is fully initialized, to avoid crashes during logging itself.

## Class and File Structure

Expand Down Expand Up @@ -576,3 +582,4 @@ practices detailed in `Jules.md`.
tests as described in the 'Testing' section of `Jules.md`.
* **Commit Messages**: Follow standard commit message guidelines. A brief
summary line, followed by a more detailed explanation if necessary.
* **Tool Limitations & Path Specificity**: If codebase search tools (like `grep` or recursive `ls`) are limited or unavailable, and initial attempts to locate files/modules based on common directory structures are unsuccessful, explicitly ask for more specific paths rather than assuming a module doesn't exist or contains no relevant code.
2 changes: 1 addition & 1 deletion app/src/invites/ios/invites_ios_startup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ @implementation UIApplication (FIRFBI)
+ (void)load {
// C++ constructors may not be called yet so call NSLog rather than LogDebug.
NSLog(@"Loading UIApplication category for Firebase App");
::firebase::util::ForEachAppDelegateClass(^(Class clazz) {
::firebase::util::RunOnAppDelegateClasses(^(Class clazz) {
::firebase::invites::HookAppDelegateMethods(clazz);
});
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/util_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ typedef BOOL (
id self, SEL selector_value, UIApplication *application,
NSUserActivity *user_activity, void (^restoration_handler)(NSArray *));

// Call the given block once for every Objective-C class that exists that
// implements the UIApplicationDelegate protocol (except for those in a
// blacklist we keep).
void ForEachAppDelegateClass(void (^block)(Class));
// Calls the given block for each unique Objective-C class that has been
// previously passed to [UIApplication setDelegate:]. The block is executed
// immediately for all currently known unique delegate classes.
// Additionally, the block is queued to be executed if any new, unique
// Objective-C class is passed to [UIApplication setDelegate:] in the future.
void RunOnAppDelegateClasses(void (^block)(Class));

// Convert a string array into an NSMutableArray.
NSMutableArray *StringVectorToNSMutableArray(
Expand Down
129 changes: 100 additions & 29 deletions app/src/util_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,88 @@
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

#define MAX_PENDING_APP_DELEGATE_BLOCKS 8
#define MAX_SEEN_DELEGATE_CLASSES 32

static IMP g_original_setDelegate_imp = NULL;
static void (^g_pending_app_delegate_blocks[MAX_PENDING_APP_DELEGATE_BLOCKS])(Class) = {nil};
static int g_pending_block_count = 0;
static Class g_seen_delegate_classes[MAX_SEEN_DELEGATE_CLASSES] = {nil};
static int g_seen_delegate_classes_count = 0;

static void Firebase_setDelegate(id self, SEL _cmd, id<UIApplicationDelegate> delegate) {
Class new_class = nil;
if (delegate) {
new_class = [delegate class];
NSLog(@"Firebase: UIApplication setDelegate: called with class %s (Swizzled)",
class_getName(new_class));
} else {
NSLog(@"Firebase: UIApplication setDelegate: called with nil delegate (Swizzled)");
}

if (new_class) {
// 1. Superclass Check
bool superclass_already_seen = false;
Class current_super = class_getSuperclass(new_class);
while (current_super) {
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
if (g_seen_delegate_classes[i] == current_super) {
superclass_already_seen = true;
NSLog(@"Firebase: Delegate class %s has superclass %s which was already seen. Skipping processing for %s.",
class_getName(new_class), class_getName(current_super), class_getName(new_class));
break;
}
}
if (superclass_already_seen) break;
current_super = class_getSuperclass(current_super);
}

if (!superclass_already_seen) {
// 2. Direct Class Check (if no superclass was seen)
bool direct_class_already_seen = false;
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
if (g_seen_delegate_classes[i] == new_class) {
direct_class_already_seen = true;
NSLog(@"Firebase: Delegate class %s already seen directly. Skipping processing.",
class_getName(new_class));
break;
}
}

if (!direct_class_already_seen) {
// 3. Process as New Class
if (g_seen_delegate_classes_count < MAX_SEEN_DELEGATE_CLASSES) {
g_seen_delegate_classes[g_seen_delegate_classes_count] = new_class;
g_seen_delegate_classes_count++;
NSLog(@"Firebase: Added new delegate class %s to seen list (total seen: %d).",
class_getName(new_class), g_seen_delegate_classes_count);

if (g_pending_block_count > 0) {
NSLog(@"Firebase: Executing %d pending block(s) for new delegate class: %s.",
g_pending_block_count, class_getName(new_class));
for (int i = 0; i < g_pending_block_count; i++) {
if (g_pending_app_delegate_blocks[i]) {
g_pending_app_delegate_blocks[i](new_class);
// Pending blocks persist to run for future new delegate classes.
}
}
}
} else {
NSLog(@"Firebase Error: Exceeded MAX_SEEN_DELEGATE_CLASSES (%d). Cannot add new delegate class %s or run pending blocks for it.",
MAX_SEEN_DELEGATE_CLASSES, class_getName(new_class));
}
}
}
}

// Call the original setDelegate: implementation
if (g_original_setDelegate_imp) {
((void (*)(id, SEL, id<UIApplicationDelegate>))g_original_setDelegate_imp)(self, _cmd, delegate);
} else {
NSLog(@"Firebase Error: Original setDelegate: IMP not found, cannot call original method.");
}
}

@implementation FIRSAMAppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Expand Down Expand Up @@ -79,38 +161,27 @@ - (BOOL)application:(UIApplication *)application
namespace firebase {
namespace util {

void ForEachAppDelegateClass(void (^block)(Class)) {
unsigned int number_of_classes;
Class *classes = objc_copyClassList(&number_of_classes);
for (unsigned int i = 0; i < number_of_classes; i++) {
Class clazz = classes[i];
if (class_conformsToProtocol(clazz, @protocol(UIApplicationDelegate))) {
const char *class_name = class_getName(clazz);
bool blacklisted = false;
static const char *kClassNameBlacklist[] = {
// Declared in Firebase Analytics:
// //googlemac/iPhone/Firebase/Analytics/Sources/ApplicationDelegate/
// FIRAAppDelegateProxy.m
"FIRAAppDelegate",
// Declared here.
"FIRSAMAppDelegate"};
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
blacklisted = true;
break;
}
}
if (!blacklisted) {
if (GetLogLevel() <= kLogLevelDebug) {
// Call NSLog directly because we may be in a +load method,
// and C++ classes may not be constructed yet.
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
}
block(clazz);
void RunOnAppDelegateClasses(void (^block)(Class)) {
if (g_seen_delegate_classes_count > 0) {
NSLog(@"Firebase: RunOnAppDelegateClasses executing block for %d already seen delegate class(es).",
g_seen_delegate_classes_count);
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
if (g_seen_delegate_classes[i]) { // Safety check
block(g_seen_delegate_classes[i]);
}
}
} else {
NSLog(@"Firebase: RunOnAppDelegateClasses - no delegate classes seen yet. Block will be queued for future delegates.");
}

// Always try to queue the block for any future new delegate classes.
if (g_pending_block_count < MAX_PENDING_APP_DELEGATE_BLOCKS) {
g_pending_app_delegate_blocks[g_pending_block_count] = [block copy];
g_pending_block_count++;
NSLog(@"Firebase: RunOnAppDelegateClasses - added block to pending list (total pending: %d). This block will run on future new delegate classes.", g_pending_block_count);
} else {
NSLog(@"Firebase Error: RunOnAppDelegateClasses - pending block queue is full (max %d). Cannot add new block for future execution. Discarding block.", MAX_PENDING_APP_DELEGATE_BLOCKS);
}
free(classes);
}

NSDictionary *StringMapToNSDictionary(const std::map<std::string, std::string> &string_map) {
Expand Down
2 changes: 1 addition & 1 deletion messaging/src/ios/messaging.mm
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ @implementation UIApplication (FIRFCM)
+ (void)load {
// C++ constructors may not be called yet so call NSLog rather than LogInfo.
NSLog(@"FCM: Loading UIApplication FIRFCM category");
::firebase::util::ForEachAppDelegateClass(^(Class clazz) {
::firebase::util::RunOnAppDelegateClasses(^(Class clazz) {
FirebaseMessagingHookAppDelegate(clazz);
});
}
Expand Down
Loading