Extract PackagerConnectionSettings to ensure easier reusability of PackagerConnection module

Reviewed By: cwdick

Differential Revision: D4689535

fbshipit-source-id: f698837f407a03bf91521cc5e921c66f5755e6e0
This commit is contained in:
Lukas Piatkowski
2017-03-17 09:55:23 -07:00
committed by Facebook Github Bot
parent 50ff7167cb
commit 60142adc72
8 changed files with 132 additions and 65 deletions

View File

@@ -17,6 +17,7 @@ import android.preference.PreferenceManager;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
/**
* Helper class for accessing developers settings that should not be accessed outside of the package
@@ -31,7 +32,6 @@ public class DevInternalSettings implements
private static final String PREFS_FPS_DEBUG_KEY = "fps_debug";
private static final String PREFS_JS_DEV_MODE_DEBUG_KEY = "js_dev_mode_debug";
private static final String PREFS_JS_MINIFY_DEBUG_KEY = "js_minify_debug";
private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host";
private static final String PREFS_ANIMATIONS_DEBUG_KEY = "animations_debug";
private static final String PREFS_RELOAD_ON_JS_CHANGE_KEY = "reload_on_js_change";
private static final String PREFS_INSPECTOR_DEBUG_KEY = "inspector_debug";
@@ -40,6 +40,7 @@ public class DevInternalSettings implements
private final SharedPreferences mPreferences;
private final Listener mListener;
private final PackagerConnectionSettings mPackagerConnectionSettings;
public DevInternalSettings(
Context applicationContext,
@@ -47,6 +48,11 @@ public class DevInternalSettings implements
mListener = listener;
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPreferences.registerOnSharedPreferenceChangeListener(this);
mPackagerConnectionSettings = new PackagerConnectionSettings(applicationContext);
}
public PackagerConnectionSettings getPackagerConnectionSettings() {
return mPackagerConnectionSettings;
}
@Override
@@ -73,10 +79,6 @@ public class DevInternalSettings implements
return mPreferences.getBoolean(PREFS_JS_MINIFY_DEBUG_KEY, false);
}
public @Nullable String getDebugServerHost() {
return mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (mListener != null) {
if (PREFS_FPS_DEBUG_KEY.equals(key) ||

View File

@@ -73,7 +73,6 @@ public class DevServerHelper {
private static final String ONCHANGE_ENDPOINT_URL_FORMAT =
"http://%s/onchange";
private static final String WEBSOCKET_PROXY_URL_FORMAT = "ws://%s/debugger-proxy?role=client";
private static final String PACKAGER_CONNECTION_URL_FORMAT = "ws://%s/message?role=android-rn-devserverhelper";
private static final String PACKAGER_STATUS_URL_FORMAT = "http://%s/status";
private static final String HEAP_CAPTURE_UPLOAD_URL_FORMAT = "http://%s/jscheapcaptureupload";
private static final String INSPECTOR_DEVICE_URL_FORMAT = "http://%s/inspector/device?name=%s";
@@ -152,7 +151,7 @@ public class DevServerHelper {
});
handlers.putAll(new FileIoHandler().handlers());
mPackagerClient = new JSPackagerClient(getPackagerConnectionURL(), handlers);
mPackagerClient = new JSPackagerClient("devserverhelper", mSettings.getPackagerConnectionSettings(), handlers);
mPackagerClient.init();
return null;
@@ -213,22 +212,18 @@ public class DevServerHelper {
}
public String getWebsocketProxyURL() {
return String.format(Locale.US, WEBSOCKET_PROXY_URL_FORMAT, getDebugServerHost());
}
private String getPackagerConnectionURL() {
return String.format(Locale.US, PACKAGER_CONNECTION_URL_FORMAT, getDebugServerHost());
return String.format(Locale.US, WEBSOCKET_PROXY_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
}
public String getHeapCaptureUploadUrl() {
return String.format(Locale.US, HEAP_CAPTURE_UPLOAD_URL_FORMAT, getDebugServerHost());
return String.format(Locale.US, HEAP_CAPTURE_UPLOAD_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
}
public String getInspectorDeviceUrl() {
return String.format(
Locale.US,
INSPECTOR_DEVICE_URL_FORMAT,
getDebugServerHost(),
mSettings.getPackagerConnectionSettings().getDebugServerHost(),
AndroidInfoHelpers.getFriendlyDeviceName());
}
@@ -260,30 +255,6 @@ public class DevServerHelper {
return mSettings.isHotModuleReplacementEnabled();
}
/**
* @return the host to use when connecting to the bundle server.
*/
private String getDebugServerHost() {
// Check debug server host setting first. If empty try to detect emulator type and use default
// hostname for those
String hostFromSettings = mSettings.getDebugServerHost();
if (!TextUtils.isEmpty(hostFromSettings)) {
return Assertions.assertNotNull(hostFromSettings);
}
String host = AndroidInfoHelpers.getServerHost();
if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) {
FLog.w(
ReactConstants.TAG,
"You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' " +
"to forward the debug server's port to the device.");
}
return host;
}
private static String createBundleURL(String host, String jsModulePath, boolean devMode, boolean hmr, boolean jsMinify) {
return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode, hmr, jsMinify);
}
@@ -294,7 +265,7 @@ public class DevServerHelper {
public String getDevServerBundleURL(final String jsModulePath) {
return createBundleURL(
getDebugServerHost(),
mSettings.getPackagerConnectionSettings().getDebugServerHost(),
jsModulePath,
getDevMode(),
getHMR(),
@@ -438,7 +409,7 @@ public class DevServerHelper {
}
public void isPackagerRunning(final PackagerStatusCallback callback) {
String statusURL = createPackagerStatusURL(getDebugServerHost());
String statusURL = createPackagerStatusURL(mSettings.getPackagerConnectionSettings().getDebugServerHost());
Request request = new Request.Builder()
.url(statusURL)
.build();
@@ -558,11 +529,11 @@ public class DevServerHelper {
}
private String createOnChangeEndpointUrl() {
return String.format(Locale.US, ONCHANGE_ENDPOINT_URL_FORMAT, getDebugServerHost());
return String.format(Locale.US, ONCHANGE_ENDPOINT_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
}
private String createLaunchJSDevtoolsCommandUrl() {
return String.format(Locale.US, LAUNCH_JS_DEVTOOLS_COMMAND_URL_FORMAT, getDebugServerHost());
return String.format(Locale.US, LAUNCH_JS_DEVTOOLS_COMMAND_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost());
}
public void launchJSDevtools() {
@@ -584,11 +555,11 @@ public class DevServerHelper {
}
public String getSourceMapUrl(String mainModuleName) {
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
}
public String getSourceUrl(String mainModuleName) {
return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
return String.format(Locale.US, BUNDLE_URL_FORMAT, mSettings.getPackagerConnectionSettings().getDebugServerHost(), mainModuleName, getDevMode(), getHMR(), getJSMinifyMode());
}
public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
@@ -607,7 +578,7 @@ public class DevServerHelper {
public @Nullable File downloadBundleResourceFromUrlSync(
final String resourcePath,
final File outputFile) {
final String resourceURL = createResourceURL(getDebugServerHost(), resourcePath);
final String resourceURL = createResourceURL(mSettings.getPackagerConnectionSettings().getDebugServerHost(), resourcePath);
final Request request = new Request.Builder()
.url(resourceURL)
.build();