Move implementation of extracting bundles from assets to react/jni

Summary:
Splits JSModulesUnbundle into interface+implementation.

public

Reviewed By: astreet

Differential Revision: D2905186

fb-gh-sync-id: 3e621f7a7239d3f1e730334da2fe7cbeb17a1de4
This commit is contained in:
Chris Hopman 2016-02-05 18:09:28 -08:00 committed by facebook-github-bot-2
parent 52fcfc31cd
commit cc926211b6
13 changed files with 54 additions and 48 deletions

View File

@ -39,7 +39,6 @@ react_library(
force_static = True,
srcs = [
'Bridge.cpp',
'JSModulesUnbundle.cpp',
'Value.cpp',
'MethodCall.cpp',
'JSCHelpers.cpp',

View File

@ -37,7 +37,7 @@ void Bridge::executeApplicationScript(const std::string& script, const std::stri
}
void Bridge::loadApplicationUnbundle(
JSModulesUnbundle&& unbundle,
std::unique_ptr<JSModulesUnbundle> unbundle,
const std::string& startupCode,
const std::string& sourceURL) {
m_jsExecutor->loadApplicationUnbundle(std::move(unbundle), startupCode, sourceURL);

View File

@ -59,7 +59,7 @@ public:
* and injects each module as individual file.
*/
void loadApplicationUnbundle(
JSModulesUnbundle&& unbundle,
std::unique_ptr<JSModulesUnbundle> unbundle,
const std::string& startupCode,
const std::string& sourceURL);
void setGlobalVariable(const std::string& propName, const std::string& jsonValue);

View File

@ -39,7 +39,7 @@ public:
* Add an application "unbundle" file
*/
virtual void loadApplicationUnbundle(
JSModulesUnbundle&& bundle,
std::unique_ptr<JSModulesUnbundle> bundle,
const std::string& startupCode,
const std::string& sourceURL) = 0;

View File

@ -151,15 +151,13 @@ void JSCExecutor::executeApplicationScript(
}
void JSCExecutor::loadApplicationUnbundle(
JSModulesUnbundle&& unbundle,
std::unique_ptr<JSModulesUnbundle> unbundle,
const std::string& startupCode,
const std::string& sourceURL) {
m_unbundle = std::move(unbundle);
if (!m_isUnbundleInitialized) {
m_isUnbundleInitialized = true;
if (!m_unbundle) {
installGlobalFunction(m_context, "nativeRequire", nativeRequire);
}
m_unbundle = std::move(unbundle);
executeApplicationScript(startupCode, sourceURL);
}
@ -242,7 +240,7 @@ void JSCExecutor::flushQueueImmediate(std::string queueJSON) {
}
void JSCExecutor::loadModule(uint32_t moduleId) {
auto module = m_unbundle.getModule(moduleId);
auto module = m_unbundle->getModule(moduleId);
auto sourceUrl = String::createExpectingAscii(module.name);
auto source = String::createExpectingAscii(module.code);
evaluateScript(m_context, source, sourceUrl);

View File

@ -35,7 +35,7 @@ public:
const std::string& script,
const std::string& sourceURL) override;
virtual void loadApplicationUnbundle(
JSModulesUnbundle&& unbundle,
std::unique_ptr<JSModulesUnbundle> unbundle,
const std::string& startupCode,
const std::string& sourceURL) override;
virtual std::string flush() override;
@ -66,10 +66,9 @@ private:
FlushImmediateCallback m_flushImmediateCallback;
std::unordered_map<int, JSCWebWorker> m_webWorkers;
std::unordered_map<int, Object> m_webWorkerJSObjs;
JSModulesUnbundle m_unbundle;
bool m_isUnbundleInitialized = false;
std::shared_ptr<MessageQueueThread> m_messageQueueThread;
std::string m_deviceCacheDir;
std::unique_ptr<JSModulesUnbundle> m_unbundle;
int addWebWorker(const std::string& script, JSValueRef workerRef);
void postMessageToWebWorker(int worker, JSValueRef message, JSValueRef *exn);

View File

@ -2,7 +2,6 @@
#pragma once
#include <android/asset_manager.h>
#include <cstdint>
#include <fb/noncopyable.h>
#include <string>
@ -15,7 +14,6 @@ class JSModulesUnbundle : noncopyable {
/**
* Represents the set of JavaScript modules that the application consists of.
* The source code of each module can be retrieved by module ID.
* This implementation reads modules as single file from the assets of an apk.
*
* The class is non-copyable because copying instances might involve copying
* several megabytes of memory.
@ -28,20 +26,8 @@ public:
std::string name;
std::string code;
};
JSModulesUnbundle() = default;
JSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile);
JSModulesUnbundle(JSModulesUnbundle&& other) noexcept;
JSModulesUnbundle& operator= (JSModulesUnbundle&& other) noexcept;
static bool isUnbundle(
AAssetManager *assetManager,
const std::string& assetName);
Module getModule(uint32_t moduleId) const;
private:
AAssetManager *m_assetManager = nullptr;
std::string m_moduleDirectory;
virtual ~JSModulesUnbundle() {}
virtual Module getModule(uint32_t moduleId) const = 0;
};
}

View File

@ -31,6 +31,7 @@ jni_library(
'JMessageQueueThread.cpp',
'JSCPerfLogging.cpp',
'JSLoader.cpp',
'JniJSModulesUnbundle.cpp',
'NativeArray.cpp',
'OnLoad.cpp',
'ProxyExecutor.cpp',
@ -41,6 +42,7 @@ jni_library(
'ProxyExecutor.h',
'JMessageQueueThread.h',
'JNativeRunnable.h',
'JniJSModulesUnbundle.h',
'JSCPerfLogging.h',
'JSLogging.h',
],

View File

@ -1,6 +1,6 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "JSModulesUnbundle.h"
#include "JniJSModulesUnbundle.h"
#include <cstdint>
#include <fb/assert.h>
@ -36,22 +36,11 @@ static asset_ptr openAsset(
AAsset_close);
}
JSModulesUnbundle::JSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile) :
JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile) :
m_assetManager(assetManager),
m_moduleDirectory(jsModulesDir(entryFile)) {}
JSModulesUnbundle::JSModulesUnbundle(JSModulesUnbundle&& other) noexcept {
*this = std::move(other);
}
JSModulesUnbundle& JSModulesUnbundle::operator= (JSModulesUnbundle&& other) noexcept {
std::swap(m_assetManager, other.m_assetManager);
std::swap(m_moduleDirectory, other.m_moduleDirectory);
return *this;
}
bool JSModulesUnbundle::isUnbundle(
bool JniJSModulesUnbundle::isUnbundle(
AAssetManager *assetManager,
const std::string& assetName) {
if (!assetManager) {
@ -69,7 +58,7 @@ bool JSModulesUnbundle::isUnbundle(
return fileHeader == htole32(MAGIC_FILE_HEADER);
}
JSModulesUnbundle::Module JSModulesUnbundle::getModule(uint32_t moduleId) const {
JSModulesUnbundle::Module JniJSModulesUnbundle::getModule(uint32_t moduleId) const {
// can be nullptr for default constructor.
FBASSERTMSGF(m_assetManager != nullptr, "Unbundle has not been initialized with an asset manager");

View File

@ -0,0 +1,31 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <android/asset_manager.h>
#include <react/JSModulesUnbundle.h>
namespace facebook {
namespace react {
class JniJSModulesUnbundle : public JSModulesUnbundle {
/**
* This implementation reads modules as single file from the assets of an apk.
*/
public:
JniJSModulesUnbundle() = default;
JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile);
JniJSModulesUnbundle(JniJSModulesUnbundle&& other) = delete;
JniJSModulesUnbundle& operator= (JSModulesUnbundle&& other) = delete;
static bool isUnbundle(
AAssetManager *assetManager,
const std::string& assetName);
virtual Module getModule(uint32_t moduleId) const override;
private:
AAssetManager *m_assetManager = nullptr;
std::string m_moduleDirectory;
};
}
}

View File

@ -22,6 +22,7 @@
#include "ProxyExecutor.h"
#include "OnLoad.h"
#include "JMessageQueueThread.h"
#include "JniJSModulesUnbundle.h"
#include "JSLogging.h"
#include "JSCPerfLogging.h"
#include "WebWorkers.h"
@ -668,7 +669,8 @@ static void loadApplicationUnbundle(
try {
// Load the application unbundle and collect/dispatch any native calls that might have occured
bridge->loadApplicationUnbundle(
JSModulesUnbundle(assetManager, startupFileName),
std::unique_ptr<JSModulesUnbundle>(
new JniJSModulesUnbundle(assetManager, startupFileName)),
startupCode,
startupFileName);
bridge->flush();
@ -693,7 +695,7 @@ static void loadScriptFromAssets(JNIEnv* env, jobject obj, jobject assetManager,
#endif
env->CallStaticVoidMethod(markerClass, gLogMarkerMethod, env->NewStringUTF("loadScriptFromAssets_read"));
if (JSModulesUnbundle::isUnbundle(manager, assetNameStr)) {
if (JniJSModulesUnbundle::isUnbundle(manager, assetNameStr)) {
loadApplicationUnbundle(bridge, manager, script, assetNameStr);
} else {
executeApplicationScript(bridge, script, assetNameStr);

View File

@ -51,7 +51,7 @@ void ProxyExecutor::executeApplicationScript(
jni::make_jstring(sourceURL).get());
}
void ProxyExecutor::loadApplicationUnbundle(JSModulesUnbundle&&, const std::string&, const std::string&) {
void ProxyExecutor::loadApplicationUnbundle(std::unique_ptr<JSModulesUnbundle>, const std::string&, const std::string&) {
jni::throwNewJavaException(
"java/lang/UnsupportedOperationException",
"Loading application unbundles is not supported for proxy executors");

View File

@ -34,7 +34,7 @@ public:
const std::string& script,
const std::string& sourceURL) override;
virtual void loadApplicationUnbundle(
JSModulesUnbundle&& bundle,
std::unique_ptr<JSModulesUnbundle> bundle,
const std::string& startupCode,
const std::string& sourceURL) override;
virtual std::string flush() override;