@Override public final List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { List<NativeModule> modules = new ArrayList<>(); for (ModuleSpec holder : getNativeModules(reactContext)) { NativeModule nativeModule; SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createNativeModule") .arg("module", holder.getType()) .flush(); try { ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, holder.getType().getSimpleName()); nativeModule = holder.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } modules.add(nativeModule); } return modules; }
@Override protected Result<ReactApplicationContext> doInBackground(ReactContextInitParams... params) { // TODO(t11687218): Look over all threading // Default priority is Process.THREAD_PRIORITY_BACKGROUND which means we'll be put in a cgroup // that only has access to a small fraction of CPU time. The priority will be reset after // this task finishes: https://android.googlesource.com/platform/frameworks/base/+/d630f105e8bc0021541aacb4dc6498a49048ecea/core/java/android/os/AsyncTask.java#256 Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); Assertions.assertCondition(params != null && params.length > 0 && params[0] != null); try { JavaScriptExecutor jsExecutor = params[0].getJsExecutorFactory().create(); ReactApplicationContext reactApplicationContext = createReactContext(jsExecutor, params[0].getJsBundleLoader()); ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_START); return Result.of(reactApplicationContext); } catch (Exception e) { // Pass exception to onPostExecute() so it can be handled on the main thread return Result.of(e); } }
/** * Uses configured {@link ReactPackage} instances to create all view managers. */ public List<ViewManager> createAllViewManagers( ReactApplicationContext catalystApplicationContext) { ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers"); try { List<ViewManager> allViewManagers = new ArrayList<>(); for (ReactPackage reactPackage : mPackages) { allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext)); } return allViewManagers; } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END); } }
private NativeModule create() { SoftAssertions.assertCondition(mModule == null, "Creating an already created module."); ReactMarker.logMarker(CREATE_MODULE_START, mName); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createModule") .arg("name", mName) .flush(); NativeModule module = assertNotNull(mProvider).get(); mProvider = null; if (mInitializeNeeded) { doInitialize(module); mInitializeNeeded = false; } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_MODULE_END); return module; }
void notifyJSInstanceInitialized() { mReactApplicationContext.assertOnNativeModulesQueueThread("From version React Native v0.44, " + "native modules are explicitly not initialized on the UI thread. See " + "https://github.com/facebook/react-native/wiki/Breaking-Changes#d4611211-reactnativeandroidbreaking-move-nativemodule-initialization-off-ui-thread---aaachiuuu " + " for more details."); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_START); Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "NativeModuleRegistry_notifyJSInstanceInitialized"); try { for (ModuleHolder module : mModules.values()) { module.initialize(); } } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_END); } }
private UIManagerModule createUIManager(ReactApplicationContext reactContext) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); try { List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers( reactContext); return new UIManagerModule( reactContext, viewManagersList, mUIImplementationProvider, mLazyViewManagersEnabled); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); } }
@DoNotStrip public NativeArray getConstants() { BaseJavaModule baseJavaModule = getModule(); ReactMarker.logMarker(GET_CONSTANTS_START, getName()); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = baseJavaModule.getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); ReactMarker.logMarker(CONVERT_CONSTANTS_START, getName()); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); ReactMarker.logMarker(CONVERT_CONSTANTS_END); ReactMarker.logMarker(GET_CONSTANTS_END); return array; }
/** * Uses configured {@link ReactPackage} instances to create all view managers */ @Override public List<ViewManager> createAllViewManagers( ReactApplicationContext catalystApplicationContext) { ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers"); try { List<ViewManager> allViewManagers = new ArrayList<>(); for (ReactPackage reactPackage : mPackages) { allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext)); } return allViewManagers; } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END); } }
private NativeModule create() { boolean isEagerModule = mInfo instanceof LegacyModuleInfo; String name = isEagerModule ? ((LegacyModuleInfo) mInfo).mType.getSimpleName() : mInfo.name(); if (!isEagerModule) { ReactMarker.logMarker(CREATE_MODULE_START); } SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createModule") .arg("name", name) .flush(); NativeModule module = assertNotNull(mProvider).get(); if (mInitializeNeeded) { doInitialize(module); mInitializeNeeded = false; } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); if (!isEagerModule) { ReactMarker.logMarker(CREATE_MODULE_END); } return module; }
void notifyCatalystInstanceInitialized() { UiThreadUtil.assertOnUiThread(); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_START); Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "NativeModuleRegistry_notifyCatalystInstanceInitialized"); try { for (ModuleHolder module : mModules.values()) { module.initialize(); } } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_END); } }
private void runCreateReactContextOnNewThread(final ReactContextInitParams initParams) { if (mCurrentReactContext != null) { tearDownReactContext(mCurrentReactContext); mCurrentReactContext = null; } mCreateReactContextThread = new Thread(new Runnable() { @Override public void run() { try { final ReactApplicationContext reactApplicationContext = createReactContext( initParams.getJsExecutorFactory().create(), initParams.getJsBundleLoader()); ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_START); UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { mCreateReactContextThread = null; try { setupReactContext(reactApplicationContext); } catch (Exception e) { mDevSupportManager.handleException(e); } if (mPendingReactContextInitParams != null) { runCreateReactContextOnNewThread(mPendingReactContextInitParams); mPendingReactContextInitParams = null; } } }); } catch (Exception e) { mDevSupportManager.handleException(e); } } }); mCreateReactContextThread.setPriority(Thread.MAX_PRIORITY); mCreateReactContextThread.start(); }
private void setupReactContext(ReactApplicationContext reactContext) { ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_END); ReactMarker.logMarker(SETUP_REACT_CONTEXT_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "setupReactContext"); UiThreadUtil.assertOnUiThread(); Assertions.assertCondition(mCurrentReactContext == null); mCurrentReactContext = Assertions.assertNotNull(reactContext); CatalystInstance catalystInstance = Assertions.assertNotNull(reactContext.getCatalystInstance()); catalystInstance.initialize(); mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); moveReactContextToCurrentLifecycleState(); for (ReactRootView rootView : mAttachedRootViews) { attachMeasuredRootViewToInstance(rootView, catalystInstance); } ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; listeners = mReactInstanceEventListeners.toArray(listeners); for (ReactInstanceEventListener listener : listeners) { listener.onReactContextInitialized(reactContext); } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); }
private static Map<String, Object> createConstants( List<ViewManager> viewManagerList, boolean lazyViewManagersEnabled) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants"); try { return UIManagerModuleConstantsHelper.createConstants( viewManagerList, lazyViewManagersEnabled); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END); } }
private void doInitialize(NativeModule module) { SystraceMessage.Builder section = SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "initialize"); if (module instanceof CxxModuleWrapper) { section.arg("className", module.getClass().getSimpleName()); } else { section.arg("name", mName); } section.flush(); ReactMarker.logMarker(ReactMarkerConstants.INITIALIZE_MODULE_START, mName); module.initialize(); ReactMarker.logMarker(ReactMarkerConstants.INITIALIZE_MODULE_END); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
@DoNotStrip public @Nullable NativeArray getConstants() { if (!mModuleHolder.getHasConstants()) { return null; } BaseJavaModule baseJavaModule = getModule(); ReactMarker.logMarker(GET_CONSTANTS_START, getName()); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = baseJavaModule.getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); ReactMarker.logMarker(CONVERT_CONSTANTS_START, getName()); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); ReactMarker.logMarker(CONVERT_CONSTANTS_END); ReactMarker.logMarker(GET_CONSTANTS_END); return array; }
@Override public ReactModuleInfoProvider getReactModuleInfoProvider() { ReactMarker.logMarker( ReactMarkerConstants.CORE_REACT_PACKAGE_GET_REACT_MODULE_INFO_PROVIDER_START); // This has to be done via reflection or we break open source. ReactModuleInfoProvider reactModuleInfoProvider = LazyReactPackage.getReactModuleInfoProviderViaReflection(this); ReactMarker.logMarker( ReactMarkerConstants.CORE_REACT_PACKAGE_GET_REACT_MODULE_INFO_PROVIDER_END); return reactModuleInfoProvider; }
private static Map<String, Object> createConstants(List<ViewManager> viewManagerList) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants"); try { return UIManagerModuleConstantsHelper.createConstants(viewManagerList); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END); } }
private void setupReactContext(ReactApplicationContext reactContext) { ReactMarker.logMarker(SETUP_REACT_CONTEXT_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "setupReactContext"); UiThreadUtil.assertOnUiThread(); Assertions.assertCondition(mCurrentReactContext == null); mCurrentReactContext = Assertions.assertNotNull(reactContext); CatalystInstance catalystInstance = Assertions.assertNotNull(reactContext.getCatalystInstance()); catalystInstance.initialize(); mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); moveReactContextToCurrentLifecycleState(); for (ReactRootView rootView : mAttachedRootViews) { attachMeasuredRootViewToInstance(rootView, catalystInstance); } ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; listeners = mReactInstanceEventListeners.toArray(listeners); for (ReactInstanceEventListener listener : listeners) { listener.onReactContextInitialized(reactContext); } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); }
private UIManagerModule createUIManager(ReactApplicationContext reactContext) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); try { List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers( reactContext); return new UIManagerModule( reactContext, viewManagersList, mUIImplementationProvider); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); } }
public void processPackage(ReactPackage reactPackage) { if (mLazyNativeModulesEnabled) { if (!(reactPackage instanceof LazyReactPackage)) { throw new IllegalStateException("Lazy native modules requires all ReactPackage to " + "inherit from LazyReactPackage"); } LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage; List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext); Map<Class, ReactModuleInfo> reactModuleInfoMap = lazyReactPackage.getReactModuleInfoProvider() .getReactModuleInfos(); for (ModuleSpec moduleSpec : moduleSpecs) { Class<? extends NativeModule> type = moduleSpec.getType(); ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(type); ModuleHolder moduleHolder; if (reactModuleInfo == null) { if (BaseJavaModule.class.isAssignableFrom(type)) { throw new IllegalStateException("Native Java module " + type.getSimpleName() + " should be annotated with @ReactModule and added to a @ReactModuleList."); } ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, moduleSpec.getType().getName()); NativeModule module = moduleSpec.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); moduleHolder = new ModuleHolder(module); } else { moduleHolder = new ModuleHolder( reactModuleInfo.name(), reactModuleInfo.canOverrideExistingModule(), reactModuleInfo.supportsWebWorkers(), reactModuleInfo.needsEagerInit(), reactModuleInfo.hasConstants(), moduleSpec.getProvider()); } String name = moduleHolder.getName(); if (namesToType.containsKey(name)) { Class<? extends NativeModule> existingNativeModule = namesToType.get(name); if (!moduleHolder.getCanOverrideExistingModule()) { throw new IllegalStateException("Native module " + type.getSimpleName() + " tried to override " + existingNativeModule.getSimpleName() + " for module name " + name + ". If this was your intention, set canOverrideExistingModule=true"); } mModules.remove(existingNativeModule); } namesToType.put(name, type); mModules.put(type, moduleHolder); } } else { FLog.d( ReactConstants.TAG, reactPackage.getClass().getSimpleName() + " is not a LazyReactPackage, falling back to old version."); List<NativeModule> nativeModules; if (reactPackage instanceof ReactInstancePackage) { ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage; nativeModules = reactInstancePackage.createNativeModules( mReactApplicationContext, mReactInstanceManager); } else { nativeModules = reactPackage.createNativeModules(mReactApplicationContext); } for (NativeModule nativeModule : nativeModules) { addNativeModule(nativeModule); } } }
@Override public void startProcessPackage() { ReactMarker.logMarker(PROCESS_CORE_REACT_PACKAGE_START); }
@Override public void endProcessPackage() { ReactMarker.logMarker(PROCESS_CORE_REACT_PACKAGE_END); }
public void processPackage(ReactPackage reactPackage) { if (mLazyNativeModulesEnabled) { if (!(reactPackage instanceof LazyReactPackage)) { throw new IllegalStateException("Lazy native modules requires all ReactPackage to " + "inherit from LazyReactPackage"); } LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage; List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext); Map<Class, ReactModuleInfo> reactModuleInfoMap = lazyReactPackage.getReactModuleInfoProvider() .getReactModuleInfos(); for (ModuleSpec moduleSpec : moduleSpecs) { Class<? extends NativeModule> type = moduleSpec.getType(); ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(type); ModuleHolder moduleHolder; if (reactModuleInfo == null) { if (BaseJavaModule.class.isAssignableFrom(type)) { throw new IllegalStateException("Native Java module " + type.getSimpleName() + " should be annotated with @ReactModule and added to a @ReactModuleList."); } ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, moduleSpec.getType().getName()); NativeModule module = moduleSpec.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); moduleHolder = new ModuleHolder(module); } else { moduleHolder = new ModuleHolder( reactModuleInfo.name(), reactModuleInfo.canOverrideExistingModule(), reactModuleInfo.supportsWebWorkers(), reactModuleInfo.needsEagerInit(), moduleSpec.getProvider()); } String name = moduleHolder.getName(); if (namesToType.containsKey(name)) { Class<? extends NativeModule> existingNativeModule = namesToType.get(name); if (!moduleHolder.getCanOverrideExistingModule()) { throw new IllegalStateException("Native module " + type.getSimpleName() + " tried to override " + existingNativeModule.getSimpleName() + " for module name " + name + ". If this was your intention, set canOverrideExistingModule=true"); } mModules.remove(existingNativeModule); } namesToType.put(name, type); mModules.put(type, moduleHolder); } } else { FLog.d( ReactConstants.TAG, reactPackage.getClass().getSimpleName() + " is not a LazyReactPackage, falling back to old version."); List<NativeModule> nativeModules; if (reactPackage instanceof ReactInstancePackage) { ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage; nativeModules = reactInstancePackage.createNativeModules( mReactApplicationContext, mReactInstanceManager); } else { nativeModules = reactPackage.createNativeModules(mReactApplicationContext); } for (NativeModule nativeModule : nativeModules) { addNativeModule(nativeModule); } } }