Skip to content

Commit

Permalink
Added Android support for loading multiple RAM bundles
Browse files Browse the repository at this point in the history
Differential Revision: D5901574

fbshipit-source-id: 395bae41e58505918d7ad20ac432eba3361361ea
  • Loading branch information
fromcelticpark authored and facebook-github-bot committed Sep 29, 2017
1 parent e691699 commit 4162d73
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions ReactAndroid/src/main/jni/react/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LOCAL_SRC_FILES := \
JSLoader.cpp \
JSLogging.cpp \
JniJSModulesUnbundle.cpp \
JniRAMBundleRegistry.cpp \
MethodInvoker.cpp \
ModuleRegistryBuilder.cpp \
NativeArray.cpp \
Expand Down
5 changes: 3 additions & 2 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CxxModuleWrapper.h"
#include "JavaScriptExecutorHolder.h"
#include "JniJSModulesUnbundle.h"
#include "JniRAMBundleRegistry.h"
#include "JNativeRunnable.h"
#include "NativeArray.h"

Expand Down Expand Up @@ -186,8 +187,8 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
auto manager = extractAssetManager(assetManager);
auto script = loadScriptFromAssets(manager, sourceURL);
if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) {
auto bundle = folly::make_unique<JniJSModulesUnbundle>(manager, sourceURL);
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
auto bundle = JniJSModulesUnbundle::fromEntryFile(manager, sourceURL);
auto registry = folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
instance_->loadRAMBundle(
std::move(registry),
std::move(script),
Expand Down
12 changes: 10 additions & 2 deletions ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <sys/endian.h>
#include <utility>

#include <folly/Memory.h>

using magic_number_t = uint32_t;
const magic_number_t MAGIC_FILE_HEADER = 0xFB0BD1E5;
const std::string MAGIC_FILE_NAME = "UNBUNDLE";
Expand All @@ -36,9 +38,15 @@ static asset_ptr openAsset(
AAsset_close);
}

JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile) :
std::unique_ptr<JniJSModulesUnbundle> JniJSModulesUnbundle::fromEntryFile(
AAssetManager *assetManager,
const std::string& entryFile) {
return folly::make_unique<JniJSModulesUnbundle>(assetManager, jsModulesDir(entryFile));
}

JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& moduleDirectory) :
m_assetManager(assetManager),
m_moduleDirectory(jsModulesDir(entryFile)) {}
m_moduleDirectory(moduleDirectory) {}

bool JniJSModulesUnbundle::isUnbundle(
AAssetManager *assetManager,
Expand Down
6 changes: 5 additions & 1 deletion ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once

#include <memory>

#include <android/asset_manager.h>
#include <cxxreact/JSModulesUnbundle.h>

Expand All @@ -14,10 +16,12 @@ class JniJSModulesUnbundle : public JSModulesUnbundle {
*/
public:
JniJSModulesUnbundle() = default;
JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile);
JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& moduleDirectory);
JniJSModulesUnbundle(JniJSModulesUnbundle&& other) = delete;
JniJSModulesUnbundle& operator= (JSModulesUnbundle&& other) = delete;

static std::unique_ptr<JniJSModulesUnbundle> fromEntryFile(AAssetManager *assetManager, const std::string& entryFile);

static bool isUnbundle(
AAssetManager *assetManager,
const std::string& assetName);
Expand Down
36 changes: 36 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#include "JniRAMBundleRegistry.h"

#include <libgen.h>

#include <folly/Conv.h>
#include <folly/Memory.h>

#include "JniJSModulesUnbundle.h"

namespace facebook {
namespace react {

static std::string jsBundlesDir(const std::string& entryFile) {
std::string dir = dirname(entryFile.c_str());
std::string entryName = basename(entryFile.c_str());
entryName.erase(entryName.find("."), std::string::npos);

std::string path = "js-bundles/" + entryName + "/";
// android's asset manager does not work with paths that start with a dot
return dir == "." ? path : dir + "/" + path;
}

JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile) :
RAMBundleRegistry(std::move(mainBundle)),
m_assetManager(assetManager),
m_baseDirectoryPath(jsBundlesDir(entryFile)) {}

std::unique_ptr<JSModulesUnbundle> JniRAMBundleRegistry::bundleById(uint32_t index) const {
std::string bundlePathById = m_baseDirectoryPath + folly::to<std::string>(index) + "/js-modules/";
return folly::make_unique<JniJSModulesUnbundle>(m_assetManager, bundlePathById);
}

}
}
21 changes: 21 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#include <android/asset_manager.h>
#include <cxxreact/RAMBundleRegistry.h>

namespace facebook {
namespace react {

class JniRAMBundleRegistry : public RAMBundleRegistry {
public:
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile);

protected:
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const override;
private:
AAssetManager *m_assetManager = nullptr;
std::string m_baseDirectoryPath;
};

}
}

0 comments on commit 4162d73

Please sign in to comment.