xref: /expo/ios/Exponent/Kernel/Core/EXKernel.m (revision 05aa4e6c)
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
21*05aa4e6cSTomasz Sapeta#ifndef EX_DETACHED
22*05aa4e6cSTomasz Sapeta// Kernel is DevMenu's delegate only in non-detached builds.
23*05aa4e6cSTomasz Sapeta#import "EXDevMenuManager.h"
24*05aa4e6cSTomasz Sapeta#import "EXDevMenuDelegateProtocol.h"
25*05aa4e6cSTomasz Sapeta
26*05aa4e6cSTomasz Sapeta@interface EXKernel () <EXDevMenuDelegateProtocol>
27*05aa4e6cSTomasz Sapeta@end
28*05aa4e6cSTomasz Sapeta#endif
29*05aa4e6cSTomasz Sapeta
300fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN
310fbf6a33SBen Roth
32386119d0SHein Rutjesstatic NSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey";
33ea7a561bSBartłomiej Bukowski
340fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain";
350fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask";
360fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey";
37d0248f72SBrent VatneNSString * const kEXReloadActiveAppRequest = @"EXReloadActiveAppRequest";
380fbf6a33SBen Roth
3989c6c39cSEric Samelson@interface EXKernel () <EXKernelAppRegistryDelegate>
400fbf6a33SBen Roth
410fbf6a33SBen Roth@end
420fbf6a33SBen Roth
4362cc3d03STomasz Sapeta// Protocol that should be implemented by all versions of EXAppState class.
4462cc3d03STomasz Sapeta@protocol EXAppStateProtocol
4562cc3d03STomasz Sapeta
4662cc3d03STomasz Sapeta@property (nonatomic, strong, readonly) NSString *lastKnownState;
4762cc3d03STomasz Sapeta
4862cc3d03STomasz Sapeta- (void)setState:(NSString *)state;
4962cc3d03STomasz Sapeta
5062cc3d03STomasz Sapeta@end
5162cc3d03STomasz Sapeta
520fbf6a33SBen Roth@implementation EXKernel
530fbf6a33SBen Roth
540fbf6a33SBen Roth+ (instancetype)sharedInstance
550fbf6a33SBen Roth{
560fbf6a33SBen Roth  static EXKernel *theKernel;
570fbf6a33SBen Roth  static dispatch_once_t once;
580fbf6a33SBen Roth  dispatch_once(&once, ^{
590fbf6a33SBen Roth    if (!theKernel) {
600fbf6a33SBen Roth      theKernel = [[EXKernel alloc] init];
610fbf6a33SBen Roth    }
620fbf6a33SBen Roth  });
630fbf6a33SBen Roth  return theKernel;
640fbf6a33SBen Roth}
650fbf6a33SBen Roth
660fbf6a33SBen Roth- (instancetype)init
670fbf6a33SBen Roth{
680fbf6a33SBen Roth  if (self = [super init]) {
6989c6c39cSEric Samelson    // init app registry: keep track of RN bridges we are running
7089c6c39cSEric Samelson    _appRegistry = [[EXKernelAppRegistry alloc] init];
7189c6c39cSEric Samelson    _appRegistry.delegate = self;
729f8d9b9dSBen Roth
739f8d9b9dSBen Roth    // init service registry: classes which manage shared resources among all bridges
74d7f51e5eSBen Roth    _serviceRegistry = [[EXKernelServiceRegistry alloc] init];
759f8d9b9dSBen Roth
76*05aa4e6cSTomasz Sapeta#ifndef EX_DETACHED
77f67462bcSTomasz Sapeta    // Set the delegate of dev menu manager. Maybe it should be a separate class? Will see later once the delegate protocol gets too big.
78f67462bcSTomasz Sapeta    [[EXDevMenuManager sharedInstance] setDelegate:self];
79*05aa4e6cSTomasz Sapeta#endif
80f67462bcSTomasz Sapeta
81d0248f72SBrent Vatne    // register for notifications to request reloading the visible app
82d0248f72SBrent Vatne    [[NSNotificationCenter defaultCenter] addObserver:self
83d0248f72SBrent Vatne                                             selector:@selector(_handleRequestReloadVisibleApp:)
84d0248f72SBrent Vatne                                                 name:kEXReloadActiveAppRequest
85d0248f72SBrent Vatne                                               object:nil];
86d0248f72SBrent Vatne
870fbf6a33SBen Roth    for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
880fbf6a33SBen Roth                             UIApplicationDidEnterBackgroundNotification,
890fbf6a33SBen Roth                             UIApplicationDidFinishLaunchingNotification,
900fbf6a33SBen Roth                             UIApplicationWillResignActiveNotification,
910fbf6a33SBen Roth                             UIApplicationWillEnterForegroundNotification]) {
920fbf6a33SBen Roth
930fbf6a33SBen Roth      [[NSNotificationCenter defaultCenter] addObserver:self
940fbf6a33SBen Roth                                               selector:@selector(_handleAppStateDidChange:)
950fbf6a33SBen Roth                                                   name:name
960fbf6a33SBen Roth                                                 object:nil];
970fbf6a33SBen Roth    }
982c229102SBen Roth    NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion);
990fbf6a33SBen Roth  }
1000fbf6a33SBen Roth  return self;
1010fbf6a33SBen Roth}
1020fbf6a33SBen Roth
1030fbf6a33SBen Roth- (void)dealloc
1040fbf6a33SBen Roth{
1050fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] removeObserver:self];
1060fbf6a33SBen Roth}
1070fbf6a33SBen Roth
1085daa5350SBen Roth#pragma mark - Misc
1095daa5350SBen Roth
1100fbf6a33SBen Roth+ (NSString *)deviceInstallUUID
1110fbf6a33SBen Roth{
1120fbf6a33SBen Roth  NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
1130fbf6a33SBen Roth  if (!uuid) {
1140fbf6a33SBen Roth    uuid = [[NSUUID UUID] UUIDString];
1150fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
1160fbf6a33SBen Roth    [[NSUserDefaults standardUserDefaults] synchronize];
1170fbf6a33SBen Roth  }
1180fbf6a33SBen Roth  return uuid;
1190fbf6a33SBen Roth}
1200fbf6a33SBen Roth
12185f025b0SBen Roth- (void)logAnalyticsEvent:(NSString *)eventId forAppRecord:(EXKernelAppRecord *)appRecord
12285f025b0SBen Roth{
12385f025b0SBen Roth  if (_appRegistry.homeAppRecord && appRecord == _appRegistry.homeAppRecord) {
12485f025b0SBen Roth    return;
12585f025b0SBen Roth  }
12685f025b0SBen Roth  NSString *validatedSdkVersion = [[EXVersions sharedInstance] availableSdkVersionForManifest:appRecord.appLoader.manifest];
12785f025b0SBen Roth  NSDictionary *props = (validatedSdkVersion) ? @{ @"SDK_VERSION": validatedSdkVersion } : @{};
12885f025b0SBen Roth  [[EXAnalytics sharedInstance] logEvent:eventId
12985f025b0SBen Roth                             manifestUrl:appRecord.appLoader.manifestUrl
13085f025b0SBen Roth                         eventProperties:props];
13185f025b0SBen Roth}
13285f025b0SBen Roth
1339f8d9b9dSBen Roth#pragma mark - bridge registry delegate
1349f8d9b9dSBen Roth
13589c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord
1369f8d9b9dSBen Roth{
1379f8d9b9dSBen Roth  // forward to service registry
13889c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord];
1399f8d9b9dSBen Roth}
1409f8d9b9dSBen Roth
14189c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord
1429f8d9b9dSBen Roth{
1439f8d9b9dSBen Roth  // forward to service registry
14489c6c39cSEric Samelson  [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord];
1459f8d9b9dSBen Roth}
1469f8d9b9dSBen Roth
147f5e2fbc5SBen Roth#pragma mark - Interfacing with JS
148f5e2fbc5SBen Roth
149f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app
150f5e2fbc5SBen Roth{
151f5e2fbc5SBen Roth  // fire a Linking url event on this (possibly versioned) bridge
152f5e2fbc5SBen Roth  EXReactAppManager *appManager = app.appManager;
153f5e2fbc5SBen Roth  id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"];
154f5e2fbc5SBen Roth  if (!linkingModule) {
155f5e2fbc5SBen Roth    DDLogError(@"Could not find the Linking module to open URL (%@)", urlString);
156f5e2fbc5SBen Roth  } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) {
157f5e2fbc5SBen Roth    [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]];
158f5e2fbc5SBen Roth  } else {
159f5e2fbc5SBen Roth    DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString);
160f5e2fbc5SBen Roth  }
161e7ac6e4fSBen Roth  [self _moveAppToVisible:app];
162f5e2fbc5SBen Roth}
1630fbf6a33SBen Roth
1645daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName
1650fbf6a33SBen Roth{
1660fbf6a33SBen Roth  id destinationBridge = appManager.reactBridge;
1670fbf6a33SBen Roth
1680fbf6a33SBen Roth  if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) {
1690fbf6a33SBen Roth    id batchedBridge = [destinationBridge batchedBridge];
1700fbf6a33SBen Roth    id moduleData = [batchedBridge moduleDataForName:moduleName];
1710fbf6a33SBen Roth
1720fbf6a33SBen Roth    // React Native before SDK 11 didn't strip the "RCT" prefix from module names
1730fbf6a33SBen Roth    if (!moduleData && ![moduleName hasPrefix:@"RCT"]) {
1740fbf6a33SBen Roth      moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]];
1750fbf6a33SBen Roth    }
1760fbf6a33SBen Roth
1770fbf6a33SBen Roth    if (moduleData) {
1780fbf6a33SBen Roth      return [moduleData instance];
1790fbf6a33SBen Roth    }
1800fbf6a33SBen Roth  } else {
181d2210950SBen Roth    // bridge can be null if the record is in an error state and never created a bridge.
182d2210950SBen Roth    if (destinationBridge) {
1830fbf6a33SBen Roth      DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge");
1840fbf6a33SBen Roth    }
185d2210950SBen Roth  }
1860fbf6a33SBen Roth  return nil;
1870fbf6a33SBen Roth}
1880fbf6a33SBen Roth
1897100de8cSSzymon20000- (BOOL)sendNotification:(EXPendingNotification *)notification
190c67a7dcdSBen Roth{
1911684b5d5SStanisław Chmiela  EXKernelAppRecord *destinationApp = [_appRegistry standaloneAppRecord] ?: [_appRegistry newestRecordWithExperienceId:notification.experienceId];
19227150200STomasz Sapeta
19327150200STomasz Sapeta  // This allows home app record to receive notification events as well.
19427150200STomasz Sapeta  if (!destinationApp && [_appRegistry.homeAppRecord.experienceId isEqualToString:notification.experienceId]) {
19527150200STomasz Sapeta    destinationApp = _appRegistry.homeAppRecord;
19627150200STomasz Sapeta  }
19727150200STomasz Sapeta
198c67a7dcdSBen Roth  if (destinationApp) {
199c67a7dcdSBen Roth    // send the body to the already-open experience
2007e7b37d3SEric Samelson    BOOL success = [self _dispatchJSEvent:@"Exponent.notification" body:notification.properties toApp:destinationApp];
201e7ac6e4fSBen Roth    [self _moveAppToVisible:destinationApp];
2027e7b37d3SEric Samelson    return success;
203c67a7dcdSBen Roth  } else {
204c67a7dcdSBen Roth    // no app is currently running for this experience id.
205c67a7dcdSBen Roth    // if we're Expo Client, we can query Home for a past experience in the user's history, and route the notification there.
206c67a7dcdSBen Roth    if (_browserController) {
207c67a7dcdSBen Roth      __weak typeof(self) weakSelf = self;
2087100de8cSSzymon20000      [_browserController getHistoryUrlForExperienceId:notification.experienceId completion:^(NSString *urlString) {
209c67a7dcdSBen Roth        if (urlString) {
210c67a7dcdSBen Roth          NSURL *url = [NSURL URLWithString:urlString];
211c67a7dcdSBen Roth          if (url) {
2127100de8cSSzymon20000            [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": notification.properties }];
213c67a7dcdSBen Roth          }
214c67a7dcdSBen Roth        }
215c67a7dcdSBen Roth      }];
2167100de8cSSzymon20000      // If we're here, there's no active app in appRegistry matching notification.experienceId
2177100de8cSSzymon20000      // and we are in Expo Client, since _browserController is not nil.
2187100de8cSSzymon20000      // If so, we can return YES (meaning "notification has been successfully dispatched")
2197100de8cSSzymon20000      // because we pass the notification as initialProps in completion handler
2207100de8cSSzymon20000      // of getHistoryUrlForExperienceId:. If urlString passed to completion handler is empty,
2217100de8cSSzymon20000      // the notification is forgotten (this is the expected behavior).
2227100de8cSSzymon20000      return YES;
223c67a7dcdSBen Roth    }
224c67a7dcdSBen Roth  }
2257100de8cSSzymon20000
2267100de8cSSzymon20000  return NO;
227c67a7dcdSBen Roth}
228c67a7dcdSBen Roth
2290fbf6a33SBen Roth/**
2300fbf6a33SBen Roth *  If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well.
2310fbf6a33SBen Roth */
2320fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge
2330fbf6a33SBen Roth{
2340fbf6a33SBen Roth  [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge];
2350fbf6a33SBen Roth  if ([bridge respondsToSelector:@selector(batchedBridge)]) {
2360fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]];
2370fbf6a33SBen Roth  } else if ([bridge respondsToSelector:@selector(parentBridge)]) {
2380fbf6a33SBen Roth    [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]];
2390fbf6a33SBen Roth  }
2400fbf6a33SBen Roth}
2410fbf6a33SBen Roth
2427e7b37d3SEric Samelson- (BOOL)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord
243f148175aSBen Roth{
2447e7b37d3SEric Samelson  if (!appRecord.appManager.reactBridge) {
2457e7b37d3SEric Samelson    return NO;
2467e7b37d3SEric Samelson  }
247c67a7dcdSBen Roth  [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
248c67a7dcdSBen Roth                                             args:eventBody ? @[eventName, eventBody] : @[eventName]];
2497e7b37d3SEric Samelson  return YES;
2500fbf6a33SBen Roth}
2510fbf6a33SBen Roth
252d699b379SBen Roth#pragma mark - App props
253d699b379SBen Roth
2547100de8cSSzymon20000- (nullable NSDictionary *)initialAppPropsFromLaunchOptions:(NSDictionary *)launchOptions
255d699b379SBen Roth{
2567100de8cSSzymon20000  return nil;
257d699b379SBen Roth}
258d699b379SBen Roth
2598a7f6184SBen Roth#pragma mark - App State
2608e3c2ccdSBen Roth
26189fd6b24SBen Roth- (EXKernelAppRecord *)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps
262f5e2fbc5SBen Roth{
263c67a7dcdSBen Roth  NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps];
264f5e2fbc5SBen Roth  EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
265e7ac6e4fSBen Roth  [self _moveAppToVisible:record];
26689fd6b24SBen Roth  return record;
267f5e2fbc5SBen Roth}
268f5e2fbc5SBen Roth
269d0248f72SBrent Vatne
270b333b418SBrent Vatne- (void)reloadVisibleApp
271b333b418SBrent Vatne{
272b333b418SBrent Vatne  if (_browserController) {
273b333b418SBrent Vatne    [EXUtil performSynchronouslyOnMainThread:^{
274b333b418SBrent Vatne      [self->_browserController reloadVisibleApp];
275b333b418SBrent Vatne    }];
276b333b418SBrent Vatne  }
277b333b418SBrent Vatne}
278b333b418SBrent Vatne
2797f6367d0SBen Roth- (void)switchTasks
2800fbf6a33SBen Roth{
2817f6367d0SBen Roth  if (!_browserController) {
2827f6367d0SBen Roth    return;
2836f62abf2SBen Roth  }
2846f62abf2SBen Roth
2857f6367d0SBen Roth  if (_visibleApp != _appRegistry.homeAppRecord) {
286*05aa4e6cSTomasz Sapeta#ifndef EX_DETACHED // Just to compile without access to EXDevMenuManager, we wouldn't get here either way because browser controller is unset in this case.
2877f6367d0SBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
288f67462bcSTomasz Sapeta      [[EXDevMenuManager sharedInstance] toggle];
2897f6367d0SBen Roth    }];
290*05aa4e6cSTomasz Sapeta#endif
291b05926a2SBen Roth  } else {
2927f6367d0SBen Roth    EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry;
2937f6367d0SBen Roth    for (NSString *recordId in appRegistry.appEnumerator) {
2947f6367d0SBen Roth      EXKernelAppRecord *record = [appRegistry recordForId:recordId];
2957f6367d0SBen Roth      // foreground the first thing we find
296e7ac6e4fSBen Roth      [self _moveAppToVisible:record];
2977f6367d0SBen Roth    }
2987f6367d0SBen Roth  }
2997f6367d0SBen Roth}
3007f6367d0SBen Roth
301928bc3baSBen Roth- (void)reloadAppWithExperienceId:(NSString *)experienceId
302928bc3baSBen Roth{
303928bc3baSBen Roth  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
304928bc3baSBen Roth  if (_browserController) {
305928bc3baSBen Roth    [self createNewAppWithUrl:appRecord.appLoader.manifestUrl initialProps:nil];
306928bc3baSBen Roth  } else if (_appRegistry.standaloneAppRecord && appRecord == _appRegistry.standaloneAppRecord) {
307928bc3baSBen Roth    [appRecord.viewController refresh];
308928bc3baSBen Roth  }
309928bc3baSBen Roth}
310928bc3baSBen Roth
3119777fddbSEric Samelson- (void)reloadAppFromCacheWithExperienceId:(NSString *)experienceId
3129777fddbSEric Samelson{
3139777fddbSEric Samelson  EXKernelAppRecord *appRecord = [_appRegistry newestRecordWithExperienceId:experienceId];
3149777fddbSEric Samelson  [appRecord.viewController reloadFromCache];
3159777fddbSEric Samelson}
3169777fddbSEric Samelson
317e7ac6e4fSBen Roth- (void)viewController:(__unused EXViewController *)vc didNavigateAppToVisible:(EXKernelAppRecord *)appRecord
3187f6367d0SBen Roth{
3197f6367d0SBen Roth  EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp;
3207f6367d0SBen Roth  if (appRecord != appRecordPreviouslyVisible) {
3217f6367d0SBen Roth    if (appRecordPreviouslyVisible) {
322e7ac6e4fSBen Roth      [appRecordPreviouslyVisible.viewController appStateDidBecomeInactive];
3237f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge];
32462cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"];
32562cc3d03STomasz Sapeta      if (appStateModule != nil) {
3260fbf6a33SBen Roth        [appStateModule setState:@"background"];
3270fbf6a33SBen Roth      }
3280fbf6a33SBen Roth    }
3297f6367d0SBen Roth    if (appRecord) {
330e7ac6e4fSBen Roth      [appRecord.viewController appStateDidBecomeActive];
3317f6367d0SBen Roth      [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge];
33262cc3d03STomasz Sapeta      id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"];
33362cc3d03STomasz Sapeta      if (appStateModule != nil) {
3340fbf6a33SBen Roth        [appStateModule setState:@"active"];
3350fbf6a33SBen Roth      }
3367f6367d0SBen Roth      _visibleApp = appRecord;
33785f025b0SBen Roth      [[EXAnalytics sharedInstance] logAppVisibleEvent];
3380fbf6a33SBen Roth    } else {
3397f6367d0SBen Roth      _visibleApp = nil;
3407f6367d0SBen Roth    }
3417f6367d0SBen Roth
3427f6367d0SBen Roth    if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) {
3437f6367d0SBen Roth      [self _unregisterUnusedAppRecords];
3447f6367d0SBen Roth    }
3457f6367d0SBen Roth  }
3467f6367d0SBen Roth}
3477f6367d0SBen Roth
3487f6367d0SBen Roth- (void)_unregisterUnusedAppRecords
3497f6367d0SBen Roth{
3507f6367d0SBen Roth  for (NSString *recordId in _appRegistry.appEnumerator) {
3517f6367d0SBen Roth    EXKernelAppRecord *record = [_appRegistry recordForId:recordId];
3527f6367d0SBen Roth    if (record && record != _visibleApp) {
3537f6367d0SBen Roth      [_appRegistry unregisterAppWithRecordId:recordId];
3547f6367d0SBen Roth      break;
3550fbf6a33SBen Roth    }
3560fbf6a33SBen Roth  }
357b05926a2SBen Roth}
3580fbf6a33SBen Roth
3590fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification
3600fbf6a33SBen Roth{
3610fbf6a33SBen Roth  NSString *newState;
3620fbf6a33SBen Roth
3630fbf6a33SBen Roth  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
3640fbf6a33SBen Roth    newState = @"inactive";
3650fbf6a33SBen Roth  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
3660fbf6a33SBen Roth    newState = @"background";
3670fbf6a33SBen Roth  } else {
3680fbf6a33SBen Roth    switch (RCTSharedApplication().applicationState) {
3690fbf6a33SBen Roth      case UIApplicationStateActive:
3700fbf6a33SBen Roth        newState = @"active";
3710fbf6a33SBen Roth        break;
3720fbf6a33SBen Roth      case UIApplicationStateBackground: {
3730fbf6a33SBen Roth        newState = @"background";
3740fbf6a33SBen Roth        break;
3750fbf6a33SBen Roth      }
3760fbf6a33SBen Roth      default: {
3770fbf6a33SBen Roth        newState = @"unknown";
3780fbf6a33SBen Roth        break;
3790fbf6a33SBen Roth      }
3800fbf6a33SBen Roth    }
3810fbf6a33SBen Roth  }
3820fbf6a33SBen Roth
3837f6367d0SBen Roth  if (_visibleApp) {
3847f6367d0SBen Roth    EXReactAppManager *appManager = _visibleApp.appManager;
38562cc3d03STomasz Sapeta    id<EXAppStateProtocol> appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"];
3860fbf6a33SBen Roth    NSString *lastKnownState;
38762cc3d03STomasz Sapeta    if (appStateModule != nil) {
3880fbf6a33SBen Roth      lastKnownState = [appStateModule lastKnownState];
3890fbf6a33SBen Roth      [appStateModule setState:newState];
3900fbf6a33SBen Roth    }
3910fbf6a33SBen Roth    if (!lastKnownState || ![newState isEqualToString:lastKnownState]) {
3920fbf6a33SBen Roth      if ([newState isEqualToString:@"active"]) {
393e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeActive];
3947f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge];
3950fbf6a33SBen Roth      } else if ([newState isEqualToString:@"background"]) {
396e7ac6e4fSBen Roth        [_visibleApp.viewController appStateDidBecomeInactive];
3977f6367d0SBen Roth        [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge];
3980fbf6a33SBen Roth      }
3990fbf6a33SBen Roth    }
4000fbf6a33SBen Roth  }
4010fbf6a33SBen Roth}
4020fbf6a33SBen Roth
403d0248f72SBrent Vatne
404d0248f72SBrent Vatne- (void)_handleRequestReloadVisibleApp:(NSNotification *)notification
405d0248f72SBrent Vatne{
406d0248f72SBrent Vatne  [self reloadVisibleApp];
407d0248f72SBrent Vatne}
408d0248f72SBrent Vatne
409e7ac6e4fSBen Roth- (void)_moveAppToVisible:(EXKernelAppRecord *)appRecord
410e7ac6e4fSBen Roth{
411e7ac6e4fSBen Roth  if (_browserController) {
412e7ac6e4fSBen Roth    [EXUtil performSynchronouslyOnMainThread:^{
413d718b4b8SBen Roth      [self->_browserController moveAppToVisible:appRecord];
414e7ac6e4fSBen Roth    }];
415e7ac6e4fSBen Roth  }
416e7ac6e4fSBen Roth}
417e7ac6e4fSBen Roth
418*05aa4e6cSTomasz Sapeta#ifndef EX_DETACHED
419f67462bcSTomasz Sapeta#pragma mark - EXDevMenuDelegateProtocol
420f67462bcSTomasz Sapeta
421f67462bcSTomasz Sapeta- (RCTBridge *)mainBridgeForDevMenuManager:(EXDevMenuManager *)manager
422f67462bcSTomasz Sapeta{
423f67462bcSTomasz Sapeta  return _appRegistry.homeAppRecord.appManager.reactBridge;
424f67462bcSTomasz Sapeta}
425f67462bcSTomasz Sapeta
426f67462bcSTomasz Sapeta- (nullable RCTBridge *)appBridgeForDevMenuManager:(EXDevMenuManager *)manager
427f67462bcSTomasz Sapeta{
428f67462bcSTomasz Sapeta  if (_visibleApp == _appRegistry.homeAppRecord) {
429f67462bcSTomasz Sapeta    return nil;
430f67462bcSTomasz Sapeta  }
431f67462bcSTomasz Sapeta  return _visibleApp.appManager.reactBridge;
432f67462bcSTomasz Sapeta}
433f67462bcSTomasz Sapeta
434f67462bcSTomasz Sapeta- (BOOL)devMenuManager:(EXDevMenuManager *)manager canChangeVisibility:(BOOL)visibility
435f67462bcSTomasz Sapeta{
436f67462bcSTomasz Sapeta  return !visibility || _visibleApp != _appRegistry.homeAppRecord;
437f67462bcSTomasz Sapeta}
438f67462bcSTomasz Sapeta
439*05aa4e6cSTomasz Sapeta#endif
440*05aa4e6cSTomasz Sapeta
4410fbf6a33SBen Roth@end
4420fbf6a33SBen Roth
4430fbf6a33SBen RothNS_ASSUME_NONNULL_END
444