File tree Expand file tree Collapse file tree 7 files changed +28
-41
lines changed
ReactAndroid/src/main/jni/react/jni Expand file tree Collapse file tree 7 files changed +28
-41
lines changed Original file line number Diff line number Diff line change 1010#include < string>
1111
1212#include < android/asset_manager.h>
13- #include < cxxreact/JSExecutor.h>
1413#include < fbjni/fbjni.h>
1514
1615namespace facebook ::react {
1716
17+ class JSBigString ;
18+
1819struct JAssetManager : jni::JavaClass<JAssetManager> {
1920 static constexpr auto kJavaDescriptor = " Landroid/content/res/AssetManager;" ;
2021};
Original file line number Diff line number Diff line change 66 */
77
88#include " ResourceLoader.h"
9+
10+ #include < cxxreact/JSBigString.h>
911#include < glog/logging.h>
10- #include < fstream>
11- #include < sstream>
1212
1313namespace facebook ::react {
1414/* static */ bool ResourceLoader::isAbsolutePath (const std::string& path) {
@@ -32,19 +32,13 @@ namespace facebook::react {
3232 return isResourceFile (path);
3333}
3434
35- /* static */ std::string ResourceLoader::getFileContents (
35+ /* static */ std::unique_ptr< const JSBigString> ResourceLoader::getFileContents (
3636 const std::string& path) {
37- if (isAbsolutePath (path)) {
38- std::ifstream file (path, std::ios::binary);
39- if (!file.good ()) {
40- return getResourceFileContents (path);
41- }
42- std::stringstream buffer;
43- buffer << file.rdbuf ();
44- return buffer.str ();
37+ if (isResourceFile (path)) {
38+ return getResourceFileContents (path);
39+ } else {
40+ return JSBigFileString::fromPath (path);
4541 }
46-
47- return getResourceFileContents (path);
4842}
4943
5044/* static */ std::filesystem::path ResourceLoader::getCacheDirectory (
Original file line number Diff line number Diff line change 1111#include < string>
1212
1313namespace facebook ::react {
14+
15+ class JSBigString ;
16+
1417class ResourceLoader {
1518 public:
1619 static bool isDirectory (const std::string &path);
1720 static bool isFile (const std::string &path);
1821 static bool isAbsolutePath (const std::string &path);
19- static std::string getFileContents (const std::string &path);
22+ static std::unique_ptr< const JSBigString> getFileContents (const std::string &path);
2023 static std::filesystem::path getCacheDirectory (const std::string &path = std::string());
2124
2225 protected:
2326 static bool isResourceDirectory (const std::string &path);
2427 static bool isResourceFile (const std::string &path);
25- static std::string getResourceFileContents (const std::string &path);
28+ static std::unique_ptr< const JSBigString> getResourceFileContents (const std::string &path);
2629
2730 private:
2831 static constexpr const auto CACHE_DIR = " .react-native-cxx-cache" ;
Original file line number Diff line number Diff line change 1010
1111#include < android/asset_manager.h>
1212#include < android/asset_manager_jni.h>
13+ #include < cxxreact/JSBigString.h>
1314#include < fbjni/fbjni.h>
15+ #include < react/jni/JSLoader.h>
1416#include < react/jni/JniHelper.h>
1517
1618namespace facebook ::react {
1719
1820namespace {
21+
1922AAssetManager* assetManager_ = nullptr ;
2023
2124AAssetManager* getAssetManager () {
@@ -52,17 +55,9 @@ bool ResourceLoader::isResourceFile(const std::string& path) {
5255 return true ;
5356}
5457
55- std::string ResourceLoader::getResourceFileContents (const std::string& path) {
56- auto asset = AAssetManager_open (
57- getAssetManager (), path.c_str (), AASSET_MODE_STREAMING);
58- if (asset == nullptr ) {
59- throw std::runtime_error (" File not found " + path);
60- }
61-
62- std::string result (
63- (const char *)AAsset_getBuffer (asset), (size_t )AAsset_getLength (asset));
64- AAsset_close (asset);
65- return result;
58+ std::unique_ptr<const JSBigString> ResourceLoader::getResourceFileContents (
59+ const std::string& path) {
60+ return loadScriptFromAssets (getAssetManager (), path);
6661}
6762
6863std::filesystem::path ResourceLoader::getCacheRootPath () {
Original file line number Diff line number Diff line change 55 * LICENSE file in the root directory of this source tree.
66 */
77
8+ #include < cxxreact/JSBigString.h>
89#include < react/io/ResourceLoader.h>
910
1011#include < cassert>
11- #include < fstream>
12- #include < sstream>
1312
1413namespace facebook ::react {
1514
@@ -21,17 +20,12 @@ bool ResourceLoader::isResourceFile(const std::string& path) {
2120 return std::filesystem::exists (path) && !std::filesystem::is_directory (path);
2221}
2322
24- std::string ResourceLoader::getResourceFileContents (const std::string& path) {
25- std::ifstream file (path, std::ios::binary);
26- if (!file.good ()) {
27- throw std::runtime_error (" File not found " + path);
28- }
29- std::stringstream buffer;
30- buffer << file.rdbuf ();
31- return buffer.str ();
23+ /* static */ std::unique_ptr<const JSBigString>
24+ ResourceLoader::getResourceFileContents (const std::string& path) {
25+ return JSBigFileString::fromPath (path);
3226}
3327
34- std::filesystem::path ResourceLoader::getCacheRootPath () {
28+ /* static */ std::filesystem::path ResourceLoader::getCacheRootPath () {
3529 return std::filesystem::temp_directory_path ();
3630}
3731} // namespace facebook::react
Original file line number Diff line number Diff line change 55 * LICENSE file in the root directory of this source tree.
66 */
77
8+ #include " JniHelper.h"
9+
810#include < fbjni/Context.h>
911#include < fbjni/fbjni.h>
1012
Original file line number Diff line number Diff line change @@ -484,9 +484,7 @@ bool ReactHost::loadScriptFromDevServer() {
484484bool ReactHost::loadScriptFromBundlePath (const std::string& bundlePath) {
485485 try {
486486 LOG (INFO) << " Loading JS bundle from bundle path: " << bundlePath;
487- // TODO: use platform-native asset loading strategy
488- auto script = std::make_unique<JSBigStdString>(
489- ResourceLoader::getFileContents (bundlePath));
487+ auto script = ResourceLoader::getFileContents (bundlePath);
490488 reactInstance_->loadScript (std::move (script), bundlePath);
491489 LOG (INFO) << " Loaded JS bundle from bundle path: " << bundlePath;
492490 return true ;
You can’t perform that action at this time.
0 commit comments