|
| 1 | +#import <Foundation/Foundation.h> |
| 2 | +#import "InstabugExampleMethodCallHandler.h" |
| 3 | +#import <Instabug/IBGCrashReporting.h> |
| 4 | +#import <Instabug/Instabug.h> |
| 5 | +#import <Flutter/Flutter.h> |
| 6 | + |
| 7 | +// MARK: - Private Interface |
| 8 | +@interface InstabugExampleMethodCallHandler() |
| 9 | +@property (nonatomic, strong) NSMutableArray *oomBelly; |
| 10 | +@property (nonatomic, strong) dispatch_queue_t serialQueue; |
| 11 | +@end |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +// MARK: - Constants |
| 16 | + |
| 17 | +extern NSString * const kSendNativeNonFatalCrashMethod; |
| 18 | +extern NSString * const kSendNativeFatalCrashMethod; |
| 19 | +extern NSString * const kSendNativeFatalHangMethod; |
| 20 | +extern NSString * const kSendOOMMethod; |
| 21 | + |
| 22 | +extern NSString * const kInstabugChannelName; |
| 23 | + |
| 24 | +// MARK: - MethodCallHandler Implementation |
| 25 | + |
| 26 | +@implementation InstabugExampleMethodCallHandler |
| 27 | + |
| 28 | +NSString * const kSendNativeNonFatalCrashMethod = @"sendNativeNonFatalCrash"; |
| 29 | +NSString * const kSendNativeFatalCrashMethod = @"sendNativeFatalCrash"; |
| 30 | +NSString * const kSendNativeFatalHangMethod = @"sendNativeFatalHang"; |
| 31 | +NSString * const kSendOOMMethod = @"sendOom"; |
| 32 | + |
| 33 | +NSString * const kInstabugChannelName = @"instabug_flutter_example"; |
| 34 | + |
| 35 | +// MARK: - Initializer |
| 36 | + |
| 37 | +- (instancetype)init { |
| 38 | + self = [super init]; |
| 39 | + if (self) { |
| 40 | + self.serialQueue = dispatch_queue_create("QUEUE>SERIAL", DISPATCH_QUEUE_SERIAL); |
| 41 | + } |
| 42 | + return self; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +// MARK: - Flutter Plugin Methods |
| 47 | + |
| 48 | +- (dispatch_queue_t)methodQueue { |
| 49 | + return dispatch_get_main_queue(); |
| 50 | +} |
| 51 | + |
| 52 | ++ (BOOL)requiresMainQueueSetup |
| 53 | +{ |
| 54 | + return NO; |
| 55 | +} |
| 56 | + |
| 57 | +- (void)oomCrash { |
| 58 | + dispatch_async(self.serialQueue, ^{ |
| 59 | + self.oomBelly = [NSMutableArray array]; |
| 60 | + [UIApplication.sharedApplication beginBackgroundTaskWithName:@"OOM Crash" expirationHandler:nil]; |
| 61 | + while (true) { |
| 62 | + unsigned long dinnerLength = 1024 * 1024 * 10; |
| 63 | + char *dinner = malloc(sizeof(char) * dinnerLength); |
| 64 | + for (int i=0; i < dinnerLength; i++) |
| 65 | + { |
| 66 | + //write to each byte ensure that the memory pages are actually allocated |
| 67 | + dinner[i] = '0'; |
| 68 | + } |
| 69 | + NSData *plate = [NSData dataWithBytesNoCopy:dinner length:dinnerLength freeWhenDone:YES]; |
| 70 | + [self.oomBelly addObject:plate]; |
| 71 | + } |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
| 76 | + if ([kSendNativeNonFatalCrashMethod isEqualToString:call.method]) { |
| 77 | + NSLog(@"Sending native non-fatal crash from iOS"); |
| 78 | + [self sendNativeNonFatal:call.arguments]; |
| 79 | + result(nil); |
| 80 | + } else if ([kSendNativeFatalCrashMethod isEqualToString:call.method]) { |
| 81 | + NSLog(@"Sending native fatal crash from iOS"); |
| 82 | + [self sendNativeFatalCrash]; |
| 83 | + result(nil); |
| 84 | + } else if ([kSendNativeFatalHangMethod isEqualToString:call.method]) { |
| 85 | + NSLog(@"Sending native fatal hang for 3000 ms from iOS"); |
| 86 | + [self sendFatalHang]; |
| 87 | + result(nil); |
| 88 | + } else if ([kSendOOMMethod isEqualToString:call.method]) { |
| 89 | + NSLog(@"Sending out of memory from iOS"); |
| 90 | + [self sendOOM]; |
| 91 | + result(nil); |
| 92 | + } else { |
| 93 | + result(FlutterMethodNotImplemented); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// MARK: - Helper Methods |
| 98 | + |
| 99 | +- (void)sendNativeNonFatal:(NSString *)exceptionObject { |
| 100 | + IBGNonFatalException *nonFatalException = [IBGCrashReporting exception:[NSException exceptionWithName:@"native Handled NS Exception" reason:@"Test iOS Handled Crash" userInfo:@{@"Key": @"Value"}]]; |
| 101 | + |
| 102 | + [nonFatalException report]; |
| 103 | +} |
| 104 | + |
| 105 | +- (void)sendNativeFatalCrash { |
| 106 | + NSException *exception = [NSException exceptionWithName:@"native Unhandled NS Exception" reason:@"Test iOS Unhandled Crash" userInfo:nil]; |
| 107 | + @throw exception; |
| 108 | +} |
| 109 | + |
| 110 | +- (void)sendFatalHang { |
| 111 | + [NSThread sleepForTimeInterval:3.0f]; |
| 112 | +} |
| 113 | + |
| 114 | +- (void)sendOOM { |
| 115 | + [self oomCrash]; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)sendNativeNonFatal { |
| 119 | +} |
| 120 | + |
| 121 | +@end |
0 commit comments