-
Notifications
You must be signed in to change notification settings - Fork 24k
/
BridgelessReactContext.java
180 lines (156 loc) · 6.02 KB
/
BridgelessReactContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.runtime;
import android.content.Context;
import android.util.Log;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.JavaScriptModuleRegistry;
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.common.annotations.FrameworkAPI;
import com.facebook.react.common.annotations.UnstableReactNativeAPI;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.events.EventDispatcherProvider;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
/**
* This class is used instead of {@link ReactApplicationContext} when React Native is operating in
* bridgeless mode. The purpose of this class is to override some methods on {@link
* com.facebook.react.bridge.ReactContext} that use the {@link
* com.facebook.react.bridge.CatalystInstance}, which doesn't exist in bridgeless mode.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
class BridgelessReactContext extends ReactApplicationContext implements EventDispatcherProvider {
private final ReactHostImpl mReactHost;
private final AtomicReference<String> mSourceURL = new AtomicReference<>();
private final String TAG = this.getClass().getSimpleName();
BridgelessReactContext(Context context, ReactHostImpl host) {
super(context);
mReactHost = host;
if (ReactFeatureFlags.unstable_useFabricInterop) {
initializeInteropModules();
}
}
@Override
public boolean isBridgeless() {
return true;
}
@Override
public EventDispatcher getEventDispatcher() {
return mReactHost.getEventDispatcher();
}
public void setSourceURL(String sourceURL) {
mSourceURL.set(sourceURL);
}
@Override
public @Nullable String getSourceURL() {
return mSourceURL.get();
}
@Override
public @Nullable UIManager getFabricUIManager() {
return mReactHost.getUIManager();
}
@Override
public CatalystInstance getCatalystInstance() {
Log.w(
TAG,
"[WARNING] Bridgeless doesn't support CatalystInstance. Accessing an API that's not part of the new architecture is not encouraged usage.");
return new BridgelessCatalystInstance(mReactHost);
}
@Override
public boolean hasActiveReactInstance() {
return mReactHost.isInstanceInitialized();
}
DevSupportManager getDevSupportManager() {
return mReactHost.getDevSupportManager();
}
@Override
public void registerSegment(int segmentId, String path, Callback callback) {
mReactHost.registerSegment(segmentId, path, callback);
}
private static class BridgelessJSModuleInvocationHandler implements InvocationHandler {
private final ReactHostImpl mReactHost;
private final Class<? extends JavaScriptModule> mJSModuleInterface;
public BridgelessJSModuleInvocationHandler(
ReactHostImpl reactHost, Class<? extends JavaScriptModule> jsModuleInterface) {
mReactHost = reactHost;
mJSModuleInterface = jsModuleInterface;
}
@Override
public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args) {
NativeArray jsArgs = args != null ? Arguments.fromJavaArgs(args) : new WritableNativeArray();
mReactHost.callFunctionOnModule(
JavaScriptModuleRegistry.getJSModuleName(mJSModuleInterface), method.getName(), jsArgs);
return null;
}
}
@Override
public <T extends JavaScriptModule> T getJSModule(Class<T> jsInterface) {
if (mInteropModuleRegistry != null
&& mInteropModuleRegistry.shouldReturnInteropModule(jsInterface)) {
return mInteropModuleRegistry.getInteropModule(jsInterface);
}
JavaScriptModule interfaceProxy =
(JavaScriptModule)
Proxy.newProxyInstance(
jsInterface.getClassLoader(),
new Class[] {jsInterface},
new BridgelessJSModuleInvocationHandler(mReactHost, jsInterface));
return (T) interfaceProxy;
}
@Override
public <T extends NativeModule> boolean hasNativeModule(Class<T> nativeModuleInterface) {
return mReactHost.hasNativeModule(nativeModuleInterface);
}
@Override
public Collection<NativeModule> getNativeModules() {
return mReactHost.getNativeModules();
}
@Override
public @Nullable <T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface) {
return mReactHost.getNativeModule(nativeModuleInterface);
}
/**
* @return the RuntimeExecutor, a thread-safe handler for accessing the runtime. If the runtime is
* not initialized yet, it will return null.
*/
@Override
@FrameworkAPI
@UnstableReactNativeAPI
public @Nullable RuntimeExecutor getRuntimeExecutor() {
return mReactHost.getRuntimeExecutor();
}
@Override
@FrameworkAPI
@UnstableReactNativeAPI
public @Nullable JavaScriptContextHolder getJavaScriptContextHolder() {
return mReactHost.getJavaScriptContextHolder();
}
@Override
public void handleException(Exception e) {
mReactHost.handleHostException(e);
}
DefaultHardwareBackBtnHandler getDefaultHardwareBackBtnHandler() {
return mReactHost.getDefaultBackButtonHandler();
}
}