/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ #import "RCTBridge+Private.h" #import #import "RCTConvert.h" #import "RCTEventDispatcher.h" #import "RCTKeyCommands.h" #import "RCTLog.h" #import "RCTModuleData.h" #import "RCTPerformanceLogger.h" #import "RCTProfile.h" #import "RCTUtils.h" NSString *const RCTReloadNotification = @"RCTReloadNotification"; NSString *const RCTJavaScriptWillStartLoadingNotification = @"RCTJavaScriptWillStartLoadingNotification"; NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification"; NSString *const RCTJavaScriptDidFailToLoadNotification = @"RCTJavaScriptDidFailToLoadNotification"; NSString *const RCTDidInitializeModuleNotification = @"RCTDidInitializeModuleNotification"; static NSMutableArray *RCTModuleClasses; NSArray *RCTGetModuleClasses(void); NSArray *RCTGetModuleClasses(void) { return RCTModuleClasses; } /** * Register the given class as a bridge module. All modules must be registered * prior to the first bridge initialization. */ void RCTRegisterModule(Class); void RCTRegisterModule(Class moduleClass) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ RCTModuleClasses = [NSMutableArray new]; }); RCTAssert([moduleClass conformsToProtocol:@protocol(RCTBridgeModule)], @"%@ does not conform to the RCTBridgeModule protocol", moduleClass); // Register module [RCTModuleClasses addObject:moduleClass]; } /** * This function returns the module name for a given class. */ NSString *RCTBridgeModuleNameForClass(Class cls) { #if RCT_DEV RCTAssert([cls conformsToProtocol:@protocol(RCTBridgeModule)], @"Bridge module `%@` does not conform to RCTBridgeModule", cls); #endif NSString *name = [cls moduleName]; if (name.length == 0) { name = NSStringFromClass(cls); } if ([name hasPrefix:@"RK"]) { name = [name stringByReplacingCharactersInRange:(NSRange){0,@"RK".length} withString:@"RCT"]; } return name; } @implementation RCTBridge { NSURL *_delegateBundleURL; } dispatch_queue_t RCTJSThread; + (void)initialize { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // Set up JS thread RCTJSThread = (id)kCFNull; }); } static RCTBridge *RCTCurrentBridgeInstance = nil; /** * The last current active bridge instance. This is set automatically whenever * the bridge is accessed. It can be useful for static functions or singletons * that need to access the bridge for purposes such as logging, but should not * be relied upon to return any particular instance, due to race conditions. */ + (instancetype)currentBridge { return RCTCurrentBridgeInstance; } + (void)setCurrentBridge:(RCTBridge *)currentBridge { RCTCurrentBridgeInstance = currentBridge; } - (instancetype)initWithDelegate:(id)delegate launchOptions:(NSDictionary *)launchOptions { return [self initWithDelegate:delegate bundleURL:nil moduleProvider:nil launchOptions:launchOptions]; } - (instancetype)initWithBundleURL:(NSURL *)bundleURL moduleProvider:(RCTBridgeModuleProviderBlock)block launchOptions:(NSDictionary *)launchOptions { return [self initWithDelegate:nil bundleURL:bundleURL moduleProvider:block launchOptions:launchOptions]; } - (instancetype)initWithDelegate:(id)delegate bundleURL:(NSURL *)bundleURL moduleProvider:(RCTBridgeModuleProviderBlock)block launchOptions:(NSDictionary *)launchOptions { if (self = [super init]) { _delegate = delegate; _bundleURL = bundleURL; _moduleProvider = block; _launchOptions = [launchOptions copy]; [self setUp]; RCTExecuteOnMainQueue(^{ [self bindKeys]; }); } return self; } RCT_NOT_IMPLEMENTED(- (instancetype)init) - (void)dealloc { /** * This runs only on the main thread, but crashes the subclass * RCTAssertMainQueue(); */ [[NSNotificationCenter defaultCenter] removeObserver:self]; [self invalidate]; } - (void)bindKeys { RCTAssertMainQueue(); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:RCTReloadNotification object:nil]; #if TARGET_IPHONE_SIMULATOR RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; // reload in current mode [commands registerKeyCommandWithInput:@"r" modifierFlags:UIKeyModifierCommand action:^(__unused UIKeyCommand *command) { [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil userInfo:nil]; }]; #endif } - (RCTPerformanceLogger *)performanceLogger { return self.batchedBridge.performanceLogger; } - (NSArray *)moduleClasses { return self.batchedBridge.moduleClasses; } - (id)moduleForName:(NSString *)moduleName { return [self.batchedBridge moduleForName:moduleName]; } - (id)moduleForClass:(Class)moduleClass { return [self moduleForName:RCTBridgeModuleNameForClass(moduleClass)]; } - (NSArray *)modulesConformingToProtocol:(Protocol *)protocol { NSMutableArray *modules = [NSMutableArray new]; for (Class moduleClass in self.moduleClasses) { if ([moduleClass conformsToProtocol:protocol]) { id module = [self moduleForClass:moduleClass]; if (module) { [modules addObject:module]; } } } return [modules copy]; } - (BOOL)moduleIsInitialized:(Class)moduleClass { return [self.batchedBridge moduleIsInitialized:moduleClass]; } - (void)whitelistedModulesDidChange { [self.batchedBridge whitelistedModulesDidChange]; } - (void)reload { /** * Any thread */ dispatch_async(dispatch_get_main_queue(), ^{ [self invalidate]; [self setUp]; }); } - (void)setUp { RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTBridge setUp]", nil); // Only update bundleURL from delegate if delegate bundleURL has changed NSURL *previousDelegateURL = _delegateBundleURL; _delegateBundleURL = [self.delegate sourceURLForBridge:self]; if (_delegateBundleURL && ![_delegateBundleURL isEqual:previousDelegateURL]) { _bundleURL = _delegateBundleURL; } // Sanitize the bundle URL _bundleURL = [RCTConvert NSURL:_bundleURL.absoluteString]; [self createBatchedBridge]; RCT_PROFILE_END_EVENT(0, @"", nil); } - (void)createBatchedBridge { self.batchedBridge = [[RCTBatchedBridge alloc] initWithParentBridge:self]; } - (BOOL)isLoading { return self.batchedBridge.loading; } - (BOOL)isValid { return self.batchedBridge.valid; } - (BOOL)isBatchActive { return [_batchedBridge isBatchActive]; } - (void)invalidate { RCTBridge *batchedBridge = self.batchedBridge; self.batchedBridge = nil; if (batchedBridge) { RCTExecuteOnMainQueue(^{ [batchedBridge invalidate]; }); } } - (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args { NSArray *ids = [moduleDotMethod componentsSeparatedByString:@"."]; NSString *module = ids[0]; NSString *method = ids[1]; [self enqueueJSCall:module method:method args:args completion:NULL]; } - (void)enqueueJSCall:(NSString *)module method:(NSString *)method args:(NSArray *)args completion:(dispatch_block_t)completion { [self.batchedBridge enqueueJSCall:module method:method args:args completion:completion]; } - (void)enqueueCallback:(NSNumber *)cbID args:(NSArray *)args { [self.batchedBridge enqueueCallback:cbID args:args]; } @end