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" 8928bc3baSBen Roth#import "EXKernelAppLoader.h" 989c6c39cSEric Samelson#import "EXKernelAppRecord.h" 10b05926a2SBen Roth#import "EXKernelLinkingManager.h" 110fbf6a33SBen Roth#import "EXLinkingManager.h" 120fbf6a33SBen Roth#import "EXVersions.h" 130fbf6a33SBen Roth 140fbf6a33SBen Roth#import <React/RCTBridge+Private.h> 150fbf6a33SBen Roth#import <React/RCTEventDispatcher.h> 160fbf6a33SBen Roth#import <React/RCTModuleData.h> 170fbf6a33SBen Roth#import <React/RCTUtils.h> 180fbf6a33SBen Roth 190fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN 200fbf6a33SBen Roth 210fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain"; 220fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask"; 230fbf6a33SBen RothNSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey"; 240fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey"; 250fbf6a33SBen Roth 262f2bf1c0SBen Rothconst NSUInteger kEXErrorCodeAppForbidden = 424242; 272f2bf1c0SBen Roth 2889c6c39cSEric Samelson@interface EXKernel () <EXKernelAppRegistryDelegate> 290fbf6a33SBen Roth 300fbf6a33SBen Roth@end 310fbf6a33SBen Roth 320fbf6a33SBen Roth@implementation EXKernel 330fbf6a33SBen Roth 340fbf6a33SBen Roth+ (instancetype)sharedInstance 350fbf6a33SBen Roth{ 360fbf6a33SBen Roth static EXKernel *theKernel; 370fbf6a33SBen Roth static dispatch_once_t once; 380fbf6a33SBen Roth dispatch_once(&once, ^{ 390fbf6a33SBen Roth if (!theKernel) { 400fbf6a33SBen Roth theKernel = [[EXKernel alloc] init]; 410fbf6a33SBen Roth } 420fbf6a33SBen Roth }); 430fbf6a33SBen Roth return theKernel; 440fbf6a33SBen Roth} 450fbf6a33SBen Roth 460fbf6a33SBen Roth- (instancetype)init 470fbf6a33SBen Roth{ 480fbf6a33SBen Roth if (self = [super init]) { 4989c6c39cSEric Samelson // init app registry: keep track of RN bridges we are running 5089c6c39cSEric Samelson _appRegistry = [[EXKernelAppRegistry alloc] init]; 5189c6c39cSEric Samelson _appRegistry.delegate = self; 529f8d9b9dSBen Roth 539f8d9b9dSBen Roth // init service registry: classes which manage shared resources among all bridges 54d7f51e5eSBen Roth _serviceRegistry = [[EXKernelServiceRegistry alloc] init]; 559f8d9b9dSBen Roth 560fbf6a33SBen Roth for (NSString *name in @[UIApplicationDidBecomeActiveNotification, 570fbf6a33SBen Roth UIApplicationDidEnterBackgroundNotification, 580fbf6a33SBen Roth UIApplicationDidFinishLaunchingNotification, 590fbf6a33SBen Roth UIApplicationWillResignActiveNotification, 600fbf6a33SBen Roth UIApplicationWillEnterForegroundNotification]) { 610fbf6a33SBen Roth 620fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] addObserver:self 630fbf6a33SBen Roth selector:@selector(_handleAppStateDidChange:) 640fbf6a33SBen Roth name:name 650fbf6a33SBen Roth object:nil]; 660fbf6a33SBen Roth } 672c229102SBen Roth NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion); 680fbf6a33SBen Roth } 690fbf6a33SBen Roth return self; 700fbf6a33SBen Roth} 710fbf6a33SBen Roth 720fbf6a33SBen Roth- (void)dealloc 730fbf6a33SBen Roth{ 740fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] removeObserver:self]; 750fbf6a33SBen Roth} 760fbf6a33SBen Roth 775daa5350SBen Roth#pragma mark - Misc 785daa5350SBen Roth 790fbf6a33SBen Roth+ (NSString *)deviceInstallUUID 800fbf6a33SBen Roth{ 810fbf6a33SBen Roth NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey]; 820fbf6a33SBen Roth if (!uuid) { 830fbf6a33SBen Roth uuid = [[NSUUID UUID] UUIDString]; 840fbf6a33SBen Roth [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey]; 850fbf6a33SBen Roth [[NSUserDefaults standardUserDefaults] synchronize]; 860fbf6a33SBen Roth } 870fbf6a33SBen Roth return uuid; 880fbf6a33SBen Roth} 890fbf6a33SBen Roth 9085f025b0SBen Roth- (void)logAnalyticsEvent:(NSString *)eventId forAppRecord:(EXKernelAppRecord *)appRecord 9185f025b0SBen Roth{ 9285f025b0SBen Roth if (_appRegistry.homeAppRecord && appRecord == _appRegistry.homeAppRecord) { 9385f025b0SBen Roth return; 9485f025b0SBen Roth } 9585f025b0SBen Roth NSString *validatedSdkVersion = [[EXVersions sharedInstance] availableSdkVersionForManifest:appRecord.appLoader.manifest]; 9685f025b0SBen Roth NSDictionary *props = (validatedSdkVersion) ? @{ @"SDK_VERSION": validatedSdkVersion } : @{}; 9785f025b0SBen Roth [[EXAnalytics sharedInstance] logEvent:eventId 9885f025b0SBen Roth manifestUrl:appRecord.appLoader.manifestUrl 9985f025b0SBen Roth eventProperties:props]; 10085f025b0SBen Roth} 10185f025b0SBen Roth 1029f8d9b9dSBen Roth#pragma mark - bridge registry delegate 1039f8d9b9dSBen Roth 10489c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord 1059f8d9b9dSBen Roth{ 1069f8d9b9dSBen Roth // forward to service registry 10789c6c39cSEric Samelson [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord]; 1089f8d9b9dSBen Roth} 1099f8d9b9dSBen Roth 11089c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord 1119f8d9b9dSBen Roth{ 1129f8d9b9dSBen Roth // forward to service registry 11389c6c39cSEric Samelson [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord]; 1149f8d9b9dSBen Roth} 1159f8d9b9dSBen Roth 116f5e2fbc5SBen Roth#pragma mark - Interfacing with JS 117f5e2fbc5SBen Roth 118f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app 119f5e2fbc5SBen Roth{ 120f5e2fbc5SBen Roth // fire a Linking url event on this (possibly versioned) bridge 121f5e2fbc5SBen Roth EXReactAppManager *appManager = app.appManager; 122f5e2fbc5SBen Roth id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"]; 123f5e2fbc5SBen Roth if (!linkingModule) { 124f5e2fbc5SBen Roth DDLogError(@"Could not find the Linking module to open URL (%@)", urlString); 125f5e2fbc5SBen Roth } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) { 126f5e2fbc5SBen Roth [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]]; 127f5e2fbc5SBen Roth } else { 128f5e2fbc5SBen Roth DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString); 129f5e2fbc5SBen Roth } 130e7ac6e4fSBen Roth [self _moveAppToVisible:app]; 131f5e2fbc5SBen Roth} 1320fbf6a33SBen Roth 1335daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName 1340fbf6a33SBen Roth{ 1350fbf6a33SBen Roth id destinationBridge = appManager.reactBridge; 1360fbf6a33SBen Roth 1370fbf6a33SBen Roth if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) { 1380fbf6a33SBen Roth id batchedBridge = [destinationBridge batchedBridge]; 1390fbf6a33SBen Roth id moduleData = [batchedBridge moduleDataForName:moduleName]; 1400fbf6a33SBen Roth 1410fbf6a33SBen Roth // React Native before SDK 11 didn't strip the "RCT" prefix from module names 1420fbf6a33SBen Roth if (!moduleData && ![moduleName hasPrefix:@"RCT"]) { 1430fbf6a33SBen Roth moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]]; 1440fbf6a33SBen Roth } 1450fbf6a33SBen Roth 1460fbf6a33SBen Roth if (moduleData) { 1470fbf6a33SBen Roth return [moduleData instance]; 1480fbf6a33SBen Roth } 1490fbf6a33SBen Roth } else { 150d2210950SBen Roth // bridge can be null if the record is in an error state and never created a bridge. 151d2210950SBen Roth if (destinationBridge) { 1520fbf6a33SBen Roth DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge"); 1530fbf6a33SBen Roth } 154d2210950SBen Roth } 1550fbf6a33SBen Roth return nil; 1560fbf6a33SBen Roth} 1570fbf6a33SBen Roth 158c67a7dcdSBen Roth- (void)sendNotification:(NSDictionary *)notifBody 159c67a7dcdSBen Roth toExperienceWithId:(NSString *)destinationExperienceId 160c67a7dcdSBen Roth fromBackground:(BOOL)isFromBackground 161c67a7dcdSBen Roth isRemote:(BOOL)isRemote 162c67a7dcdSBen Roth{ 163c67a7dcdSBen Roth EXKernelAppRecord *destinationApp = [_appRegistry newestRecordWithExperienceId:destinationExperienceId]; 164*d699b379SBen Roth NSDictionary *bodyWithOrigin = [self _notificationPropsWithBody:notifBody isFromBackground:isFromBackground isRemote:isRemote]; 165c67a7dcdSBen Roth if (destinationApp) { 166c67a7dcdSBen Roth // send the body to the already-open experience 167c67a7dcdSBen Roth [self _dispatchJSEvent:@"Exponent.notification" body:bodyWithOrigin toApp:destinationApp]; 168e7ac6e4fSBen Roth [self _moveAppToVisible:destinationApp]; 169c67a7dcdSBen Roth } else { 170c67a7dcdSBen Roth // no app is currently running for this experience id. 171c67a7dcdSBen Roth // if we're Expo Client, we can query Home for a past experience in the user's history, and route the notification there. 172c67a7dcdSBen Roth if (_browserController) { 173c67a7dcdSBen Roth __weak typeof(self) weakSelf = self; 174c67a7dcdSBen Roth [_browserController getHistoryUrlForExperienceId:destinationExperienceId completion:^(NSString *urlString) { 175c67a7dcdSBen Roth if (urlString) { 176c67a7dcdSBen Roth NSURL *url = [NSURL URLWithString:urlString]; 177c67a7dcdSBen Roth if (url) { 178c67a7dcdSBen Roth [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": bodyWithOrigin }]; 179c67a7dcdSBen Roth } 180c67a7dcdSBen Roth } 181c67a7dcdSBen Roth }]; 182c67a7dcdSBen Roth } 183c67a7dcdSBen Roth } 184c67a7dcdSBen Roth} 185c67a7dcdSBen Roth 1860fbf6a33SBen Roth/** 1870fbf6a33SBen Roth * If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well. 1880fbf6a33SBen Roth */ 1890fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge 1900fbf6a33SBen Roth{ 1910fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge]; 1920fbf6a33SBen Roth if ([bridge respondsToSelector:@selector(batchedBridge)]) { 1930fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]]; 1940fbf6a33SBen Roth } else if ([bridge respondsToSelector:@selector(parentBridge)]) { 1950fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]]; 1960fbf6a33SBen Roth } 1970fbf6a33SBen Roth} 1980fbf6a33SBen Roth 199c67a7dcdSBen Roth- (void)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord 200f148175aSBen Roth{ 201c67a7dcdSBen Roth [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit" 202c67a7dcdSBen Roth args:eventBody ? @[eventName, eventBody] : @[eventName]]; 2030fbf6a33SBen Roth} 2040fbf6a33SBen Roth 205*d699b379SBen Roth#pragma mark - App props 206*d699b379SBen Roth 207*d699b379SBen Roth- (NSDictionary *)initialAppPropsFromLaunchOptions:(NSDictionary *)launchOptions 208*d699b379SBen Roth{ 209*d699b379SBen Roth NSMutableDictionary *initialProps = [NSMutableDictionary dictionary]; 210*d699b379SBen Roth 211*d699b379SBen Roth NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 212*d699b379SBen Roth if (remoteNotification) { 213*d699b379SBen Roth initialProps[@"notification"] = [self _notificationPropsWithBody:remoteNotification[@"body"] isFromBackground:YES isRemote:YES]; 214*d699b379SBen Roth } 215*d699b379SBen Roth UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 216*d699b379SBen Roth if (localNotification) { 217*d699b379SBen Roth initialProps[@"notification"] = [self _notificationPropsWithBody:localNotification.userInfo[@"body"] isFromBackground:YES isRemote:NO]; 218*d699b379SBen Roth } 219*d699b379SBen Roth return initialProps; 220*d699b379SBen Roth} 221*d699b379SBen Roth 222*d699b379SBen Roth- (NSDictionary *)_notificationPropsWithBody:(NSDictionary *)notifBody isFromBackground:(BOOL)isFromBackground isRemote:(BOOL)isRemote 223*d699b379SBen Roth{ 224*d699b379SBen Roth // if the notification came from the background, in most but not all cases, this means the user acted on an iOS notification 225*d699b379SBen Roth // and caused the app to launch. 226*d699b379SBen Roth // From SO: 227*d699b379SBen Roth // > Note that "App opened from Notification" will be a false positive if the notification is sent while the user is on a different 228*d699b379SBen Roth // > screen (for example, if they pull down the status bar and then receive a notification from your app). 229*d699b379SBen Roth if (!notifBody) { 230*d699b379SBen Roth notifBody = @{}; 231*d699b379SBen Roth } 232*d699b379SBen Roth return @{ 233*d699b379SBen Roth @"origin": (isFromBackground) ? @"selected" : @"received", 234*d699b379SBen Roth @"remote": @(isRemote), 235*d699b379SBen Roth @"data": notifBody, 236*d699b379SBen Roth }; 237*d699b379SBen Roth} 238*d699b379SBen Roth 2398a7f6184SBen Roth#pragma mark - App State 2408e3c2ccdSBen Roth 24189fd6b24SBen Roth- (EXKernelAppRecord *)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps 242f5e2fbc5SBen Roth{ 243c67a7dcdSBen Roth NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps]; 244f5e2fbc5SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 245e7ac6e4fSBen Roth [self _moveAppToVisible:record]; 24689fd6b24SBen Roth return record; 247f5e2fbc5SBen Roth} 248f5e2fbc5SBen Roth 2497f6367d0SBen Roth- (void)switchTasks 2500fbf6a33SBen Roth{ 2517f6367d0SBen Roth if (!_browserController) { 2527f6367d0SBen Roth return; 2536f62abf2SBen Roth } 2546f62abf2SBen Roth 2557f6367d0SBen Roth if (_visibleApp != _appRegistry.homeAppRecord) { 2567f6367d0SBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 257edc8619eSBen Roth [_browserController toggleMenuWithCompletion:nil]; 2587f6367d0SBen Roth }]; 259b05926a2SBen Roth } else { 2607f6367d0SBen Roth EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry; 2617f6367d0SBen Roth for (NSString *recordId in appRegistry.appEnumerator) { 2627f6367d0SBen Roth EXKernelAppRecord *record = [appRegistry recordForId:recordId]; 2637f6367d0SBen Roth // foreground the first thing we find 264e7ac6e4fSBen Roth [self _moveAppToVisible:record]; 2657f6367d0SBen Roth } 2667f6367d0SBen Roth } 2677f6367d0SBen Roth} 2687f6367d0SBen Roth 269928bc3baSBen Roth- (void)reloadAppWithExperienceId:(NSString *)experienceId 270928bc3baSBen Roth{ 271928bc3baSBen Roth EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId]; 272928bc3baSBen Roth if (_browserController) { 273928bc3baSBen Roth [self createNewAppWithUrl:appRecord.appLoader.manifestUrl initialProps:nil]; 274928bc3baSBen Roth } else if (_appRegistry.standaloneAppRecord && appRecord == _appRegistry.standaloneAppRecord) { 275928bc3baSBen Roth [appRecord.viewController refresh]; 276928bc3baSBen Roth } 277928bc3baSBen Roth} 278928bc3baSBen Roth 2799777fddbSEric Samelson- (void)reloadAppFromCacheWithExperienceId:(NSString *)experienceId 2809777fddbSEric Samelson{ 2819777fddbSEric Samelson EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId]; 2829777fddbSEric Samelson [appRecord.viewController reloadFromCache]; 2839777fddbSEric Samelson} 2849777fddbSEric Samelson 285e7ac6e4fSBen Roth- (void)viewController:(__unused EXViewController *)vc didNavigateAppToVisible:(EXKernelAppRecord *)appRecord 2867f6367d0SBen Roth{ 2877f6367d0SBen Roth EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp; 2887f6367d0SBen Roth if (appRecord != appRecordPreviouslyVisible) { 2897f6367d0SBen Roth if (appRecordPreviouslyVisible) { 290e7ac6e4fSBen Roth [appRecordPreviouslyVisible.viewController appStateDidBecomeInactive]; 2917f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge]; 2927f6367d0SBen Roth id appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"]; 2930fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 2940fbf6a33SBen Roth [appStateModule setState:@"background"]; 2950fbf6a33SBen Roth } 2960fbf6a33SBen Roth } 2977f6367d0SBen Roth if (appRecord) { 298e7ac6e4fSBen Roth [appRecord.viewController appStateDidBecomeActive]; 2997f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge]; 3007f6367d0SBen Roth id appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"]; 3010fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 3020fbf6a33SBen Roth [appStateModule setState:@"active"]; 3030fbf6a33SBen Roth } 3047f6367d0SBen Roth _visibleApp = appRecord; 30585f025b0SBen Roth [[EXAnalytics sharedInstance] logAppVisibleEvent]; 3060fbf6a33SBen Roth } else { 3077f6367d0SBen Roth _visibleApp = nil; 3087f6367d0SBen Roth } 3097f6367d0SBen Roth 3107f6367d0SBen Roth if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) { 3117f6367d0SBen Roth [self _unregisterUnusedAppRecords]; 3127f6367d0SBen Roth } 3137f6367d0SBen Roth } 3147f6367d0SBen Roth} 3157f6367d0SBen Roth 3167f6367d0SBen Roth- (void)_unregisterUnusedAppRecords 3177f6367d0SBen Roth{ 3187f6367d0SBen Roth for (NSString *recordId in _appRegistry.appEnumerator) { 3197f6367d0SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 3207f6367d0SBen Roth if (record && record != _visibleApp) { 3217f6367d0SBen Roth [_appRegistry unregisterAppWithRecordId:recordId]; 3227f6367d0SBen Roth break; 3230fbf6a33SBen Roth } 3240fbf6a33SBen Roth } 325b05926a2SBen Roth} 3260fbf6a33SBen Roth 3270fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification 3280fbf6a33SBen Roth{ 3290fbf6a33SBen Roth NSString *newState; 3300fbf6a33SBen Roth 3310fbf6a33SBen Roth if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { 3320fbf6a33SBen Roth newState = @"inactive"; 3330fbf6a33SBen Roth } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) { 3340fbf6a33SBen Roth newState = @"background"; 3350fbf6a33SBen Roth } else { 3360fbf6a33SBen Roth switch (RCTSharedApplication().applicationState) { 3370fbf6a33SBen Roth case UIApplicationStateActive: 3380fbf6a33SBen Roth newState = @"active"; 3390fbf6a33SBen Roth break; 3400fbf6a33SBen Roth case UIApplicationStateBackground: { 3410fbf6a33SBen Roth newState = @"background"; 3420fbf6a33SBen Roth break; 3430fbf6a33SBen Roth } 3440fbf6a33SBen Roth default: { 3450fbf6a33SBen Roth newState = @"unknown"; 3460fbf6a33SBen Roth break; 3470fbf6a33SBen Roth } 3480fbf6a33SBen Roth } 3490fbf6a33SBen Roth } 3500fbf6a33SBen Roth 3517f6367d0SBen Roth if (_visibleApp) { 3527f6367d0SBen Roth EXReactAppManager *appManager = _visibleApp.appManager; 3535daa5350SBen Roth id appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"]; 3540fbf6a33SBen Roth NSString *lastKnownState; 3550fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(lastKnownState)]) { 3560fbf6a33SBen Roth lastKnownState = [appStateModule lastKnownState]; 3570fbf6a33SBen Roth } 3580fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 3590fbf6a33SBen Roth [appStateModule setState:newState]; 3600fbf6a33SBen Roth } 3610fbf6a33SBen Roth if (!lastKnownState || ![newState isEqualToString:lastKnownState]) { 3620fbf6a33SBen Roth if ([newState isEqualToString:@"active"]) { 363e7ac6e4fSBen Roth [_visibleApp.viewController appStateDidBecomeActive]; 3647f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge]; 3650fbf6a33SBen Roth } else if ([newState isEqualToString:@"background"]) { 366e7ac6e4fSBen Roth [_visibleApp.viewController appStateDidBecomeInactive]; 3677f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge]; 3680fbf6a33SBen Roth } 3690fbf6a33SBen Roth } 3700fbf6a33SBen Roth } 3710fbf6a33SBen Roth} 3720fbf6a33SBen Roth 373e7ac6e4fSBen Roth- (void)_moveAppToVisible:(EXKernelAppRecord *)appRecord 374e7ac6e4fSBen Roth{ 375e7ac6e4fSBen Roth if (_browserController) { 376e7ac6e4fSBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 377e7ac6e4fSBen Roth [_browserController moveAppToVisible:appRecord]; 378e7ac6e4fSBen Roth }]; 379e7ac6e4fSBen Roth } 380e7ac6e4fSBen Roth} 381e7ac6e4fSBen Roth 3820fbf6a33SBen Roth@end 3830fbf6a33SBen Roth 3840fbf6a33SBen RothNS_ASSUME_NONNULL_END 385