From 2997c8feed357121806e46aca2c413d3fd122743 Mon Sep 17 00:00:00 2001 From: Chris Hopman Date: Fri, 5 Feb 2016 18:09:25 -0800 Subject: [PATCH] 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 --- .../src/main/jni/react/JSCExecutor.cpp | 31 +++---------------- ReactAndroid/src/main/jni/react/JSCExecutor.h | 7 +++-- .../src/main/jni/react/jni/OnLoad.cpp | 25 ++++++++++++++- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index ae1aa7a4f..81165a080 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -85,11 +85,11 @@ static std::string executeJSCallWithJSC( } std::unique_ptr JSCExecutorFactory::createJSExecutor(FlushImmediateCallback cb) { - return std::unique_ptr(new JSCExecutor(cb)); + return std::unique_ptr(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( - "getApplication", - "()Landroid/app/Application;" - ); - auto application = getApplicationMethod(getApplicationClass); - - // Get getCacheDir() from the context - auto getCacheDirMethod = findClassLocal("android/app/Application") - ->getMethod("getCacheDir", - "()Ljava/io/File;" - ); - auto cacheDirObj = getCacheDirMethod(application); - - // Call getAbsolutePath() on the returned File object - auto getAbsolutePathMethod = findClassLocal("java/io/File") - ->getMethod("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()); } } diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.h b/ReactAndroid/src/main/jni/react/JSCExecutor.h index 9d66fdf99..8ddecb100 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.h +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.h @@ -17,7 +17,10 @@ class JMessageQueueThread; class JSCExecutorFactory : public JSExecutorFactory { public: + JSCExecutorFactory(const std::string& cacheDir) : cacheDir_(cacheDir) {} virtual std::unique_ptr 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 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, diff --git a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp index 41a4044f6..49fd32ae3 100644 --- a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp +++ b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp @@ -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( + "getApplication", + "()Landroid/app/Application;" + ); + auto application = getApplicationMethod(getApplicationClass); + + // Get getCacheDir() from the context + auto getCacheDirMethod = findClassLocal("android/app/Application") + ->getMethod("getCacheDir", + "()Ljava/io/File;" + ); + auto cacheDirObj = getCacheDirMethod(application); + + // Call getAbsolutePath() on the returned File object + auto getAbsolutePathMethod = findClassLocal("java/io/File") + ->getMethod("getAbsolutePath"); + return getAbsolutePathMethod(cacheDirObj)->toStdString(); +} + struct CountableJSCExecutorFactory : CountableJSExecutorFactory { virtual std::unique_ptr createJSExecutor(FlushImmediateCallback cb) override { - return JSCExecutorFactory().createJSExecutor(cb); + return JSCExecutorFactory(getDeviceCacheDir()).createJSExecutor(cb); } };