xref: /expo/ios/Exponent/Kernel/Core/EXKernel.m (revision ea7a561b)
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"
89088dd0eSBen Roth#import "EXAppLoader.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
210fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN
220fbf6a33SBen Roth
23*ea7a561bSBartłomiej Bukowskistatic const NSString *kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey";
24*ea7a561bSBartłomiej Bukowski
250fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain";
260fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask";
270fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey";
28d0248f72SBrent VatneNSString * const kEXReloadActiveAppRequest = @"EXReloadActiveAppRequest";
290fbf6a33SBen Roth
3089c6c39cSEric Samelson@interface EXKernel () <EXKernelAppRegistryDelegate>
310fbf6a33SBen Roth
320fbf6a33SBen Roth@end
330fbf6a33SBen Roth
3462cc3d03STomasz Sapeta// Protocol that should be implemented by all versions of EXAppState class.
3562cc3d03STomasz Sapeta@protocol EXAppStateProtocol
3662cc3d03STomasz Sapeta
3762cc3d03STomasz Sapeta@property (nonatomic, strong, readonly) NSString *lastKnownState;
3862cc3d03STomasz Sapeta
3962cc3d03STomasz Sapeta- (void)setState:(NSString *)state;
4062cc3d03STomasz Sapeta
4162cc3d03STomasz Sapeta@end
4262cc3d03STomasz Sapeta
430fbf6a33SBen Roth@implementation EXKernel
440fbf6a33SBen Roth
450fbf6a33SBen Roth+ (instancetype)sharedInstance
460fbf6a33SBen Roth{
470fbf6a33SBen Roth  static EXKernel *theKernel;
480fbf6a33SBen Roth  static dispatch_once_t once;
490fbf6a33SBen Roth  dispatch_once(&once, ^{
500fbf6a33SBen Roth    if (!theKernel) {
510fbf6a33SBen Roth      theKernel = [[EXKernel alloc] init];
520fbf6a33SBen Roth    }
530fbf6a33SBen Roth  });
540fbf6a33SBen Roth  return theKernel;
550fbf6a33SBen Roth}
560fbf6a33SBen Roth
570fbf6a33SBen Roth- (instancetype)init
580fbf6a33SBen Roth{
590fbf6a33SBen Roth  if (self = [super init]) {
6089c6c39cSEric Samelson    // init app registry: keep track of RN bridges we are running
6189c6c39cSEric Samelson    _appRegistry = [[EXKernelAppRegistry alloc] init];
6289c6c39cSEric Samelson    _appRegistry.delegate = self;
639f8d9b9dSBen Roth
649f8d9b9dSBen Roth    // init service registry: classes which manage shared resources among all bridges
65d7f51e5eSBen Roth    _serviceRegistry = [[EXKernelServiceRegistry alloc] init];
669f8d9b9dSBen Roth
67d0248f72SBrent Vatne    // register for notifications to request reloading the visible app
68d0248f72SBrent Vatne    [[NSNotificationCenter defaultCenter] addObserver:self
69d0248f72SBrent Vatne                                             selector:@selector(_handleRequestReloadVisibleApp:)
70d0248f72SBrent Vatne                                                 name:kEXReloadActiveAppRequest
71d0248f72SBrent Vatne                                               object:nil];
72d0248f72SBrent Vatne
730fbf6a33SBen Roth    for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
740fbf6a33SBen Roth                             UIApplicationDidEnterBackgroundNotification,
750fbf6a33SBen Roth                             UIApplicationDidFinishLaunchingNotification,
760fbf6a33SBen Roth                             UIApplicationWillResignActiveNotification,
770fbf6a33SBen Roth                             UIApplicationWillEnterForegroundNotification]) {
780fbf6a33SBen Roth
790fbf6a33SBen Roth      [[NSNotificationCenter defaultCenter] addObserver:self
800fbf6a33SBen Roth                                               selector:@selector(_handleAppStateDidChange:)
810fbf6a33SBen Roth                                                   name:name
820fbf6a33SBen Roth                                                 object:nil];
830fbf6a33SBen Roth    }
842c229102SBen Roth    NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion);
850fbf6a33SBen Roth  }
860fbf6a33SBen Roth  return self;
870fbf6a33SBen Roth}
880fbf6a33SBen Roth
890fbf6a33SBen Roth- (void)dealloc
900fbf6a33SBen Roth{
910fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] removeObserver:self];
920fbf6a33SBen Roth}
930fbf6a33SBen Roth
945daa5350SBen Roth#pragma mark - Misc
955daa5350SBen Roth
960fbf6a33SBen Roth+ (NSString *)deviceInstallUUID
970fbf6a33SBen Roth{
980fbf6a33SBen Roth  NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
990fbf6a33SBen Roth  if (!uuid) {
1000fbf6a33SBen Roth    uuid = [[NSUUID UUID] UUIDString];
1010fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
1020fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] synchronize];
1030fbf6a33SBen Roth  }
1040fbf6a33SBen Roth  return uuid;
1050fbf6a33SBen Roth}
1060fbf6a33SBen Roth
10785f025b0SBen Roth- (void)logAnalyticsEvent:(NSString *)eventId forAppRecord:(EXKernelAppRecord *)appRecord
10885f025b0SBen Roth{
10985f025b0SBen Roth  if (_appRegistry.homeAppRecord && appRecord == _appRegistry.homeAppRecord) {
11085f025b0SBen Roth    return;
11185f025b0SBen Roth  }
11285f025b0SBen Roth  NSString *validatedSdkVersion = [[EXVersions sharedInstance] availableSdkVersionForManifest:appRecord.appLoader.manifest];
11385f025b0SBen Roth  NSDictionary *props = (validatedSdkVersion) ? @{ @"SDK_VERSION": validatedSdkVersion } : @{};
11485f025b0SBen Roth  [[EXAnalytics sharedInstance] logEvent:eventId
11585f025b0SBen Roth                             manifestUrl:appRecord.appLoader.manifestUrl
11685f025b0SBen Roth                         eventProperties:props];
11785f025b0SBen Roth}
11885f025b0SBen Roth
1199f8d9b9dSBen Roth#pragma mark - bridge registry delegate
1209f8d9b9dSBen Roth
12189c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord
1229f8d9b9dSBen Roth{
1239f8d9b9dSBen Roth  // forward to service registry
12489c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord];
1259f8d9b9dSBen Roth}
1269f8d9b9dSBen Roth
12789c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord
1289f8d9b9dSBen Roth{
1299f8d9b9dSBen Roth  // forward to service registry
13089c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord];
1319f8d9b9dSBen Roth}
1329f8d9b9dSBen Roth
133f5e2fbc5SBen Roth#pragma mark - Interfacing with JS
134f5e2fbc5SBen Roth
135f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app
136f5e2fbc5SBen Roth{
137f5e2fbc5SBen Roth  // fire a Linking url event on this (possibly versioned) bridge
138f5e2fbc5SBen Roth  EXReactAppManager *appManager = app.appManager;
139f5e2fbc5SBen Roth  id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"];
140f5e2fbc5SBen Roth  if (!linkingModule) {
141f5e2fbc5SBen Roth    DDLogError(@"Could not find the Linking module to open URL (%@)", urlString);
142f5e2fbc5SBen Roth  } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) {
143f5e2fbc5SBen Roth    [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]];
144f5e2fbc5SBen Roth  } else {
145f5e2fbc5SBen Roth    DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString);
146f5e2fbc5SBen Roth  }
147e7ac6e4fSBen Roth  [self _moveAppToVisible:app];
148f5e2fbc5SBen Roth}
1490fbf6a33SBen Roth
1505daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName
1510fbf6a33SBen Roth{
1520fbf6a33SBen Roth  id destinationBridge = appManager.reactBridge;
1530fbf6a33SBen Roth
1540fbf6a33SBen Roth  if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) {
1550fbf6a33SBen Roth    id batchedBridge = [destinationBridge batchedBridge];
1560fbf6a33SBen Roth    id moduleData = [batchedBridge moduleDataForName:moduleName];
1570fbf6a33SBen Roth
1580fbf6a33SBen Roth    // React Native before SDK 11 didn't strip the "RCT" prefix from module names
1590fbf6a33SBen Roth    if (!moduleData && ![moduleName hasPrefix:@"RCT"]) {
1600fbf6a33SBen Roth      moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]];
1610fbf6a33SBen Roth    }
1620fbf6a33SBen Roth
1630fbf6a33SBen Roth    if (moduleData) {
1640fbf6a33SBen Roth      return [moduleData instance];
1650fbf6a33SBen Roth    }
1660fbf6a33SBen Roth  } else {
167d2210950SBen Roth    // bridge can be null if the record is in an error state and never created a bridge.
168d2210950SBen Roth    if (destinationBridge) {
1690fbf6a33SBen Roth      DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge");
1700fbf6a33SBen Roth    }
171d2210950SBen Roth  }
1720fbf6a33SBen Roth  return nil;
1730fbf6a33SBen Roth}
1740fbf6a33SBen Roth
1757100de8cSSzymon20000- (BOOL)sendNotification:(EXPendingNotification *)notification
176c67a7dcdSBen Roth{
1771684b5d5SStanisław Chmiela  EXKernelAppRecord *destinationApp = [_appRegistry standaloneAppRecord] ?: [_appRegistry newestRecordWithExperienceId:notification.experienceId];
17827150200STomasz Sapeta
17927150200STomasz Sapeta  // This allows home app record to receive notification events as well.
18027150200STomasz Sapeta  if (!destinationApp && [_appRegistry.homeAppRecord.experienceId isEqualToString:notification.experienceId]) {
18127150200STomasz Sapeta    destinationApp = _appRegistry.homeAppRecord;
18227150200STomasz Sapeta  }
18327150200STomasz Sapeta
184c67a7dcdSBen Roth  if (destinationApp) {
185c67a7dcdSBen Roth    // send the body to the already-open experience
1867e7b37d3SEric Samelson    BOOL success = [self _dispatchJSEvent:@"Exponent.notification" body:notification.properties toApp:destinationApp];
187e7ac6e4fSBen Roth    [self _moveAppToVisible:destinationApp];
1887e7b37d3SEric Samelson    return success;
189c67a7dcdSBen Roth  } else {
190c67a7dcdSBen Roth    // no app is currently running for this experience id.
191c67a7dcdSBen Roth    // if we're Expo Client, we can query Home for a past experience in the user's history, and route the notification there.
192c67a7dcdSBen Roth    if (_browserController) {
193c67a7dcdSBen Roth      __weak typeof(self) weakSelf = self;
1947100de8cSSzymon20000      [_browserController getHistoryUrlForExperienceId:notification.experienceId completion:^(NSString *urlString) {
195c67a7dcdSBen Roth        if (urlString) {
196c67a7dcdSBen Roth          NSURL *url = [NSURL URLWithString:urlString];
197c67a7dcdSBen Roth          if (url) {
1987100de8cSSzymon20000            [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": notification.properties }];
199c67a7dcdSBen Roth          }
200c67a7dcdSBen Roth        }
201c67a7dcdSBen Roth      }];
2027100de8cSSzymon20000      // If we're here, there's no active app in appRegistry matching notification.experienceId
2037100de8cSSzymon20000      // and we are in Expo Client, since _browserController is not nil.
2047100de8cSSzymon20000      // If so, we can return YES (meaning "notification has been successfully dispatched")
2057100de8cSSzymon20000      // because we pass the notification as initialProps in completion handler
2067100de8cSSzymon20000      // of getHistoryUrlForExperienceId:. If urlString passed to completion handler is empty,
2077100de8cSSzymon20000      // the notification is forgotten (this is the expected behavior).
2087100de8cSSzymon20000      return YES;
209c67a7dcdSBen Roth    }
210c67a7dcdSBen Roth  }
2117100de8cSSzymon20000
2127100de8cSSzymon20000  return NO;
213c67a7dcdSBen Roth}
214c67a7dcdSBen Roth
2150fbf6a33SBen Roth/**
2160fbf6a33SBen Roth *  If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well.
2170fbf6a33SBen Roth */
2180fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge
2190fbf6a33SBen Roth{
2200fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge];
2210fbf6a33SBen Roth  if ([bridge respondsToSelector:@selector(batchedBridge)]) {
2220fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]];
2230fbf6a33SBen Roth  } else if ([bridge respondsToSelector:@selector(parentBridge)]) {
2240fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]];
2250fbf6a33SBen Roth  }
2260fbf6a33SBen Roth}
2270fbf6a33SBen Roth
2287e7b37d3SEric Samelson- (BOOL)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord
229f148175aSBen Roth{
2307e7b37d3SEric Samelson  if (!appRecord.appManager.reactBridge) {
2317e7b37d3SEric Samelson    return NO;
2327e7b37d3SEric Samelson  }
233c67a7dcdSBen Roth  [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
234c67a7dcdSBen Roth                                             args:eventBody ? @[eventName, eventBody] : @[eventName]];
2357e7b37d3SEric Samelson  return YES;
2360fbf6a33SBen Roth}
2370fbf6a33SBen Roth
238d699b379SBen Roth#pragma mark - App props
239d699b379SBen Roth
2407100de8cSSzymon20000- (nullable NSDictionary *)initialAppPropsFromLaunchOptions:(NSDictionary *)launchOptions
241d699b379SBen Roth{
2427100de8cSSzymon20000  return nil;
243d699b379SBen Roth}
244d699b379SBen Roth
2458a7f6184SBen Roth#pragma mark - App State
2468e3c2ccdSBen Roth
24789fd6b24SBen Roth- (EXKernelAppRecord *)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps
248f5e2fbc5SBen Roth{
249c67a7dcdSBen Roth  NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps];
250f5e2fbc5SBen Roth  EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
251e7ac6e4fSBen Roth  [self _moveAppToVisible:record];
25289fd6b24SBen Roth  return record;
253f5e2fbc5SBen Roth}
254f5e2fbc5SBen Roth
255d0248f72SBrent Vatne
256b333b418SBrent Vatne- (void)reloadVisibleApp
257b333b418SBrent Vatne{
258b333b418SBrent Vatne  if (_browserController) {
259b333b418SBrent Vatne    [EXUtil performSynchronouslyOnMainThread:^{
260b333b418SBrent Vatne      [self->_browserController reloadVisibleApp];
261b333b418SBrent Vatne    }];
262b333b418SBrent Vatne  }
263b333b418SBrent Vatne}
264b333b418SBrent Vatne
2657f6367d0SBen Roth- (void)switchTasks
2660fbf6a33SBen Roth{
2677f6367d0SBen Roth  if (!_browserController) {
2687f6367d0SBen Roth    return;
2696f62abf2SBen Roth  }
2706f62abf2SBen Roth
2717f6367d0SBen Roth  if (_visibleApp != _appRegistry.homeAppRecord) {
2727f6367d0SBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
27324a0cefbSTomasz Sapeta      if ([self->_browserController isMenuVisible]) {
27424a0cefbSTomasz Sapeta        EXHomeModule *homeModule = [self nativeModuleForAppManager:self->_appRegistry.homeAppRecord.appManager named:@"ExponentKernel"];
27524a0cefbSTomasz Sapeta        [homeModule requestToCloseDevMenu];
27624a0cefbSTomasz Sapeta      } else {
277d718b4b8SBen Roth        [self->_browserController toggleMenuWithCompletion:nil];
27824a0cefbSTomasz Sapeta      }
2797f6367d0SBen Roth    }];
280b05926a2SBen Roth  } else {
2817f6367d0SBen Roth    EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry;
2827f6367d0SBen Roth    for (NSString *recordId in appRegistry.appEnumerator) {
2837f6367d0SBen Roth      EXKernelAppRecord *record = [appRegistry recordForId:recordId];
2847f6367d0SBen Roth      // foreground the first thing we find
285e7ac6e4fSBen Roth      [self _moveAppToVisible:record];
2867f6367d0SBen Roth    }
2877f6367d0SBen Roth  }
2887f6367d0SBen Roth}
2897f6367d0SBen Roth
290928bc3baSBen Roth- (void)reloadAppWithExperienceId:(NSString *)experienceId
291928bc3baSBen Roth{
292928bc3baSBen Roth  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
293928bc3baSBen Roth  if (_browserController) {
294928bc3baSBen Roth    [self createNewAppWithUrl:appRecord.appLoader.manifestUrl initialProps:nil];
295928bc3baSBen Roth  } else if (_appRegistry.standaloneAppRecord && appRecord == _appRegistry.standaloneAppRecord) {
296928bc3baSBen Roth    [appRecord.viewController refresh];
297928bc3baSBen Roth  }
298928bc3baSBen Roth}
299928bc3baSBen Roth
3009777fddbSEric Samelson- (void)reloadAppFromCacheWithExperienceId:(NSString *)experienceId
3019777fddbSEric Samelson{
3029777fddbSEric Samelson  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
3039777fddbSEric Samelson  [appRecord.viewController reloadFromCache];
3049777fddbSEric Samelson}
3059777fddbSEric Samelson
306e7ac6e4fSBen Roth- (void)viewController:(__unused EXViewController *)vc didNavigateAppToVisible:(EXKernelAppRecord *)appRecord
3077f6367d0SBen Roth{
3087f6367d0SBen Roth  EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp;
3097f6367d0SBen Roth  if (appRecord != appRecordPreviouslyVisible) {
3107f6367d0SBen Roth    if (appRecordPreviouslyVisible) {
311e7ac6e4fSBen Roth      [appRecordPreviouslyVisible.viewController appStateDidBecomeInactive];
3127f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge];
31362cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"];
31462cc3d03STomasz Sapeta      if (appStateModule != nil) {
3150fbf6a33SBen Roth        [appStateModule setState:@"background"];
3160fbf6a33SBen Roth      }
3170fbf6a33SBen Roth    }
3187f6367d0SBen Roth    if (appRecord) {
319e7ac6e4fSBen Roth      [appRecord.viewController appStateDidBecomeActive];
3207f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge];
32162cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"];
32262cc3d03STomasz Sapeta      if (appStateModule != nil) {
3230fbf6a33SBen Roth        [appStateModule setState:@"active"];
3240fbf6a33SBen Roth      }
3257f6367d0SBen Roth      _visibleApp = appRecord;
32685f025b0SBen Roth      [[EXAnalytics sharedInstance] logAppVisibleEvent];
3270fbf6a33SBen Roth    } else {
3287f6367d0SBen Roth      _visibleApp = nil;
3297f6367d0SBen Roth    }
3307f6367d0SBen Roth
3317f6367d0SBen Roth    if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) {
3327f6367d0SBen Roth      [self _unregisterUnusedAppRecords];
3337f6367d0SBen Roth    }
3347f6367d0SBen Roth  }
3357f6367d0SBen Roth}
3367f6367d0SBen Roth
3377f6367d0SBen Roth- (void)_unregisterUnusedAppRecords
3387f6367d0SBen Roth{
3397f6367d0SBen Roth  for (NSString *recordId in _appRegistry.appEnumerator) {
3407f6367d0SBen Roth    EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
3417f6367d0SBen Roth    if (record && record != _visibleApp) {
3427f6367d0SBen Roth      [_appRegistry unregisterAppWithRecordId:recordId];
3437f6367d0SBen Roth      break;
3440fbf6a33SBen Roth    }
3450fbf6a33SBen Roth  }
346b05926a2SBen Roth}
3470fbf6a33SBen Roth
3480fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification
3490fbf6a33SBen Roth{
3500fbf6a33SBen Roth  NSString *newState;
3510fbf6a33SBen Roth
3520fbf6a33SBen Roth  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
3530fbf6a33SBen Roth    newState = @"inactive";
3540fbf6a33SBen Roth  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
3550fbf6a33SBen Roth    newState = @"background";
3560fbf6a33SBen Roth  } else {
3570fbf6a33SBen Roth    switch (RCTSharedApplication().applicationState) {
3580fbf6a33SBen Roth      case UIApplicationStateActive:
3590fbf6a33SBen Roth        newState = @"active";
3600fbf6a33SBen Roth        break;
3610fbf6a33SBen Roth      case UIApplicationStateBackground: {
3620fbf6a33SBen Roth        newState = @"background";
3630fbf6a33SBen Roth        break;
3640fbf6a33SBen Roth      }
3650fbf6a33SBen Roth      default: {
3660fbf6a33SBen Roth        newState = @"unknown";
3670fbf6a33SBen Roth        break;
3680fbf6a33SBen Roth      }
3690fbf6a33SBen Roth    }
3700fbf6a33SBen Roth  }
3710fbf6a33SBen Roth
3727f6367d0SBen Roth  if (_visibleApp) {
3737f6367d0SBen Roth    EXReactAppManager *appManager = _visibleApp.appManager;
37462cc3d03STomasz Sapeta    id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"];
3750fbf6a33SBen Roth    NSString *lastKnownState;
37662cc3d03STomasz Sapeta    if (appStateModule != nil) {
3770fbf6a33SBen Roth      lastKnownState = [appStateModule lastKnownState];
3780fbf6a33SBen Roth      [appStateModule setState:newState];
3790fbf6a33SBen Roth    }
3800fbf6a33SBen Roth    if (!lastKnownState || ![newState isEqualToString:lastKnownState]) {
3810fbf6a33SBen Roth      if ([newState isEqualToString:@"active"]) {
382e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeActive];
3837f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge];
3840fbf6a33SBen Roth      } else if ([newState isEqualToString:@"background"]) {
385e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeInactive];
3867f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge];
3870fbf6a33SBen Roth      }
3880fbf6a33SBen Roth    }
3890fbf6a33SBen Roth  }
3900fbf6a33SBen Roth}
3910fbf6a33SBen Roth
392d0248f72SBrent Vatne
393d0248f72SBrent Vatne- (void)_handleRequestReloadVisibleApp:(NSNotification *)notification
394d0248f72SBrent Vatne{
395d0248f72SBrent Vatne  [self reloadVisibleApp];
396d0248f72SBrent Vatne}
397d0248f72SBrent Vatne
398e7ac6e4fSBen Roth- (void)_moveAppToVisible:(EXKernelAppRecord *)appRecord
399e7ac6e4fSBen Roth{
400e7ac6e4fSBen Roth  if (_browserController) {
401e7ac6e4fSBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
402d718b4b8SBen Roth      [self->_browserController moveAppToVisible:appRecord];
403e7ac6e4fSBen Roth    }];
404e7ac6e4fSBen Roth  }
405e7ac6e4fSBen Roth}
406e7ac6e4fSBen Roth
4070fbf6a33SBen Roth@end
4080fbf6a33SBen Roth
4090fbf6a33SBen RothNS_ASSUME_NONNULL_END
410