Move getting the cache dir into react/jni

Summary:
This just moves the jni code that actual figures out the cache dir into OnLoad.cpp and then passes it down to the JSCEXecutorFactory.

public

Reviewed By: astreet

Differential Revision: D2905176

fb-gh-sync-id: bedf52fbeaab6beabac25c87aad00a98ddf182f7
This commit is contained in:
Chris Hopman 2016-02-05 18:09:25 -08:00 committed by facebook-github-bot-2
parent 75ca46e332
commit 2997c8feed
3 changed files with 33 additions and 30 deletions

View File

@ -85,11 +85,11 @@ static std::string executeJSCallWithJSC(
}
std::unique_ptr<JSExecutor> JSCExecutorFactory::createJSExecutor(FlushImmediateCallback cb) {
return std::unique_ptr<JSExecutor>(new JSCExecutor(cb));
return std::unique_ptr<JSExecutor>(new JSCExecutor(cb, cacheDir_));
}
JSCExecutor::JSCExecutor(FlushImmediateCallback cb) :
m_flushImmediateCallback(cb) {
JSCExecutor::JSCExecutor(FlushImmediateCallback cb, const std::string& cacheDir) :
m_flushImmediateCallback(cb), m_deviceCacheDir(cacheDir) {
m_context = JSGlobalContextCreateInGroup(nullptr, nullptr);
m_messageQueueThread = JMessageQueueThread::currentMessageQueueThread();
s_globalContextRefToJSCExecutor[m_context] = this;
@ -130,29 +130,6 @@ JSCExecutor::~JSCExecutor() {
JSGlobalContextRelease(m_context);
}
std::string JSCExecutor::getDeviceCacheDir(){
// Get the Application Context object
auto getApplicationClass = findClassLocal(
"com/facebook/react/common/ApplicationHolder");
auto getApplicationMethod = getApplicationClass->getStaticMethod<jobject()>(
"getApplication",
"()Landroid/app/Application;"
);
auto application = getApplicationMethod(getApplicationClass);
// Get getCacheDir() from the context
auto getCacheDirMethod = findClassLocal("android/app/Application")
->getMethod<jobject()>("getCacheDir",
"()Ljava/io/File;"
);
auto cacheDirObj = getCacheDirMethod(application);
// Call getAbsolutePath() on the returned File object
auto getAbsolutePathMethod = findClassLocal("java/io/File")
->getMethod<jstring()>("getAbsolutePath");
return getAbsolutePathMethod(cacheDirObj)->toStdString();
}
void JSCExecutor::executeApplicationScript(
const std::string& script,
const std::string& sourceURL) {
@ -170,7 +147,7 @@ void JSCExecutor::executeApplicationScript(
} else {
// If we're evaluating a script, get the device's cache dir
// in which a cache file for that script will be stored.
evaluateScript(m_context, jsScript, jsSourceURL, getDeviceCacheDir().c_str());
evaluateScript(m_context, jsScript, jsSourceURL, m_deviceCacheDir.c_str());
}
}

View File

@ -17,7 +17,10 @@ class JMessageQueueThread;
class JSCExecutorFactory : public JSExecutorFactory {
public:
JSCExecutorFactory(const std::string& cacheDir) : cacheDir_(cacheDir) {}
virtual std::unique_ptr<JSExecutor> createJSExecutor(FlushImmediateCallback cb) override;
private:
std::string cacheDir_;
};
class JSCExecutor : public JSExecutor, public JSCWebWorkerOwner {
@ -25,7 +28,7 @@ public:
/**
* Should be invoked from the JS thread.
*/
explicit JSCExecutor(FlushImmediateCallback flushImmediateCallback);
explicit JSCExecutor(FlushImmediateCallback flushImmediateCallback, const std::string& cacheDir);
~JSCExecutor() override;
virtual void executeApplicationScript(
@ -66,12 +69,12 @@ private:
std::shared_ptr<JMessageQueueThread> m_messageQueueThread;
JSModulesUnbundle m_unbundle;
bool m_isUnbundleInitialized = false;
std::string m_deviceCacheDir;
int addWebWorker(const std::string& script, JSValueRef workerRef);
void postMessageToWebWorker(int worker, JSValueRef message, JSValueRef *exn);
void terminateWebWorker(int worker);
void loadModule(uint32_t moduleId);
std::string getDeviceCacheDir();
static JSValueRef nativeStartWorker(
JSContextRef ctx,

View File

@ -786,9 +786,32 @@ static void handleMemoryPressureCritical(JNIEnv* env, jobject obj) {
namespace executors {
std::string getDeviceCacheDir() {
// Get the Application Context object
auto getApplicationClass = findClassLocal(
"com/facebook/react/common/ApplicationHolder");
auto getApplicationMethod = getApplicationClass->getStaticMethod<jobject()>(
"getApplication",
"()Landroid/app/Application;"
);
auto application = getApplicationMethod(getApplicationClass);
// Get getCacheDir() from the context
auto getCacheDirMethod = findClassLocal("android/app/Application")
->getMethod<jobject()>("getCacheDir",
"()Ljava/io/File;"
);
auto cacheDirObj = getCacheDirMethod(application);
// Call getAbsolutePath() on the returned File object
auto getAbsolutePathMethod = findClassLocal("java/io/File")
->getMethod<jstring()>("getAbsolutePath");
return getAbsolutePathMethod(cacheDirObj)->toStdString();
}
struct CountableJSCExecutorFactory : CountableJSExecutorFactory {
virtual std::unique_ptr<JSExecutor> createJSExecutor(FlushImmediateCallback cb) override {
return JSCExecutorFactory().createJSExecutor(cb);
return JSCExecutorFactory(getDeviceCacheDir()).createJSExecutor(cb);
}
};