10fbf6a33SBen Roth// Copyright 2015-present 650 Industries. All rights reserved. 20fbf6a33SBen Roth 30fbf6a33SBen Roth#import "EXAnalytics.h" 40fbf6a33SBen Roth#import "EXAppState.h" 50187d3b3SBen Roth#import "EXAppViewController.h" 6e791dcdeSBen Roth#import "EXBuildConstants.h" 70fbf6a33SBen Roth#import "EXKernel.h" 8*7c3ed0c0SDouglas Lowder#import "EXAbstractLoader.h" 989c6c39cSEric Samelson#import "EXKernelAppRecord.h" 10b05926a2SBen Roth#import "EXKernelLinkingManager.h" 110fbf6a33SBen Roth#import "EXLinkingManager.h" 120fbf6a33SBen Roth#import "EXVersions.h" 1324a0cefbSTomasz Sapeta#import "EXHomeModule.h" 140fbf6a33SBen Roth 158810a243SEric Samelson#import <EXConstants/EXConstantsService.h> 160fbf6a33SBen Roth#import <React/RCTBridge+Private.h> 170fbf6a33SBen Roth#import <React/RCTEventDispatcher.h> 180fbf6a33SBen Roth#import <React/RCTModuleData.h> 190fbf6a33SBen Roth#import <React/RCTUtils.h> 200fbf6a33SBen Roth 2105aa4e6cSTomasz Sapeta#ifndef EX_DETACHED 2205aa4e6cSTomasz Sapeta// Kernel is DevMenu's delegate only in non-detached builds. 2305aa4e6cSTomasz Sapeta#import "EXDevMenuManager.h" 2405aa4e6cSTomasz Sapeta#import "EXDevMenuDelegateProtocol.h" 2505aa4e6cSTomasz Sapeta 2605aa4e6cSTomasz Sapeta@interface EXKernel () <EXDevMenuDelegateProtocol> 2705aa4e6cSTomasz Sapeta@end 2805aa4e6cSTomasz Sapeta#endif 2905aa4e6cSTomasz Sapeta 300fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN 310fbf6a33SBen Roth 320fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain"; 330fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask"; 340fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey"; 35d0248f72SBrent VatneNSString * const kEXReloadActiveAppRequest = @"EXReloadActiveAppRequest"; 360fbf6a33SBen Roth 3789c6c39cSEric Samelson@interface EXKernel () <EXKernelAppRegistryDelegate> 380fbf6a33SBen Roth 390fbf6a33SBen Roth@end 400fbf6a33SBen Roth 4162cc3d03STomasz Sapeta// Protocol that should be implemented by all versions of EXAppState class. 4262cc3d03STomasz Sapeta@protocol EXAppStateProtocol 4362cc3d03STomasz Sapeta 4462cc3d03STomasz Sapeta@property (nonatomic, strong, readonly) NSString *lastKnownState; 4562cc3d03STomasz Sapeta 4662cc3d03STomasz Sapeta- (void)setState:(NSString *)state; 4762cc3d03STomasz Sapeta 4862cc3d03STomasz Sapeta@end 4962cc3d03STomasz Sapeta 500fbf6a33SBen Roth@implementation EXKernel 510fbf6a33SBen Roth 520fbf6a33SBen Roth+ (instancetype)sharedInstance 530fbf6a33SBen Roth{ 540fbf6a33SBen Roth static EXKernel *theKernel; 550fbf6a33SBen Roth static dispatch_once_t once; 560fbf6a33SBen Roth dispatch_once(&once, ^{ 570fbf6a33SBen Roth if (!theKernel) { 580fbf6a33SBen Roth theKernel = [[EXKernel alloc] init]; 590fbf6a33SBen Roth } 600fbf6a33SBen Roth }); 610fbf6a33SBen Roth return theKernel; 620fbf6a33SBen Roth} 630fbf6a33SBen Roth 640fbf6a33SBen Roth- (instancetype)init 650fbf6a33SBen Roth{ 660fbf6a33SBen Roth if (self = [super init]) { 6789c6c39cSEric Samelson // init app registry: keep track of RN bridges we are running 6889c6c39cSEric Samelson _appRegistry = [[EXKernelAppRegistry alloc] init]; 6989c6c39cSEric Samelson _appRegistry.delegate = self; 709f8d9b9dSBen Roth 719f8d9b9dSBen Roth // init service registry: classes which manage shared resources among all bridges 72d7f51e5eSBen Roth _serviceRegistry = [[EXKernelServiceRegistry alloc] init]; 739f8d9b9dSBen Roth 7405aa4e6cSTomasz Sapeta#ifndef EX_DETACHED 75f67462bcSTomasz Sapeta // Set the delegate of dev menu manager. Maybe it should be a separate class? Will see later once the delegate protocol gets too big. 76f67462bcSTomasz Sapeta [[EXDevMenuManager sharedInstance] setDelegate:self]; 7705aa4e6cSTomasz Sapeta#endif 78f67462bcSTomasz Sapeta 79d0248f72SBrent Vatne // register for notifications to request reloading the visible app 80d0248f72SBrent Vatne [[NSNotificationCenter defaultCenter] addObserver:self 81d0248f72SBrent Vatne selector:@selector(_handleRequestReloadVisibleApp:) 82d0248f72SBrent Vatne name:kEXReloadActiveAppRequest 83d0248f72SBrent Vatne object:nil]; 84d0248f72SBrent Vatne 850fbf6a33SBen Roth for (NSString *name in @[UIApplicationDidBecomeActiveNotification, 860fbf6a33SBen Roth UIApplicationDidEnterBackgroundNotification, 870fbf6a33SBen Roth UIApplicationDidFinishLaunchingNotification, 880fbf6a33SBen Roth UIApplicationWillResignActiveNotification, 890fbf6a33SBen Roth UIApplicationWillEnterForegroundNotification]) { 900fbf6a33SBen Roth 910fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] addObserver:self 920fbf6a33SBen Roth selector:@selector(_handleAppStateDidChange:) 930fbf6a33SBen Roth name:name 940fbf6a33SBen Roth object:nil]; 950fbf6a33SBen Roth } 962c229102SBen Roth NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion); 970fbf6a33SBen Roth } 980fbf6a33SBen Roth return self; 990fbf6a33SBen Roth} 1000fbf6a33SBen Roth 1010fbf6a33SBen Roth- (void)dealloc 1020fbf6a33SBen Roth{ 1030fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] removeObserver:self]; 1040fbf6a33SBen Roth} 1050fbf6a33SBen Roth 1065daa5350SBen Roth#pragma mark - Misc 1075daa5350SBen Roth 10885f025b0SBen Roth- (void)logAnalyticsEvent:(NSString *)eventId forAppRecord:(EXKernelAppRecord *)appRecord 10985f025b0SBen Roth{ 11085f025b0SBen Roth if (_appRegistry.homeAppRecord && appRecord == _appRegistry.homeAppRecord) { 11185f025b0SBen Roth return; 11285f025b0SBen Roth } 11385f025b0SBen Roth NSString *validatedSdkVersion = [[EXVersions sharedInstance] availableSdkVersionForManifest:appRecord.appLoader.manifest]; 11485f025b0SBen Roth NSDictionary *props = (validatedSdkVersion) ? @{ @"SDK_VERSION": validatedSdkVersion } : @{}; 11585f025b0SBen Roth [[EXAnalytics sharedInstance] logEvent:eventId 11685f025b0SBen Roth manifestUrl:appRecord.appLoader.manifestUrl 11785f025b0SBen Roth eventProperties:props]; 11885f025b0SBen Roth} 11985f025b0SBen Roth 1209f8d9b9dSBen Roth#pragma mark - bridge registry delegate 1219f8d9b9dSBen Roth 12289c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord 1239f8d9b9dSBen Roth{ 1249f8d9b9dSBen Roth // forward to service registry 12589c6c39cSEric Samelson [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord]; 1269f8d9b9dSBen Roth} 1279f8d9b9dSBen Roth 12889c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord 1299f8d9b9dSBen Roth{ 1309f8d9b9dSBen Roth // forward to service registry 13189c6c39cSEric Samelson [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord]; 1329f8d9b9dSBen Roth} 1339f8d9b9dSBen Roth 134f5e2fbc5SBen Roth#pragma mark - Interfacing with JS 135f5e2fbc5SBen Roth 136f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app 137f5e2fbc5SBen Roth{ 138f5e2fbc5SBen Roth // fire a Linking url event on this (possibly versioned) bridge 139f5e2fbc5SBen Roth EXReactAppManager *appManager = app.appManager; 140f5e2fbc5SBen Roth id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"]; 141f5e2fbc5SBen Roth if (!linkingModule) { 142f5e2fbc5SBen Roth DDLogError(@"Could not find the Linking module to open URL (%@)", urlString); 143f5e2fbc5SBen Roth } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) { 144f5e2fbc5SBen Roth [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]]; 145f5e2fbc5SBen Roth } else { 146f5e2fbc5SBen Roth DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString); 147f5e2fbc5SBen Roth } 148e7ac6e4fSBen Roth [self _moveAppToVisible:app]; 149f5e2fbc5SBen Roth} 1500fbf6a33SBen Roth 1515daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName 1520fbf6a33SBen Roth{ 1530fbf6a33SBen Roth id destinationBridge = appManager.reactBridge; 1540fbf6a33SBen Roth 1550fbf6a33SBen Roth if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) { 1560fbf6a33SBen Roth id batchedBridge = [destinationBridge batchedBridge]; 1570fbf6a33SBen Roth id moduleData = [batchedBridge moduleDataForName:moduleName]; 1580fbf6a33SBen Roth 1590fbf6a33SBen Roth if (moduleData) { 1600fbf6a33SBen Roth return [moduleData instance]; 1610fbf6a33SBen Roth } 1620fbf6a33SBen Roth } else { 163d2210950SBen Roth // bridge can be null if the record is in an error state and never created a bridge. 164d2210950SBen Roth if (destinationBridge) { 1650fbf6a33SBen Roth DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge"); 1660fbf6a33SBen Roth } 167d2210950SBen Roth } 1680fbf6a33SBen Roth return nil; 1690fbf6a33SBen Roth} 1700fbf6a33SBen Roth 1717100de8cSSzymon20000- (BOOL)sendNotification:(EXPendingNotification *)notification 172c67a7dcdSBen Roth{ 173167fd314SWill Schurman EXKernelAppRecord *destinationApp = [_appRegistry standaloneAppRecord] ?: [_appRegistry newestRecordWithScopeKey:notification.scopeKey]; 17427150200STomasz Sapeta 17527150200STomasz Sapeta // This allows home app record to receive notification events as well. 176167fd314SWill Schurman if (!destinationApp && [_appRegistry.homeAppRecord.scopeKey isEqualToString:notification.scopeKey]) { 17727150200STomasz Sapeta destinationApp = _appRegistry.homeAppRecord; 17827150200STomasz Sapeta } 17927150200STomasz Sapeta 180c67a7dcdSBen Roth if (destinationApp) { 181c67a7dcdSBen Roth // send the body to the already-open experience 1827e7b37d3SEric Samelson BOOL success = [self _dispatchJSEvent:@"Exponent.notification" body:notification.properties toApp:destinationApp]; 183e7ac6e4fSBen Roth [self _moveAppToVisible:destinationApp]; 1847e7b37d3SEric Samelson return success; 185c67a7dcdSBen Roth } else { 186c67a7dcdSBen Roth // no app is currently running for this experience id. 187dc898499SJames Ide // if we're Expo Go, we can query Home for a past experience in the user's history, and route the notification there. 188c67a7dcdSBen Roth if (_browserController) { 189c67a7dcdSBen Roth __weak typeof(self) weakSelf = self; 190167fd314SWill Schurman [_browserController getHistoryUrlForScopeKey:notification.scopeKey completion:^(NSString *urlString) { 191c67a7dcdSBen Roth if (urlString) { 192c67a7dcdSBen Roth NSURL *url = [NSURL URLWithString:urlString]; 193c67a7dcdSBen Roth if (url) { 1947100de8cSSzymon20000 [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": notification.properties }]; 195c67a7dcdSBen Roth } 196c67a7dcdSBen Roth } 197c67a7dcdSBen Roth }]; 1987100de8cSSzymon20000 // If we're here, there's no active app in appRegistry matching notification.experienceId 199dc898499SJames Ide // and we are in Expo Go, since _browserController is not nil. 2007100de8cSSzymon20000 // If so, we can return YES (meaning "notification has been successfully dispatched") 2017100de8cSSzymon20000 // because we pass the notification as initialProps in completion handler 2027100de8cSSzymon20000 // of getHistoryUrlForExperienceId:. If urlString passed to completion handler is empty, 2037100de8cSSzymon20000 // the notification is forgotten (this is the expected behavior). 2047100de8cSSzymon20000 return YES; 205c67a7dcdSBen Roth } 206c67a7dcdSBen Roth } 2077100de8cSSzymon20000 2087100de8cSSzymon20000 return NO; 209c67a7dcdSBen Roth} 210c67a7dcdSBen Roth 2110fbf6a33SBen Roth/** 2120fbf6a33SBen Roth * If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well. 2130fbf6a33SBen Roth */ 2140fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge 2150fbf6a33SBen Roth{ 2160fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge]; 2170fbf6a33SBen Roth if ([bridge respondsToSelector:@selector(batchedBridge)]) { 2180fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]]; 2190fbf6a33SBen Roth } else if ([bridge respondsToSelector:@selector(parentBridge)]) { 2200fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]]; 2210fbf6a33SBen Roth } 2220fbf6a33SBen Roth} 2230fbf6a33SBen Roth 2247e7b37d3SEric Samelson- (BOOL)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord 225f148175aSBen Roth{ 2267e7b37d3SEric Samelson if (!appRecord.appManager.reactBridge) { 2277e7b37d3SEric Samelson return NO; 2287e7b37d3SEric Samelson } 229c67a7dcdSBen Roth [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit" 230c67a7dcdSBen Roth args:eventBody ? @[eventName, eventBody] : @[eventName]]; 2317e7b37d3SEric Samelson return YES; 2320fbf6a33SBen Roth} 2330fbf6a33SBen Roth 234d699b379SBen Roth#pragma mark - App props 235d699b379SBen Roth 2367100de8cSSzymon20000- (nullable NSDictionary *)initialAppPropsFromLaunchOptions:(NSDictionary *)launchOptions 237d699b379SBen Roth{ 2387100de8cSSzymon20000 return nil; 239d699b379SBen Roth} 240d699b379SBen Roth 2418a7f6184SBen Roth#pragma mark - App State 2428e3c2ccdSBen Roth 24389fd6b24SBen Roth- (EXKernelAppRecord *)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps 244f5e2fbc5SBen Roth{ 245c67a7dcdSBen Roth NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps]; 246f5e2fbc5SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 247e7ac6e4fSBen Roth [self _moveAppToVisible:record]; 24889fd6b24SBen Roth return record; 249f5e2fbc5SBen Roth} 250f5e2fbc5SBen Roth 251d0248f72SBrent Vatne 252b333b418SBrent Vatne- (void)reloadVisibleApp 253b333b418SBrent Vatne{ 254b333b418SBrent Vatne if (_browserController) { 255b333b418SBrent Vatne [EXUtil performSynchronouslyOnMainThread:^{ 256b333b418SBrent Vatne [self->_browserController reloadVisibleApp]; 257b333b418SBrent Vatne }]; 258b333b418SBrent Vatne } 259b333b418SBrent Vatne} 260b333b418SBrent Vatne 2617f6367d0SBen Roth- (void)switchTasks 2620fbf6a33SBen Roth{ 2637f6367d0SBen Roth if (!_browserController) { 2647f6367d0SBen Roth return; 2656f62abf2SBen Roth } 2666f62abf2SBen Roth 2677f6367d0SBen Roth if (_visibleApp != _appRegistry.homeAppRecord) { 26805aa4e6cSTomasz Sapeta#ifndef EX_DETACHED // Just to compile without access to EXDevMenuManager, we wouldn't get here either way because browser controller is unset in this case. 2697f6367d0SBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 270f67462bcSTomasz Sapeta [[EXDevMenuManager sharedInstance] toggle]; 2717f6367d0SBen Roth }]; 27205aa4e6cSTomasz Sapeta#endif 273b05926a2SBen Roth } else { 2747f6367d0SBen Roth EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry; 2757f6367d0SBen Roth for (NSString *recordId in appRegistry.appEnumerator) { 2767f6367d0SBen Roth EXKernelAppRecord *record = [appRegistry recordForId:recordId]; 2777f6367d0SBen Roth // foreground the first thing we find 278e7ac6e4fSBen Roth [self _moveAppToVisible:record]; 2797f6367d0SBen Roth } 2807f6367d0SBen Roth } 2817f6367d0SBen Roth} 2827f6367d0SBen Roth 283167fd314SWill Schurman- (void)reloadAppWithScopeKey:(NSString *)scopeKey 284928bc3baSBen Roth{ 285167fd314SWill Schurman EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithScopeKey:scopeKey]; 286928bc3baSBen Roth if (_browserController) { 287928bc3baSBen Roth [self createNewAppWithUrl:appRecord.appLoader.manifestUrl initialProps:nil]; 288928bc3baSBen Roth } else if (_appRegistry.standaloneAppRecord && appRecord == _appRegistry.standaloneAppRecord) { 289928bc3baSBen Roth [appRecord.viewController refresh]; 290928bc3baSBen Roth } 291928bc3baSBen Roth} 292928bc3baSBen Roth 293167fd314SWill Schurman- (void)reloadAppFromCacheWithScopeKey:(NSString *)scopeKey 2949777fddbSEric Samelson{ 295167fd314SWill Schurman EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithScopeKey:scopeKey]; 2969777fddbSEric Samelson [appRecord.viewController reloadFromCache]; 2979777fddbSEric Samelson} 2989777fddbSEric Samelson 299e7ac6e4fSBen Roth- (void)viewController:(__unused EXViewController *)vc didNavigateAppToVisible:(EXKernelAppRecord *)appRecord 3007f6367d0SBen Roth{ 3017f6367d0SBen Roth EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp; 3027f6367d0SBen Roth if (appRecord != appRecordPreviouslyVisible) { 3037f6367d0SBen Roth if (appRecordPreviouslyVisible) { 304e7ac6e4fSBen Roth [appRecordPreviouslyVisible.viewController appStateDidBecomeInactive]; 3057f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge]; 30662cc3d03STomasz Sapeta id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"]; 30762cc3d03STomasz Sapeta if (appStateModule != nil) { 3080fbf6a33SBen Roth [appStateModule setState:@"background"]; 3090fbf6a33SBen Roth } 3100fbf6a33SBen Roth } 3117f6367d0SBen Roth if (appRecord) { 312e7ac6e4fSBen Roth [appRecord.viewController appStateDidBecomeActive]; 3137f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge]; 31462cc3d03STomasz Sapeta id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"]; 31562cc3d03STomasz Sapeta if (appStateModule != nil) { 3160fbf6a33SBen Roth [appStateModule setState:@"active"]; 3170fbf6a33SBen Roth } 3187f6367d0SBen Roth _visibleApp = appRecord; 31985f025b0SBen Roth [[EXAnalytics sharedInstance] logAppVisibleEvent]; 3200fbf6a33SBen Roth } else { 3217f6367d0SBen Roth _visibleApp = nil; 3227f6367d0SBen Roth } 3237f6367d0SBen Roth 3247f6367d0SBen Roth if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) { 3257f6367d0SBen Roth [self _unregisterUnusedAppRecords]; 3267f6367d0SBen Roth } 3277f6367d0SBen Roth } 3287f6367d0SBen Roth} 3297f6367d0SBen Roth 3307f6367d0SBen Roth- (void)_unregisterUnusedAppRecords 3317f6367d0SBen Roth{ 3327f6367d0SBen Roth for (NSString *recordId in _appRegistry.appEnumerator) { 3337f6367d0SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 3347f6367d0SBen Roth if (record && record != _visibleApp) { 3357f6367d0SBen Roth [_appRegistry unregisterAppWithRecordId:recordId]; 3367f6367d0SBen Roth break; 3370fbf6a33SBen Roth } 3380fbf6a33SBen Roth } 339b05926a2SBen Roth} 3400fbf6a33SBen Roth 3410fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification 3420fbf6a33SBen Roth{ 3430fbf6a33SBen Roth NSString *newState; 3440fbf6a33SBen Roth 3450fbf6a33SBen Roth if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { 3460fbf6a33SBen Roth newState = @"inactive"; 3470fbf6a33SBen Roth } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) { 3480fbf6a33SBen Roth newState = @"background"; 3490fbf6a33SBen Roth } else { 3500fbf6a33SBen Roth switch (RCTSharedApplication().applicationState) { 3510fbf6a33SBen Roth case UIApplicationStateActive: 3520fbf6a33SBen Roth newState = @"active"; 3530fbf6a33SBen Roth break; 3540fbf6a33SBen Roth case UIApplicationStateBackground: { 3550fbf6a33SBen Roth newState = @"background"; 3560fbf6a33SBen Roth break; 3570fbf6a33SBen Roth } 3580fbf6a33SBen Roth default: { 3590fbf6a33SBen Roth newState = @"unknown"; 3600fbf6a33SBen Roth break; 3610fbf6a33SBen Roth } 3620fbf6a33SBen Roth } 3630fbf6a33SBen Roth } 3640fbf6a33SBen Roth 3657f6367d0SBen Roth if (_visibleApp) { 3667f6367d0SBen Roth EXReactAppManager *appManager = _visibleApp.appManager; 36762cc3d03STomasz Sapeta id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"]; 3680fbf6a33SBen Roth NSString *lastKnownState; 36962cc3d03STomasz Sapeta if (appStateModule != nil) { 3700fbf6a33SBen Roth lastKnownState = [appStateModule lastKnownState]; 3710fbf6a33SBen Roth [appStateModule setState:newState]; 3720fbf6a33SBen Roth } 3730fbf6a33SBen Roth if (!lastKnownState || ![newState isEqualToString:lastKnownState]) { 3740fbf6a33SBen Roth if ([newState isEqualToString:@"active"]) { 375e7ac6e4fSBen Roth [_visibleApp.viewController appStateDidBecomeActive]; 3767f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge]; 3770fbf6a33SBen Roth } else if ([newState isEqualToString:@"background"]) { 378e7ac6e4fSBen Roth [_visibleApp.viewController appStateDidBecomeInactive]; 3797f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge]; 3800fbf6a33SBen Roth } 3810fbf6a33SBen Roth } 3820fbf6a33SBen Roth } 3830fbf6a33SBen Roth} 3840fbf6a33SBen Roth 385d0248f72SBrent Vatne 386d0248f72SBrent Vatne- (void)_handleRequestReloadVisibleApp:(NSNotification *)notification 387d0248f72SBrent Vatne{ 388d0248f72SBrent Vatne [self reloadVisibleApp]; 389d0248f72SBrent Vatne} 390d0248f72SBrent Vatne 391e7ac6e4fSBen Roth- (void)_moveAppToVisible:(EXKernelAppRecord *)appRecord 392e7ac6e4fSBen Roth{ 393e7ac6e4fSBen Roth if (_browserController) { 394e7ac6e4fSBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 395d718b4b8SBen Roth [self->_browserController moveAppToVisible:appRecord]; 396e7ac6e4fSBen Roth }]; 397e7ac6e4fSBen Roth } 398e7ac6e4fSBen Roth} 399e7ac6e4fSBen Roth 40005aa4e6cSTomasz Sapeta#ifndef EX_DETACHED 401f67462bcSTomasz Sapeta#pragma mark - EXDevMenuDelegateProtocol 402f67462bcSTomasz Sapeta 403f67462bcSTomasz Sapeta- (RCTBridge *)mainBridgeForDevMenuManager:(EXDevMenuManager *)manager 404f67462bcSTomasz Sapeta{ 405f67462bcSTomasz Sapeta return _appRegistry.homeAppRecord.appManager.reactBridge; 406f67462bcSTomasz Sapeta} 407f67462bcSTomasz Sapeta 408f67462bcSTomasz Sapeta- (nullable RCTBridge *)appBridgeForDevMenuManager:(EXDevMenuManager *)manager 409f67462bcSTomasz Sapeta{ 410f67462bcSTomasz Sapeta if (_visibleApp == _appRegistry.homeAppRecord) { 411f67462bcSTomasz Sapeta return nil; 412f67462bcSTomasz Sapeta } 413f67462bcSTomasz Sapeta return _visibleApp.appManager.reactBridge; 414f67462bcSTomasz Sapeta} 415f67462bcSTomasz Sapeta 416f67462bcSTomasz Sapeta- (BOOL)devMenuManager:(EXDevMenuManager *)manager canChangeVisibility:(BOOL)visibility 417f67462bcSTomasz Sapeta{ 418f67462bcSTomasz Sapeta return !visibility || _visibleApp != _appRegistry.homeAppRecord; 419f67462bcSTomasz Sapeta} 420f67462bcSTomasz Sapeta 42105aa4e6cSTomasz Sapeta#endif 42205aa4e6cSTomasz Sapeta 4230fbf6a33SBen Roth@end 4240fbf6a33SBen Roth 4250fbf6a33SBen RothNS_ASSUME_NONNULL_END 426