Refactor CxxModuleWrapper to support different use cases

Reviewed By: javache

Differential Revision: D4807680

fbshipit-source-id: 48eccfb382814a0c4082f56617e0359e61345da7
This commit is contained in:
Marc Horowitz
2017-04-05 00:51:55 -07:00
committed by Facebook Github Bot
parent 0ec9c93eeb
commit a893d0bb23
11 changed files with 179 additions and 101 deletions

View File

@@ -2,70 +2,25 @@
package com.facebook.react.cxxbridge;
import java.util.Map;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.ExecutorToken;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReadableNativeArray;
import com.facebook.soloader.SoLoader;
/**
* A Java Object which represents a cross-platform C++ module
*
* This module implements the NativeModule interface but will never be invoked from Java,
* instead the underlying Cxx module will be extracted by the bridge and called directly.
* This does nothing interesting, except avoid breaking existing code.
*/
@DoNotStrip
public class CxxModuleWrapper implements NativeModule
public class CxxModuleWrapper extends CxxModuleWrapperBase
{
static {
SoLoader.loadLibrary(CatalystInstanceImpl.REACT_NATIVE_LIB);
}
@DoNotStrip
private HybridData mHybridData;
public CxxModuleWrapper(String library, String factory) {
SoLoader.loadLibrary(library);
mHybridData =
initHybrid(SoLoader.unpackLibraryAndDependencies(library).getAbsolutePath(), factory);
}
@Override
public native String getName();
@Override
public Map<String, NativeMethod> getMethods() {
throw new UnsupportedOperationException();
}
@Override
public void initialize() {
// do nothing
}
@Override
public boolean canOverrideExistingModule() {
return false;
}
@Override
public boolean supportsWebWorkers() {
return false;
}
@Override
public void onCatalystInstanceDestroy() {
mHybridData.resetNative();
}
// For creating a wrapper from C++, or from a derived class.
protected CxxModuleWrapper(HybridData hd) {
mHybridData = hd;
super(hd);
}
private native HybridData initHybrid(String soPath, String factory);
private static native CxxModuleWrapper makeDsoNative(String soPath, String factory);
public static CxxModuleWrapper makeDso(String library, String factory) {
SoLoader.loadLibrary(library);
String soPath = SoLoader.unpackLibraryAndDependencies(library).getAbsolutePath();
return makeDsoNative(soPath, factory);
}
}

View File

@@ -0,0 +1,60 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.cxxbridge;
import java.util.Map;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.NativeModule;
import com.facebook.soloader.SoLoader;
/**
* A Java Object which represents a cross-platform C++ module
*
* This module implements the NativeModule interface but will never be invoked from Java,
* instead the underlying Cxx module will be extracted by the bridge and called directly.
*/
@DoNotStrip
public class CxxModuleWrapperBase implements NativeModule
{
static {
SoLoader.loadLibrary(CatalystInstanceImpl.REACT_NATIVE_LIB);
}
@DoNotStrip
private HybridData mHybridData;
@Override
public native String getName();
@Override
public Map<String, NativeMethod> getMethods() {
throw new UnsupportedOperationException();
}
@Override
public void initialize() {
// do nothing
}
@Override
public boolean canOverrideExistingModule() {
return false;
}
@Override
public boolean supportsWebWorkers() {
return false;
}
@Override
public void onCatalystInstanceDestroy() {
mHybridData.resetNative();
}
// For creating a wrapper from C++, or from a derived class.
protected CxxModuleWrapperBase(HybridData hd) {
mHybridData = hd;
}
}

View File

@@ -46,7 +46,7 @@ public class NativeModuleRegistry {
ArrayList<JavaModuleWrapper> javaModules = new ArrayList<>();
for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) {
Class<?> type = entry.getKey();
if (!CxxModuleWrapper.class.isAssignableFrom(type)) {
if (!CxxModuleWrapperBase.class.isAssignableFrom(type)) {
javaModules.add(new JavaModuleWrapper(jsInstance, entry.getValue()));
}
}
@@ -57,7 +57,7 @@ public class NativeModuleRegistry {
ArrayList<ModuleHolder> cxxModules = new ArrayList<>();
for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) {
Class<?> type = entry.getKey();
if (CxxModuleWrapper.class.isAssignableFrom(type)) {
if (CxxModuleWrapperBase.class.isAssignableFrom(type)) {
cxxModules.add(entry.getValue());
}
}