stash the Source URL when loading a script.

Reviewed By: javache

Differential Revision: D4437195

fbshipit-source-id: 14698fc81bbe24cab81668bfb54578fc434abc58
This commit is contained in:
Ashok Menon 2017-01-20 11:50:20 -08:00 committed by Facebook Github Bot
parent cec787563a
commit 2ade5f3781
5 changed files with 61 additions and 36 deletions

View File

@ -181,10 +181,40 @@ public class CatalystInstanceImpl implements CatalystInstance {
MessageQueueThread moduleQueue,
ModuleRegistryHolder registryHolder);
/* package */ native void setSourceURL(String sourceURL);
/* package */ native void loadScriptFromAssets(AssetManager assetManager, String assetURL);
/* package */ native void loadScriptFromFile(String fileName, String sourceURL);
/* package */ native void loadScriptFromOptimizedBundle(String path, String sourceURL, int flags);
/**
* This API is used in situations where the JS bundle is being executed not on
* the device, but on a host machine. In that case, we must provide two source
* URLs for the JS bundle: One to be used on the device, and one to be used on
* the remote debugging machine.
*
* @param deviceURL A source URL that is accessible from this device.
* @param remoteURL A source URL that is accessible from the remote machine
* executing the JS.
*/
/* package */ void setSourceURLs(String deviceURL, String remoteURL) {
mSourceURL = deviceURL;
jniSetSourceURL(remoteURL);
}
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL) {
mSourceURL = assetURL;
jniLoadScriptFromAssets(assetManager, assetURL);
}
/* package */ void loadScriptFromFile(String fileName, String sourceURL) {
mSourceURL = sourceURL;
jniLoadScriptFromFile(fileName, sourceURL);
}
/* package */ void loadScriptFromOptimizedBundle(String path, String sourceURL, int flags) {
mSourceURL = sourceURL;
jniLoadScriptFromOptimizedBundle(path, sourceURL, flags);
}
private native void jniSetSourceURL(String sourceURL);
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL);
private native void jniLoadScriptFromFile(String fileName, String sourceURL);
private native void jniLoadScriptFromOptimizedBundle(String path, String sourceURL, int flags);
@Override
public void runJSBundle() {
@ -192,7 +222,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
mJSBundleHasLoaded = true;
// incrementPendingJSCalls();
mSourceURL = mJSBundleLoader.loadScript(CatalystInstanceImpl.this);
mJSBundleLoader.loadScript(CatalystInstanceImpl.this);
synchronized (mJSCallsPendingInitLock) {
// Loading the bundle is queued on the JS thread, but may not have

View File

@ -83,7 +83,7 @@ public abstract class JSBundleLoader {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.setSourceURL(proxySourceURL);
instance.setSourceURLs(realSourceURL, proxySourceURL);
return realSourceURL;
}
};

View File

@ -26,22 +26,11 @@ import com.facebook.react.module.annotations.ReactModule;
public class SourceCodeModule extends BaseJavaModule {
private final ReactContext mReactContext;
private @Nullable String mSourceUrl;
public SourceCodeModule(ReactContext reactContext) {
mReactContext = reactContext;
}
@Override
public void initialize() {
super.initialize();
mSourceUrl =
Assertions.assertNotNull(
mReactContext.getCatalystInstance().getSourceURL(),
"No source URL loaded, have you initialised the instance?");
}
@Override
public String getName() {
return "RCTSourceCode";
@ -50,7 +39,13 @@ public class SourceCodeModule extends BaseJavaModule {
@Override
public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<>();
constants.put("scriptURL", mSourceUrl);
String sourceURL =
Assertions.assertNotNull(
mReactContext.getCatalystInstance().getSourceURL(),
"No source URL loaded, have you initialised the instance?");
constants.put("scriptURL", sourceURL);
return constants;
}
}

View File

@ -103,13 +103,13 @@ void CatalystInstanceImpl::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid),
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
makeNativeMethod("setSourceURL", CatalystInstanceImpl::setSourceURL),
makeNativeMethod("loadScriptFromAssets",
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
makeNativeMethod("jniLoadScriptFromAssets",
"(Landroid/content/res/AssetManager;Ljava/lang/String;)V",
CatalystInstanceImpl::loadScriptFromAssets),
makeNativeMethod("loadScriptFromFile", CatalystInstanceImpl::loadScriptFromFile),
makeNativeMethod("loadScriptFromOptimizedBundle",
CatalystInstanceImpl::loadScriptFromOptimizedBundle),
CatalystInstanceImpl::jniLoadScriptFromAssets),
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
makeNativeMethod("jniLoadScriptFromOptimizedBundle",
CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle),
makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction),
makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback),
makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken),
@ -159,12 +159,12 @@ void CatalystInstanceImpl::initializeBridge(
mrh->getModuleRegistry());
}
void CatalystInstanceImpl::setSourceURL(const std::string& sourceURL) {
void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
instance_->setSourceURL(sourceURL);
}
void CatalystInstanceImpl::loadScriptFromAssets(jobject assetManager,
const std::string& assetURL) {
void CatalystInstanceImpl::jniLoadScriptFromAssets(jobject assetManager,
const std::string& assetURL) {
const int kAssetsLength = 9; // strlen("assets://");
auto sourceURL = assetURL.substr(kAssetsLength);
@ -192,8 +192,8 @@ bool CatalystInstanceImpl::isIndexedRAMBundle(const char *sourcePath) {
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
}
void CatalystInstanceImpl::loadScriptFromFile(const std::string& fileName,
const std::string& sourceURL) {
void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
const std::string& sourceURL) {
auto zFileName = fileName.c_str();
if (isIndexedRAMBundle(zFileName)) {
auto bundle = folly::make_unique<JSIndexedRAMBundle>(zFileName);
@ -207,9 +207,9 @@ void CatalystInstanceImpl::loadScriptFromFile(const std::string& fileName,
}
}
void CatalystInstanceImpl::loadScriptFromOptimizedBundle(const std::string& bundlePath,
const std::string& sourceURL,
jint flags) {
void CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle(const std::string& bundlePath,
const std::string& sourceURL,
jint flags) {
return instance_->loadScriptFromOptimizedBundle(std::move(bundlePath),
std::move(sourceURL),
flags);

View File

@ -53,11 +53,11 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
/**
* Sets the source URL of the underlying bridge without loading any JS code.
*/
void setSourceURL(const std::string& sourceURL);
void jniSetSourceURL(const std::string& sourceURL);
void loadScriptFromAssets(jobject assetManager, const std::string& assetURL);
void loadScriptFromFile(const std::string& fileName, const std::string& sourceURL);
void loadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags);
void jniLoadScriptFromAssets(jobject assetManager, const std::string& assetURL);
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL);
void jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags);
void callJSFunction(JExecutorToken* token, std::string module, std::string method, NativeArray* arguments);
void callJSCallback(JExecutorToken* token, jint callbackId, NativeArray* arguments);
local_ref<JExecutorToken::JavaPart> getMainExecutorToken();