|
| 1 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 2 | +// This source code is licensed under the MIT license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. |
| 4 | + |
| 5 | +#include "BlobCollector.h" |
| 6 | + |
| 7 | +#include <fb/fbjni.h> |
| 8 | +#include <memory> |
| 9 | +#include <mutex> |
| 10 | + |
| 11 | +using namespace facebook; |
| 12 | + |
| 13 | +namespace facebook { |
| 14 | +namespace react { |
| 15 | + |
| 16 | +static constexpr auto kBlobModuleJavaDescriptor = |
| 17 | + "com/facebook/react/modules/blob/BlobModule"; |
| 18 | + |
| 19 | +BlobCollector::BlobCollector( |
| 20 | + jni::global_ref<jobject> blobModule, |
| 21 | + const std::string &blobId) |
| 22 | + : blobModule_(blobModule), blobId_(blobId) {} |
| 23 | + |
| 24 | +BlobCollector::~BlobCollector() { |
| 25 | + jni::ThreadScope::WithClassLoader([&] { |
| 26 | + static auto removeMethod = jni::findClassStatic(kBlobModuleJavaDescriptor) |
| 27 | + ->getMethod<void(jstring)>("remove"); |
| 28 | + removeMethod(blobModule_, jni::make_jstring(blobId_).get()); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +void BlobCollector::nativeInstall( |
| 33 | + jni::alias_ref<jhybridobject> jThis, |
| 34 | + jni::alias_ref<jobject> blobModule, |
| 35 | + jlong jsContextNativePointer) { |
| 36 | + auto &runtime = *((jsi::Runtime *) jsContextNativePointer); |
| 37 | + auto blobModuleRef = jni::make_global(blobModule); |
| 38 | + runtime.global().setProperty( |
| 39 | + runtime, |
| 40 | + "__blobCollectorProvider", |
| 41 | + jsi::Function::createFromHostFunction( |
| 42 | + runtime, |
| 43 | + jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"), |
| 44 | + 1, |
| 45 | + [blobModuleRef]( |
| 46 | + jsi::Runtime &rt, |
| 47 | + const jsi::Value &thisVal, |
| 48 | + const jsi::Value *args, |
| 49 | + size_t count) { |
| 50 | + auto blobId = args[0].asString(rt).utf8(rt); |
| 51 | + auto blobCollector = |
| 52 | + std::make_shared<BlobCollector>(blobModuleRef, blobId); |
| 53 | + return jsi::Object::createFromHostObject(rt, blobCollector); |
| 54 | + })); |
| 55 | +} |
| 56 | + |
| 57 | +void BlobCollector::registerNatives() { |
| 58 | + registerHybrid( |
| 59 | + {makeNativeMethod("nativeInstall", BlobCollector::nativeInstall)}); |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace react |
| 63 | +} // namespace facebook |
0 commit comments