Skip to content

Commit 7cb10a8

Browse files
javachemeta-codesync[bot]
authored andcommitted
Use AssetManagerString on Android (#54384)
Summary: Pull Request resolved: #54384 Delegate to the existing AssetManager loading logic in RN Android in the platform-specific ResourceLoader bits. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D86099684 fbshipit-source-id: 43d33aa9b4f353e817966d76b8272950d954feab
1 parent 5f69ff7 commit 7cb10a8

File tree

7 files changed

+28
-41
lines changed

7 files changed

+28
-41
lines changed

packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
#include <string>
1111

1212
#include <android/asset_manager.h>
13-
#include <cxxreact/JSExecutor.h>
1413
#include <fbjni/fbjni.h>
1514

1615
namespace facebook::react {
1716

17+
class JSBigString;
18+
1819
struct JAssetManager : jni::JavaClass<JAssetManager> {
1920
static constexpr auto kJavaDescriptor = "Landroid/content/res/AssetManager;";
2021
};

packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
*/
77

88
#include "ResourceLoader.h"
9+
10+
#include <cxxreact/JSBigString.h>
911
#include <glog/logging.h>
10-
#include <fstream>
11-
#include <sstream>
1212

1313
namespace 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(

packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111
#include <string>
1212

1313
namespace facebook::react {
14+
15+
class JSBigString;
16+
1417
class 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";

packages/react-native/ReactCxxPlatform/react/io/platform/android/ResourceLoader.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
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

1618
namespace facebook::react {
1719

1820
namespace {
21+
1922
AAssetManager* assetManager_ = nullptr;
2023

2124
AAssetManager* 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

6863
std::filesystem::path ResourceLoader::getCacheRootPath() {

packages/react-native/ReactCxxPlatform/react/io/platform/cxx/ResourceLoader.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
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

1413
namespace 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

packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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

packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ bool ReactHost::loadScriptFromDevServer() {
484484
bool 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;

0 commit comments

Comments
 (0)