Skip to content

Commit

Permalink
android: allow registering custom packager command handlers
Browse files Browse the repository at this point in the history
Summary:
@public
Apps may need to listen for custom commands via the packager connection. This allows registering such listeners.

Reviewed By: raluca-elena

Differential Revision: D8654477

fbshipit-source-id: 5f17298a88fec31b8939236fef48ee46c0ba2ee8
  • Loading branch information
fkgozali authored and facebook-github-bot committed Jun 27, 2018
1 parent bc2f12c commit b3ef1c3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
3 changes: 3 additions & 0 deletions ReactAndroid/src/main/java/com/facebook/react/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/views/imagehelper:imagehelper"),
],
exported_deps = [
react_native_target("java/com/facebook/react/packagerconnection:packagerconnection"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.modules.fabric.ReactFabric;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewManager;
Expand All @@ -90,6 +91,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -205,7 +207,8 @@ public static ReactInstanceManagerBuilder builder() {
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
int minTimeLeftInFrameForNonBatchedOperationMs,
@Nullable JSIModulePackage jsiModulePackage) {
@Nullable JSIModulePackage jsiModulePackage,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.ctor()");
initializeSoLoaderIfNecessary(applicationContext);

Expand All @@ -227,7 +230,8 @@ public static ReactInstanceManagerBuilder builder() {
useDeveloperSupport,
redBoxHandler,
devBundleDownloadListener,
minNumShakes);
minNumShakes,
customPackagerCommandHandlers);
mBridgeIdleDebugListener = bridgeIdleDebugListener;
mLifecycleState = initialLifecycleState;
mMemoryPressureRouter = new MemoryPressureRouter(applicationContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.packagerconnection.RequestHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -50,6 +52,7 @@ public class ReactInstanceManagerBuilder {
private int mMinNumShakes = 1;
private int mMinTimeLeftInFrameForNonBatchedOperationMs = -1;
private @Nullable JSIModulePackage mJSIModulesPackage;
private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;

/* package protected */ ReactInstanceManagerBuilder() {
}
Expand Down Expand Up @@ -216,6 +219,12 @@ public ReactInstanceManagerBuilder setMinTimeLeftInFrameForNonBatchedOperationMs
return this;
}

public ReactInstanceManagerBuilder setCustomPackagerCommandHandlers(
Map<String, RequestHandler> customPackagerCommandHandlers) {
mCustomPackagerCommandHandlers = customPackagerCommandHandlers;
return this;
}

/**
* Instantiates a new {@link ReactInstanceManager}.
* Before calling {@code build}, the following must be called:
Expand Down Expand Up @@ -266,6 +275,7 @@ public ReactInstanceManager build() {
mDevBundleDownloadListener,
mMinNumShakes,
mMinTimeLeftInFrameForNonBatchedOperationMs,
mJSIModulesPackage);
mJSIModulesPackage,
mCustomPackagerCommandHandlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public interface PackagerCommandListener {
void onPackagerReloadCommand();
void onPackagerDevMenuCommand();
void onCaptureHeapCommand(final Responder responder);

// Allow apps to provide listeners for custom packager commands.
@Nullable Map<String, RequestHandler> customCommandHandlers();
}

public interface PackagerCustomCommandProvider {

}

public interface SymbolicationListener {
Expand Down Expand Up @@ -162,6 +169,10 @@ public void onRequest(@Nullable Object params, Responder responder) {
commandListener.onCaptureHeapCommand(responder);
}
});
Map<String, RequestHandler> customHandlers = commandListener.customCommandHandlers();
if (customHandlers != null) {
handlers.putAll(customHandlers);
}
handlers.putAll(new FileIoHandler().handlers());

ConnectionCallback onPackagerConnectedCallback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.packagerconnection.RequestHandler;

import java.lang.reflect.Constructor;
import java.util.Map;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -41,7 +43,8 @@ public static DevSupportManager create(
enableOnCreate,
null,
null,
minNumShakes);
minNumShakes,
null);
}

public static DevSupportManager create(
Expand All @@ -51,7 +54,8 @@ public static DevSupportManager create(
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
if (!enableOnCreate) {
return new DisabledDevSupportManager();
}
Expand All @@ -74,15 +78,17 @@ public static DevSupportManager create(
boolean.class,
RedBoxHandler.class,
DevBundleDownloadListener.class,
int.class);
int.class,
Map.class);
return (DevSupportManager) constructor.newInstance(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
true,
redBoxHandler,
devBundleDownloadListener,
minNumShakes);
minNumShakes,
customPackagerCommandHandlers);
} catch (Exception e) {
throw new RuntimeException(
"Requested enabled DevSupportManager, but DevSupportManagerImpl class was not found" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -150,6 +151,8 @@ private enum ErrorType {

private InspectorPackagerConnection.BundleStatus mBundleStatus;

private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;

private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8");
Expand Down Expand Up @@ -196,7 +199,8 @@ public DevSupportManagerImpl(
enableOnCreate,
null,
null,
minNumShakes);
minNumShakes,
null);
}

public DevSupportManagerImpl(
Expand All @@ -206,7 +210,8 @@ public DevSupportManagerImpl(
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
mReactInstanceManagerHelper = reactInstanceManagerHelper;
mApplicationContext = applicationContext;
mJSAppBundleName = packagerPathForJSBundleName;
Expand All @@ -232,6 +237,8 @@ public void onShake() {
}
}, minNumShakes);

mCustomPackagerCommandHandlers = customPackagerCommandHandlers;

// Prepare reload APP broadcast receiver (will be registered/unregistered from #reload)
mReloadAppBroadcastReceiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -841,6 +848,11 @@ public void run() {
});
}

@Override
public @Nullable Map<String, RequestHandler> customCommandHandlers() {
return mCustomPackagerCommandHandlers;
}

private void handleCaptureHeap(final Responder responder) {
if (mCurrentContext == null) {
return;
Expand Down

0 comments on commit b3ef1c3

Please sign in to comment.