Skip to content

Commit c3bb4ff

Browse files
committed
Refactor the globals view controller list
FLEXGlobalsTableViewControllerEntry now has a method to be initialized with a class conforming to a new protocol, FLEXGlobalsTableViewControllerEntry, which itself provides the properties the entry needs. This removes a lot of boilerplate from the globals view controller itself.
1 parent 55b579a commit c3bb4ff

20 files changed

+360
-253
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// FLEXAddressExplorerCoordinator.h
3+
// FLEX
4+
//
5+
// Created by Tanner Bennett on 7/10/19.
6+
// Copyright © 2019 Flipboard. All rights reserved.
7+
//
8+
9+
#import "FLEXGlobalsTableViewControllerEntry.h"
10+
11+
@interface FLEXAddressExplorerCoordinator : NSObject <FLEXGlobalsTableViewControllerEntry>
12+
13+
@end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Classes/GlobalStateExplorers/FLEXClassesTableViewController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
//
88

99
#import "FLEXTableViewController.h"
10+
#import "FLEXGlobalsTableViewControllerEntry.h"
1011

11-
@interface FLEXClassesTableViewController : FLEXTableViewController
12+
@interface FLEXClassesTableViewController : FLEXTableViewController <FLEXGlobalsTableViewControllerEntry>
1213

1314
@property (nonatomic, copy) NSString *binaryImageName;
1415

Classes/GlobalStateExplorers/FLEXClassesTableViewController.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ - (void)updateTitle
6868
}
6969

7070

71+
#pragma mark - FLEXGlobalsTableViewControllerEntry
72+
73+
+ (NSString *)globalsEntryTitle {
74+
return [NSString stringWithFormat:@"📕 %@ Classes", [FLEXUtility applicationName]];
75+
}
76+
77+
+ (instancetype)globalsEntryViewController {
78+
FLEXClassesTableViewController *classesViewController = [self new];
79+
classesViewController.binaryImageName = [FLEXUtility applicationImageName];
80+
81+
return classesViewController;
82+
}
83+
84+
7185
#pragma mark - Search bar
7286

7387
- (void)updateSearchResults:(NSString *)searchText

Classes/GlobalStateExplorers/FLEXCookiesTableViewController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// Copyright © 2015 Flipboard. All rights reserved.
77
//
88

9-
#import <UIKit/UIKit.h>
9+
#import "FLEXGlobalsTableViewControllerEntry.h"
1010

11-
@interface FLEXCookiesTableViewController : UITableViewController
11+
@interface FLEXCookiesTableViewController : UITableViewController <FLEXGlobalsTableViewControllerEntry>
1212

1313
@end

Classes/GlobalStateExplorers/FLEXCookiesTableViewController.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
6868
[self.navigationController pushViewController:cookieViewController animated:YES];
6969
}
7070

71+
#pragma mark - FLEXGlobalsTableViewControllerEntry
72+
73+
+ (NSString *)globalsEntryTitle {
74+
return @"🍪 Cookies";
75+
}
76+
77+
+ (instancetype)globalsEntryViewController {
78+
return [self new];
79+
}
80+
7181
@end

0 commit comments

Comments
 (0)