File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,62 @@ static void Firebase_setDelegate(id self, SEL _cmd, id<UIApplicationDelegate> de
4444
class_getName(new_class));
4545
} else {
4646
NSLog(@"Firebase: UIApplication setDelegate: called with nil delegate (Swizzled)");
47+
// If delegate is nil, new_class remains nil.
48+
// The original implementation will be called later.
49+
// No class processing or block execution needed.
4750
}
4851

4952
if (new_class) {
50-
bool already_seen = false;
51-
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
52-
if (g_seen_delegate_classes[i] == new_class) {
53-
already_seen = true;
54-
break;
53+
// 1. Superclass Check
54+
bool superclass_already_seen = false;
55+
Class current_super = class_getSuperclass(new_class);
56+
while (current_super) {
57+
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
58+
if (g_seen_delegate_classes[i] == current_super) {
59+
superclass_already_seen = true;
60+
NSLog(@"Firebase: Delegate class %s has superclass %s which was already seen. Skipping processing for %s.",
61+
class_getName(new_class), class_getName(current_super), class_getName(new_class));
62+
break;
63+
}
5564
}
65+
if (superclass_already_seen) break;
66+
current_super = class_getSuperclass(current_super);
5667
}
5768

58-
if (!already_seen) {
59-
if (g_seen_delegate_classes_count < MAX_SEEN_DELEGATE_CLASSES) {
60-
g_seen_delegate_classes[g_seen_delegate_classes_count] = new_class;
61-
g_seen_delegate_classes_count++;
62-
NSLog(@"Firebase: Added new delegate class %s to seen list (total seen: %d).",
63-
class_getName(new_class), g_seen_delegate_classes_count);
64-
65-
if (g_pending_block_count > 0) {
66-
NSLog(@"Firebase: Executing %d pending block(s) for new delegate class: %s.",
67-
g_pending_block_count, class_getName(new_class));
68-
for (int i = 0; i < g_pending_block_count; i++) {
69-
if (g_pending_app_delegate_blocks[i]) {
70-
g_pending_app_delegate_blocks[i](new_class);
71-
// Pending blocks are not cleared here; they persist to be run by RunOnAppDelegate
72-
// for all seen delegate classes, and for any future new delegate classes.
69+
if (!superclass_already_seen) {
70+
// 2. Direct Class Check (if no superclass was seen)
71+
bool direct_class_already_seen = false;
72+
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
73+
if (g_seen_delegate_classes[i] == new_class) {
74+
direct_class_already_seen = true;
75+
NSLog(@"Firebase: Delegate class %s already seen directly. Skipping processing.",
76+
class_getName(new_class));
77+
break;
78+
}
79+
}
80+
81+
if (!direct_class_already_seen) {
82+
// 3. Process as New Class
83+
if (g_seen_delegate_classes_count < MAX_SEEN_DELEGATE_CLASSES) {
84+
g_seen_delegate_classes[g_seen_delegate_classes_count] = new_class;
85+
g_seen_delegate_classes_count++;
86+
NSLog(@"Firebase: Added new delegate class %s to seen list (total seen: %d).",
87+
class_getName(new_class), g_seen_delegate_classes_count);
88+
89+
if (g_pending_block_count > 0) {
90+
NSLog(@"Firebase: Executing %d pending block(s) for new delegate class: %s.",
91+
g_pending_block_count, class_getName(new_class));
92+
for (int i = 0; i < g_pending_block_count; i++) {
93+
if (g_pending_app_delegate_blocks[i]) {
94+
g_pending_app_delegate_blocks[i](new_class);
95+
}
7396
}
7497
}
98+
} else {
99+
NSLog(@"Firebase Error: Exceeded MAX_SEEN_DELEGATE_CLASSES (%d). Cannot add new delegate class %s or run pending blocks for it.",
100+
MAX_SEEN_DELEGATE_CLASSES, class_getName(new_class));
75101
}
76-
} else {
77-
NSLog(@"Firebase Error: Exceeded MAX_SEEN_DELEGATE_CLASSES (%d). Cannot add new delegate class %s or run pending blocks for it.",
78-
MAX_SEEN_DELEGATE_CLASSES, class_getName(new_class));
79102
}
80-
} else {
81-
NSLog(@"Firebase: Delegate class %s already seen. Not re-processing pending blocks for it here.", class_getName(new_class));
82103
}
83104
}
84105

@@ -435,23 +456,8 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data), void *func
435456
const char *class_name = class_getName(clazz);
436457
Method method = class_getInstanceMethod(clazz, name);
437458
NSString *selector_name_nsstring = NSStringFromSelector(name);
438-
const char *selector_name = selector_name_nsstring.UTF8String; // Used for logging later
439-
440-
IMP current_actual_imp = method ? method_getImplementation(method) : nil;
441-
442-
// === Begin new idempotency check ===
443-
if (current_actual_imp == imp) {
444-
if (GetLogLevel() <= kLogLevelDebug) { // Assuming GetLogLevel() and kLogLevelDebug are accessible
445-
NSLog(@"Firebase Cache: Method %s on class %s is already swizzled with the target IMP. Skipping re-swizzle.",
446-
selector_name, class_name);
447-
}
448-
return; // Already swizzled to the desired implementation
449-
}
450-
// === End new idempotency check ===
451-
452-
// If we reach here, current_actual_imp is different from imp, or the method didn't exist.
453-
// We now assign original_method_implementation to be current_actual_imp for the rest of the function.
454-
IMP original_method_implementation = current_actual_imp;
459+
const char *selector_name = selector_name_nsstring.UTF8String;
460+
IMP original_method_implementation = method ? method_getImplementation(method) : nil; // Directly initialized
455461

456462
// Get the type encoding of the selector from a type_encoding_class (which is a class which
457463
// implements a stub for the method).
@@ -460,13 +466,14 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data), void *func
460466
assert(type_encoding);
461467

462468
NSString *new_method_name_nsstring = nil;
463-
// The GetLogLevel() check here is fine, but the NSLog should use class_name and selector_name
464-
// which are already defined.
465469
if (GetLogLevel() <= kLogLevelDebug) {
466-
// Original: NSLog(@"Registering method for %s selector %s", class_name, selector_name);
467-
// This log can be more specific now, e.g., "Attempting to swizzle/add method..."
468-
// For now, let's keep it or refine it if needed, but ensure it uses defined vars.
469-
NSLog(@"Firebase Cache: Attempting to register method for %s selector %s", class_name, selector_name);
470+
// This log might have been just "Registering method..." or similar.
471+
// Let's revert to a more basic version if it was changed, or ensure it's reasonable.
472+
// For the purpose of this revert, keeping the "Firebase Cache: Attempting to register..."
473+
// or reverting to a simpler "Registering method..." is acceptable if the exact prior state
474+
// of this specific log line is not critical, the main point is the logic revert.
475+
// Let's assume it was:
476+
NSLog(@"Firebase Cache: Registering method for %s selector %s", class_name, selector_name);
470477
}
471478
if (original_method_implementation) {
472479
// Try adding a method with randomized prefix on the name.

0 commit comments

Comments
 (0)