You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
16 KiB

  1. //
  2. // FLEXGlobalsTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXLibrariesTableViewController.h"
  12. #import "FLEXClassesTableViewController.h"
  13. #import "FLEXObjectExplorerViewController.h"
  14. #import "FLEXObjectExplorerFactory.h"
  15. #import "FLEXLiveObjectsTableViewController.h"
  16. #import "FLEXFileBrowserTableViewController.h"
  17. #import "FLEXCookiesTableViewController.h"
  18. #import "FLEXGlobalsTableViewControllerEntry.h"
  19. #import "FLEXManager+Private.h"
  20. #import "FLEXSystemLogTableViewController.h"
  21. #import "FLEXNetworkHistoryTableViewController.h"
  22. static __weak UIWindow *s_applicationWindow = nil;
  23. typedef NS_ENUM(NSUInteger, FLEXGlobalsRow) {
  24. FLEXGlobalsRowNetworkHistory,
  25. FLEXGlobalsRowSystemLog,
  26. FLEXGlobalsRowLiveObjects,
  27. FLEXGlobalsRowAddressInspector,
  28. FLEXGlobalsRowFileBrowser,
  29. FLEXGlobalsCookies,
  30. FLEXGlobalsRowSystemLibraries,
  31. FLEXGlobalsRowAppClasses,
  32. FLEXGlobalsRowAppDelegate,
  33. FLEXGlobalsRowRootViewController,
  34. FLEXGlobalsRowUserDefaults,
  35. FLEXGlobalsRowMainBundle,
  36. FLEXGlobalsRowApplication,
  37. FLEXGlobalsRowKeyWindow,
  38. FLEXGlobalsRowMainScreen,
  39. FLEXGlobalsRowCurrentDevice,
  40. FLEXGlobalsRowCount
  41. };
  42. @interface FLEXGlobalsTableViewController ()
  43. @property (nonatomic, readonly, copy) NSArray<FLEXGlobalsTableViewControllerEntry *> *entries;
  44. @end
  45. @implementation FLEXGlobalsTableViewController
  46. + (NSArray<FLEXGlobalsTableViewControllerEntry *> *)defaultGlobalEntries
  47. {
  48. NSMutableArray<FLEXGlobalsTableViewControllerEntry *> *defaultGlobalEntries = [NSMutableArray array];
  49. for (FLEXGlobalsRow defaultRowIndex = 0; defaultRowIndex < FLEXGlobalsRowCount; defaultRowIndex++) {
  50. FLEXGlobalsTableViewControllerEntryNameFuture titleFuture = nil;
  51. FLEXGlobalsTableViewControllerViewControllerFuture viewControllerFuture = nil;
  52. FLEXGlobalsTableViewControllerRowAction rowAction = nil;
  53. switch (defaultRowIndex) {
  54. case FLEXGlobalsRowAppClasses:
  55. titleFuture = ^NSString *{
  56. return [NSString stringWithFormat:@"📕 %@ Classes", [FLEXUtility applicationName]];
  57. };
  58. viewControllerFuture = ^UIViewController *{
  59. FLEXClassesTableViewController *classesViewController = [[FLEXClassesTableViewController alloc] init];
  60. classesViewController.binaryImageName = [FLEXUtility applicationImageName];
  61. return classesViewController;
  62. };
  63. break;
  64. case FLEXGlobalsRowAddressInspector:
  65. titleFuture = ^NSString *{
  66. return @"🔎 Address Explorer";
  67. };
  68. rowAction = ^(FLEXGlobalsTableViewController *host) {
  69. NSString *title = @"Explore Object at Address";
  70. NSString *message = @"Paste a hexadecimal address below, starting with '0x'. "
  71. "Use the unsafe option if you need to bypass pointer validation, "
  72. "but know that it may crash the app if the address is invalid.";
  73. UIAlertController *addressInput = [UIAlertController alertControllerWithTitle:title
  74. message:message
  75. preferredStyle:UIAlertControllerStyleAlert];
  76. void (^handler)(UIAlertAction *) = ^(UIAlertAction *action) {
  77. if (action.style == UIAlertActionStyleCancel) {
  78. [host deselectSelectedRow]; return;
  79. }
  80. NSString *address = addressInput.textFields.firstObject.text;
  81. [host tryExploreAddress:address safely:action.style == UIAlertActionStyleDefault];
  82. };
  83. [addressInput addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  84. NSString *copied = [UIPasteboard generalPasteboard].string;
  85. textField.placeholder = @"0x00000070deadbeef";
  86. // Go ahead and paste our clipboard if we have an address copied
  87. if ([copied hasPrefix:@"0x"]) {
  88. textField.text = copied;
  89. [textField selectAll:nil];
  90. }
  91. }];
  92. [addressInput addAction:[UIAlertAction actionWithTitle:@"Explore"
  93. style:UIAlertActionStyleDefault
  94. handler:handler]];
  95. [addressInput addAction:[UIAlertAction actionWithTitle:@"Unsafe Explore"
  96. style:UIAlertActionStyleDestructive
  97. handler:handler]];
  98. [addressInput addAction:[UIAlertAction actionWithTitle:@"Cancel"
  99. style:UIAlertActionStyleCancel
  100. handler:handler]];
  101. [host presentViewController:addressInput animated:YES completion:nil];
  102. };
  103. break;
  104. case FLEXGlobalsRowSystemLibraries: {
  105. NSString *titleString = @"📚 System Libraries";
  106. titleFuture = ^NSString *{
  107. return titleString;
  108. };
  109. viewControllerFuture = ^UIViewController *{
  110. FLEXLibrariesTableViewController *librariesViewController = [[FLEXLibrariesTableViewController alloc] init];
  111. librariesViewController.title = titleString;
  112. return librariesViewController;
  113. };
  114. break;
  115. }
  116. case FLEXGlobalsRowLiveObjects:
  117. titleFuture = ^NSString *{
  118. return @"💩 Heap Objects";
  119. };
  120. viewControllerFuture = ^UIViewController *{
  121. return [[FLEXLiveObjectsTableViewController alloc] init];
  122. };
  123. break;
  124. case FLEXGlobalsRowAppDelegate:
  125. titleFuture = ^NSString *{
  126. return [NSString stringWithFormat:@"👉 %@", [[[UIApplication sharedApplication] delegate] class]];
  127. };
  128. viewControllerFuture = ^UIViewController *{
  129. id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
  130. return [FLEXObjectExplorerFactory explorerViewControllerForObject:appDelegate];
  131. };
  132. break;
  133. case FLEXGlobalsRowRootViewController:
  134. titleFuture = ^NSString *{
  135. return [NSString stringWithFormat:@"🌴 %@", [[s_applicationWindow rootViewController] class]];
  136. };
  137. viewControllerFuture = ^UIViewController *{
  138. UIViewController *rootViewController = [s_applicationWindow rootViewController];
  139. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  140. };
  141. break;
  142. case FLEXGlobalsRowUserDefaults:
  143. titleFuture = ^NSString *{
  144. return @"🚶 +[NSUserDefaults standardUserDefaults]";
  145. };
  146. viewControllerFuture = ^UIViewController *{
  147. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  148. return [FLEXObjectExplorerFactory explorerViewControllerForObject:standardUserDefaults];
  149. };
  150. break;
  151. case FLEXGlobalsRowMainBundle:
  152. titleFuture = ^NSString *{
  153. return @"📦 +[NSBundle mainBundle]";
  154. };
  155. viewControllerFuture = ^UIViewController *{
  156. NSBundle *mainBundle = [NSBundle mainBundle];
  157. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainBundle];
  158. };
  159. break;
  160. case FLEXGlobalsRowApplication:
  161. titleFuture = ^NSString *{
  162. return @"💾 +[UIApplication sharedApplication]";
  163. };
  164. viewControllerFuture = ^UIViewController *{
  165. UIApplication *sharedApplication = [UIApplication sharedApplication];
  166. return [FLEXObjectExplorerFactory explorerViewControllerForObject:sharedApplication];
  167. };
  168. break;
  169. case FLEXGlobalsRowKeyWindow:
  170. titleFuture = ^NSString *{
  171. return @"🔑 -[UIApplication keyWindow]";
  172. };
  173. viewControllerFuture = ^UIViewController *{
  174. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  175. };
  176. break;
  177. case FLEXGlobalsRowMainScreen:
  178. titleFuture = ^NSString *{
  179. return @"💻 +[UIScreen mainScreen]";
  180. };
  181. viewControllerFuture = ^UIViewController *{
  182. UIScreen *mainScreen = [UIScreen mainScreen];
  183. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainScreen];
  184. };
  185. break;
  186. case FLEXGlobalsRowCurrentDevice:
  187. titleFuture = ^NSString *{
  188. return @"📱 +[UIDevice currentDevice]";
  189. };
  190. viewControllerFuture = ^UIViewController *{
  191. UIDevice *currentDevice = [UIDevice currentDevice];
  192. return [FLEXObjectExplorerFactory explorerViewControllerForObject:currentDevice];
  193. };
  194. break;
  195. case FLEXGlobalsCookies:
  196. titleFuture = ^NSString *{
  197. return @"🍪 Cookies";
  198. };
  199. viewControllerFuture = ^UIViewController *{
  200. return [[FLEXCookiesTableViewController alloc] init];
  201. };
  202. break;
  203. case FLEXGlobalsRowFileBrowser:
  204. titleFuture = ^NSString *{
  205. return @"📁 File Browser";
  206. };
  207. viewControllerFuture = ^UIViewController *{
  208. return [[FLEXFileBrowserTableViewController alloc] init];
  209. };
  210. break;
  211. case FLEXGlobalsRowSystemLog:
  212. titleFuture = ^{
  213. return @"⚠️ System Log";
  214. };
  215. viewControllerFuture = ^{
  216. return [[FLEXSystemLogTableViewController alloc] init];
  217. };
  218. break;
  219. case FLEXGlobalsRowNetworkHistory:
  220. titleFuture = ^{
  221. return @"📡 Network History";
  222. };
  223. viewControllerFuture = ^{
  224. return [[FLEXNetworkHistoryTableViewController alloc] init];
  225. };
  226. break;
  227. case FLEXGlobalsRowCount:
  228. break;
  229. }
  230. NSAssert(viewControllerFuture || rowAction, @"The switch-case above must assign one of these");
  231. if (viewControllerFuture) {
  232. [defaultGlobalEntries addObject:[FLEXGlobalsTableViewControllerEntry
  233. entryWithNameFuture:titleFuture
  234. viewControllerFuture:viewControllerFuture]];
  235. } else {
  236. [defaultGlobalEntries addObject:[FLEXGlobalsTableViewControllerEntry
  237. entryWithNameFuture:titleFuture
  238. action:rowAction]];
  239. }
  240. }
  241. return defaultGlobalEntries;
  242. }
  243. - (id)initWithStyle:(UITableViewStyle)style
  244. {
  245. self = [super initWithStyle:style];
  246. if (self) {
  247. self.title = @"💪 FLEX";
  248. _entries = [[[self class] defaultGlobalEntries] arrayByAddingObjectsFromArray:[FLEXManager sharedManager].userGlobalEntries];
  249. }
  250. return self;
  251. }
  252. - (void)deselectSelectedRow {
  253. NSIndexPath *selected = self.tableView.indexPathForSelectedRow;
  254. [self.tableView deselectRowAtIndexPath:selected animated:YES];
  255. }
  256. #pragma mark - Public
  257. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  258. {
  259. s_applicationWindow = applicationWindow;
  260. }
  261. #pragma mark - UIViewController
  262. - (void)viewDidLoad
  263. {
  264. [super viewDidLoad];
  265. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  266. }
  267. #pragma mark - Misc
  268. - (void)donePressed:(id)sender
  269. {
  270. [self.delegate globalsViewControllerDidFinish:self];
  271. }
  272. - (void)tryExploreAddress:(NSString *)addressString safely:(BOOL)safely {
  273. NSScanner *scanner = [NSScanner scannerWithString:addressString];
  274. unsigned long long hexValue = 0;
  275. BOOL didParseAddress = [scanner scanHexLongLong:&hexValue];
  276. const void *pointerValue = (void *)hexValue;
  277. NSString *error = nil;
  278. if (didParseAddress) {
  279. if (safely && ![FLEXRuntimeUtility pointerIsValidObjcObject:pointerValue]) {
  280. error = @"The given address is unlikely to be a valid object.";
  281. }
  282. } else {
  283. error = @"Malformed address. Make sure it's not too long and starts with '0x'.";
  284. }
  285. if (!error) {
  286. id object = (__bridge id)pointerValue;
  287. FLEXObjectExplorerViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  288. [self.navigationController pushViewController:explorer animated:YES];
  289. } else {
  290. [FLEXUtility alert:@"Uh-oh" message:error from:self];
  291. [self deselectSelectedRow];
  292. }
  293. }
  294. #pragma mark - Table Data Helpers
  295. - (FLEXGlobalsTableViewControllerEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  296. {
  297. return self.entries[indexPath.row];
  298. }
  299. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  300. {
  301. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  302. return entry.entryNameFuture();
  303. }
  304. #pragma mark - Table View Data Source
  305. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  306. {
  307. return 1;
  308. }
  309. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  310. {
  311. return [self.entries count];
  312. }
  313. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  314. {
  315. static NSString *CellIdentifier = @"Cell";
  316. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  317. if (!cell) {
  318. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  319. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  320. cell.textLabel.font = [FLEXUtility defaultFontOfSize:14.0];
  321. }
  322. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  323. return cell;
  324. }
  325. #pragma mark - Table View Delegate
  326. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  327. {
  328. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  329. if (entry.viewControllerFuture) {
  330. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  331. } else {
  332. entry.rowAction(self);
  333. }
  334. }
  335. @end