Skip to content

Commit

Permalink
Extract PackagerConnectionSettings to ensure easier reusability of Pa…
Browse files Browse the repository at this point in the history
…ckagerConnection module

Reviewed By: cwdick

Differential Revision: D4689535

fbshipit-source-id: f698837f407a03bf91521cc5e921c66f5755e6e0
  • Loading branch information
lukaspiatkowski authored and facebook-github-bot committed Mar 17, 2017
1 parent 50ff716 commit 60142ad
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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
Expand All @@ -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";
Expand All @@ -40,13 +40,19 @@ public class DevInternalSettings implements

private final SharedPreferences mPreferences;
private final Listener mListener;
private final PackagerConnectionSettings mPackagerConnectionSettings;

public DevInternalSettings(
Context applicationContext,
Listener listener) {
mListener = listener;
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPreferences.registerOnSharedPreferenceChangeListener(this);
mPackagerConnectionSettings = new PackagerConnectionSettings(applicationContext);
}

public PackagerConnectionSettings getPackagerConnectionSettings() {
return mPackagerConnectionSettings;
}

@Override
Expand All @@ -73,10 +79,6 @@ public boolean isJSMinifyEnabled() {
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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -152,7 +151,7 @@ public void onRequest(@Nullable Object params, JSPackagerClient.Responder respon
});
handlers.putAll(new FileIoHandler().handlers());

mPackagerClient = new JSPackagerClient(getPackagerConnectionURL(), handlers);
mPackagerClient = new JSPackagerClient("devserverhelper", mSettings.getPackagerConnectionSettings(), handlers);
mPackagerClient.init();

return null;
Expand Down Expand Up @@ -213,22 +212,18 @@ public static String getReloadAppAction(Context context) {
}

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());
}

Expand Down Expand Up @@ -260,30 +255,6 @@ private boolean getHMR() {
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);
}
Expand All @@ -294,7 +265,7 @@ private static String createResourceURL(String host, String resourcePath) {

public String getDevServerBundleURL(final String jsModulePath) {
return createBundleURL(
getDebugServerHost(),
mSettings.getPackagerConnectionSettings().getDebugServerHost(),
jsModulePath,
getDevMode(),
getHMR(),
Expand Down Expand Up @@ -438,7 +409,7 @@ public void cancelDownloadBundleFromURL() {
}

public void isPackagerRunning(final PackagerStatusCallback callback) {
String statusURL = createPackagerStatusURL(getDebugServerHost());
String statusURL = createPackagerStatusURL(mSettings.getPackagerConnectionSettings().getDebugServerHost());
Request request = new Request.Builder()
.url(statusURL)
.build();
Expand Down Expand Up @@ -558,11 +529,11 @@ public void onResponse(Call call, Response response) throws IOException {
}

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() {
Expand All @@ -584,11 +555,11 @@ public void onResponse(Call call, Response response) throws IOException {
}

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) {
Expand All @@ -607,7 +578,7 @@ public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@ include_defs("//ReactAndroid/DEFS")

android_library(
name = "systeminfo",
srcs = glob(["**/*.java"]),
srcs = [
"AndroidInfoModule.java",
],
exported_deps = [
":systeminfo-moduleless",
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
],
)

android_library(
name = "systeminfo-moduleless",
srcs = [
"AndroidInfoHelpers.java",
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ android_library(
react_native_dep("third-party/java/okhttp:okhttp3"),
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
react_native_dep("third-party/java/okio:okio"),
react_native_target("java/com/facebook/react/modules/systeminfo:systeminfo-moduleless"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import java.util.HashMap;
import java.util.Map;

import android.net.Uri;

import com.facebook.common.logging.FLog;
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;

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

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

public JSPackagerClient(String url, Map<String, RequestHandler> requestHandlers) {
public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Map<String, RequestHandler> requestHandlers) {
super();

Uri.Builder builder = new Uri.Builder();
builder.scheme("ws")
.encodedAuthority(settings.getDebugServerHost())
.appendPath("message")
.appendQueryParameter("device", AndroidInfoHelpers.getFriendlyDeviceName())
.appendQueryParameter("app", settings.getPackageName())
.appendQueryParameter("clientid", clientId);
String url = builder.build().toString();

mWebSocket = new ReconnectingWebSocket(url, this);
mRequestHandlers = requestHandlers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

package com.facebook.react.packagerconnection;

import javax.annotation.Nullable;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;

public class PackagerConnectionSettings {
private static final String TAG = PackagerConnectionSettings.class.getSimpleName();
private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host";

private final SharedPreferences mPreferences;
private final String mPackageName;

public PackagerConnectionSettings(Context applicationContext) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPackageName = applicationContext.getPackageName();
}

public String getDebugServerHost() {
// Check host setting first. If empty try to detect emulator type and use default
// hostname for those
String hostFromSettings = mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null);

if (!TextUtils.isEmpty(hostFromSettings)) {
return Assertions.assertNotNull(hostFromSettings);
}

String host = AndroidInfoHelpers.getServerHost();

if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) {
FLog.w(
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;
}

public @Nullable String getPackageName() {
return mPackageName;
}
}
Loading

0 comments on commit 60142ad

Please sign in to comment.