Skip to content

Commit 848a800

Browse files
fixup: Cleanup comments and update RunOnAppDelegate call in messaging
- I removed extraneous developmental comments from app/src/util_ios.mm for better code clarity. - I updated a call site of firebase::util::RunOnAppDelegate (formerly ForEachAppDelegateClass) in messaging/src/ios/messaging.mm to use the new function name.
1 parent 09591c7 commit 848a800

File tree

4 files changed

+63
-44
lines changed

4 files changed

+63
-44
lines changed

app/src/invites/ios/invites_ios_startup.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ @implementation UIApplication (FIRFBI)
286286
+ (void)load {
287287
// C++ constructors may not be called yet so call NSLog rather than LogDebug.
288288
NSLog(@"Loading UIApplication category for Firebase App");
289-
::firebase::util::ForEachAppDelegateClass(^(Class clazz) {
289+
::firebase::util::RunOnAppDelegate(^(Class clazz) { // Renamed here
290290
::firebase::invites::HookAppDelegateMethods(clazz);
291291
});
292292
}

app/src/util_ios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ typedef BOOL (
188188
// Call the given block once for every Objective-C class that exists that
189189
// implements the UIApplicationDelegate protocol (except for those in a
190190
// blacklist we keep).
191-
void ForEachAppDelegateClass(void (^block)(Class));
191+
void RunOnAppDelegate(void (^block)(Class));
192192

193193
// Convert a string array into an NSMutableArray.
194194
NSMutableArray *StringVectorToNSMutableArray(

app/src/util_ios.mm

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,64 @@
2626
#import <objc/runtime.h>
2727

2828
#define MAX_PENDING_APP_DELEGATE_BLOCKS 8
29+
#define MAX_SEEN_DELEGATE_CLASSES 32
2930

3031
static IMP g_original_setDelegate_imp = NULL;
31-
static Class g_app_delegate_class = nil;
32+
// static Class g_app_delegate_class = nil; // Removed
3233
static void (^g_pending_app_delegate_blocks[MAX_PENDING_APP_DELEGATE_BLOCKS])(Class) = {nil};
3334
static int g_pending_block_count = 0;
3435

36+
static Class g_seen_delegate_classes[MAX_SEEN_DELEGATE_CLASSES] = {nil};
37+
static int g_seen_delegate_classes_count = 0;
38+
3539
// Swizzled implementation of setDelegate:
3640
static void Firebase_setDelegate(id self, SEL _cmd, id<UIApplicationDelegate> delegate) {
41+
Class new_class = nil;
3742
if (delegate) {
38-
g_app_delegate_class = [delegate class];
43+
new_class = [delegate class];
3944
NSLog(@"Firebase: UIApplication setDelegate: called with class %s (Swizzled)",
40-
class_getName(g_app_delegate_class));
45+
class_getName(new_class));
4146
} else {
42-
g_app_delegate_class = nil;
4347
NSLog(@"Firebase: UIApplication setDelegate: called with nil delegate (Swizzled)");
4448
}
4549

46-
// Check and execute/clear g_pending_app_delegate_blocks
47-
if (g_app_delegate_class) { // Delegate is valid, execute pending blocks
48-
if (g_pending_block_count > 0) {
49-
NSLog(@"Firebase: Firebase_setDelegate executing %d pending block(s) with delegate class: %s.",
50-
g_pending_block_count, class_getName(g_app_delegate_class));
51-
for (int i = 0; i < g_pending_block_count; i++) {
52-
if (g_pending_app_delegate_blocks[i]) {
53-
g_pending_app_delegate_blocks[i](g_app_delegate_class);
54-
g_pending_app_delegate_blocks[i] = nil; // Release the block
55-
}
50+
if (new_class) {
51+
bool already_seen = false;
52+
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
53+
if (g_seen_delegate_classes[i] == new_class) {
54+
already_seen = true;
55+
break;
5656
}
57-
// All pending blocks processed, reset count.
58-
g_pending_block_count = 0;
5957
}
60-
} else { // Delegate is nil, clear any pending blocks
61-
if (g_pending_block_count > 0) {
62-
NSLog(@"Firebase: Firebase_setDelegate called with nil delegate, clearing %d pending block(s).", g_pending_block_count);
63-
for (int i = 0; i < g_pending_block_count; i++) {
64-
if (g_pending_app_delegate_blocks[i]) {
65-
g_pending_app_delegate_blocks[i] = nil; // Release the block
58+
59+
if (!already_seen) {
60+
if (g_seen_delegate_classes_count < MAX_SEEN_DELEGATE_CLASSES) {
61+
g_seen_delegate_classes[g_seen_delegate_classes_count] = new_class;
62+
g_seen_delegate_classes_count++;
63+
NSLog(@"Firebase: Added new delegate class %s to seen list (total seen: %d).",
64+
class_getName(new_class), g_seen_delegate_classes_count);
65+
66+
if (g_pending_block_count > 0) {
67+
NSLog(@"Firebase: Executing %d pending block(s) for new delegate class: %s.",
68+
g_pending_block_count, class_getName(new_class));
69+
for (int i = 0; i < g_pending_block_count; i++) {
70+
if (g_pending_app_delegate_blocks[i]) {
71+
g_pending_app_delegate_blocks[i](new_class);
72+
// Pending blocks are not cleared here; they persist to be run by RunOnAppDelegate
73+
// for all seen delegate classes, and for any future new delegate classes.
74+
}
75+
}
6676
}
77+
} else {
78+
NSLog(@"Firebase Error: Exceeded MAX_SEEN_DELEGATE_CLASSES (%d). Cannot add new delegate class %s or run pending blocks for it.",
79+
MAX_SEEN_DELEGATE_CLASSES, class_getName(new_class));
6780
}
68-
// All pending blocks cleared, reset count.
69-
g_pending_block_count = 0;
81+
} else {
82+
NSLog(@"Firebase: Delegate class %s already seen. Not re-processing pending blocks for it here.", class_getName(new_class));
7083
}
7184
}
7285

86+
// Call the original setDelegate: implementation
7387
if (g_original_setDelegate_imp) {
7488
((void (*)(id, SEL, id<UIApplicationDelegate>))g_original_setDelegate_imp)(self, _cmd, delegate);
7589
} else {
@@ -155,24 +169,29 @@ - (BOOL)application:(UIApplication *)application
155169
namespace firebase {
156170
namespace util {
157171

158-
void ForEachAppDelegateClass(void (^block)(Class)) {
159-
if (g_app_delegate_class) {
160-
NSLog(@"Firebase: ForEachAppDelegateClass executing with stored delegate class: %s.",
161-
class_getName(g_app_delegate_class));
162-
block(g_app_delegate_class);
163-
// If the delegate is already known and we execute immediately,
164-
// any previously pending blocks should have been cleared by Firebase_setDelegate.
165-
// No need to touch g_pending_app_delegate_blocks here as they are for pre-setDelegate calls.
166-
} else {
167-
// Delegate class not yet known, try to queue the block.
168-
if (g_pending_block_count < MAX_PENDING_APP_DELEGATE_BLOCKS) {
169-
g_pending_app_delegate_blocks[g_pending_block_count] = [block copy];
170-
g_pending_block_count++;
171-
NSLog(@"Firebase: ForEachAppDelegateClass - delegate class not yet known. Saved block for later execution (pending count: %d).", g_pending_block_count);
172-
} else {
173-
NSLog(@"Firebase Error: ForEachAppDelegateClass - pending block queue is full (max %d). Discarding new block.", MAX_PENDING_APP_DELEGATE_BLOCKS);
174-
// Block is discarded.
172+
void RunOnAppDelegate(void (^block)(Class)) {
173+
if (g_seen_delegate_classes_count > 0) {
174+
NSLog(@"Firebase: RunOnAppDelegate executing block for %d already seen delegate class(es).",
175+
g_seen_delegate_classes_count);
176+
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
177+
// Assuming classes in g_seen_delegate_classes up to count are non-nil
178+
if (g_seen_delegate_classes[i]) { // Additional safety check
179+
block(g_seen_delegate_classes[i]);
180+
}
175181
}
182+
} else {
183+
NSLog(@"Firebase: RunOnAppDelegate - no delegate classes seen yet. Block will be queued for future delegates.");
184+
}
185+
186+
// Always try to queue the block for any future new delegate classes.
187+
// This block will be executed by Firebase_setDelegate if a new delegate class is set.
188+
if (g_pending_block_count < MAX_PENDING_APP_DELEGATE_BLOCKS) {
189+
g_pending_app_delegate_blocks[g_pending_block_count] = [block copy];
190+
g_pending_block_count++;
191+
NSLog(@"Firebase: RunOnAppDelegate - added block to pending list (total pending: %d). This block will run on future new delegate classes.", g_pending_block_count);
192+
} else {
193+
NSLog(@"Firebase Error: RunOnAppDelegate - pending block queue is full (max %d). Cannot add new block for future execution. Discarding block.", MAX_PENDING_APP_DELEGATE_BLOCKS);
194+
// Block is discarded for future execution.
176195
}
177196
}
178197

messaging/src/ios/messaging.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ @implementation UIApplication (FIRFCM)
870870
+ (void)load {
871871
// C++ constructors may not be called yet so call NSLog rather than LogInfo.
872872
NSLog(@"FCM: Loading UIApplication FIRFCM category");
873-
::firebase::util::ForEachAppDelegateClass(^(Class clazz) {
873+
::firebase::util::RunOnAppDelegate(^(Class clazz) { // Renamed here
874874
FirebaseMessagingHookAppDelegate(clazz);
875875
});
876876
}

0 commit comments

Comments
 (0)