xref: /expo/ios/Exponent/Kernel/Core/EXKernel.m (revision 9f8d9b9d)
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 "EXKernelDevMenuViewController.h"
60fbf6a33SBen Roth#import "EXFrame.h"
70fbf6a33SBen Roth#import "EXFrameReactAppManager.h"
80fbf6a33SBen Roth#import "EXKernel.h"
90fbf6a33SBen Roth#import "EXKernelBridgeRecord.h"
100fbf6a33SBen Roth#import "EXKernelDevMotionHandler.h"
110fbf6a33SBen Roth#import "EXKernelDevKeyCommands.h"
120fbf6a33SBen Roth#import "EXKernelModule.h"
130fbf6a33SBen Roth#import "EXLinkingManager.h"
140fbf6a33SBen Roth#import "EXManifestResource.h"
150fbf6a33SBen Roth#import "EXShellManager.h"
160fbf6a33SBen Roth#import "EXVersions.h"
170fbf6a33SBen Roth#import "EXViewController.h"
180fbf6a33SBen Roth
190fbf6a33SBen Roth#import <React/RCTBridge+Private.h>
200fbf6a33SBen Roth#import <React/RCTEventDispatcher.h>
210fbf6a33SBen Roth#import <React/RCTModuleData.h>
220fbf6a33SBen Roth#import <React/RCTUtils.h>
230fbf6a33SBen Roth
240fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN
250fbf6a33SBen Roth
260fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain";
270fbf6a33SBen RothNSNotificationName kEXKernelJSIsLoadedNotification = @"EXKernelJSIsLoadedNotification";
280fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask";
290fbf6a33SBen RothNSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey";
300fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey";
310fbf6a33SBen RothNSString * const EXKernelDisableNuxDefaultsKey = @"EXKernelDisableNuxDefaultsKey";
320fbf6a33SBen Roth
33*9f8d9b9dSBen Roth@interface EXKernel () <EXKernelBridgeRegistryDelegate>
340fbf6a33SBen Roth
350fbf6a33SBen Roth@property (nonatomic, weak) EXViewController *vcExponentRoot;
360fbf6a33SBen Roth
370fbf6a33SBen Roth@end
380fbf6a33SBen Roth
390fbf6a33SBen Roth@implementation EXKernel
400fbf6a33SBen Roth
410fbf6a33SBen Roth+ (instancetype)sharedInstance
420fbf6a33SBen Roth{
430fbf6a33SBen Roth  static EXKernel *theKernel;
440fbf6a33SBen Roth  static dispatch_once_t once;
450fbf6a33SBen Roth  dispatch_once(&once, ^{
460fbf6a33SBen Roth    if (!theKernel) {
470fbf6a33SBen Roth      theKernel = [[EXKernel alloc] init];
480fbf6a33SBen Roth    }
490fbf6a33SBen Roth  });
500fbf6a33SBen Roth  return theKernel;
510fbf6a33SBen Roth}
520fbf6a33SBen Roth
530fbf6a33SBen Roth+ (BOOL)isDevKernel
540fbf6a33SBen Roth{
550fbf6a33SBen Roth  // if we're in detached state (i.e. ExponentView) then never expect local kernel
560fbf6a33SBen Roth  BOOL isDetachedKernel = ([[EXVersions sharedInstance].versions objectForKey:@"detachedNativeVersions"] != nil);
570fbf6a33SBen Roth  if (isDetachedKernel) {
580fbf6a33SBen Roth    return NO;
590fbf6a33SBen Roth  }
600fbf6a33SBen Roth
610fbf6a33SBen Roth  // otherwise, expect local kernel when we are attached to xcode
620fbf6a33SBen Roth#if DEBUG
630fbf6a33SBen Roth  return YES;
640fbf6a33SBen Roth#endif
650fbf6a33SBen Roth  return NO;
660fbf6a33SBen Roth}
670fbf6a33SBen Roth
680fbf6a33SBen Roth- (instancetype)init
690fbf6a33SBen Roth{
700fbf6a33SBen Roth  if (self = [super init]) {
71*9f8d9b9dSBen Roth    // init bridge registry: keep track of RN bridges we are running
720fbf6a33SBen Roth    _bridgeRegistry = [[EXKernelBridgeRegistry alloc] init];
73*9f8d9b9dSBen Roth    _bridgeRegistry.delegate = self;
74*9f8d9b9dSBen Roth
75*9f8d9b9dSBen Roth    // init service registry: classes which manage shared resources among all bridges
76d7f51e5eSBen Roth    _serviceRegistry = [[EXKernelServiceRegistry alloc] init];
77*9f8d9b9dSBen Roth
780fbf6a33SBen Roth    [EXKernelDevMotionHandler sharedInstance];
790fbf6a33SBen Roth    [EXKernelDevKeyCommands sharedInstance];
800fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_onKernelJSLoaded) name:kEXKernelJSIsLoadedNotification object:nil];
810fbf6a33SBen Roth    for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
820fbf6a33SBen Roth                             UIApplicationDidEnterBackgroundNotification,
830fbf6a33SBen Roth                             UIApplicationDidFinishLaunchingNotification,
840fbf6a33SBen Roth                             UIApplicationWillResignActiveNotification,
850fbf6a33SBen Roth                             UIApplicationWillEnterForegroundNotification]) {
860fbf6a33SBen Roth
870fbf6a33SBen Roth      [[NSNotificationCenter defaultCenter] addObserver:self
880fbf6a33SBen Roth                                               selector:@selector(_handleAppStateDidChange:)
890fbf6a33SBen Roth                                                   name:name
900fbf6a33SBen Roth                                                 object:nil];
910fbf6a33SBen Roth    }
920fbf6a33SBen Roth    NSLog(@"Expo iOS Client Version %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"EXClientVersion"]);
930fbf6a33SBen Roth  }
940fbf6a33SBen Roth  return self;
950fbf6a33SBen Roth}
960fbf6a33SBen Roth
970fbf6a33SBen Roth- (void)dealloc
980fbf6a33SBen Roth{
990fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] removeObserver:self];
1000fbf6a33SBen Roth}
1010fbf6a33SBen Roth
1020fbf6a33SBen Roth- (void)sendNotification:(NSDictionary *)notifBody
1030fbf6a33SBen Roth      toExperienceWithId:(NSString *)destinationExperienceId
1040fbf6a33SBen Roth          fromBackground:(BOOL)isFromBackground
1050fbf6a33SBen Roth                isRemote:(BOOL)isRemote
1060fbf6a33SBen Roth{
1070fbf6a33SBen Roth  EXReactAppManager *destinationAppManager = _bridgeRegistry.kernelAppManager;
1080fbf6a33SBen Roth  for (id bridge in [_bridgeRegistry bridgeEnumerator]) {
1090fbf6a33SBen Roth    EXKernelBridgeRecord *bridgeRecord = [_bridgeRegistry recordForBridge:bridge];
1100fbf6a33SBen Roth    if (bridgeRecord.experienceId && [bridgeRecord.experienceId isEqualToString:destinationExperienceId]) {
1110fbf6a33SBen Roth      destinationAppManager = bridgeRecord.appManager;
1120fbf6a33SBen Roth      break;
1130fbf6a33SBen Roth    }
1140fbf6a33SBen Roth  }
1150fbf6a33SBen Roth  // if the notification came from the background, in most but not all cases, this means the user acted on an iOS notification
1160fbf6a33SBen Roth  // and caused the app to launch.
1170fbf6a33SBen Roth  // From SO:
1180fbf6a33SBen Roth  // > Note that "App opened from Notification" will be a false positive if the notification is sent while the user is on a different
1190fbf6a33SBen Roth  // > screen (for example, if they pull down the status bar and then receive a notification from your app).
1200fbf6a33SBen Roth  NSDictionary *bodyWithOrigin = @{
1210fbf6a33SBen Roth                                   @"origin": (isFromBackground) ? @"selected" : @"received",
1220fbf6a33SBen Roth                                   @"remote": @(isRemote),
1230fbf6a33SBen Roth                                   @"data": notifBody,
1240fbf6a33SBen Roth                                   };
1250fbf6a33SBen Roth  if (destinationAppManager) {
1260fbf6a33SBen Roth    if (destinationAppManager == _bridgeRegistry.kernelAppManager) {
1270fbf6a33SBen Roth      // send both the body and the experience id, so we can open a new experience from the kernel
1280fbf6a33SBen Roth      [self _dispatchJSEvent:@"Exponent.notification"
1290fbf6a33SBen Roth                        body:@{
1300fbf6a33SBen Roth                               @"body": bodyWithOrigin,
1310fbf6a33SBen Roth                               @"experienceId": destinationExperienceId,
1320fbf6a33SBen Roth                               }
1330fbf6a33SBen Roth                onAppManager:_bridgeRegistry.kernelAppManager];
1340fbf6a33SBen Roth    } else {
1350fbf6a33SBen Roth      // send the body to the already-open experience
1360fbf6a33SBen Roth      [self _dispatchJSEvent:@"Exponent.notification" body:bodyWithOrigin onAppManager:destinationAppManager];
1370fbf6a33SBen Roth      [self _moveAppManagerToForeground:destinationAppManager];
1380fbf6a33SBen Roth    }
1390fbf6a33SBen Roth  }
1400fbf6a33SBen Roth}
1410fbf6a33SBen Roth
1420fbf6a33SBen Roth- (void)registerRootExponentViewController:(EXViewController *)exponentViewController
1430fbf6a33SBen Roth{
1440fbf6a33SBen Roth  _vcExponentRoot = exponentViewController;
1450fbf6a33SBen Roth}
1460fbf6a33SBen Roth
1470fbf6a33SBen Roth- (EXViewController *)rootViewController
1480fbf6a33SBen Roth{
1490fbf6a33SBen Roth  return _vcExponentRoot;
1500fbf6a33SBen Roth}
1510fbf6a33SBen Roth
1520fbf6a33SBen Roth- (void)_onKernelJSLoaded
1530fbf6a33SBen Roth{
1540fbf6a33SBen Roth  // used by appetize: optionally disable nux
1550fbf6a33SBen Roth  BOOL disableNuxDefaultsValue = [[NSUserDefaults standardUserDefaults] boolForKey:EXKernelDisableNuxDefaultsKey];
1560fbf6a33SBen Roth  if (disableNuxDefaultsValue) {
1570fbf6a33SBen Roth    [self dispatchKernelJSEvent:@"resetNuxState" body:@{ @"isNuxCompleted": @YES } onSuccess:nil onFailure:nil];
1580fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] removeObjectForKey:EXKernelDisableNuxDefaultsKey];
1590fbf6a33SBen Roth  }
1600fbf6a33SBen Roth}
1610fbf6a33SBen Roth
1625daa5350SBen Roth#pragma mark - Misc
1635daa5350SBen Roth
1645daa5350SBen Roth- (void)openUrl:(NSString *)url onAppManager:(EXReactAppManager *)appManager
1655daa5350SBen Roth{
1665daa5350SBen Roth  // fire a Linking url event on this (possibly versioned) bridge
1675daa5350SBen Roth  id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"];
1685daa5350SBen Roth  if (!linkingModule) {
1695daa5350SBen Roth    DDLogError(@"Could not find the Linking module to open URL (%@)", url);
1705daa5350SBen Roth  } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) {
1715daa5350SBen Roth    [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:url]];
1725daa5350SBen Roth  } else {
1735daa5350SBen Roth    DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", url);
1745daa5350SBen Roth  }
1755daa5350SBen Roth  [self _moveAppManagerToForeground:appManager];
1765daa5350SBen Roth}
1770fbf6a33SBen Roth
1780fbf6a33SBen Roth+ (NSString *)deviceInstallUUID
1790fbf6a33SBen Roth{
1800fbf6a33SBen Roth  NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
1810fbf6a33SBen Roth  if (!uuid) {
1820fbf6a33SBen Roth    uuid = [[NSUUID UUID] UUIDString];
1830fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
1840fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] synchronize];
1850fbf6a33SBen Roth  }
1860fbf6a33SBen Roth  return uuid;
1870fbf6a33SBen Roth}
1880fbf6a33SBen Roth
189*9f8d9b9dSBen Roth#pragma mark - bridge registry delegate
190*9f8d9b9dSBen Roth
191*9f8d9b9dSBen Roth- (void)bridgeRegistry:(EXKernelBridgeRegistry *)registry didRegisterBridgeRecord:(EXKernelBridgeRecord *)bridgeRecord
192*9f8d9b9dSBen Roth{
193*9f8d9b9dSBen Roth  // forward to service registry
194*9f8d9b9dSBen Roth  [_serviceRegistry bridgeRegistry:registry didRegisterBridgeRecord:bridgeRecord];
195*9f8d9b9dSBen Roth}
196*9f8d9b9dSBen Roth
197*9f8d9b9dSBen Roth- (void)bridgeRegistry:(EXKernelBridgeRegistry *)registry willUnregisterBridgeRecord:(EXKernelBridgeRecord *)bridgeRecord
198*9f8d9b9dSBen Roth{
199*9f8d9b9dSBen Roth  // forward to service registry
200*9f8d9b9dSBen Roth  [_serviceRegistry bridgeRegistry:registry willUnregisterBridgeRecord:bridgeRecord];
201*9f8d9b9dSBen Roth}
202*9f8d9b9dSBen Roth
2030fbf6a33SBen Roth#pragma mark - Bridge stuff
2040fbf6a33SBen Roth
2050fbf6a33SBen Roth- (void)dispatchKernelJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody onSuccess:(void (^_Nullable)(NSDictionary * _Nullable))success onFailure:(void (^_Nullable)(NSString * _Nullable))failure
2060fbf6a33SBen Roth{
2075daa5350SBen Roth  EXKernelModule *kernelModule = [self nativeModuleForAppManager:_bridgeRegistry.kernelAppManager named:@"ExponentKernel"];
2080fbf6a33SBen Roth  if (kernelModule) {
2090fbf6a33SBen Roth    [kernelModule dispatchJSEvent:eventName body:eventBody onSuccess:success onFailure:failure];
2100fbf6a33SBen Roth  }
2110fbf6a33SBen Roth}
2120fbf6a33SBen Roth
2130fbf6a33SBen Roth- (void)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody onAppManager:(EXReactAppManager *)appManager
2140fbf6a33SBen Roth{
2150fbf6a33SBen Roth  [appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
2160fbf6a33SBen Roth                                   args:eventBody ? @[eventName, eventBody] : @[eventName]];
2170fbf6a33SBen Roth}
2180fbf6a33SBen Roth
2195daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName
2200fbf6a33SBen Roth{
2210fbf6a33SBen Roth  id destinationBridge = appManager.reactBridge;
2220fbf6a33SBen Roth
2230fbf6a33SBen Roth  if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) {
2240fbf6a33SBen Roth    id batchedBridge = [destinationBridge batchedBridge];
2250fbf6a33SBen Roth    id moduleData = [batchedBridge moduleDataForName:moduleName];
2260fbf6a33SBen Roth
2270fbf6a33SBen Roth    // React Native before SDK 11 didn't strip the "RCT" prefix from module names
2280fbf6a33SBen Roth    if (!moduleData && ![moduleName hasPrefix:@"RCT"]) {
2290fbf6a33SBen Roth      moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]];
2300fbf6a33SBen Roth    }
2310fbf6a33SBen Roth
2320fbf6a33SBen Roth    if (moduleData) {
2330fbf6a33SBen Roth      return [moduleData instance];
2340fbf6a33SBen Roth    }
2350fbf6a33SBen Roth  } else {
2360fbf6a33SBen Roth    DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge");
2370fbf6a33SBen Roth  }
2380fbf6a33SBen Roth  return nil;
2390fbf6a33SBen Roth}
2400fbf6a33SBen Roth
2410fbf6a33SBen Roth- (void)_moveAppManagerToForeground: (EXReactAppManager *)appManager
2420fbf6a33SBen Roth{
2430fbf6a33SBen Roth  if (appManager != _bridgeRegistry.kernelAppManager) {
2440fbf6a33SBen Roth    EXFrameReactAppManager *frameAppManager = (EXFrameReactAppManager *)appManager;
2450fbf6a33SBen Roth    // kernel JS needs to bring the relevant frame/bridge to visibility.
2460fbf6a33SBen Roth    NSURL *frameUrlToForeground = frameAppManager.frame.initialUri;
2470fbf6a33SBen Roth    [self dispatchKernelJSEvent:kEXKernelShouldForegroundTaskEvent body:@{ @"taskUrl":frameUrlToForeground.absoluteString } onSuccess:nil onFailure:nil];
2480fbf6a33SBen Roth  }
2490fbf6a33SBen Roth}
2500fbf6a33SBen Roth
2510fbf6a33SBen Roth/**
2520fbf6a33SBen Roth *  If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well.
2530fbf6a33SBen Roth */
2540fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge
2550fbf6a33SBen Roth{
2560fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge];
2570fbf6a33SBen Roth  if ([bridge respondsToSelector:@selector(batchedBridge)]) {
2580fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]];
2590fbf6a33SBen Roth  } else if ([bridge respondsToSelector:@selector(parentBridge)]) {
2600fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]];
2610fbf6a33SBen Roth  }
2620fbf6a33SBen Roth}
2630fbf6a33SBen Roth
2640fbf6a33SBen Roth#pragma mark - EXKernelModuleDelegate
2650fbf6a33SBen Roth
266f148175aSBen Roth- (BOOL)kernelModuleShouldEnableLegacyMenuBehavior:(EXKernelModule *)module
267f148175aSBen Roth{
268f148175aSBen Roth  return [EXKernelDevKeyCommands sharedInstance].isLegacyMenuBehaviorEnabled;
269f148175aSBen Roth}
270f148175aSBen Roth
271f148175aSBen Roth- (void)kernelModule:(EXKernelModule *)module didSelectEnableLegacyMenuBehavior:(BOOL)isEnabled
272f148175aSBen Roth{
273f148175aSBen Roth  [EXKernelDevKeyCommands sharedInstance].isLegacyMenuBehaviorEnabled = isEnabled;
274f148175aSBen Roth}
275f148175aSBen Roth
2760fbf6a33SBen Roth- (BOOL)kernelModuleShouldEnableDevtools:(__unused EXKernelModule *)module
2770fbf6a33SBen Roth{
2780fbf6a33SBen Roth  return (
2790fbf6a33SBen Roth    _bridgeRegistry.lastKnownForegroundAppManager != _bridgeRegistry.kernelAppManager &&
2800fbf6a33SBen Roth    [_bridgeRegistry.lastKnownForegroundAppManager areDevtoolsEnabled]
2810fbf6a33SBen Roth  );
2820fbf6a33SBen Roth}
2830fbf6a33SBen Roth
2840fbf6a33SBen Roth- (NSDictionary<NSString *, NSString *> *)devMenuItemsForKernelModule:(EXKernelModule *)module
2850fbf6a33SBen Roth{
2860fbf6a33SBen Roth  return [_bridgeRegistry.lastKnownForegroundAppManager devMenuItems];
2870fbf6a33SBen Roth}
2880fbf6a33SBen Roth
2890fbf6a33SBen Roth- (void)kernelModule:(EXKernelModule *)module didSelectDevMenuItemWithKey:(NSString *)key
2900fbf6a33SBen Roth{
2910fbf6a33SBen Roth  [_bridgeRegistry.lastKnownForegroundAppManager selectDevMenuItemWithKey:key];
2920fbf6a33SBen Roth}
2930fbf6a33SBen Roth
2940fbf6a33SBen Roth- (void)kernelModuleDidSelectKernelDevMenu:(__unused EXKernelModule *)module
2950fbf6a33SBen Roth{
2960fbf6a33SBen Roth  EXKernelDevMenuViewController *vcDevMenu = [[EXKernelDevMenuViewController alloc] init];
2970fbf6a33SBen Roth  if (_vcExponentRoot) {
2980fbf6a33SBen Roth    [_vcExponentRoot presentViewController:vcDevMenu animated:YES completion:nil];
2990fbf6a33SBen Roth  }
3000fbf6a33SBen Roth}
3010fbf6a33SBen Roth
3028e3c2ccdSBen Roth- (BOOL)kernelModuleShouldAutoReloadCurrentTask:(EXKernelModule *)module
3038e3c2ccdSBen Roth{
3048e3c2ccdSBen Roth  NSString *foregroundTaskExperienceId = _bridgeRegistry.lastKnownForegroundAppManager.experienceId;
305fd8b7de4SBen Roth  return [_serviceRegistry.errorRecoveryManager experienceIdShouldReloadOnError:foregroundTaskExperienceId];
3068e3c2ccdSBen Roth}
3078e3c2ccdSBen Roth
3080fbf6a33SBen Roth- (void)kernelModule:(__unused EXKernelModule *)module taskDidForegroundWithType:(NSInteger)type params:(NSDictionary *)params
3090fbf6a33SBen Roth{
3100fbf6a33SBen Roth  EXKernelRoute routetype = (EXKernelRoute)type;
3110fbf6a33SBen Roth  [[EXAnalytics sharedInstance] logForegroundEventForRoute:routetype fromJS:YES];
3120fbf6a33SBen Roth
3136f62abf2SBen Roth  NSString *urlToForeground, *urlToBackground;
3140fbf6a33SBen Roth  if (params) {
3156f62abf2SBen Roth    urlToForeground = RCTNilIfNull(params[@"url"]);
3166f62abf2SBen Roth    urlToBackground = RCTNilIfNull(params[@"urlToBackground"]);
3176f62abf2SBen Roth  }
3186f62abf2SBen Roth
3190fbf6a33SBen Roth  EXReactAppManager *appManagerToForeground = nil;
3200fbf6a33SBen Roth  EXReactAppManager *appManagerToBackground = nil;
3210fbf6a33SBen Roth
3220fbf6a33SBen Roth  if (routetype == kEXKernelRouteHome) {
3230fbf6a33SBen Roth    appManagerToForeground = _bridgeRegistry.kernelAppManager;
3240fbf6a33SBen Roth  }
3250fbf6a33SBen Roth  if (routetype == kEXKernelRouteBrowser && !urlToBackground) {
3260fbf6a33SBen Roth    appManagerToBackground = _bridgeRegistry.kernelAppManager;
3270fbf6a33SBen Roth  }
3280fbf6a33SBen Roth
3290fbf6a33SBen Roth  for (id bridge in [_bridgeRegistry bridgeEnumerator]) {
3300fbf6a33SBen Roth    EXKernelBridgeRecord *bridgeRecord = [_bridgeRegistry recordForBridge:bridge];
3310fbf6a33SBen Roth    if (urlToForeground && [bridgeRecord.appManager.frame.initialUri.absoluteString isEqualToString:urlToForeground]) {
3320fbf6a33SBen Roth      appManagerToForeground = bridgeRecord.appManager;
3330fbf6a33SBen Roth    } else if (urlToBackground && [bridgeRecord.appManager.frame.initialUri.absoluteString isEqualToString:urlToBackground]) {
3340fbf6a33SBen Roth      appManagerToBackground = bridgeRecord.appManager;
3350fbf6a33SBen Roth    }
3360fbf6a33SBen Roth  }
3370fbf6a33SBen Roth
3380fbf6a33SBen Roth  if (appManagerToBackground) {
3390fbf6a33SBen Roth    [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManagerToBackground.reactBridge];
3405daa5350SBen Roth    id appStateModule = [self nativeModuleForAppManager:appManagerToBackground named:@"AppState"];
3410fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(setState:)]) {
3420fbf6a33SBen Roth      [appStateModule setState:@"background"];
3430fbf6a33SBen Roth    }
3440fbf6a33SBen Roth  }
3450fbf6a33SBen Roth  if (appManagerToForeground) {
3460fbf6a33SBen Roth    [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManagerToForeground.reactBridge];
3475daa5350SBen Roth    id appStateModule = [self nativeModuleForAppManager:appManagerToForeground named:@"AppState"];
3480fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(setState:)]) {
3490fbf6a33SBen Roth      [appStateModule setState:@"active"];
3500fbf6a33SBen Roth    }
3510fbf6a33SBen Roth    _bridgeRegistry.lastKnownForegroundBridge = appManagerToForeground.reactBridge;
3520fbf6a33SBen Roth  } else {
3530fbf6a33SBen Roth    _bridgeRegistry.lastKnownForegroundBridge = nil;
3540fbf6a33SBen Roth  }
3550fbf6a33SBen Roth}
3560fbf6a33SBen Roth
3570fbf6a33SBen Roth- (void)kernelModule:(EXKernelModule *)module didRequestManifestWithUrl:(NSURL *)url originalUrl:(NSURL *)originalUrl success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure
3580fbf6a33SBen Roth{
3590fbf6a33SBen Roth  if (!([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"])) {
3600fbf6a33SBen Roth    NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
3610fbf6a33SBen Roth    components.scheme = @"http";
3620fbf6a33SBen Roth    url = [components URL];
3630fbf6a33SBen Roth  }
3640fbf6a33SBen Roth  EXCachedResourceBehavior cacheBehavior = kEXCachedResourceFallBackToCache;
3650fbf6a33SBen Roth  if ([url.host isEqualToString:@"localhost"]) {
3660fbf6a33SBen Roth    // we can't pre-detect if this person is using a developer tool, but using localhost is a pretty solid indicator.
3670fbf6a33SBen Roth    cacheBehavior = kEXCachedResourceNoCache;
3680fbf6a33SBen Roth  }
3690fbf6a33SBen Roth  EXManifestResource *manifestResource = [[EXManifestResource alloc] initWithManifestUrl:url originalUrl:originalUrl];
3700fbf6a33SBen Roth  [manifestResource loadResourceWithBehavior:cacheBehavior successBlock:^(NSData * _Nonnull data) {
3710fbf6a33SBen Roth    NSString *manifestString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
3720fbf6a33SBen Roth    success(manifestString);
3730fbf6a33SBen Roth  } errorBlock:^(NSError * _Nonnull error) {
3740fbf6a33SBen Roth#if DEBUG
3750fbf6a33SBen Roth    if ([EXShellManager sharedInstance].isShell && error && error.code == 404) {
3760fbf6a33SBen Roth      NSString *message = error.localizedDescription;
3770fbf6a33SBen Roth      message = [NSString stringWithFormat:@"Make sure you are serving your project from XDE or exp (%@)", message];
3780fbf6a33SBen Roth      error = [NSError errorWithDomain:error.domain code:error.code userInfo:@{ NSLocalizedDescriptionKey: message }];
3790fbf6a33SBen Roth    }
3800fbf6a33SBen Roth#endif
3810fbf6a33SBen Roth    failure(error);
3820fbf6a33SBen Roth  }];
3830fbf6a33SBen Roth}
3840fbf6a33SBen Roth
3850fbf6a33SBen Roth#pragma mark - App State
3860fbf6a33SBen Roth
3870fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification
3880fbf6a33SBen Roth{
3890fbf6a33SBen Roth  NSString *newState;
3900fbf6a33SBen Roth
3910fbf6a33SBen Roth  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
3920fbf6a33SBen Roth    newState = @"inactive";
3930fbf6a33SBen Roth  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
3940fbf6a33SBen Roth    newState = @"background";
3950fbf6a33SBen Roth  } else {
3960fbf6a33SBen Roth    switch (RCTSharedApplication().applicationState) {
3970fbf6a33SBen Roth      case UIApplicationStateActive:
3980fbf6a33SBen Roth        newState = @"active";
3990fbf6a33SBen Roth        break;
4000fbf6a33SBen Roth      case UIApplicationStateBackground: {
4010fbf6a33SBen Roth        newState = @"background";
4020fbf6a33SBen Roth        break;
4030fbf6a33SBen Roth      }
4040fbf6a33SBen Roth      default: {
4050fbf6a33SBen Roth        newState = @"unknown";
4060fbf6a33SBen Roth        break;
4070fbf6a33SBen Roth      }
4080fbf6a33SBen Roth    }
4090fbf6a33SBen Roth  }
4100fbf6a33SBen Roth
4110fbf6a33SBen Roth  if (_bridgeRegistry.lastKnownForegroundBridge) {
4120231e9c3SBen Roth    EXReactAppManager *appManager = [_bridgeRegistry lastKnownForegroundAppManager];
4135daa5350SBen Roth    id appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"];
4140fbf6a33SBen Roth    NSString *lastKnownState;
4150fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(lastKnownState)]) {
4160fbf6a33SBen Roth      lastKnownState = [appStateModule lastKnownState];
4170fbf6a33SBen Roth    }
4180fbf6a33SBen Roth    if ([appStateModule respondsToSelector:@selector(setState:)]) {
4190fbf6a33SBen Roth      [appStateModule setState:newState];
4200fbf6a33SBen Roth    }
4210fbf6a33SBen Roth    if (!lastKnownState || ![newState isEqualToString:lastKnownState]) {
4220fbf6a33SBen Roth      if ([newState isEqualToString:@"active"]) {
4230fbf6a33SBen Roth        [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:_bridgeRegistry.lastKnownForegroundBridge];
4240fbf6a33SBen Roth      } else if ([newState isEqualToString:@"background"]) {
4250fbf6a33SBen Roth        [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:_bridgeRegistry.lastKnownForegroundBridge];
4260fbf6a33SBen Roth      }
4270fbf6a33SBen Roth    }
4280fbf6a33SBen Roth  }
4290fbf6a33SBen Roth}
4300fbf6a33SBen Roth
4310fbf6a33SBen Roth@end
4320fbf6a33SBen Roth
4330fbf6a33SBen RothNS_ASSUME_NONNULL_END
434