|
| 1 | +// |
| 2 | +// FLEXAddressExplorerCoordinator.m |
| 3 | +// FLEX |
| 4 | +// |
| 5 | +// Created by Tanner Bennett on 7/10/19. |
| 6 | +// Copyright © 2019 Flipboard. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FLEXAddressExplorerCoordinator.h" |
| 10 | +#import "FLEXGlobalsTableViewController.h" |
| 11 | +#import "FLEXObjectExplorerFactory.h" |
| 12 | +#import "FLEXObjectExplorerViewController.h" |
| 13 | +#import "FLEXRuntimeUtility.h" |
| 14 | +#import "FLEXUtility.h" |
| 15 | + |
| 16 | +@interface FLEXGlobalsTableViewController (FLEXAddressExploration) |
| 17 | +- (void)deselectSelectedRow; |
| 18 | +- (void)tryExploreAddress:(NSString *)addressString safely:(BOOL)safely; |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation FLEXAddressExplorerCoordinator |
| 22 | + |
| 23 | +#pragma mark - FLEXGlobalsTableViewControllerEntry |
| 24 | + |
| 25 | ++ (NSString *)globalsEntryTitle { |
| 26 | + return @"🔎 Address Explorer"; |
| 27 | +} |
| 28 | + |
| 29 | ++ (FLEXGlobalsTableViewControllerRowAction)globalsEntryRowAction { |
| 30 | + return ^(FLEXGlobalsTableViewController *host) { |
| 31 | + NSString *title = @"Explore Object at Address"; |
| 32 | + NSString *message = @"Paste a hexadecimal address below, starting with '0x'. " |
| 33 | + "Use the unsafe option if you need to bypass pointer validation, " |
| 34 | + "but know that it may crash the app if the address is invalid."; |
| 35 | + |
| 36 | + UIAlertController *addressInput = [UIAlertController alertControllerWithTitle:title |
| 37 | + message:message |
| 38 | + preferredStyle:UIAlertControllerStyleAlert]; |
| 39 | + void (^handler)(UIAlertAction *) = ^(UIAlertAction *action) { |
| 40 | + if (action.style == UIAlertActionStyleCancel) { |
| 41 | + [host deselectSelectedRow]; return; |
| 42 | + } |
| 43 | + NSString *address = addressInput.textFields.firstObject.text; |
| 44 | + [host tryExploreAddress:address safely:action.style == UIAlertActionStyleDefault]; |
| 45 | + }; |
| 46 | + [addressInput addTextFieldWithConfigurationHandler:^(UITextField *textField) { |
| 47 | + NSString *copied = [UIPasteboard generalPasteboard].string; |
| 48 | + textField.placeholder = @"0x00000070deadbeef"; |
| 49 | + // Go ahead and paste our clipboard if we have an address copied |
| 50 | + if ([copied hasPrefix:@"0x"]) { |
| 51 | + textField.text = copied; |
| 52 | + [textField selectAll:nil]; |
| 53 | + } |
| 54 | + }]; |
| 55 | + [addressInput addAction:[UIAlertAction actionWithTitle:@"Explore" |
| 56 | + style:UIAlertActionStyleDefault |
| 57 | + handler:handler]]; |
| 58 | + [addressInput addAction:[UIAlertAction actionWithTitle:@"Unsafe Explore" |
| 59 | + style:UIAlertActionStyleDestructive |
| 60 | + handler:handler]]; |
| 61 | + [addressInput addAction:[UIAlertAction actionWithTitle:@"Cancel" |
| 62 | + style:UIAlertActionStyleCancel |
| 63 | + handler:handler]]; |
| 64 | + [host presentViewController:addressInput animated:YES completion:nil]; |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +@end |
| 69 | + |
| 70 | +@implementation FLEXGlobalsTableViewController (FLEXAddressExploration) |
| 71 | + |
| 72 | +- (void)deselectSelectedRow { |
| 73 | + NSIndexPath *selected = self.tableView.indexPathForSelectedRow; |
| 74 | + [self.tableView deselectRowAtIndexPath:selected animated:YES]; |
| 75 | +} |
| 76 | + |
| 77 | +- (void)tryExploreAddress:(NSString *)addressString safely:(BOOL)safely { |
| 78 | + NSScanner *scanner = [NSScanner scannerWithString:addressString]; |
| 79 | + unsigned long long hexValue = 0; |
| 80 | + BOOL didParseAddress = [scanner scanHexLongLong:&hexValue]; |
| 81 | + const void *pointerValue = (void *)hexValue; |
| 82 | + |
| 83 | + NSString *error = nil; |
| 84 | + |
| 85 | + if (didParseAddress) { |
| 86 | + if (safely && ![FLEXRuntimeUtility pointerIsValidObjcObject:pointerValue]) { |
| 87 | + error = @"The given address is unlikely to be a valid object."; |
| 88 | + } |
| 89 | + } else { |
| 90 | + error = @"Malformed address. Make sure it's not too long and starts with '0x'."; |
| 91 | + } |
| 92 | + |
| 93 | + if (!error) { |
| 94 | + id object = (__bridge id)pointerValue; |
| 95 | + FLEXObjectExplorerViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:object]; |
| 96 | + [self.navigationController pushViewController:explorer animated:YES]; |
| 97 | + } else { |
| 98 | + [FLEXUtility alert:@"Uh-oh" message:error from:self]; |
| 99 | + [self deselectSelectedRow]; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +@end |
0 commit comments