-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release underlying resources when JS instance is GC'ed on Android try…
… 2 (#26155) Summary: Reland #24767 The commit had to be reverted because it caused a crash when using remote debugging in chrome. This is normal since jsi is not available in that environment. The crash was caused by `jsContext.get()` being 0, then being dereferenced later in c++. We can simply skip initializing the blob collector in this case. This also includes the fix from #25720 to fix a crash when using hermes. ## Changelog [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: #26155 Test Plan: Test using RN tester with jsc and hermes Test remote debugging Reviewed By: mdvacca, fred2028 Differential Revision: D17072644 Pulled By: makovkastar fbshipit-source-id: 079d1d43501e854297fbbe586ba229920c892584
- Loading branch information
1 parent
3a96f4c
commit 766f470
Showing
10 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ReactAndroid/src/main/java/com/facebook/react/modules/blob/BlobCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.facebook.react.modules.blob; | ||
|
||
import com.facebook.react.bridge.JavaScriptContextHolder; | ||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.soloader.SoLoader; | ||
|
||
/* package */ class BlobCollector { | ||
static { | ||
SoLoader.loadLibrary("reactnativeblob"); | ||
} | ||
|
||
static void install(final ReactContext reactContext, final BlobModule blobModule) { | ||
reactContext.runOnJSQueueThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
JavaScriptContextHolder jsContext = reactContext.getJavaScriptContextHolder(); | ||
// When debugging in chrome the JS context is not available. | ||
if (jsContext.get() != 0) { | ||
nativeInstall(blobModule, jsContext.get()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private static native void nativeInstall(Object blobModule, long jsContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ReactAndroid/src/main/java/com/facebook/react/modules/blob/jni/Android.mk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
LOCAL_PATH := $(call my-dir) | ||
|
||
include $(CLEAR_VARS) | ||
|
||
LOCAL_MODULE := reactnativeblob | ||
|
||
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp) | ||
|
||
LOCAL_C_INCLUDES := $(LOCAL_PATH) | ||
|
||
LOCAL_CFLAGS += -fvisibility=hidden -fexceptions -frtti | ||
|
||
LOCAL_STATIC_LIBRARIES := libjsi libjsireact | ||
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libreactnativejni | ||
|
||
include $(BUILD_SHARED_LIBRARY) |
22 changes: 22 additions & 0 deletions
22
ReactAndroid/src/main/java/com/facebook/react/modules/blob/jni/BUCK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_target", "react_native_xplat_dep", "rn_xplat_cxx_library") | ||
|
||
rn_xplat_cxx_library( | ||
name = "jni", | ||
srcs = glob(["*.cpp"]), | ||
headers = glob(["*.h"]), | ||
header_namespace = "", | ||
compiler_flags = [ | ||
"-fexceptions", | ||
"-frtti", | ||
"-std=c++14", | ||
"-Wall", | ||
], | ||
platforms = ANDROID, | ||
soname = "libreactnativeblob.$(ext)", | ||
visibility = ["PUBLIC"], | ||
deps = [ | ||
react_native_target("jni/react/jni:jni"), | ||
FBJNI_TARGET, | ||
react_native_xplat_dep("jsi:jsi"), | ||
], | ||
) |
63 changes: 63 additions & 0 deletions
63
ReactAndroid/src/main/java/com/facebook/react/modules/blob/jni/BlobCollector.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "BlobCollector.h" | ||
|
||
#include <fb/fbjni.h> | ||
#include <memory> | ||
#include <mutex> | ||
|
||
using namespace facebook; | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
static constexpr auto kBlobModuleJavaDescriptor = | ||
"com/facebook/react/modules/blob/BlobModule"; | ||
|
||
BlobCollector::BlobCollector( | ||
jni::global_ref<jobject> blobModule, | ||
const std::string &blobId) | ||
: blobModule_(blobModule), blobId_(blobId) {} | ||
|
||
BlobCollector::~BlobCollector() { | ||
jni::ThreadScope::WithClassLoader([&] { | ||
static auto removeMethod = jni::findClassStatic(kBlobModuleJavaDescriptor) | ||
->getMethod<void(jstring)>("remove"); | ||
removeMethod(blobModule_, jni::make_jstring(blobId_).get()); | ||
}); | ||
} | ||
|
||
void BlobCollector::nativeInstall( | ||
jni::alias_ref<jhybridobject> jThis, | ||
jni::alias_ref<jobject> blobModule, | ||
jlong jsContextNativePointer) { | ||
auto &runtime = *((jsi::Runtime *) jsContextNativePointer); | ||
auto blobModuleRef = jni::make_global(blobModule); | ||
runtime.global().setProperty( | ||
runtime, | ||
"__blobCollectorProvider", | ||
jsi::Function::createFromHostFunction( | ||
runtime, | ||
jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"), | ||
1, | ||
[blobModuleRef]( | ||
jsi::Runtime &rt, | ||
const jsi::Value &thisVal, | ||
const jsi::Value *args, | ||
size_t count) { | ||
auto blobId = args[0].asString(rt).utf8(rt); | ||
auto blobCollector = | ||
std::make_shared<BlobCollector>(blobModuleRef, blobId); | ||
return jsi::Object::createFromHostObject(rt, blobCollector); | ||
})); | ||
} | ||
|
||
void BlobCollector::registerNatives() { | ||
registerHybrid( | ||
{makeNativeMethod("nativeInstall", BlobCollector::nativeInstall)}); | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
39 changes: 39 additions & 0 deletions
39
ReactAndroid/src/main/java/com/facebook/react/modules/blob/jni/BlobCollector.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <fb/fbjni.h> | ||
#include <jsi/jsi.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class BlobCollector : public jni::HybridClass<BlobCollector>, | ||
public jsi::HostObject { | ||
public: | ||
BlobCollector( | ||
jni::global_ref<jobject> blobManager, | ||
const std::string &blobId); | ||
~BlobCollector(); | ||
|
||
static constexpr auto kJavaDescriptor = | ||
"Lcom/facebook/react/modules/blob/BlobCollector;"; | ||
|
||
static void nativeInstall( | ||
jni::alias_ref<jhybridobject> jThis, | ||
jni::alias_ref<jobject> blobModule, | ||
jlong jsContextNativePointer); | ||
|
||
static void registerNatives(); | ||
|
||
private: | ||
friend HybridBase; | ||
|
||
jni::global_ref<jobject> blobModule_; | ||
const std::string blobId_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
13 changes: 13 additions & 0 deletions
13
ReactAndroid/src/main/java/com/facebook/react/modules/blob/jni/OnLoad.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include <fb/fbjni.h> | ||
|
||
#include "BlobCollector.h" | ||
|
||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { | ||
return facebook::jni::initialize( | ||
vm, [] { facebook::react::BlobCollector::registerNatives(); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters