下面列出了com.facebook.react.bridge.JavaScriptExecutor#com.facebook.systrace.Systrace 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onProducerStart(String requestId, String producerName) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
StringBuilder entryName = new StringBuilder();
entryName.append("FRESCO_PRODUCER_");
entryName.append(producerName.replace(':', '_'));
Pair<Integer,String> requestPair = Pair.create(mCurrentID, entryName.toString());
Systrace.beginAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
requestPair.second,
mCurrentID);
mProducerID.put(requestId, requestPair);
mCurrentID++;
}
@Override
public void onProducerFinishWithSuccess(
String requestId,
String producerName,
Map<String, String> extraMap) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mProducerID.containsKey(requestId)) {
Pair<Integer, String> entry = mProducerID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mProducerID.remove(requestId);
}
}
@Override
public void onProducerFinishWithFailure(
String requestId,
String producerName,
Throwable throwable,
Map<String, String> extraMap) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mProducerID.containsKey(requestId)) {
Pair<Integer, String> entry = mProducerID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mProducerID.remove(requestId);
}
}
@Override
public void onProducerFinishWithCancellation(
String requestId, String producerName, Map<String, String> extraMap) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mProducerID.containsKey(requestId)) {
Pair<Integer, String> entry = mProducerID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mProducerID.remove(requestId);
}
}
@Override
public void onProducerEvent(String requestId, String producerName, String producerEventName) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
StringBuilder entryName = new StringBuilder();
entryName.append("FRESCO_PRODUCER_EVENT_");
entryName.append(requestId.replace(':', '_'));
entryName.append("_");
entryName.append(producerName.replace(':', '_'));
entryName.append("_");
entryName.append(producerEventName.replace(':', '_'));
Systrace.traceInstant(
Systrace.TRACE_TAG_REACT_FRESCO,
entryName.toString(),
Systrace.EventScope.THREAD);
}
@Override
public void onRequestStart(
ImageRequest request,
Object callerContext,
String requestId,
boolean isPrefetch) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
StringBuilder entryName = new StringBuilder();
entryName.append("FRESCO_REQUEST_");
entryName.append(request.getSourceUri().toString().replace(':', '_'));
Pair<Integer,String> requestPair = Pair.create(mCurrentID, entryName.toString());
Systrace.beginAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
requestPair.second,
mCurrentID);
mRequestsID.put(requestId, requestPair);
mCurrentID++;
}
@Override
public void onRequestFailure(
ImageRequest request,
String requestId,
Throwable throwable,
boolean isPrefetch) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mRequestsID.containsKey(requestId)) {
Pair<Integer, String> entry = mRequestsID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mRequestsID.remove(requestId);
}
}
@Override
public void runJSBundle() {
Log.d(ReactConstants.TAG, "CatalystInstanceImpl.runJSBundle()");
Assertions.assertCondition(!mJSBundleHasLoaded, "JS bundle was already loaded!");
// incrementPendingJSCalls();
mJSBundleLoader.loadScript(CatalystInstanceImpl.this);
synchronized (mJSCallsPendingInitLock) {
// Loading the bundle is queued on the JS thread, but may not have
// run yet. It's safe to set this here, though, since any work it
// gates will be queued on the JS thread behind the load.
mAcceptCalls = true;
for (PendingJSCall function : mJSCallsPendingInit) {
function.call(this);
}
mJSCallsPendingInit.clear();
mJSBundleHasLoaded = true;
}
// This is registered after JS starts since it makes a JS call
Systrace.registerListener(mTraceListener);
}
private void incrementPendingJSCalls() {
int oldPendingCalls = mPendingJSCalls.getAndIncrement();
boolean wasIdle = oldPendingCalls == 0;
Systrace.traceCounter(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
mJsPendingCallsTitleForTrace,
oldPendingCalls + 1);
if (wasIdle && !mBridgeIdleListeners.isEmpty()) {
mNativeModulesQueueThread.runOnQueue(new Runnable() {
@Override
public void run() {
for (NotThreadSafeBridgeIdleDebugListener listener : mBridgeIdleListeners) {
listener.onTransitionToBridgeBusy();
}
}
});
}
}
private void decrementPendingJSCalls() {
int newPendingCalls = mPendingJSCalls.decrementAndGet();
// TODO(9604406): handle case of web workers injecting messages to main thread
//Assertions.assertCondition(newPendingCalls >= 0);
boolean isNowIdle = newPendingCalls == 0;
Systrace.traceCounter(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
mJsPendingCallsTitleForTrace,
newPendingCalls);
if (isNowIdle && !mBridgeIdleListeners.isEmpty()) {
mNativeModulesQueueThread.runOnQueue(new Runnable() {
@Override
public void run() {
for (NotThreadSafeBridgeIdleDebugListener listener : mBridgeIdleListeners) {
listener.onTransitionToBridgeIdle();
}
}
});
}
}
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.markInitializable();
}
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_END);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node,
* including its children set (note that the children nodes will not be cloned).
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNode(ReactShadowNode node) {
if (DEBUG) {
FLog.d(TAG, "cloneNode \n\tnode: " + node);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNode")
.flush();
try {
ReactShadowNode clone = node.mutableCopy(node.getInstanceHandle());
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* children set will be empty.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewChildren(ReactShadowNode node) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewChildren \n\tnode: " + node);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewChildren")
.flush();
try {
ReactShadowNode clone = node.mutableCopyWithNewChildren(node.getInstanceHandle());
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewProps(
ReactShadowNode node, @Nullable ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewProps \n\tnode: " + node + "\n\tprops: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewProps")
.flush();
try {
ReactShadowNode clone = node.mutableCopyWithNewProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter and its
* children set will be empty.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewChildrenAndProps(
ReactShadowNode node, ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewChildrenAndProps \n\tnode: " + node + "\n\tnewProps: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewChildrenAndProps")
.flush();
try {
ReactShadowNode clone =
node.mutableCopyWithNewChildrenAndProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Appends the child {@link ReactShadowNode} to the children set of the parent {@link
* ReactShadowNode}.
*/
@Nullable
@DoNotStrip
public void appendChild(ReactShadowNode parent, ReactShadowNode child) {
if (DEBUG) {
FLog.d(TAG, "appendChild \n\tparent: " + parent + "\n\tchild: " + child);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.appendChild")
.flush();
try {
// If the child to append was already committed (child.isSealed()),
// then we add a mutation of it. In the future this will be performed by FabricJS / Fiber.
//TODO: T27926878 avoid cloning shared child
if (child.isSealed()) {
child = child.mutableCopy(child.getInstanceHandle());
}
parent.addChildAt(child, parent.getChildCount());
} catch (Throwable t) {
handleException(parent, t);
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Uses configured {@link ReactPackage} instances to create all view managers.
*/
public List<ViewManager> getOrCreateViewManagers(
ReactApplicationContext catalystApplicationContext) {
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers");
try {
if (mViewManagers == null) {
synchronized (mPackages) {
if (mViewManagers == null) {
mViewManagers = new ArrayList<>();
for (ReactPackage reactPackage : mPackages) {
mViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext));
}
return mViewManagers;
}
}
}
return mViewManagers;
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END);
}
}
private void attachRootViewToInstance(
final ReactRootView rootView) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.attachRootViewToInstance()");
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
UIManager uiManagerModule = UIManagerHelper.getUIManager(mCurrentReactContext, rootView.getUIManagerType());
final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.runApplication();
Systrace.beginAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE,
"pre_rootView.onAttachedToReactInstance",
rootTag);
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
Systrace.endAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE,
"pre_rootView.onAttachedToReactInstance",
rootTag);
rootView.onAttachedToReactInstance();
}
});
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@Override
public void doFrameGuarded(long frameTimeNanos) {
if (mIsInIllegalUIState) {
FLog.w(
ReactConstants.TAG,
"Not flushing pending UI operations because of previously thrown Exception");
return;
}
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "dispatchNonBatchedUIOperations");
try {
dispatchPendingNonBatchedOperations(frameTimeNanos);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
flushPendingBatches();
ReactChoreographer.getInstance().postFrameCallback(
ReactChoreographer.CallbackType.DISPATCH_UI, this);
}
/**
* Invoked at the end of the transaction to commit any updates to the node hierarchy.
*/
public void dispatchViewUpdates(int batchId) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"UIImplementation.dispatchViewUpdates")
.arg("batchId", batchId)
.flush();
final long commitStartTime = SystemClock.uptimeMillis();
try {
updateViewHierarchy();
mNativeViewHierarchyOptimizer.onBatchComplete();
mOperationsQueue.dispatchViewUpdates(batchId, commitStartTime, mLastCalculateLayoutTime);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Sends the given Event to JS, coalescing eligible events if JS is backed up.
*/
public void dispatchEvent(Event event) {
Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized");
for (EventDispatcherListener listener : mListeners) {
listener.onEventDispatch(event);
}
synchronized (mEventsStagingLock) {
mEventStaging.add(event);
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
event.getEventName(),
event.getUniqueID());
}
if (mReactEventEmitter != null) {
// If the host activity is paused, the frame callback may not be currently
// posted. Ensure that it is so that this event gets delivered promptly.
mCurrentFrameCallback.maybePostFromNonUI();
} else {
// No JS application has started yet, or resumed. This can happen when a ReactRootView is
// added to view hierarchy, but ReactContext creation has not completed yet. In this case, any
// touch event dispatch will hit this codepath, and we simply queue them so that they
// are dispatched once ReactContext creation completes and JS app is running.
}
}
@Override
public void doFrame(long frameTimeNanos) {
UiThreadUtil.assertOnUiThread();
if (mShouldStop) {
mIsPosted = false;
} else {
post();
}
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ScheduleDispatchFrameCallback");
try {
moveStagedEventsToDispatchQueue();
if (!mHasDispatchScheduled) {
mHasDispatchScheduled = true;
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"ScheduleDispatchFrameCallback",
mHasDispatchScheduledCount.get());
mReactContext.runOnJSQueueThread(mDispatchEventsRunnable);
}
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
@ReactMethod(isBlockingSynchronousMethod = true)
public @Nullable WritableMap getConstantsForViewManager(final String viewManagerName) {
ViewManager targetView =
viewManagerName != null ? mUIImplementation.resolveViewManager(viewManagerName) : null;
if (targetView == null) {
return null;
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "UIManagerModule.getConstantsForViewManager")
.arg("ViewManager", targetView.getName())
.arg("Lazy", true)
.flush();
try {
Map<String, Object> viewManagerConstants =
UIManagerModuleConstantsHelper.createConstantsForViewManager(
targetView, null, null, null, mCustomDirectEvents);
if (viewManagerConstants != null) {
return Arguments.makeNativeMap(viewManagerConstants);
}
return null;
} finally {
SystraceMessage.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE).flush();
}
}
/**
* To implement the transactional requirement mentioned in the class javadoc, we only commit
* UI changes to the actual view hierarchy once a batch of JS->Java calls have been completed.
* We know this is safe because all JS->Java calls that are triggered by a Java->JS call (e.g.
* the delivery of a touch event or execution of 'renderApplication') end up in a single
* JS->Java transaction.
*
* A better way to do this would be to have JS explicitly signal to this module when a UI
* transaction is done. Right now, though, this is how iOS does it, and we should probably
* update the JS and native code and make this change at the same time.
*
* TODO(5279396): Make JS UI library explicitly notify the native UI module of the end of a UI
* transaction using a standard native call
*/
@Override
public void onBatchComplete() {
int batchId = mBatchId;
mBatchId++;
SystraceMessage.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "onBatchCompleteUI")
.arg("BatchId", batchId)
.flush();
for (UIManagerModuleListener listener : mListeners) {
listener.willDispatchViewUpdates(this);
}
try {
mUIImplementation.dispatchViewUpdates(batchId);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
@Override
public void onRequestSuccess(ImageRequest request, String requestId, boolean isPrefetch) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mRequestsID.containsKey(requestId)) {
Pair<Integer, String> entry = mRequestsID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mRequestsID.remove(requestId);
}
}
@Override
public void onRequestCancellation(String requestId) {
if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) {
return;
}
if (mRequestsID.containsKey(requestId)) {
Pair<Integer, String> entry = mRequestsID.get(requestId);
Systrace.endAsyncSection(
Systrace.TRACE_TAG_REACT_FRESCO,
entry.second,
entry.first);
mRequestsID.remove(requestId);
}
}
@Override
public void onTraceStarted() {
CatalystInstanceImpl impl = mOuter.get();
if (impl != null) {
impl.getJSModule(com.facebook.react.bridge.Systrace.class).setEnabled(true);
}
}
@Override
public void onTraceStopped() {
CatalystInstanceImpl impl = mOuter.get();
if (impl != null) {
impl.getJSModule(com.facebook.react.bridge.Systrace.class).setEnabled(false);
}
}
@DoNotStrip
private void findMethods() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "findMethods");
Set<String> methodNames = new HashSet<>();
Class<? extends NativeModule> classForMethods = mModuleHolder.getModule().getClass();
Class<? extends NativeModule> superClass =
(Class<? extends NativeModule>) classForMethods.getSuperclass();
if (ReactModuleWithSpec.class.isAssignableFrom(superClass)) {
// For java module that is based on generated flow-type spec, inspect the
// spec abstract class instead, which is the super class of the given java
// module.
classForMethods = superClass;
}
Method[] targetMethods = classForMethods.getDeclaredMethods();
for (Method targetMethod : targetMethods) {
ReactMethod annotation = targetMethod.getAnnotation(ReactMethod.class);
if (annotation != null) {
String methodName = targetMethod.getName();
if (methodNames.contains(methodName)) {
// We do not support method overloading since js sees a function as an object regardless
// of number of params.
throw new IllegalArgumentException(
"Java Module " + getName() + " method name already registered: " + methodName);
}
MethodDescriptor md = new MethodDescriptor();
JavaMethodWrapper method = new JavaMethodWrapper(this, targetMethod, annotation.isBlockingSynchronousMethod());
md.name = methodName;
md.type = method.getType();
if (md.type == BaseJavaModule.METHOD_TYPE_SYNC) {
md.signature = method.getSignature();
md.method = targetMethod;
}
mMethods.add(method);
mDescs.add(md);
}
}
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@DoNotStrip
public @Nullable NativeMap getConstants() {
if (!mModuleHolder.getHasConstants()) {
return null;
}
final String moduleName = getName();
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "JavaModuleWrapper.getConstants")
.arg("moduleName", moduleName)
.flush();
ReactMarker.logMarker(GET_CONSTANTS_START, moduleName);
BaseJavaModule baseJavaModule = getModule();
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "module.getConstants");
Map<String, Object> map = baseJavaModule.getConstants();
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "create WritableNativeMap");
ReactMarker.logMarker(CONVERT_CONSTANTS_START, moduleName);
try {
return Arguments.makeNativeMap(map);
} finally {
ReactMarker.logMarker(CONVERT_CONSTANTS_END);
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(GET_CONSTANTS_END);
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
}
}