xref: /expo/ios/Exponent/Kernel/Core/EXKernel.m (revision 27150200)
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"
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
3262cc3d03STomasz Sapeta// Protocol that should be implemented by all versions of EXAppState class.
3362cc3d03STomasz Sapeta@protocol EXAppStateProtocol
3462cc3d03STomasz Sapeta
3562cc3d03STomasz Sapeta@property (nonatomic, strong, readonly) NSString *lastKnownState;
3662cc3d03STomasz Sapeta
3762cc3d03STomasz Sapeta- (void)setState:(NSString *)state;
3862cc3d03STomasz Sapeta
3962cc3d03STomasz Sapeta@end
4062cc3d03STomasz Sapeta
410fbf6a33SBen Roth@implementation EXKernel
420fbf6a33SBen Roth
430fbf6a33SBen Roth+ (instancetype)sharedInstance
440fbf6a33SBen Roth{
450fbf6a33SBen Roth  static EXKernel *theKernel;
460fbf6a33SBen Roth  static dispatch_once_t once;
470fbf6a33SBen Roth  dispatch_once(&once, ^{
480fbf6a33SBen Roth    if (!theKernel) {
490fbf6a33SBen Roth      theKernel = [[EXKernel alloc] init];
500fbf6a33SBen Roth    }
510fbf6a33SBen Roth  });
520fbf6a33SBen Roth  return theKernel;
530fbf6a33SBen Roth}
540fbf6a33SBen Roth
550fbf6a33SBen Roth- (instancetype)init
560fbf6a33SBen Roth{
570fbf6a33SBen Roth  if (self = [super init]) {
5889c6c39cSEric Samelson    // init app registry: keep track of RN bridges we are running
5989c6c39cSEric Samelson    _appRegistry = [[EXKernelAppRegistry alloc] init];
6089c6c39cSEric Samelson    _appRegistry.delegate = self;
619f8d9b9dSBen Roth
629f8d9b9dSBen Roth    // init service registry: classes which manage shared resources among all bridges
63d7f51e5eSBen Roth    _serviceRegistry = [[EXKernelServiceRegistry alloc] init];
649f8d9b9dSBen Roth
650fbf6a33SBen Roth    for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
660fbf6a33SBen Roth                             UIApplicationDidEnterBackgroundNotification,
670fbf6a33SBen Roth                             UIApplicationDidFinishLaunchingNotification,
680fbf6a33SBen Roth                             UIApplicationWillResignActiveNotification,
690fbf6a33SBen Roth                             UIApplicationWillEnterForegroundNotification]) {
700fbf6a33SBen Roth
710fbf6a33SBen Roth      [[NSNotificationCenter defaultCenter] addObserver:self
720fbf6a33SBen Roth                                               selector:@selector(_handleAppStateDidChange:)
730fbf6a33SBen Roth                                                   name:name
740fbf6a33SBen Roth                                                 object:nil];
750fbf6a33SBen Roth    }
762c229102SBen Roth    NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion);
770fbf6a33SBen Roth  }
780fbf6a33SBen Roth  return self;
790fbf6a33SBen Roth}
800fbf6a33SBen Roth
810fbf6a33SBen Roth- (void)dealloc
820fbf6a33SBen Roth{
830fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] removeObserver:self];
840fbf6a33SBen Roth}
850fbf6a33SBen Roth
865daa5350SBen Roth#pragma mark - Misc
875daa5350SBen Roth
880fbf6a33SBen Roth+ (NSString *)deviceInstallUUID
890fbf6a33SBen Roth{
900fbf6a33SBen Roth  NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
910fbf6a33SBen Roth  if (!uuid) {
920fbf6a33SBen Roth    uuid = [[NSUUID UUID] UUIDString];
930fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
940fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] synchronize];
950fbf6a33SBen Roth  }
960fbf6a33SBen Roth  return uuid;
970fbf6a33SBen Roth}
980fbf6a33SBen Roth
9985f025b0SBen Roth- (void)logAnalyticsEvent:(NSString *)eventId forAppRecord:(EXKernelAppRecord *)appRecord
10085f025b0SBen Roth{
10185f025b0SBen Roth  if (_appRegistry.homeAppRecord && appRecord == _appRegistry.homeAppRecord) {
10285f025b0SBen Roth    return;
10385f025b0SBen Roth  }
10485f025b0SBen Roth  NSString *validatedSdkVersion = [[EXVersions sharedInstance] availableSdkVersionForManifest:appRecord.appLoader.manifest];
10585f025b0SBen Roth  NSDictionary *props = (validatedSdkVersion) ? @{ @"SDK_VERSION": validatedSdkVersion } : @{};
10685f025b0SBen Roth  [[EXAnalytics sharedInstance] logEvent:eventId
10785f025b0SBen Roth                             manifestUrl:appRecord.appLoader.manifestUrl
10885f025b0SBen Roth                         eventProperties:props];
10985f025b0SBen Roth}
11085f025b0SBen Roth
1119f8d9b9dSBen Roth#pragma mark - bridge registry delegate
1129f8d9b9dSBen Roth
11389c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord
1149f8d9b9dSBen Roth{
1159f8d9b9dSBen Roth  // forward to service registry
11689c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord];
1179f8d9b9dSBen Roth}
1189f8d9b9dSBen Roth
11989c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord
1209f8d9b9dSBen Roth{
1219f8d9b9dSBen Roth  // forward to service registry
12289c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord];
1239f8d9b9dSBen Roth}
1249f8d9b9dSBen Roth
125f5e2fbc5SBen Roth#pragma mark - Interfacing with JS
126f5e2fbc5SBen Roth
127f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app
128f5e2fbc5SBen Roth{
129f5e2fbc5SBen Roth  // fire a Linking url event on this (possibly versioned) bridge
130f5e2fbc5SBen Roth  EXReactAppManager *appManager = app.appManager;
131f5e2fbc5SBen Roth  id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"];
132f5e2fbc5SBen Roth  if (!linkingModule) {
133f5e2fbc5SBen Roth    DDLogError(@"Could not find the Linking module to open URL (%@)", urlString);
134f5e2fbc5SBen Roth  } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) {
135f5e2fbc5SBen Roth    [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]];
136f5e2fbc5SBen Roth  } else {
137f5e2fbc5SBen Roth    DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString);
138f5e2fbc5SBen Roth  }
139e7ac6e4fSBen Roth  [self _moveAppToVisible:app];
140f5e2fbc5SBen Roth}
1410fbf6a33SBen Roth
1425daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName
1430fbf6a33SBen Roth{
1440fbf6a33SBen Roth  id destinationBridge = appManager.reactBridge;
1450fbf6a33SBen Roth
1460fbf6a33SBen Roth  if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) {
1470fbf6a33SBen Roth    id batchedBridge = [destinationBridge batchedBridge];
1480fbf6a33SBen Roth    id moduleData = [batchedBridge moduleDataForName:moduleName];
1490fbf6a33SBen Roth
1500fbf6a33SBen Roth    // React Native before SDK 11 didn't strip the "RCT" prefix from module names
1510fbf6a33SBen Roth    if (!moduleData && ![moduleName hasPrefix:@"RCT"]) {
1520fbf6a33SBen Roth      moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]];
1530fbf6a33SBen Roth    }
1540fbf6a33SBen Roth
1550fbf6a33SBen Roth    if (moduleData) {
1560fbf6a33SBen Roth      return [moduleData instance];
1570fbf6a33SBen Roth    }
1580fbf6a33SBen Roth  } else {
159d2210950SBen Roth    // bridge can be null if the record is in an error state and never created a bridge.
160d2210950SBen Roth    if (destinationBridge) {
1610fbf6a33SBen Roth      DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge");
1620fbf6a33SBen Roth    }
163d2210950SBen Roth  }
1640fbf6a33SBen Roth  return nil;
1650fbf6a33SBen Roth}
1660fbf6a33SBen Roth
1677100de8cSSzymon20000- (BOOL)sendNotification:(EXPendingNotification *)notification
168c67a7dcdSBen Roth{
1697100de8cSSzymon20000  EXKernelAppRecord *destinationApp = [_appRegistry newestRecordWithExperienceId:notification.experienceId];
170*27150200STomasz Sapeta
171*27150200STomasz Sapeta  // This allows home app record to receive notification events as well.
172*27150200STomasz Sapeta  if (!destinationApp && [_appRegistry.homeAppRecord.experienceId isEqualToString:notification.experienceId]) {
173*27150200STomasz Sapeta    destinationApp = _appRegistry.homeAppRecord;
174*27150200STomasz Sapeta  }
175*27150200STomasz Sapeta
176c67a7dcdSBen Roth  if (destinationApp) {
177c67a7dcdSBen Roth    // send the body to the already-open experience
1787100de8cSSzymon20000    [self _dispatchJSEvent:@"Exponent.notification" body:notification.properties toApp:destinationApp];
179e7ac6e4fSBen Roth    [self _moveAppToVisible:destinationApp];
1807100de8cSSzymon20000    return YES;
181c67a7dcdSBen Roth  } else {
182c67a7dcdSBen Roth    // no app is currently running for this experience id.
183c67a7dcdSBen Roth    // if we're Expo Client, we can query Home for a past experience in the user's history, and route the notification there.
184c67a7dcdSBen Roth    if (_browserController) {
185c67a7dcdSBen Roth      __weak typeof(self) weakSelf = self;
1867100de8cSSzymon20000      [_browserController getHistoryUrlForExperienceId:notification.experienceId completion:^(NSString *urlString) {
187c67a7dcdSBen Roth        if (urlString) {
188c67a7dcdSBen Roth          NSURL *url = [NSURL URLWithString:urlString];
189c67a7dcdSBen Roth          if (url) {
1907100de8cSSzymon20000            [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": notification.properties }];
191c67a7dcdSBen Roth          }
192c67a7dcdSBen Roth        }
193c67a7dcdSBen Roth      }];
1947100de8cSSzymon20000      // If we're here, there's no active app in appRegistry matching notification.experienceId
1957100de8cSSzymon20000      // and we are in Expo Client, since _browserController is not nil.
1967100de8cSSzymon20000      // If so, we can return YES (meaning "notification has been successfully dispatched")
1977100de8cSSzymon20000      // because we pass the notification as initialProps in completion handler
1987100de8cSSzymon20000      // of getHistoryUrlForExperienceId:. If urlString passed to completion handler is empty,
1997100de8cSSzymon20000      // the notification is forgotten (this is the expected behavior).
2007100de8cSSzymon20000      return YES;
201c67a7dcdSBen Roth    }
202c67a7dcdSBen Roth  }
2037100de8cSSzymon20000
2047100de8cSSzymon20000  return NO;
205c67a7dcdSBen Roth}
206c67a7dcdSBen Roth
2070fbf6a33SBen Roth/**
2080fbf6a33SBen Roth *  If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well.
2090fbf6a33SBen Roth */
2100fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge
2110fbf6a33SBen Roth{
2120fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge];
2130fbf6a33SBen Roth  if ([bridge respondsToSelector:@selector(batchedBridge)]) {
2140fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]];
2150fbf6a33SBen Roth  } else if ([bridge respondsToSelector:@selector(parentBridge)]) {
2160fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]];
2170fbf6a33SBen Roth  }
2180fbf6a33SBen Roth}
2190fbf6a33SBen Roth
220c67a7dcdSBen Roth- (void)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord
221f148175aSBen Roth{
222c67a7dcdSBen Roth  [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
223c67a7dcdSBen Roth                                             args:eventBody ? @[eventName, eventBody] : @[eventName]];
2240fbf6a33SBen Roth}
2250fbf6a33SBen Roth
226d699b379SBen Roth#pragma mark - App props
227d699b379SBen Roth
2287100de8cSSzymon20000- (nullable NSDictionary *)initialAppPropsFromLaunchOptions:(NSDictionary *)launchOptions
229d699b379SBen Roth{
2307100de8cSSzymon20000  return nil;
231d699b379SBen Roth}
232d699b379SBen Roth
2338a7f6184SBen Roth#pragma mark - App State
2348e3c2ccdSBen Roth
23589fd6b24SBen Roth- (EXKernelAppRecord *)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps
236f5e2fbc5SBen Roth{
237c67a7dcdSBen Roth  NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps];
238f5e2fbc5SBen Roth  EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
239e7ac6e4fSBen Roth  [self _moveAppToVisible:record];
24089fd6b24SBen Roth  return record;
241f5e2fbc5SBen Roth}
242f5e2fbc5SBen Roth
2437f6367d0SBen Roth- (void)switchTasks
2440fbf6a33SBen Roth{
2457f6367d0SBen Roth  if (!_browserController) {
2467f6367d0SBen Roth    return;
2476f62abf2SBen Roth  }
2486f62abf2SBen Roth
2497f6367d0SBen Roth  if (_visibleApp != _appRegistry.homeAppRecord) {
2507f6367d0SBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
251d718b4b8SBen Roth      [self->_browserController toggleMenuWithCompletion:nil];
2527f6367d0SBen Roth    }];
253b05926a2SBen Roth  } else {
2547f6367d0SBen Roth    EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry;
2557f6367d0SBen Roth    for (NSString *recordId in appRegistry.appEnumerator) {
2567f6367d0SBen Roth      EXKernelAppRecord *record = [appRegistry recordForId:recordId];
2577f6367d0SBen Roth      // foreground the first thing we find
258e7ac6e4fSBen Roth      [self _moveAppToVisible:record];
2597f6367d0SBen Roth    }
2607f6367d0SBen Roth  }
2617f6367d0SBen Roth}
2627f6367d0SBen Roth
263928bc3baSBen Roth- (void)reloadAppWithExperienceId:(NSString *)experienceId
264928bc3baSBen Roth{
265928bc3baSBen Roth  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
266928bc3baSBen Roth  if (_browserController) {
267928bc3baSBen Roth    [self createNewAppWithUrl:appRecord.appLoader.manifestUrl initialProps:nil];
268928bc3baSBen Roth  } else if (_appRegistry.standaloneAppRecord && appRecord == _appRegistry.standaloneAppRecord) {
269928bc3baSBen Roth    [appRecord.viewController refresh];
270928bc3baSBen Roth  }
271928bc3baSBen Roth}
272928bc3baSBen Roth
2739777fddbSEric Samelson- (void)reloadAppFromCacheWithExperienceId:(NSString *)experienceId
2749777fddbSEric Samelson{
2759777fddbSEric Samelson  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
2769777fddbSEric Samelson  [appRecord.viewController reloadFromCache];
2779777fddbSEric Samelson}
2789777fddbSEric Samelson
279e7ac6e4fSBen Roth- (void)viewController:(__unused EXViewController *)vc didNavigateAppToVisible:(EXKernelAppRecord *)appRecord
2807f6367d0SBen Roth{
2817f6367d0SBen Roth  EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp;
2827f6367d0SBen Roth  if (appRecord != appRecordPreviouslyVisible) {
2837f6367d0SBen Roth    if (appRecordPreviouslyVisible) {
284e7ac6e4fSBen Roth      [appRecordPreviouslyVisible.viewController appStateDidBecomeInactive];
2857f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge];
28662cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"];
28762cc3d03STomasz Sapeta      if (appStateModule != nil) {
2880fbf6a33SBen Roth        [appStateModule setState:@"background"];
2890fbf6a33SBen Roth      }
2900fbf6a33SBen Roth    }
2917f6367d0SBen Roth    if (appRecord) {
292e7ac6e4fSBen Roth      [appRecord.viewController appStateDidBecomeActive];
2937f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge];
29462cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"];
29562cc3d03STomasz Sapeta      if (appStateModule != nil) {
2960fbf6a33SBen Roth        [appStateModule setState:@"active"];
2970fbf6a33SBen Roth      }
2987f6367d0SBen Roth      _visibleApp = appRecord;
29985f025b0SBen Roth      [[EXAnalytics sharedInstance] logAppVisibleEvent];
3000fbf6a33SBen Roth    } else {
3017f6367d0SBen Roth      _visibleApp = nil;
3027f6367d0SBen Roth    }
3037f6367d0SBen Roth
3047f6367d0SBen Roth    if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) {
3057f6367d0SBen Roth      [self _unregisterUnusedAppRecords];
3067f6367d0SBen Roth    }
3077f6367d0SBen Roth  }
3087f6367d0SBen Roth}
3097f6367d0SBen Roth
3107f6367d0SBen Roth- (void)_unregisterUnusedAppRecords
3117f6367d0SBen Roth{
3127f6367d0SBen Roth  for (NSString *recordId in _appRegistry.appEnumerator) {
3137f6367d0SBen Roth    EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
3147f6367d0SBen Roth    if (record && record != _visibleApp) {
3157f6367d0SBen Roth      [_appRegistry unregisterAppWithRecordId:recordId];
3167f6367d0SBen Roth      break;
3170fbf6a33SBen Roth    }
3180fbf6a33SBen Roth  }
319b05926a2SBen Roth}
3200fbf6a33SBen Roth
3210fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification
3220fbf6a33SBen Roth{
3230fbf6a33SBen Roth  NSString *newState;
3240fbf6a33SBen Roth
3250fbf6a33SBen Roth  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
3260fbf6a33SBen Roth    newState = @"inactive";
3270fbf6a33SBen Roth  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
3280fbf6a33SBen Roth    newState = @"background";
3290fbf6a33SBen Roth  } else {
3300fbf6a33SBen Roth    switch (RCTSharedApplication().applicationState) {
3310fbf6a33SBen Roth      case UIApplicationStateActive:
3320fbf6a33SBen Roth        newState = @"active";
3330fbf6a33SBen Roth        break;
3340fbf6a33SBen Roth      case UIApplicationStateBackground: {
3350fbf6a33SBen Roth        newState = @"background";
3360fbf6a33SBen Roth        break;
3370fbf6a33SBen Roth      }
3380fbf6a33SBen Roth      default: {
3390fbf6a33SBen Roth        newState = @"unknown";
3400fbf6a33SBen Roth        break;
3410fbf6a33SBen Roth      }
3420fbf6a33SBen Roth    }
3430fbf6a33SBen Roth  }
3440fbf6a33SBen Roth
3457f6367d0SBen Roth  if (_visibleApp) {
3467f6367d0SBen Roth    EXReactAppManager *appManager = _visibleApp.appManager;
34762cc3d03STomasz Sapeta    id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"];
3480fbf6a33SBen Roth    NSString *lastKnownState;
34962cc3d03STomasz Sapeta    if (appStateModule != nil) {
3500fbf6a33SBen Roth      lastKnownState = [appStateModule lastKnownState];
3510fbf6a33SBen Roth      [appStateModule setState:newState];
3520fbf6a33SBen Roth    }
3530fbf6a33SBen Roth    if (!lastKnownState || ![newState isEqualToString:lastKnownState]) {
3540fbf6a33SBen Roth      if ([newState isEqualToString:@"active"]) {
355e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeActive];
3567f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge];
3570fbf6a33SBen Roth      } else if ([newState isEqualToString:@"background"]) {
358e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeInactive];
3597f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge];
3600fbf6a33SBen Roth      }
3610fbf6a33SBen Roth    }
3620fbf6a33SBen Roth  }
3630fbf6a33SBen Roth}
3640fbf6a33SBen Roth
365e7ac6e4fSBen Roth- (void)_moveAppToVisible:(EXKernelAppRecord *)appRecord
366e7ac6e4fSBen Roth{
367e7ac6e4fSBen Roth  if (_browserController) {
368e7ac6e4fSBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
369d718b4b8SBen Roth      [self->_browserController moveAppToVisible:appRecord];
370e7ac6e4fSBen Roth    }];
371e7ac6e4fSBen Roth  }
372e7ac6e4fSBen Roth}
373e7ac6e4fSBen Roth
3740fbf6a33SBen Roth@end
3750fbf6a33SBen Roth
3760fbf6a33SBen RothNS_ASSUME_NONNULL_END
377