FLEXGlobalsTableViewController.m 16 KB

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