-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathutil_ios.mm
More file actions
717 lines (642 loc) · 28.7 KB
/
Copy pathutil_ios.mm
File metadata and controls
717 lines (642 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app/src/util_ios.h"
#include "app/src/assert.h"
#include "app/src/include/firebase/internal/common.h"
#include "app/src/log.h"
#include <assert.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
using firebase::GetLogLevel;
using firebase::kLogLevelDebug;
// Key used in Info.plist to specify a custom AppDelegate class name.
static NSString *const kFirebaseAppDelegateClassNameKey = @"FirebaseAppDelegateClassName";
#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];
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: UIApplication setDelegate: called with class %s (Swizzled)",
class_getName(new_class));
} else {
if (GetLogLevel() <= kLogLevelDebug)
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;
if (GetLogLevel() <= kLogLevelDebug)
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;
if (GetLogLevel() <= kLogLevelDebug)
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++;
if (GetLogLevel() <= kLogLevelDebug)
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) {
if (GetLogLevel() <= kLogLevelDebug)
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 {
return NO;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return NO;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary *)options {
return NO;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
}
#if defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:
(void (^)(NSArray<id<UIUserActivityRestoring>> *restorationHandler))restorationHandler {
return NO;
}
#else
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *))restorationHandler {
return NO;
}
#endif // defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0
@end
@implementation UIApplication (FirebaseAppDelegateSwizzling)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *appDelegateClassName =
[[NSBundle mainBundle] objectForInfoDictionaryKey:kFirebaseAppDelegateClassNameKey];
if (appDelegateClassName && [appDelegateClassName isKindOfClass:[NSString class]] &&
appDelegateClassName.length > 0) {
Class specificClass = NSClassFromString(appDelegateClassName);
if (specificClass) {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Info.plist key '%@' found. Targeting AppDelegate class: %@. Swizzling "
@"of [UIApplication setDelegate:] will be skipped.",
kFirebaseAppDelegateClassNameKey, appDelegateClassName);
// Set this class as the sole "seen" delegate for Firebase processing.
// g_seen_delegate_classes_count should be 0 here in +load, but clear just in case.
for (int i = 0; i < g_seen_delegate_classes_count; i++) {
g_seen_delegate_classes[i] = nil;
}
g_seen_delegate_classes[0] = specificClass;
g_seen_delegate_classes_count = 1;
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: %@ is now the only delegate class Firebase will initially process.",
appDelegateClassName);
// If there are already blocks pending (e.g., from other Firebase components' +load
// methods), execute them now for the specified delegate. These blocks will remain in the
// pending queue, mirroring the behavior of the original swizzled setDelegate: method which
// also does not clear pending blocks after execution (as they might apply to future
// delegates). In this Info.plist mode, however, Firebase won't process further setDelegate:
// calls.
if (g_pending_block_count > 0) {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: +load (Info.plist Mode) - Executing %d PENDING block(s) for "
@"specified delegate: %@. (Blocks are not removed from queue).",
g_pending_block_count, NSStringFromClass(specificClass));
for (int i = 0; i < g_pending_block_count; i++) {
if (g_pending_app_delegate_blocks[i]) {
g_pending_app_delegate_blocks[i](specificClass);
}
}
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: +load (Info.plist Mode) - Pending blocks executed for specific "
@"delegate.");
}
// Skip swizzling. g_original_setDelegate_imp remains NULL.
return;
} else {
NSLog(@"Firebase Error: Info.plist key '%@' specifies class '%@', which was not found. "
@"Proceeding with default swizzling.",
kFirebaseAppDelegateClassNameKey, appDelegateClassName);
}
} else {
if (appDelegateClassName) { // Key is present but value is invalid (e.g., empty string or
// wrong type).
NSLog(@"Firebase Error: Info.plist key '%@' has an invalid value ('%@'). Proceeding "
@"with default swizzling.",
kFirebaseAppDelegateClassNameKey, appDelegateClassName);
} else { // Key is not present.
// This is the default case, no special logging needed here beyond the swizzling log itself.
}
}
// Standard behavior: Swizzle [UIApplication setDelegate:]
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Proceeding with swizzling of [UIApplication setDelegate:].");
Class uiApplicationClass = [UIApplication class];
SEL originalSelector = @selector(setDelegate:);
Method originalMethod = class_getInstanceMethod(uiApplicationClass, originalSelector);
if (!originalMethod) {
NSLog(
@"Firebase Error: Original [UIApplication setDelegate:] method not found for swizzling.");
return;
}
IMP previousImp = method_setImplementation(originalMethod, (IMP)Firebase_setDelegate);
if (previousImp) {
g_original_setDelegate_imp = previousImp;
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Successfully swizzled [UIApplication setDelegate:] and stored original "
@"IMP.");
} else {
NSLog(@"Firebase Error: Swizzled [UIApplication setDelegate:], but original IMP was NULL (or "
@"method_setImplementation failed to return the previous IMP).");
}
});
}
@end
namespace firebase {
namespace util {
void RunOnAppDelegateClasses(void (^block)(Class)) {
if (g_seen_delegate_classes_count > 0) {
if (GetLogLevel() <= kLogLevelDebug)
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 {
if (GetLogLevel() <= kLogLevelDebug)
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++;
if (GetLogLevel() <= kLogLevelDebug)
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);
}
}
NSDictionary *StringMapToNSDictionary(const std::map<std::string, std::string> &string_map) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (auto &kv : string_map) {
dictionary[[NSString stringWithUTF8String:kv.first.c_str()]] =
[NSString stringWithUTF8String:kv.second.c_str()];
}
return dictionary;
}
NSDictionary *CharArrayMapToNSDictionary(const std::map<const char *, const char *> &string_map) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (auto &kv : string_map) {
dictionary[[NSString stringWithUTF8String:kv.first]] =
[NSString stringWithUTF8String:kv.second];
}
return dictionary;
}
NSData *StringToNSData(const std::string &str) {
return BytesToNSData(str.data(), static_cast<int>(str.length()));
}
NSData *BytesToNSData(const char *bytes, const int len) {
return [NSData dataWithBytes:bytes length:len];
}
NSData *BytesToNSData(const unsigned char *bytes, const int len) {
return [NSData dataWithBytes:bytes length:len];
}
std::string NSDataToString(NSData *data) {
return std::string(static_cast<const char *>(data.bytes), data.length);
}
NSString *StringToNSString(const std::string &str) { return CStringToNSString(str.c_str()); }
NSString *CStringToNSString(const char *c_str) {
return [NSString stringWithCString:c_str encoding:NSUTF8StringEncoding];
}
std::string NSStringToString(NSString *str) {
return str ? std::string([str cStringUsingEncoding:NSUTF8StringEncoding]) : std::string();
}
NSMutableArray *StringVectorToNSMutableArray(const std::vector<std::string> &vector) {
NSMutableArray<NSString *> *array = [[NSMutableArray alloc] initWithCapacity:vector.size()];
for (auto &element : vector) {
[array addObject:StringToNSString(element)];
}
return array;
}
NSMutableArray *StringUnorderedSetToNSMutableArray(const std::unordered_set<std::string> &set) {
NSMutableArray<NSString *> *array = [[NSMutableArray alloc] initWithCapacity:set.size()];
for (auto &element : set) {
[array addObject:StringToNSString(element)];
}
return array;
}
void NSArrayOfNSStringToVectorOfString(NSArray *array, std::vector<std::string> *string_vector) {
string_vector->reserve(array.count);
for (id object in array) {
if (![object isKindOfClass:[NSString class]]) {
FIREBASE_ASSERT_MESSAGE(false, "Object in Array is not of type NSString");
} else {
string_vector->push_back(NSStringToString((NSString *)object));
}
}
}
NSMutableArray *StdVectorToNSMutableArray(const std::vector<Variant> &vector) {
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:vector.size()];
for (auto &variant : vector) {
[array addObject:VariantToId(variant)];
}
return array;
}
NSMutableDictionary *StdMapToNSMutableDictionary(const std::map<Variant, Variant> &map) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:map.size()];
for (auto &pair : map) {
[dictionary setObject:VariantToId(pair.second) forKey:VariantToId(pair.first)];
}
return dictionary;
}
void NSArrayToStdVector(NSArray *array, std::vector<Variant> *vector) {
vector->reserve(array.count);
for (id object in array) {
vector->push_back(IdToVariant(object));
}
}
void NSDictionaryToStdMap(NSDictionary *dictionary, std::map<Variant, Variant> *map) {
for (id key in dictionary) {
id value = [dictionary objectForKey:key];
map->insert(std::make_pair(IdToVariant(key), IdToVariant(value)));
}
}
id VariantToId(const Variant &variant) {
switch (variant.type()) {
case Variant::kTypeNull: {
return [NSNull null];
}
case Variant::kTypeInt64: {
return [NSNumber numberWithLongLong:variant.int64_value()];
}
case Variant::kTypeDouble: {
return [NSNumber numberWithDouble:variant.double_value()];
}
case Variant::kTypeBool: {
return [NSNumber numberWithBool:variant.bool_value()];
}
case Variant::kTypeStaticString:
case Variant::kTypeMutableString: {
return @(variant.string_value());
}
case Variant::kTypeVector: {
return StdVectorToNSMutableArray(variant.vector());
}
case Variant::kTypeMap: {
return StdMapToNSMutableDictionary(variant.map());
}
default: {
FIREBASE_ASSERT_MESSAGE(false, "Variant has invalid type");
return [NSNull null];
}
}
}
static bool IdIsBoolean(id value) {
if ([value isKindOfClass:[NSNumber class]]) {
const char *type = [value objCType];
return strcmp(type, @encode(BOOL)) == 0 || strcmp(type, @encode(signed char)) == 0;
}
return false;
}
static bool IdIsInteger(id value) {
if ([value isKindOfClass:[NSNumber class]]) {
const char *type = [value objCType];
return strcmp(type, @encode(int)) == 0 || strcmp(type, @encode(short)) == 0 ||
strcmp(type, @encode(long)) == 0 || strcmp(type, @encode(long long)) == 0 ||
strcmp(type, @encode(unsigned char)) == 0 || strcmp(type, @encode(unsigned int)) == 0 ||
strcmp(type, @encode(unsigned short)) == 0 ||
strcmp(type, @encode(unsigned long)) == 0 ||
strcmp(type, @encode(unsigned long long)) == 0;
}
return false;
}
static bool IdIsFloatingPoint(id value) {
if ([value isKindOfClass:[NSNumber class]]) {
const char *type = [value objCType];
return strcmp(type, @encode(float)) == 0 || strcmp(type, @encode(double)) == 0;
}
return false;
}
Variant IdToVariant(id value) {
if (value == nil || value == [NSNull null]) {
return Variant();
} else if (IdIsInteger(value)) {
NSNumber *number = (NSNumber *)value;
return Variant(number.longLongValue);
} else if (IdIsFloatingPoint(value)) {
NSNumber *number = (NSNumber *)value;
return Variant(number.doubleValue);
} else if (IdIsBoolean(value)) {
NSNumber *number = (NSNumber *)value;
return Variant(number.boolValue != NO ? true : false);
} else if ([value isKindOfClass:[NSString class]]) {
return Variant::FromMutableString([value UTF8String]);
} else if ([value isKindOfClass:[NSArray class]]) {
Variant variant = Variant::EmptyVector();
NSArrayToStdVector((NSArray *)value, &variant.vector());
return variant;
} else if ([value isKindOfClass:[NSDictionary class]]) {
Variant variant = Variant::EmptyMap();
NSDictionaryToStdMap((NSDictionary *)value, &variant.map());
return variant;
} else if ([value isKindOfClass:[NSDate class]]) {
// Convert NSDates to millis since epoch.
NSDate *date = (NSDate *)value;
NSTimeInterval seconds = date.timeIntervalSince1970;
int64_t millis = static_cast<int64_t>(seconds * 1000);
return Variant(millis);
} else {
NSString *className = NSStringFromClass([value class]);
LogWarning("Unsupported NSObject type %s.", className.UTF8String);
FIREBASE_ASSERT_MESSAGE(false, "id has invalid type.");
return Variant();
}
}
void DispatchAsyncSafeMainQueue(void (^block)(void)) {
if ([NSThread isMainThread]) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}
void RunOnMainThread(void (*function_ptr)(void *function_data), void *function_data) {
dispatch_async(dispatch_get_main_queue(), ^{
function_ptr(function_data);
});
}
void RunOnBackgroundThread(void (*function_ptr)(void *function_data), void *function_data) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
function_ptr(function_data);
});
}
const int ClassMethodImplementationCache::kRandomNameGenerationRetries = 1000;
ClassMethodImplementationCache::ClassMethodImplementationCache() {
selector_implementation_names_per_selector_ = [[NSMutableDictionary alloc] init];
}
ClassMethodImplementationCache::~ClassMethodImplementationCache() {
selector_implementation_names_per_selector_ = nil;
}
// Replaces an existing method implementation on a class, caching the original implementation or
// adding the method to the class if it doesn't exist.
//
// If we need to add the method, we will use the type encoding of the selector |name| on
// |type_encoding_class|. The |type_encoding_class| is only used if a method isn't found for the
// specified selector |name| and therefore a new method needs to be added to the class.
//
// The cached method method implementation can be retrieved using GetMethod().
void ClassMethodImplementationCache::ReplaceOrAddMethod(Class clazz, SEL name, IMP imp,
Class type_encoding_class,
bool add_method) {
const char *class_name = class_getName(clazz);
Method method = class_getInstanceMethod(clazz, name);
NSString *selector_name_nsstring = NSStringFromSelector(name);
const char *selector_name = selector_name_nsstring.UTF8String; // Used for logging later
IMP current_actual_imp = method ? method_getImplementation(method) : nil;
// === Begin idempotency check ===
if (current_actual_imp == imp) {
// Assuming GetLogLevel() and kLogLevelDebug are available here.
// Based on previous file content, GetLogLevel is available in this file from util_ios.h.
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Method %s on class %s is already swizzled with the target IMP. Skipping "
@"re-swizzle.",
selector_name, class_name);
return; // Already swizzled to the desired implementation
}
// === End idempotency check ===
// If we reach here, current_actual_imp is different from imp, or the method didn't exist.
// We now assign original_method_implementation to be current_actual_imp for the rest of the
// function.
IMP original_method_implementation = current_actual_imp;
// Get the type encoding of the selector from a type_encoding_class (which is a class which
// implements a stub for the method).
const char *type_encoding =
method_getTypeEncoding(class_getInstanceMethod(type_encoding_class, name));
assert(type_encoding);
NSString *new_method_name_nsstring = nil;
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Attempting to register method for %s selector %s", class_name, selector_name);
if (original_method_implementation) {
// Try adding a method with randomized prefix on the name.
int retry = kRandomNameGenerationRetries;
while (retry--) {
// Cache the old method implementation in a new method so that we can lookup the original
// implementation from the instance of the class.
new_method_name_nsstring = GenerateRandomSelectorName(name);
SEL new_selector = NSSelectorFromString(new_method_name_nsstring);
if (class_addMethod(clazz, new_selector, original_method_implementation, type_encoding)) {
break;
}
}
const char *new_method_name = new_method_name_nsstring.UTF8String;
if (retry == 0) {
NSLog(
@"Firebase Error: Failed to add method %s on class %s as the %s method already exists on "
@"the class. To resolve this issue, change the name of the method %s on the class %s.",
new_method_name, class_name, new_method_name, new_method_name, class_name);
return;
}
method_setImplementation(method, imp);
// Save the selector name that points at the original method implementation.
SetMethod(name, new_method_name_nsstring);
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Registered method for %s selector %s (original method %s 0x%08x)",
class_name, selector_name, new_method_name,
static_cast<int>(reinterpret_cast<intptr_t>(original_method_implementation)));
} else if (add_method) {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Adding method for %s selector %s", class_name, selector_name);
// The class doesn't implement the selector so simply install our method implementation.
if (!class_addMethod(clazz, name, imp, type_encoding)) {
NSLog(@"Firebase Error: Failed to add new method %s on class %s.", selector_name, class_name);
}
} else {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Method implementation for %s selector %s not found, ignoring.", class_name,
selector_name);
}
}
// Get the original method implementation for the specific selector.
IMP ClassMethodImplementationCache::GetMethod(Class clazz, SEL name) {
assert(selector_implementation_names_per_selector_);
NSString *selector_name_nsstring = NSStringFromSelector(name);
const char *selector_name = selector_name_nsstring.UTF8String;
NSDictionary *selector_implementation_names =
selector_implementation_names_per_selector_[selector_name_nsstring];
const char *class_name = class_getName(clazz);
if (!selector_implementation_names) {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Method not cached for class %s selector %s.", class_name, selector_name);
return nil;
}
// Search for one of our randomly generated selector names on this class.
IMP method_implementation = nil;
NSString *selector_implementation_name_nsstring = nil;
Class search_class = nil;
for (NSString *key in selector_implementation_names) {
selector_implementation_name_nsstring = key;
// This object may have been dynamically subclassed like (http://goto/scion-app-delegate-proxy)
// or replaced entirely so search each superclass to see whether they respond to the
// implementation selector.
const char *selector_implementation_name = selector_implementation_name_nsstring.UTF8String;
SEL selector_implementation = NSSelectorFromString(selector_implementation_name_nsstring);
search_class = clazz;
for (; search_class; search_class = class_getSuperclass(search_class)) {
const char *search_class_name = class_getName(search_class);
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Searching for selector %s (%s) on class %s", selector_name,
selector_implementation_name, search_class_name);
Method method = class_getInstanceMethod(search_class, selector_implementation);
method_implementation = method ? method_getImplementation(method) : nil;
if (method_implementation) break;
}
if (method_implementation) break;
}
if (!method_implementation) {
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Class %s does not respond to selector %s (%s)", class_name, selector_name,
selector_implementation_name_nsstring.UTF8String);
return nil;
}
if (GetLogLevel() <= kLogLevelDebug)
NSLog(@"Firebase: Found %s (%s, 0x%08x) on class %s (%s)", selector_name,
selector_implementation_name_nsstring.UTF8String,
static_cast<int>(reinterpret_cast<intptr_t>(method_implementation)), class_name,
class_getName(search_class));
return method_implementation;
}
ClassMethodImplementationCache *ClassMethodImplementationCache::GetCreateCache(
ClassMethodImplementationCache **cache) {
assert(cache);
if (!(*cache)) *cache = new ClassMethodImplementationCache();
return *cache;
}
void ClassMethodImplementationCache::SetMethod(SEL name, NSString *implementation_selector_name) {
NSString *selector_name = NSStringFromSelector(name);
NSMutableDictionary *implementation_selector_names =
selector_implementation_names_per_selector_[selector_name];
// Create the dictionary of implementation selector names and add it to the dictionary if it's
// not already present.
if (!implementation_selector_names) {
implementation_selector_names = [[NSMutableDictionary alloc] init];
selector_implementation_names_per_selector_[selector_name] = implementation_selector_names;
}
implementation_selector_names[implementation_selector_name] = implementation_selector_name;
}
// Generate a random method name from the specified selector name. This is
// used to store the implementation of an overridden method.
NSString *ClassMethodImplementationCache::GenerateRandomSelectorName(SEL name) {
NSString *selector_name = NSStringFromSelector(name);
NSDictionary *implementation_selector_names =
selector_implementation_names_per_selector_[selector_name];
int retry = kRandomNameGenerationRetries;
while (retry--) {
// Cache the old method implementation in a new method so that we can lookup the original
// implementation from the instance of the class.
NSString *random_selector_name =
[[NSString alloc] initWithFormat:@"FIRA%x%@", arc4random(), selector_name];
if (!implementation_selector_names[random_selector_name]) {
return random_selector_name;
}
}
return nil;
}
} // namespace util
} // namespace firebase