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 "EXFrame.h" 80fbf6a33SBen Roth#import "EXKernel.h" 989c6c39cSEric Samelson#import "EXKernelAppRecord.h" 100fbf6a33SBen Roth#import "EXKernelModule.h" 11b05926a2SBen Roth#import "EXKernelLinkingManager.h" 120fbf6a33SBen Roth#import "EXLinkingManager.h" 130fbf6a33SBen Roth#import "EXVersions.h" 140fbf6a33SBen Roth 150fbf6a33SBen Roth#import <React/RCTBridge+Private.h> 160fbf6a33SBen Roth#import <React/RCTEventDispatcher.h> 170fbf6a33SBen Roth#import <React/RCTModuleData.h> 180fbf6a33SBen Roth#import <React/RCTUtils.h> 190fbf6a33SBen Roth 200fbf6a33SBen RothNS_ASSUME_NONNULL_BEGIN 210fbf6a33SBen Roth 220fbf6a33SBen RothNSString *kEXKernelErrorDomain = @"EXKernelErrorDomain"; 230fbf6a33SBen RothNSNotificationName kEXKernelJSIsLoadedNotification = @"EXKernelJSIsLoadedNotification"; 2443381aa0SAlicja WarchałNSNotificationName kEXKernelAppDidDisplay = @"EXKernelAppDidDisplay"; 250fbf6a33SBen RothNSString *kEXKernelShouldForegroundTaskEvent = @"foregroundTask"; 260fbf6a33SBen RothNSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey"; 270fbf6a33SBen RothNSString * const kEXKernelClearJSCacheUserDefaultsKey = @"EXKernelClearJSCacheUserDefaultsKey"; 280fbf6a33SBen RothNSString * const EXKernelDisableNuxDefaultsKey = @"EXKernelDisableNuxDefaultsKey"; 290fbf6a33SBen Roth 3089c6c39cSEric Samelson@interface EXKernel () <EXKernelAppRegistryDelegate> 310fbf6a33SBen Roth 320fbf6a33SBen Roth@end 330fbf6a33SBen Roth 340fbf6a33SBen Roth@implementation EXKernel 350fbf6a33SBen Roth 360fbf6a33SBen Roth+ (instancetype)sharedInstance 370fbf6a33SBen Roth{ 380fbf6a33SBen Roth static EXKernel *theKernel; 390fbf6a33SBen Roth static dispatch_once_t once; 400fbf6a33SBen Roth dispatch_once(&once, ^{ 410fbf6a33SBen Roth if (!theKernel) { 420fbf6a33SBen Roth theKernel = [[EXKernel alloc] init]; 430fbf6a33SBen Roth } 440fbf6a33SBen Roth }); 450fbf6a33SBen Roth return theKernel; 460fbf6a33SBen Roth} 470fbf6a33SBen Roth 480fbf6a33SBen Roth- (instancetype)init 490fbf6a33SBen Roth{ 500fbf6a33SBen Roth if (self = [super init]) { 5189c6c39cSEric Samelson // init app registry: keep track of RN bridges we are running 5289c6c39cSEric Samelson _appRegistry = [[EXKernelAppRegistry alloc] init]; 5389c6c39cSEric Samelson _appRegistry.delegate = self; 549f8d9b9dSBen Roth 559f8d9b9dSBen Roth // init service registry: classes which manage shared resources among all bridges 56d7f51e5eSBen Roth _serviceRegistry = [[EXKernelServiceRegistry alloc] init]; 579f8d9b9dSBen Roth 580fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_onKernelJSLoaded) name:kEXKernelJSIsLoadedNotification object:nil]; 590fbf6a33SBen Roth for (NSString *name in @[UIApplicationDidBecomeActiveNotification, 600fbf6a33SBen Roth UIApplicationDidEnterBackgroundNotification, 610fbf6a33SBen Roth UIApplicationDidFinishLaunchingNotification, 620fbf6a33SBen Roth UIApplicationWillResignActiveNotification, 630fbf6a33SBen Roth UIApplicationWillEnterForegroundNotification]) { 640fbf6a33SBen Roth 650fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] addObserver:self 660fbf6a33SBen Roth selector:@selector(_handleAppStateDidChange:) 670fbf6a33SBen Roth name:name 680fbf6a33SBen Roth object:nil]; 690fbf6a33SBen Roth } 702c229102SBen Roth NSLog(@"Expo iOS Runtime Version %@", [EXBuildConstants sharedInstance].expoRuntimeVersion); 710fbf6a33SBen Roth } 720fbf6a33SBen Roth return self; 730fbf6a33SBen Roth} 740fbf6a33SBen Roth 750fbf6a33SBen Roth- (void)dealloc 760fbf6a33SBen Roth{ 770fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] removeObserver:self]; 780fbf6a33SBen Roth} 790fbf6a33SBen Roth 800fbf6a33SBen Roth- (void)_onKernelJSLoaded 810fbf6a33SBen Roth{ 820fbf6a33SBen Roth // used by appetize: optionally disable nux 830fbf6a33SBen Roth BOOL disableNuxDefaultsValue = [[NSUserDefaults standardUserDefaults] boolForKey:EXKernelDisableNuxDefaultsKey]; 840fbf6a33SBen Roth if (disableNuxDefaultsValue) { 85*c67a7dcdSBen Roth // TODO: ben: nux home 86*c67a7dcdSBen Roth // TODO: ben: snack 87*c67a7dcdSBen Roth // [self dispatchKernelJSEvent:@"resetNuxState" body:@{ @"isNuxCompleted": @YES } onSuccess:nil onFailure:nil]; 880fbf6a33SBen Roth [[NSUserDefaults standardUserDefaults] removeObjectForKey:EXKernelDisableNuxDefaultsKey]; 890fbf6a33SBen Roth } 900fbf6a33SBen Roth} 910fbf6a33SBen Roth 925daa5350SBen Roth#pragma mark - Misc 935daa5350SBen Roth 940fbf6a33SBen Roth+ (NSString *)deviceInstallUUID 950fbf6a33SBen Roth{ 960fbf6a33SBen Roth NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey]; 970fbf6a33SBen Roth if (!uuid) { 980fbf6a33SBen Roth uuid = [[NSUUID UUID] UUIDString]; 990fbf6a33SBen Roth [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey]; 1000fbf6a33SBen Roth [[NSUserDefaults standardUserDefaults] synchronize]; 1010fbf6a33SBen Roth } 1020fbf6a33SBen Roth return uuid; 1030fbf6a33SBen Roth} 1040fbf6a33SBen Roth 1059f8d9b9dSBen Roth#pragma mark - bridge registry delegate 1069f8d9b9dSBen Roth 10789c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry didRegisterAppRecord:(EXKernelAppRecord *)appRecord 1089f8d9b9dSBen Roth{ 1099f8d9b9dSBen Roth // forward to service registry 11089c6c39cSEric Samelson [_serviceRegistry appRegistry:registry didRegisterAppRecord:appRecord]; 1119f8d9b9dSBen Roth} 1129f8d9b9dSBen Roth 11389c6c39cSEric Samelson- (void)appRegistry:(EXKernelAppRegistry *)registry willUnregisterAppRecord:(EXKernelAppRecord *)appRecord 1149f8d9b9dSBen Roth{ 1159f8d9b9dSBen Roth // forward to service registry 11689c6c39cSEric Samelson [_serviceRegistry appRegistry:registry willUnregisterAppRecord:appRecord]; 1179f8d9b9dSBen Roth} 1189f8d9b9dSBen Roth 119f5e2fbc5SBen Roth#pragma mark - Interfacing with JS 120f5e2fbc5SBen Roth 121f5e2fbc5SBen Roth- (void)sendUrl:(NSString *)urlString toAppRecord:(EXKernelAppRecord *)app 122f5e2fbc5SBen Roth{ 123f5e2fbc5SBen Roth // fire a Linking url event on this (possibly versioned) bridge 124f5e2fbc5SBen Roth EXReactAppManager *appManager = app.appManager; 125f5e2fbc5SBen Roth id linkingModule = [self nativeModuleForAppManager:appManager named:@"LinkingManager"]; 126f5e2fbc5SBen Roth if (!linkingModule) { 127f5e2fbc5SBen Roth DDLogError(@"Could not find the Linking module to open URL (%@)", urlString); 128f5e2fbc5SBen Roth } else if ([linkingModule respondsToSelector:@selector(dispatchOpenUrlEvent:)]) { 129f5e2fbc5SBen Roth [linkingModule dispatchOpenUrlEvent:[NSURL URLWithString:urlString]]; 130f5e2fbc5SBen Roth } else { 131f5e2fbc5SBen Roth DDLogError(@"Linking module doesn't support the API we use to open URL (%@)", urlString); 132f5e2fbc5SBen Roth } 133f5e2fbc5SBen Roth [self moveAppToVisible:app]; 134f5e2fbc5SBen Roth} 1350fbf6a33SBen Roth 1365daa5350SBen Roth- (id)nativeModuleForAppManager:(EXReactAppManager *)appManager named:(NSString *)moduleName 1370fbf6a33SBen Roth{ 1380fbf6a33SBen Roth id destinationBridge = appManager.reactBridge; 1390fbf6a33SBen Roth 1400fbf6a33SBen Roth if ([destinationBridge respondsToSelector:@selector(batchedBridge)]) { 1410fbf6a33SBen Roth id batchedBridge = [destinationBridge batchedBridge]; 1420fbf6a33SBen Roth id moduleData = [batchedBridge moduleDataForName:moduleName]; 1430fbf6a33SBen Roth 1440fbf6a33SBen Roth // React Native before SDK 11 didn't strip the "RCT" prefix from module names 1450fbf6a33SBen Roth if (!moduleData && ![moduleName hasPrefix:@"RCT"]) { 1460fbf6a33SBen Roth moduleData = [batchedBridge moduleDataForName:[@"RCT" stringByAppendingString:moduleName]]; 1470fbf6a33SBen Roth } 1480fbf6a33SBen Roth 1490fbf6a33SBen Roth if (moduleData) { 1500fbf6a33SBen Roth return [moduleData instance]; 1510fbf6a33SBen Roth } 1520fbf6a33SBen Roth } else { 1530fbf6a33SBen Roth DDLogError(@"Bridge does not support the API we use to get its underlying batched bridge"); 1540fbf6a33SBen Roth } 1550fbf6a33SBen Roth return nil; 1560fbf6a33SBen Roth} 1570fbf6a33SBen Roth 158*c67a7dcdSBen Roth- (void)sendNotification:(NSDictionary *)notifBody 159*c67a7dcdSBen Roth toExperienceWithId:(NSString *)destinationExperienceId 160*c67a7dcdSBen Roth fromBackground:(BOOL)isFromBackground 161*c67a7dcdSBen Roth isRemote:(BOOL)isRemote 162*c67a7dcdSBen Roth{ 163*c67a7dcdSBen Roth EXKernelAppRecord *destinationApp = [_appRegistry newestRecordWithExperienceId:destinationExperienceId]; 164*c67a7dcdSBen Roth 165*c67a7dcdSBen Roth // if the notification came from the background, in most but not all cases, this means the user acted on an iOS notification 166*c67a7dcdSBen Roth // and caused the app to launch. 167*c67a7dcdSBen Roth // From SO: 168*c67a7dcdSBen Roth // > Note that "App opened from Notification" will be a false positive if the notification is sent while the user is on a different 169*c67a7dcdSBen Roth // > screen (for example, if they pull down the status bar and then receive a notification from your app). 170*c67a7dcdSBen Roth NSDictionary *bodyWithOrigin = @{ 171*c67a7dcdSBen Roth @"origin": (isFromBackground) ? @"selected" : @"received", 172*c67a7dcdSBen Roth @"remote": @(isRemote), 173*c67a7dcdSBen Roth @"data": notifBody, 174*c67a7dcdSBen Roth }; 175*c67a7dcdSBen Roth if (destinationApp) { 176*c67a7dcdSBen Roth // send the body to the already-open experience 177*c67a7dcdSBen Roth [self _dispatchJSEvent:@"Exponent.notification" body:bodyWithOrigin toApp:destinationApp]; 178*c67a7dcdSBen Roth [self moveAppToVisible:destinationApp]; 179*c67a7dcdSBen Roth } else { 180*c67a7dcdSBen Roth // no app is currently running for this experience id. 181*c67a7dcdSBen Roth // if we're Expo Client, we can query Home for a past experience in the user's history, and route the notification there. 182*c67a7dcdSBen Roth if (_browserController) { 183*c67a7dcdSBen Roth __weak typeof(self) weakSelf = self; 184*c67a7dcdSBen Roth [_browserController getHistoryUrlForExperienceId:destinationExperienceId completion:^(NSString *urlString) { 185*c67a7dcdSBen Roth if (urlString) { 186*c67a7dcdSBen Roth NSURL *url = [NSURL URLWithString:urlString]; 187*c67a7dcdSBen Roth if (url) { 188*c67a7dcdSBen Roth [weakSelf createNewAppWithUrl:url initialProps:@{ @"notification": bodyWithOrigin }]; 189*c67a7dcdSBen Roth } 190*c67a7dcdSBen Roth } 191*c67a7dcdSBen Roth }]; 192*c67a7dcdSBen Roth } 193*c67a7dcdSBen Roth } 194*c67a7dcdSBen Roth} 195*c67a7dcdSBen Roth 1960fbf6a33SBen Roth/** 1970fbf6a33SBen Roth * If the bridge has a batchedBridge or parentBridge selector, posts the notification on that object as well. 1980fbf6a33SBen Roth */ 1990fbf6a33SBen Roth- (void)_postNotificationName: (NSNotificationName)name onAbstractBridge: (id)bridge 2000fbf6a33SBen Roth{ 2010fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:bridge]; 2020fbf6a33SBen Roth if ([bridge respondsToSelector:@selector(batchedBridge)]) { 2030fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge batchedBridge]]; 2040fbf6a33SBen Roth } else if ([bridge respondsToSelector:@selector(parentBridge)]) { 2050fbf6a33SBen Roth [[NSNotificationCenter defaultCenter] postNotificationName:name object:[bridge parentBridge]]; 2060fbf6a33SBen Roth } 2070fbf6a33SBen Roth} 2080fbf6a33SBen Roth 209*c67a7dcdSBen Roth- (void)_dispatchJSEvent:(NSString *)eventName body:(NSDictionary *)eventBody toApp:(EXKernelAppRecord *)appRecord 210f148175aSBen Roth{ 211*c67a7dcdSBen Roth [appRecord.appManager.reactBridge enqueueJSCall:@"RCTDeviceEventEmitter.emit" 212*c67a7dcdSBen Roth args:eventBody ? @[eventName, eventBody] : @[eventName]]; 2130fbf6a33SBen Roth} 2140fbf6a33SBen Roth 2158a7f6184SBen Roth#pragma mark - App State 2168e3c2ccdSBen Roth 217*c67a7dcdSBen Roth- (void)createNewAppWithUrl:(NSURL *)url initialProps:(nullable NSDictionary *)initialProps 218f5e2fbc5SBen Roth{ 219*c67a7dcdSBen Roth NSString *recordId = [_appRegistry registerAppWithManifestUrl:url initialProps:initialProps]; 220f5e2fbc5SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 221f5e2fbc5SBen Roth [self moveAppToVisible:record]; 222f5e2fbc5SBen Roth} 223f5e2fbc5SBen Roth 2247f6367d0SBen Roth- (void)switchTasks 2250fbf6a33SBen Roth{ 2267f6367d0SBen Roth if (!_browserController) { 2277f6367d0SBen Roth return; 2286f62abf2SBen Roth } 2296f62abf2SBen Roth 2307f6367d0SBen Roth if (_visibleApp != _appRegistry.homeAppRecord) { 2317f6367d0SBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 2327f6367d0SBen Roth [_browserController toggleMenu]; 2337f6367d0SBen Roth }]; 234b05926a2SBen Roth } else { 2357f6367d0SBen Roth EXKernelAppRegistry *appRegistry = [EXKernel sharedInstance].appRegistry; 2367f6367d0SBen Roth for (NSString *recordId in appRegistry.appEnumerator) { 2377f6367d0SBen Roth EXKernelAppRecord *record = [appRegistry recordForId:recordId]; 2387f6367d0SBen Roth // foreground the first thing we find 2397f6367d0SBen Roth [self moveAppToVisible:record]; 2407f6367d0SBen Roth } 2417f6367d0SBen Roth } 2427f6367d0SBen Roth} 2437f6367d0SBen Roth 2447f6367d0SBen Roth- (void)moveAppToVisible:(EXKernelAppRecord *)appRecord 2457f6367d0SBen Roth{ 2467f6367d0SBen Roth if (_browserController) { 2477f6367d0SBen Roth [EXUtil performSynchronouslyOnMainThread:^{ 2487f6367d0SBen Roth [_browserController moveAppToVisible:appRecord]; 2497f6367d0SBen Roth }]; 2507f6367d0SBen Roth } 2517f6367d0SBen Roth} 2527f6367d0SBen Roth 2537f6367d0SBen Roth- (void)appDidBecomeVisible:(EXKernelAppRecord *)appRecord 2547f6367d0SBen Roth{ 2557f6367d0SBen Roth EXKernelAppRecord *appRecordPreviouslyVisible = _visibleApp; 2567f6367d0SBen Roth if (appRecord != appRecordPreviouslyVisible) { 2577f6367d0SBen Roth if (appRecordPreviouslyVisible) { 2580187d3b3SBen Roth [appRecordPreviouslyVisible.viewController appDidBackground]; 2597f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appRecordPreviouslyVisible.appManager.reactBridge]; 2607f6367d0SBen Roth id appStateModule = [self nativeModuleForAppManager:appRecordPreviouslyVisible.appManager named:@"AppState"]; 2610fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 2620fbf6a33SBen Roth [appStateModule setState:@"background"]; 2630fbf6a33SBen Roth } 2640fbf6a33SBen Roth } 2657f6367d0SBen Roth if (appRecord) { 2660187d3b3SBen Roth [appRecord.viewController appDidBecomeVisible]; 2677f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appRecord.appManager.reactBridge]; 2687f6367d0SBen Roth id appStateModule = [self nativeModuleForAppManager:appRecord.appManager named:@"AppState"]; 2690fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 2700fbf6a33SBen Roth [appStateModule setState:@"active"]; 2710fbf6a33SBen Roth } 2727f6367d0SBen Roth _visibleApp = appRecord; 2737f6367d0SBen Roth [[EXAnalytics sharedInstance] logKernelAppVisibleEvent]; 2740fbf6a33SBen Roth } else { 2757f6367d0SBen Roth _visibleApp = nil; 2767f6367d0SBen Roth } 2777f6367d0SBen Roth 2787f6367d0SBen Roth if (_visibleApp && _visibleApp != _appRegistry.homeAppRecord) { 2797f6367d0SBen Roth [self _unregisterUnusedAppRecords]; 2807f6367d0SBen Roth } 2817f6367d0SBen Roth } 2827f6367d0SBen Roth} 2837f6367d0SBen Roth 2847f6367d0SBen Roth- (void)_unregisterUnusedAppRecords 2857f6367d0SBen Roth{ 2867f6367d0SBen Roth for (NSString *recordId in _appRegistry.appEnumerator) { 2877f6367d0SBen Roth EXKernelAppRecord *record = [_appRegistry recordForId:recordId]; 2887f6367d0SBen Roth if (record && record != _visibleApp) { 2897f6367d0SBen Roth [_appRegistry unregisterAppWithRecordId:recordId]; 2907f6367d0SBen Roth break; 2910fbf6a33SBen Roth } 2920fbf6a33SBen Roth } 293b05926a2SBen Roth} 2940fbf6a33SBen Roth 2950fbf6a33SBen Roth- (void)_handleAppStateDidChange:(NSNotification *)notification 2960fbf6a33SBen Roth{ 2970fbf6a33SBen Roth NSString *newState; 2980fbf6a33SBen Roth 2990fbf6a33SBen Roth if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { 3000fbf6a33SBen Roth newState = @"inactive"; 3010fbf6a33SBen Roth } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) { 3020fbf6a33SBen Roth newState = @"background"; 3030fbf6a33SBen Roth } else { 3040fbf6a33SBen Roth switch (RCTSharedApplication().applicationState) { 3050fbf6a33SBen Roth case UIApplicationStateActive: 3060fbf6a33SBen Roth newState = @"active"; 3070fbf6a33SBen Roth break; 3080fbf6a33SBen Roth case UIApplicationStateBackground: { 3090fbf6a33SBen Roth newState = @"background"; 3100fbf6a33SBen Roth break; 3110fbf6a33SBen Roth } 3120fbf6a33SBen Roth default: { 3130fbf6a33SBen Roth newState = @"unknown"; 3140fbf6a33SBen Roth break; 3150fbf6a33SBen Roth } 3160fbf6a33SBen Roth } 3170fbf6a33SBen Roth } 3180fbf6a33SBen Roth 3197f6367d0SBen Roth if (_visibleApp) { 3207f6367d0SBen Roth EXReactAppManager *appManager = _visibleApp.appManager; 3215daa5350SBen Roth id appStateModule = [self nativeModuleForAppManager:appManager named:@"AppState"]; 3220fbf6a33SBen Roth NSString *lastKnownState; 3230fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(lastKnownState)]) { 3240fbf6a33SBen Roth lastKnownState = [appStateModule lastKnownState]; 3250fbf6a33SBen Roth } 3260fbf6a33SBen Roth if ([appStateModule respondsToSelector:@selector(setState:)]) { 3270fbf6a33SBen Roth [appStateModule setState:newState]; 3280fbf6a33SBen Roth } 3290fbf6a33SBen Roth if (!lastKnownState || ![newState isEqualToString:lastKnownState]) { 3300fbf6a33SBen Roth if ([newState isEqualToString:@"active"]) { 3310187d3b3SBen Roth [_visibleApp.viewController appDidBecomeVisible]; 3327f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidForegroundNotification onAbstractBridge:appManager.reactBridge]; 3330fbf6a33SBen Roth } else if ([newState isEqualToString:@"background"]) { 3340187d3b3SBen Roth [_visibleApp.viewController appDidBackground]; 3357f6367d0SBen Roth [self _postNotificationName:kEXKernelBridgeDidBackgroundNotification onAbstractBridge:appManager.reactBridge]; 3360fbf6a33SBen Roth } 3370fbf6a33SBen Roth } 3380fbf6a33SBen Roth } 3390fbf6a33SBen Roth} 3400fbf6a33SBen Roth 3410fbf6a33SBen Roth@end 3420fbf6a33SBen Roth 3430fbf6a33SBen RothNS_ASSUME_NONNULL_END 344