xref: /expo/ios/Exponent/Kernel/Core/EXKernel.m (revision b05926a2)
10fbf6a33SBen Roth// Copyright 2015-present 650 Industries. All rights reserved.
20fbf6a33SBen Roth
30fbf6a33SBen Roth#import "EXAnalytics.h"
40fbf6a33SBen Roth#import "EXAppState.h"
50fbf6a33SBen Roth#import "EXFrame.h"
60fbf6a33SBen Roth#import "EXFrameReactAppManager.h"
70fbf6a33SBen Roth#import "EXKernel.h"
80fbf6a33SBen Roth#import "EXKernelBridgeRecord.h"
90fbf6a33SBen Roth#import "EXKernelModule.h"
10*b05926a2SBen Roth#import "EXKernelLinkingManager.h"
110fbf6a33SBen Roth#import "EXLinkingManager.h"
120fbf6a33SBen Roth#import "EXVersions.h"
130fbf6a33SBen Roth#import "EXViewController.h"
140fbf6a33SBen Roth
150fbf6a33SBen Roth#import <React/RCTBridge+Private.h>
160fbf6a33SBen Roth#import <React/RCTEventDispatcher.h>
170fbf6a33SBen Roth#import <React/RCTModuleData.h>
180fbf6a33SBen Roth#import <React/RCTUtils.h>
190fbf6a33SBen Roth
200fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN
210fbf6a33SBen Roth
220fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain";
230fbf6a33SBen RothNSNotificationName kEXKernelJSIsLoadedNotification = @"EXKernelJSIsLoadedNotification";
240fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask";
250fbf6a33SBen RothNSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey";
260fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey";
270fbf6a33SBen RothNSString * const EXKernelDisableNuxDefaultsKey = @"EXKernelDisableNuxDefaultsKey";
280fbf6a33SBen Roth
299f8d9b9dSBen Roth@interface EXKernel () <EXKernelBridgeRegistryDelegate>
300fbf6a33SBen Roth
310fbf6a33SBen Roth@property (nonatomic, weak) EXViewController *vcExponentRoot;
320fbf6a33SBen Roth
330fbf6a33SBen Roth@end
340fbf6a33SBen Roth
350fbf6a33SBen Roth@implementation EXKernel
360fbf6a33SBen Roth
370fbf6a33SBen Roth+ (instancetype)sharedInstance
380fbf6a33SBen Roth{
390fbf6a33SBen Roth  static EXKernel *theKernel;
400fbf6a33SBen Roth  static dispatch_once_t once;
410fbf6a33SBen Roth  dispatch_once(&once, ^{
420fbf6a33SBen Roth    if (!theKernel) {
430fbf6a33SBen Roth      theKernel = [[EXKernel alloc] init];
440fbf6a33SBen Roth    }
450fbf6a33SBen Roth  });
460fbf6a33SBen Roth  return theKernel;
470fbf6a33SBen Roth}
480fbf6a33SBen Roth
490fbf6a33SBen Roth+ (BOOL)isDevKernel
500fbf6a33SBen Roth{
510fbf6a33SBen Roth  // if we're in detached state (i.e. ExponentView) then never expect local kernel
520fbf6a33SBen Roth  BOOL isDetachedKernel = ([[EXVersions sharedInstance].versions objectForKey:@"detachedNativeVersions"] != nil);
530fbf6a33SBen Roth  if (isDetachedKernel) {
540fbf6a33SBen Roth    return NO;
550fbf6a33SBen Roth  }
560fbf6a33SBen Roth
570fbf6a33SBen Roth  // otherwise, expect local kernel when we are attached to xcode
580fbf6a33SBen Roth#if DEBUG
590fbf6a33SBen Roth  return YES;
600fbf6a33SBen Roth#endif
610fbf6a33SBen Roth  return NO;
620fbf6a33SBen Roth}
630fbf6a33SBen Roth
640fbf6a33SBen Roth- (instancetype)init
650fbf6a33SBen Roth{
660fbf6a33SBen Roth  if (self = [super init]) {
679f8d9b9dSBen Roth    // init bridge registry: keep track of RN bridges we are running
680fbf6a33SBen Roth    _bridgeRegistry = [[EXKernelBridgeRegistry alloc] init];
699f8d9b9dSBen Roth    _bridgeRegistry.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
740fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_onKernelJSLoaded) name:kEXKernelJSIsLoadedNotification object:nil];
750fbf6a33SBen Roth    for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
760fbf6a33SBen Roth                             UIApplicationDidEnterBackgroundNotification,
770fbf6a33SBen Roth                             UIApplicationDidFinishLaunchingNotification,
780fbf6a33SBen Roth                             UIApplicationWillResignActiveNotification,
790fbf6a33SBen Roth                             UIApplicationWillEnterForegroundNotification]) {
800fbf6a33SBen Roth
810fbf6a33SBen Roth      [[NSNotificationCenter defaultCenter] addObserver:self
820fbf6a33SBen Roth                                               selector:@selector(_handleAppStateDidChange:)
830fbf6a33SBen Roth                                                   name:name
840fbf6a33SBen Roth                                                 object:nil];
850fbf6a33SBen Roth    }
860fbf6a33SBen Roth    NSLog(@"Expo iOS Client Version %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"EXClientVersion"]);
870fbf6a33SBen Roth  }
880fbf6a33SBen Roth  return self;
890fbf6a33SBen Roth}
900fbf6a33SBen Roth
910fbf6a33SBen Roth- (void)dealloc
920fbf6a33SBen Roth{
930fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] removeObserver:self];
940fbf6a33SBen Roth}
950fbf6a33SBen Roth
960fbf6a33SBen Roth- (void)registerRootExponentViewController:(EXViewController *)exponentViewController
970fbf6a33SBen Roth{
980fbf6a33SBen Roth  _vcExponentRoot = exponentViewController;
990fbf6a33SBen Roth}
1000fbf6a33SBen Roth
1010fbf6a33SBen Roth- (EXViewController *)rootViewController
1020fbf6a33SBen Roth{
1030fbf6a33SBen Roth  return _vcExponentRoot;
1040fbf6a33SBen Roth}
1050fbf6a33SBen Roth
1060fbf6a33SBen Roth- (void)_onKernelJSLoaded
1070fbf6a33SBen Roth{
1080fbf6a33SBen Roth  // used by appetize: optionally disable nux
1090fbf6a33SBen Roth  BOOL disableNuxDefaultsValue = [[NSUserDefaults standardUserDefaults] boolForKey:EXKernelDisableNuxDefaultsKey];
1100fbf6a33SBen Roth  if (disableNuxDefaultsValue) {
1110fbf6a33SBen Roth    [self dispatchKernelJSEvent:@"resetNuxState" body:@{ @"isNuxCompleted": @YES } onSuccess:nil onFailure:nil];
1120fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] removeObjectForKey:EXKernelDisableNuxDefaultsKey];
1130fbf6a33SBen Roth  }
1140fbf6a33SBen Roth}
1150fbf6a33SBen Roth
1165daa5350SBen Roth#pragma mark - Misc
1175daa5350SBen Roth
1185daa5350SBen Roth- (void)openUrl:(NSString *)url onAppManager:(EXReactAppManager *)appManager
1195daa5350SBen Roth{
1205daa5350SBen Roth  // fire a Linking url event on this (possibly versioned) bridge
1215daa5350SBen Roth  id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"];
1225daa5350SBen Roth  if (!linkingModule) {
1235daa5350SBen Roth    DDLogError(@"Could not find the Linking module to open URL (%@)", url);
1245daa5350SBen Roth  } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) {
1255daa5350SBen Roth    [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:url]];
1265daa5350SBen Roth  } else {
1275daa5350SBen Roth    DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", url);
1285daa5350SBen Roth  }
1295daa5350SBen Roth  [self _moveAppManagerToForeground:appManager];
1305daa5350SBen Roth}
1310fbf6a33SBen Roth
1320fbf6a33SBen Roth+ (NSString *)deviceInstallUUID
1330fbf6a33SBen Roth{
1340fbf6a33SBen Roth  NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
1350fbf6a33SBen Roth  if (!uuid) {
1360fbf6a33SBen Roth    uuid = [[NSUUID UUID] UUIDString];
1370fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
1380fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] synchronize];
1390fbf6a33SBen Roth  }
1400fbf6a33SBen Roth  return uuid;
1410fbf6a33SBen Roth}
1420fbf6a33SBen Roth
1439f8d9b9dSBen Roth#pragma mark - bridge registry delegate
1449f8d9b9dSBen Roth
1459f8d9b9dSBen Roth- (void)bridgeRegistry:(EXKernelBridgeRegistry *)registry didRegisterBridgeRecord:(EXKernelBridgeRecord *)bridgeRecord
1469f8d9b9dSBen Roth{
1479f8d9b9dSBen Roth  // forward to service registry
1489f8d9b9dSBen Roth  [_serviceRegistry bridgeRegistry:registry didRegisterBridgeRecord:bridgeRecord];
1499f8d9b9dSBen Roth}
1509f8d9b9dSBen Roth
1519f8d9b9dSBen Roth- (void)bridgeRegistry:(EXKernelBridgeRegistry *)registry willUnregisterBridgeRecord:(EXKernelBridgeRecord *)bridgeRecord
1529f8d9b9dSBen Roth{
1539f8d9b9dSBen Roth  // forward to service registry
1549f8d9b9dSBen Roth  [_serviceRegistry bridgeRegistry:registry willUnregisterBridgeRecord:bridgeRecord];
1559f8d9b9dSBen Roth}
1569f8d9b9dSBen Roth
1578a7f6184SBen Roth#pragma mark - interfacing with app managers
1580fbf6a33SBen Roth
1590fbf6a33SBen Roth- (void)dispatchKernelJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody onSuccess:(void (^_Nullable)(NSDictionary * _Nullable))success onFailure:(void (^_Nullable)(NSString * _Nullable))failure
1600fbf6a33SBen Roth{
1615daa5350SBen Roth  EXKernelModule *kernelModule = [self nativeModuleForAppManager:_bridgeRegistry.kernelAppManager named:@"ExponentKernel"];
1620fbf6a33SBen Roth  if (kernelModule) {
1630fbf6a33SBen Roth    [kernelModule dispatchJSEvent:eventName body:eventBody onSuccess:success onFailure:failure];
1640fbf6a33SBen Roth  }
1650fbf6a33SBen Roth}
1660fbf6a33SBen Roth
1670fbf6a33SBen Roth- (void)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody onAppManager:(EXReactAppManager *)appManager
1680fbf6a33SBen Roth{
1690fbf6a33SBen Roth  [appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
1700fbf6a33SBen Roth                                   args:eventBody ? @[eventName, eventBody] : @[eventName]];
1710fbf6a33SBen Roth}
1720fbf6a33SBen Roth
1735daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName
1740fbf6a33SBen Roth{
1750fbf6a33SBen Roth  id destinationBridge = appManager.reactBridge;
1760fbf6a33SBen Roth
1770fbf6a33SBen Roth  if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) {
1780fbf6a33SBen Roth    id batchedBridge = [destinationBridge batchedBridge];
1790fbf6a33SBen Roth    id moduleData = [batchedBridge moduleDataForName:moduleName];
1800fbf6a33SBen Roth
1810fbf6a33SBen Roth    // React Native before SDK 11 didn't strip the "RCT" prefix from module names
1820fbf6a33SBen Roth    if (!moduleData && ![moduleName hasPrefix:@"RCT"]) {
1830fbf6a33SBen Roth      moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]];
1840fbf6a33SBen Roth    }
1850fbf6a33SBen Roth
1860fbf6a33SBen Roth    if (moduleData) {
1870fbf6a33SBen Roth      return [moduleData instance];
1880fbf6a33SBen Roth    }
1890fbf6a33SBen Roth  } else {
1900fbf6a33SBen Roth    DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge");
1910fbf6a33SBen Roth  }
1920fbf6a33SBen Roth  return nil;
1930fbf6a33SBen Roth}
1940fbf6a33SBen Roth
1950fbf6a33SBen Roth/**
1960fbf6a33SBen Roth *  If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well.
1970fbf6a33SBen Roth */
1980fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge
1990fbf6a33SBen Roth{
2000fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge];
2010fbf6a33SBen Roth  if ([bridge respondsToSelector:@selector(batchedBridge)]) {
2020fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]];
2030fbf6a33SBen Roth  } else if ([bridge respondsToSelector:@selector(parentBridge)]) {
2040fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]];
2050fbf6a33SBen Roth  }
2060fbf6a33SBen Roth}
2070fbf6a33SBen Roth
2088a7f6184SBen Roth- (void)sendNotification:(NSDictionary *)notifBody
2098a7f6184SBen Roth      toExperienceWithId:(NSString *)destinationExperienceId
2108a7f6184SBen Roth          fromBackground:(BOOL)isFromBackground
2118a7f6184SBen Roth                isRemote:(BOOL)isRemote
212f148175aSBen Roth{
2138a7f6184SBen Roth  EXReactAppManager *destinationAppManager = _bridgeRegistry.kernelAppManager;
2148a7f6184SBen Roth  for (id bridge in [_bridgeRegistry bridgeEnumerator]) {
2158a7f6184SBen Roth    EXKernelBridgeRecord *bridgeRecord = [_bridgeRegistry recordForBridge:bridge];
2168a7f6184SBen Roth    if (bridgeRecord.experienceId && [bridgeRecord.experienceId isEqualToString:destinationExperienceId]) {
2178a7f6184SBen Roth      destinationAppManager = bridgeRecord.appManager;
2188a7f6184SBen Roth      break;
219f148175aSBen Roth    }
220f148175aSBen Roth  }
2218a7f6184SBen Roth  // if the notification came from the background, in most but not all cases, this means the user acted on an iOS notification
2228a7f6184SBen Roth  // and caused the app to launch.
2238a7f6184SBen Roth  // From SO:
2248a7f6184SBen Roth  // > Note that "App opened from Notification" will be a false positive if the notification is sent while the user is on a different
2258a7f6184SBen Roth  // > screen (for example, if they pull down the status bar and then receive a notification from your app).
2268a7f6184SBen Roth  NSDictionary *bodyWithOrigin = @{
2278a7f6184SBen Roth                                   @"origin": (isFromBackground) ? @"selected" : @"received",
2288a7f6184SBen Roth                                   @"remote": @(isRemote),
2298a7f6184SBen Roth                                   @"data": notifBody,
2308a7f6184SBen Roth                                   };
2318a7f6184SBen Roth  if (destinationAppManager) {
2328a7f6184SBen Roth    if (destinationAppManager == _bridgeRegistry.kernelAppManager) {
2338a7f6184SBen Roth      // send both the body and the experience id, so we can open a new experience from the kernel
2348a7f6184SBen Roth      [self _dispatchJSEvent:@"Exponent.notification"
2358a7f6184SBen Roth                        body:@{
2368a7f6184SBen Roth                               @"body": bodyWithOrigin,
2378a7f6184SBen Roth                               @"experienceId": destinationExperienceId,
2380fbf6a33SBen Roth                               }
2398a7f6184SBen Roth                onAppManager:_bridgeRegistry.kernelAppManager];
2408a7f6184SBen Roth    } else {
2418a7f6184SBen Roth      // send the body to the already-open experience
2428a7f6184SBen Roth      [self _dispatchJSEvent:@"Exponent.notification" body:bodyWithOrigin onAppManager:destinationAppManager];
2438a7f6184SBen Roth      [self _moveAppManagerToForeground:destinationAppManager];
2440fbf6a33SBen Roth    }
2450fbf6a33SBen Roth  }
2460fbf6a33SBen Roth}
2470fbf6a33SBen Roth
2488a7f6184SBen Roth#pragma mark - App State
2498e3c2ccdSBen Roth
2508a7f6184SBen Roth- (void)handleJSTaskDidForegroundWithType:(NSInteger)type params:(NSDictionary *)params
2510fbf6a33SBen Roth{
2520fbf6a33SBen Roth  EXKernelRoute routetype = (EXKernelRoute)type;
2530fbf6a33SBen Roth  [[EXAnalytics sharedInstance] logForegroundEventForRoute:routetype fromJS:YES];
2540fbf6a33SBen Roth
2556f62abf2SBen Roth  NSString *urlToForeground, *urlToBackground;
2560fbf6a33SBen Roth  if (params) {
2576f62abf2SBen Roth    urlToForeground = RCTNilIfNull(params[@"url"]);
2586f62abf2SBen Roth    urlToBackground = RCTNilIfNull(params[@"urlToBackground"]);
2596f62abf2SBen Roth  }
2606f62abf2SBen Roth
2610fbf6a33SBen Roth  EXReactAppManager *appManagerToForeground = nil;
2620fbf6a33SBen Roth  EXReactAppManager *appManagerToBackground = nil;
2630fbf6a33SBen Roth
2640fbf6a33SBen Roth  if (routetype == kEXKernelRouteHome) {
2650fbf6a33SBen Roth    appManagerToForeground = _bridgeRegistry.kernelAppManager;
2660fbf6a33SBen Roth  }
2670fbf6a33SBen Roth  if (routetype == kEXKernelRouteBrowser && !urlToBackground) {
2680fbf6a33SBen Roth    appManagerToBackground = _bridgeRegistry.kernelAppManager;
2690fbf6a33SBen Roth  }
2700fbf6a33SBen Roth
2710fbf6a33SBen Roth  for (id bridge in [_bridgeRegistry bridgeEnumerator]) {
2720fbf6a33SBen Roth    EXKernelBridgeRecord *bridgeRecord = [_bridgeRegistry recordForBridge:bridge];
2730fbf6a33SBen Roth    if (urlToForeground && [bridgeRecord.appManager.frame.initialUri.absoluteString isEqualToString:urlToForeground]) {
2740fbf6a33SBen Roth      appManagerToForeground = bridgeRecord.appManager;
2750fbf6a33SBen Roth    } else if (urlToBackground && [bridgeRecord.appManager.frame.initialUri.absoluteString isEqualToString:urlToBackground]) {
2760fbf6a33SBen Roth      appManagerToBackground = bridgeRecord.appManager;
2770fbf6a33SBen Roth    }
2780fbf6a33SBen Roth  }
2790fbf6a33SBen Roth
280*b05926a2SBen Roth  if ([_serviceRegistry.linkingManager isRefreshExpectedForAppManager:appManagerToForeground]) {
281*b05926a2SBen Roth    // shell app foregrounded the same bridge as before.
282*b05926a2SBen Roth    // this would be a no-op, so we force a reload on the existing frame.
283*b05926a2SBen Roth    // this is usually triggered by calling Util.reload() when no new JS bundle is available.
284*b05926a2SBen Roth    [((EXFrameReactAppManager *)_bridgeRegistry.lastKnownForegroundAppManager).frame reload];
285*b05926a2SBen Roth  } else {
2860fbf6a33SBen Roth    if (appManagerToBackground) {
2870fbf6a33SBen Roth      [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManagerToBackground.reactBridge];
2885daa5350SBen Roth      id appStateModule = [self nativeModuleForAppManager:appManagerToBackground named:@"AppState"];
2890fbf6a33SBen Roth      if ([appStateModule respondsToSelector:@selector(setState:)]) {
2900fbf6a33SBen Roth        [appStateModule setState:@"background"];
2910fbf6a33SBen Roth      }
2920fbf6a33SBen Roth    }
2930fbf6a33SBen Roth    if (appManagerToForeground) {
2940fbf6a33SBen Roth      [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManagerToForeground.reactBridge];
2955daa5350SBen Roth      id appStateModule = [self nativeModuleForAppManager:appManagerToForeground named:@"AppState"];
2960fbf6a33SBen Roth      if ([appStateModule respondsToSelector:@selector(setState:)]) {
2970fbf6a33SBen Roth        [appStateModule setState:@"active"];
2980fbf6a33SBen Roth      }
2990fbf6a33SBen Roth      _bridgeRegistry.lastKnownForegroundBridge = appManagerToForeground.reactBridge;
3000fbf6a33SBen Roth    } else {
3010fbf6a33SBen Roth      _bridgeRegistry.lastKnownForegroundBridge = nil;
3020fbf6a33SBen Roth    }
3030fbf6a33SBen Roth  }
304*b05926a2SBen Roth}
3050fbf6a33SBen Roth
3060fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification
3070fbf6a33SBen Roth{
3080fbf6a33SBen Roth  NSString *newState;
3090fbf6a33SBen Roth
3100fbf6a33SBen Roth  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
3110fbf6a33SBen Roth    newState = @"inactive";
3120fbf6a33SBen Roth  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
3130fbf6a33SBen Roth    newState = @"background";
3140fbf6a33SBen Roth  } else {
3150fbf6a33SBen Roth    switch (RCTSharedApplication().applicationState) {
3160fbf6a33SBen Roth      case UIApplicationStateActive:
3170fbf6a33SBen Roth        newState = @"active";
3180fbf6a33SBen Roth        break;
3190fbf6a33SBen Roth      case UIApplicationStateBackground: {
3200fbf6a33SBen Roth        newState = @"background";
3210fbf6a33SBen Roth        break;
3220fbf6a33SBen Roth      }
3230fbf6a33SBen Roth      default: {
3240fbf6a33SBen Roth        newState = @"unknown";
3250fbf6a33SBen Roth        break;
3260fbf6a33SBen Roth      }
3270fbf6a33SBen Roth    }
3280fbf6a33SBen Roth  }
3290fbf6a33SBen Roth
3300fbf6a33SBen Roth  if (_bridgeRegistry.lastKnownForegroundBridge) {
3310231e9c3SBen Roth    EXReactAppManager *appManager = [_bridgeRegistry lastKnownForegroundAppManager];
3325daa5350SBen Roth    id appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"];
3330fbf6a33SBen Roth    NSString *lastKnownState;
3340fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(lastKnownState)]) {
3350fbf6a33SBen Roth      lastKnownState = [appStateModule lastKnownState];
3360fbf6a33SBen Roth    }
3370fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(setState:)]) {
3380fbf6a33SBen Roth      [appStateModule setState:newState];
3390fbf6a33SBen Roth    }
3400fbf6a33SBen Roth    if (!lastKnownState || ![newState isEqualToString:lastKnownState]) {
3410fbf6a33SBen Roth      if ([newState isEqualToString:@"active"]) {
3420fbf6a33SBen Roth        [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:_bridgeRegistry.lastKnownForegroundBridge];
3430fbf6a33SBen Roth      } else if ([newState isEqualToString:@"background"]) {
3440fbf6a33SBen Roth        [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:_bridgeRegistry.lastKnownForegroundBridge];
3450fbf6a33SBen Roth      }
3460fbf6a33SBen Roth    }
3470fbf6a33SBen Roth  }
3480fbf6a33SBen Roth}
3490fbf6a33SBen Roth
3508a7f6184SBen Roth- (void)_moveAppManagerToForeground: (EXReactAppManager *)appManager
3518a7f6184SBen Roth{
3528a7f6184SBen Roth  if (appManager != _bridgeRegistry.kernelAppManager) {
3538a7f6184SBen Roth    EXFrameReactAppManager *frameAppManager = (EXFrameReactAppManager *)appManager;
3548a7f6184SBen Roth    // kernel JS needs to bring the relevant frame/bridge to visibility.
3558a7f6184SBen Roth    NSURL *frameUrlToForeground = frameAppManager.frame.initialUri;
3568a7f6184SBen Roth    [self dispatchKernelJSEvent:kEXKernelShouldForegroundTaskEvent body:@{ @"taskUrl":frameUrlToForeground.absoluteString } onSuccess:nil onFailure:nil];
3578a7f6184SBen Roth  }
3588a7f6184SBen Roth}
3598a7f6184SBen Roth
3600fbf6a33SBen Roth@end
3610fbf6a33SBen Roth
3620fbf6a33SBen RothNS_ASSUME_NONNULL_END
363