Skip to content

Commit 60142ad

Browse files
Extract PackagerConnectionSettings to ensure easier reusability of PackagerConnection module
Reviewed By: cwdick Differential Revision: D4689535 fbshipit-source-id: f698837f407a03bf91521cc5e921c66f5755e6e0
1 parent 50ff716 commit 60142ad

File tree

8 files changed

+132
-65
lines changed

8 files changed

+132
-65
lines changed

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.facebook.react.common.annotations.VisibleForTesting;
1919
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
20+
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
2021

2122
/**
2223
* Helper class for accessing developers settings that should not be accessed outside of the package
@@ -31,7 +32,6 @@ public class DevInternalSettings implements
3132
private static final String PREFS_FPS_DEBUG_KEY = "fps_debug";
3233
private static final String PREFS_JS_DEV_MODE_DEBUG_KEY = "js_dev_mode_debug";
3334
private static final String PREFS_JS_MINIFY_DEBUG_KEY = "js_minify_debug";
34-
private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host";
3535
private static final String PREFS_ANIMATIONS_DEBUG_KEY = "animations_debug";
3636
private static final String PREFS_RELOAD_ON_JS_CHANGE_KEY = "reload_on_js_change";
3737
private static final String PREFS_INSPECTOR_DEBUG_KEY = "inspector_debug";
@@ -40,13 +40,19 @@ public class DevInternalSettings implements
4040

4141
private final SharedPreferences mPreferences;
4242
private final Listener mListener;
43+
private final PackagerConnectionSettings mPackagerConnectionSettings;
4344

4445
public DevInternalSettings(
4546
Context applicationContext,
4647
Listener listener) {
4748
mListener = listener;
4849
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
4950
mPreferences.registerOnSharedPreferenceChangeListener(this);
51+
mPackagerConnectionSettings = new PackagerConnectionSettings(applicationContext);
52+
}
53+
54+
public PackagerConnectionSettings getPackagerConnectionSettings() {
55+
return mPackagerConnectionSettings;
5056
}
5157

5258
@Override
@@ -73,10 +79,6 @@ public boolean isJSMinifyEnabled() {
7379
return mPreferences.getBoolean(PREFS_JS_MINIFY_DEBUG_KEY, false);
7480
}
7581

76-
public @Nullable String getDebugServerHost() {
77-
return mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null);
78-
}
79-
8082
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
8183
if (mListener != null) {
8284
if (PREFS_FPS_DEBUG_KEY.equals(key) ||

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class DevServerHelper {
7373
private static final String ONCHANGE_ENDPOINT_URL_FORMAT =
7474
"http://%s/onchange";
7575
private static final String WEBSOCKET_PROXY_URL_FORMAT = "ws://%s/debugger-proxy?role=client";
76-
private static final String PACKAGER_CONNECTION_URL_FORMAT = "ws://%s/message?role=android-rn-devserverhelper";
7776
private static final String PACKAGER_STATUS_URL_FORMAT = "http://%s/status";
7877
private static final String HEAP_CAPTURE_UPLOAD_URL_FORMAT = "http://%s/jscheapcaptureupload";
7978
private static final String INSPECTOR_DEVICE_URL_FORMAT = "http://%s/inspector/device?name=%s";
@@ -152,7 +151,7 @@ public void onRequest(@Nullable Object params, JSPackagerClient.Responder respon
152151
});
153152
handlers.putAll(new FileIoHandler().handlers());
154153

155-
mPackagerClient = new JSPackagerClient(getPackagerConnectionURL(), handlers);
154+
mPackagerClient = new JSPackagerClient("devserverhelper", mSettings.getPackagerConnectionSettings(), handlers);
156155
mPackagerClient.init();
157156

158157
return null;
@@ -213,22 +212,18 @@ public static String getReloadAppAction(Context context) {
213212
}
214213

215214
public String getWebsocketProxyURL() {
216-
return String.format(Locale.US, WEBSOCKET_PROXY_URL_FORMAT, getDebugServerHost());
217-
}
218-
219-
private String getPackagerConnectionURL() {
220-
return String.format(Locale.US, PACKAGER_CONNECTION_URL_FORMAT, getDebugServerHost());
215+
return String.format(Locale.US, WEBSOCKET_PROXY_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
221216
}
222217

223218
public String getHeapCaptureUploadUrl() {
224-
return String.format(Locale.US, HEAP_CAPTURE_UPLOAD_URL_FORMAT, getDebugServerHost());
219+
return String.format(Locale.US, HEAP_CAPTURE_UPLOAD_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
225220
}
226221

227222
public String getInspectorDeviceUrl() {
228223
return String.format(
229224
Locale.US,
230225
INSPECTOR_DEVICE_URL_FORMAT,
231-
getDebugServerHost(),
226+
mSettings.getPackagerConnectionSettings().getDebugServerHost(),
232227
AndroidInfoHelpers.getFriendlyDeviceName());
233228
}
234229

@@ -260,30 +255,6 @@ private boolean getHMR() {
260255
return mSettings.isHotModuleReplacementEnabled();
261256
}
262257

263-
/**
264-
* @return the host to use when connecting to the bundle server.
265-
*/
266-
private String getDebugServerHost() {
267-
// Check debug server host setting first. If empty try to detect emulator type and use default
268-
// hostname for those
269-
String hostFromSettings = mSettings.getDebugServerHost();
270-
271-
if (!TextUtils.isEmpty(hostFromSettings)) {
272-
return Assertions.assertNotNull(hostFromSettings);
273-
}
274-
275-
String host = AndroidInfoHelpers.getServerHost();
276-
277-
if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) {
278-
FLog.w(
279-
ReactConstants.TAG,
280-
"You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' " +
281-
"to forward the debug server's port to the device.");
282-
}
283-
284-
return host;
285-
}
286-
287258
private static String createBundleURL(String host, String jsModulePath, boolean devMode, boolean hmr, boolean jsMinify) {
288259
return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode, hmr, jsMinify);
289260
}
@@ -294,7 +265,7 @@ private static String createResourceURL(String host, String resourcePath) {
294265

295266
public String getDevServerBundleURL(final String jsModulePath) {
296267
return createBundleURL(
297-
getDebugServerHost(),
268+
mSettings.getPackagerConnectionSettings().getDebugServerHost(),
298269
jsModulePath,
299270
getDevMode(),
300271
getHMR(),
@@ -438,7 +409,7 @@ public void cancelDownloadBundleFromURL() {
438409
}
439410

440411
public void isPackagerRunning(final PackagerStatusCallback callback) {
441-
String statusURL = createPackagerStatusURL(getDebugServerHost());
412+
String statusURL = createPackagerStatusURL(mSettings.getPackagerConnectionSettings().getDebugServerHost());
442413
Request request = new Request.Builder()
443414
.url(statusURL)
444415
.build();
@@ -558,11 +529,11 @@ public void onResponse(Call call, Response response) throws IOException {
558529
}
559530

560531
private String createOnChangeEndpointUrl() {
561-
return String.format(Locale.US, ONCHANGE_ENDPOINT_URL_FORMAT, getDebugServerHost());
532+
return String.format(Locale.US, ONCHANGE_ENDPOINT_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
562533
}
563534

564535
private String createLaunchJSDevtoolsCommandUrl() {
565-
return String.format(Locale.US, LAUNCH_JS_DEVTOOLS_COMMAND_URL_FORMAT, getDebugServerHost());
536+
return String.format(Locale.US, LAUNCH_JS_DEVTOOLS_COMMAND_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
566537
}
567538

568539
public void launchJSDevtools() {
@@ -584,11 +555,11 @@ public void onResponse(Call call, Response response) throws IOException {
584555
}
585556

586557
public String getSourceMapUrl(String mainModuleName) {
587-
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
558+
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
588559
}
589560

590561
public String getSourceUrl(String mainModuleName) {
591-
return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
562+
return String.format(Locale.US, BUNDLE_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
592563
}
593564

594565
public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
@@ -607,7 +578,7 @@ public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
607578
public @Nullable File downloadBundleResourceFromUrlSync(
608579
final String resourcePath,
609580
final File outputFile) {
610-
final String resourceURL = createResourceURL(getDebugServerHost(), resourcePath);
581+
final String resourceURL = createResourceURL(mSettings.getPackagerConnectionSettings().getDebugServerHost(), resourcePath);
611582
final Request request = new Request.Builder()
612583
.url(resourceURL)
613584
.build();

ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,33 @@ include_defs("//ReactAndroid/DEFS")
22

33
android_library(
44
name = "systeminfo",
5-
srcs = glob(["**/*.java"]),
5+
srcs = [
6+
"AndroidInfoModule.java",
7+
],
8+
exported_deps = [
9+
":systeminfo-moduleless",
10+
],
611
visibility = [
712
"PUBLIC",
813
],
914
deps = [
10-
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
1115
react_native_dep("third-party/java/jsr-305:jsr-305"),
1216
react_native_target("java/com/facebook/react/bridge:bridge"),
1317
react_native_target("java/com/facebook/react/common:common"),
1418
react_native_target("java/com/facebook/react/module/annotations:annotations"),
1519
],
1620
)
21+
22+
android_library(
23+
name = "systeminfo-moduleless",
24+
srcs = [
25+
"AndroidInfoHelpers.java",
26+
],
27+
visibility = [
28+
"PUBLIC",
29+
],
30+
deps = [
31+
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
32+
react_native_dep("third-party/java/jsr-305:jsr-305"),
33+
],
34+
)

ReactAndroid/src/main/java/com/facebook/react/packagerconnection/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ android_library(
1313
react_native_dep("third-party/java/okhttp:okhttp3"),
1414
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
1515
react_native_dep("third-party/java/okio:okio"),
16+
react_native_target("java/com/facebook/react/modules/systeminfo:systeminfo-moduleless"),
1617
],
1718
)

ReactAndroid/src/main/java/com/facebook/react/packagerconnection/JSPackagerClient.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import java.util.HashMap;
1414
import java.util.Map;
1515

16+
import android.net.Uri;
17+
1618
import com.facebook.common.logging.FLog;
19+
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
1720

1821
import okhttp3.RequestBody;
1922
import okhttp3.ResponseBody;
@@ -26,6 +29,7 @@
2629
*/
2730
final public class JSPackagerClient implements ReconnectingWebSocket.MessageCallback {
2831
private static final String TAG = JSPackagerClient.class.getSimpleName();
32+
private static final String PACKAGER_CONNECTION_URL_FORMAT = "ws://%s/message?device=%s&app=%s&context=%s";
2933
private static final int PROTOCOL_VERSION = 2;
3034

3135
public class Responder {
@@ -83,8 +87,18 @@ final public void onNotification(@Nullable Object params) {
8387
private ReconnectingWebSocket mWebSocket;
8488
private Map<String, RequestHandler> mRequestHandlers;
8589

86-
public JSPackagerClient(String url, Map<String, RequestHandler> requestHandlers) {
90+
public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Map<String, RequestHandler> requestHandlers) {
8791
super();
92+
93+
Uri.Builder builder = new Uri.Builder();
94+
builder.scheme("ws")
95+
.encodedAuthority(settings.getDebugServerHost())
96+
.appendPath("message")
97+
.appendQueryParameter("device", AndroidInfoHelpers.getFriendlyDeviceName())
98+
.appendQueryParameter("app", settings.getPackageName())
99+
.appendQueryParameter("clientid", clientId);
100+
String url = builder.build().toString();
101+
88102
mWebSocket = new ReconnectingWebSocket(url, this);
89103
mRequestHandlers = requestHandlers;
90104
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
package com.facebook.react.packagerconnection;
11+
12+
import javax.annotation.Nullable;
13+
14+
import android.content.Context;
15+
import android.content.SharedPreferences;
16+
import android.preference.PreferenceManager;
17+
import android.text.TextUtils;
18+
19+
import com.facebook.common.logging.FLog;
20+
import com.facebook.infer.annotation.Assertions;
21+
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
22+
23+
public class PackagerConnectionSettings {
24+
private static final String TAG = PackagerConnectionSettings.class.getSimpleName();
25+
private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host";
26+
27+
private final SharedPreferences mPreferences;
28+
private final String mPackageName;
29+
30+
public PackagerConnectionSettings(Context applicationContext) {
31+
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
32+
mPackageName = applicationContext.getPackageName();
33+
}
34+
35+
public String getDebugServerHost() {
36+
// Check host setting first. If empty try to detect emulator type and use default
37+
// hostname for those
38+
String hostFromSettings = mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null);
39+
40+
if (!TextUtils.isEmpty(hostFromSettings)) {
41+
return Assertions.assertNotNull(hostFromSettings);
42+
}
43+
44+
String host = AndroidInfoHelpers.getServerHost();
45+
46+
if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) {
47+
FLog.w(
48+
TAG,
49+
"You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' " +
50+
"to forward the debug server's port to the device.");
51+
}
52+
53+
return host;
54+
}
55+
56+
public @Nullable String getPackageName() {
57+
return mPackageName;
58+
}
59+
}

0 commit comments

Comments
 (0)