-
Notifications
You must be signed in to change notification settings - Fork 24k
/
BridgeDevSupportManager.java
270 lines (251 loc) · 10.5 KB
/
BridgeDevSupportManager.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) Facebook, Inc. and its 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.devsupport;
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.debug.holder.PrinterHolder;
import com.facebook.debug.tags.ReactDebugOverlayTags;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.futures.SimpleSettableFuture;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevOptionHandler;
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback;
import com.facebook.react.packagerconnection.RequestHandler;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Interface for accessing and interacting with development features. Following features
* are supported through this manager class:
* 1) Displaying JS errors (aka RedBox)
* 2) Displaying developers menu (Reload JS, Debug JS)
* 3) Communication with developer server in order to download updated JS bundle
* 4) Starting/stopping broadcast receiver for js reload signals
* 5) Starting/stopping motion sensor listener that recognize shake gestures which in turn may
* trigger developers menu.
* 6) Launching developers settings view
*
* This class automatically monitors the state of registered views and activities to which they are
* bound to make sure that we don't display overlay or that we we don't listen for sensor events
* when app is backgrounded.
*
* {@link com.facebook.react.ReactInstanceManager} implementation is responsible for instantiating
* this class as well as for populating with a reference to {@link CatalystInstance} whenever
* instance manager recreates it (through {@link #onNewReactContextCreated). Also, instance manager
* is responsible for enabling/disabling dev support in case when app is backgrounded or when all
* the views has been detached from the instance (through {@link #setDevSupportEnabled} method).
*
* IMPORTANT: In order for developer support to work correctly it is required that the
* manifest of your application contain the following entries:
* {@code <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>}
* {@code <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>}
*/
public final class BridgeDevSupportManager extends DevSupportManagerBase {
private boolean mIsSamplingProfilerEnabled = false;
public BridgeDevSupportManager(
Context applicationContext,
ReactInstanceDevHelper reactInstanceManagerHelper,
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
super(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers);
if (getDevSettings().isStartSamplingProfilerOnInit()) {
// Only start the profiler. If its already running, there is an error
if (!mIsSamplingProfilerEnabled) {
toggleJSSamplingProfiler();
} else {
Toast.makeText(
applicationContext,
"JS Sampling Profiler was already running, so did not start the sampling profiler",
Toast.LENGTH_LONG)
.show();
}
}
addCustomDevOption(
mIsSamplingProfilerEnabled
? applicationContext.getString(
com.facebook.react.R.string.catalyst_sample_profiler_disable)
: applicationContext.getString(
com.facebook.react.R.string.catalyst_sample_profiler_enable),
new DevOptionHandler() {
@Override
public void onOptionSelected() {
toggleJSSamplingProfiler();
}
});
if (!getDevSettings().isDeviceDebugEnabled()) {
// For remote debugging, we open up Chrome running the app in a web worker.
// Note that this requires async communication, which will not work for Turbo Modules.
addCustomDevOption(
getDevSettings().isRemoteJSDebugEnabled()
? applicationContext.getString(com.facebook.react.R.string.catalyst_debug_stop)
: applicationContext.getString(com.facebook.react.R.string.catalyst_debug),
new DevOptionHandler() {
@Override
public void onOptionSelected() {
getDevSettings().setRemoteJSDebugEnabled(!getDevSettings().isRemoteJSDebugEnabled());
handleReloadJS();
}
});
}
}
@Override
public void loadSplitBundleFromServer(
final String bundlePath, final DevSplitBundleCallback callback) {
fetchSplitBundleAndCreateBundleLoader(
bundlePath,
new CallbackWithBundleLoader() {
@Override
public void onSuccess(JSBundleLoader bundleLoader) {
bundleLoader.loadScript(getCurrentContext().getCatalystInstance());
getCurrentContext()
.getJSModule(HMRClient.class)
.registerBundle(getDevServerHelper().getDevServerSplitBundleURL(bundlePath));
callback.onSuccess();
}
@Override
public void onError(String url, Throwable cause) {
callback.onError(url, cause);
}
});
}
private WebsocketJavaScriptExecutor.JSExecutorConnectCallback getExecutorConnectCallback(
final SimpleSettableFuture<Boolean> future) {
return new WebsocketJavaScriptExecutor.JSExecutorConnectCallback() {
@Override
public void onSuccess() {
future.set(true);
hideDevLoadingView();
}
@Override
public void onFailure(final Throwable cause) {
hideDevLoadingView();
FLog.e(ReactConstants.TAG, "Failed to connect to debugger!", cause);
future.setException(
new IOException(
getApplicationContext().getString(com.facebook.react.R.string.catalyst_debug_error),
cause));
}
};
}
private void reloadJSInProxyMode() {
// When using js proxy, there is no need to fetch JS bundle as proxy executor will do that
// anyway
getDevServerHelper().launchJSDevtools();
JavaJSExecutor.Factory factory =
new JavaJSExecutor.Factory() {
@Override
public JavaJSExecutor create() throws Exception {
WebsocketJavaScriptExecutor executor = new WebsocketJavaScriptExecutor();
SimpleSettableFuture<Boolean> future = new SimpleSettableFuture<>();
executor.connect(
getDevServerHelper().getWebsocketProxyURL(), getExecutorConnectCallback(future));
// TODO(t9349129) Don't use timeout
try {
future.get(90, TimeUnit.SECONDS);
return executor;
} catch (ExecutionException e) {
throw (Exception) e.getCause();
} catch (InterruptedException | TimeoutException e) {
throw new RuntimeException(e);
}
}
};
getReactInstanceDevHelper().onReloadWithJSDebugger(factory);
}
@Override
public void handleReloadJS() {
UiThreadUtil.assertOnUiThread();
ReactMarker.logMarker(
ReactMarkerConstants.RELOAD,
getDevSettings().getPackagerConnectionSettings().getDebugServerHost());
// dismiss redbox if exists
hideRedboxDialog();
if (getDevSettings().isRemoteJSDebugEnabled()) {
PrinterHolder.getPrinter()
.logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: load from Proxy");
showDevLoadingViewForRemoteJSEnabled();
reloadJSInProxyMode();
} else {
PrinterHolder.getPrinter()
.logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: load from Server");
String bundleURL =
getDevServerHelper()
.getDevServerBundleURL(Assertions.assertNotNull(getJSAppBundleName()));
reloadJSFromServer(bundleURL);
}
}
/** Starts of stops the sampling profiler */
private void toggleJSSamplingProfiler() {
JavaScriptExecutorFactory javaScriptExecutorFactory =
getReactInstanceDevHelper().getJavaScriptExecutorFactory();
if (!mIsSamplingProfilerEnabled) {
try {
javaScriptExecutorFactory.startSamplingProfiler();
Toast.makeText(getApplicationContext(), "Starting Sampling Profiler", Toast.LENGTH_SHORT)
.show();
} catch (UnsupportedOperationException e) {
Toast.makeText(
getApplicationContext(),
javaScriptExecutorFactory.toString() + " does not support Sampling Profiler",
Toast.LENGTH_LONG)
.show();
} finally {
mIsSamplingProfilerEnabled = true;
}
} else {
try {
final String outputPath =
File.createTempFile(
"sampling-profiler-trace", ".cpuprofile", getApplicationContext().getCacheDir())
.getPath();
javaScriptExecutorFactory.stopSamplingProfiler(outputPath);
Toast.makeText(
getApplicationContext(),
"Saved results from Profiler to " + outputPath,
Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
FLog.e(
ReactConstants.TAG,
"Could not create temporary file for saving results from Sampling Profiler");
} catch (UnsupportedOperationException e) {
Toast.makeText(
getApplicationContext(),
javaScriptExecutorFactory.toString() + "does not support Sampling Profiler",
Toast.LENGTH_LONG)
.show();
} finally {
mIsSamplingProfilerEnabled = false;
}
}
}
}