|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "BlobModule.h" |
| 9 | + |
| 10 | +#include <cmath> |
| 11 | +#include <limits> |
| 12 | +#include <utility> |
| 13 | + |
| 14 | +namespace facebook::react { |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +constexpr const char* kBlobCollectorProvider = "__blobCollectorProvider"; |
| 19 | + |
| 20 | +/// Lets JavaScript-held blobs free their bytes when garbage collected. |
| 21 | +/// BlobManager.createFromOptions attaches a collector to each Blob. |
| 22 | +class BlobCollector : public jsi::HostObject { |
| 23 | + public: |
| 24 | + BlobCollector(std::weak_ptr<BlobStore> store, std::string blobId) |
| 25 | + : store_(std::move(store)), blobId_(std::move(blobId)) {} |
| 26 | + |
| 27 | + BlobCollector(const BlobCollector&) = delete; |
| 28 | + BlobCollector& operator=(const BlobCollector&) = delete; |
| 29 | + BlobCollector(BlobCollector&&) = delete; |
| 30 | + BlobCollector& operator=(BlobCollector&&) = delete; |
| 31 | + |
| 32 | + ~BlobCollector() override { |
| 33 | + if (auto store = store_.lock()) { |
| 34 | + store->remove(blobId_); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private: |
| 39 | + std::weak_ptr<BlobStore> store_; |
| 40 | + std::string blobId_; |
| 41 | +}; |
| 42 | + |
| 43 | +/// Converts a JavaScript offset/size to an index, rejecting values that are not |
| 44 | +/// non-negative integers representable as `std::size_t`. |
| 45 | +std::optional<std::size_t> toBlobIndex(const jsi::Value& value) { |
| 46 | + if (!value.isNumber()) { |
| 47 | + return std::nullopt; |
| 48 | + } |
| 49 | + const double number = value.getNumber(); |
| 50 | + // `!(number >= 0)` also rejects NaN. The size_t maximum rounds up to a power |
| 51 | + // of two as a double, so `<` keeps the cast in range. |
| 52 | + if (!(number >= 0) || |
| 53 | + number >= static_cast<double>(std::numeric_limits<std::size_t>::max()) || |
| 54 | + std::trunc(number) != number) { |
| 55 | + return std::nullopt; |
| 56 | + } |
| 57 | + return static_cast<std::size_t>(number); |
| 58 | +} |
| 59 | + |
| 60 | +} // namespace |
| 61 | + |
| 62 | +std::optional<std::vector<uint8_t>> resolveBlobData( |
| 63 | + jsi::Runtime& rt, |
| 64 | + const jsi::Object& blobData, |
| 65 | + const BlobStore& store) { |
| 66 | + auto blobId = blobData.getProperty(rt, "blobId"); |
| 67 | + auto offset = toBlobIndex(blobData.getProperty(rt, "offset")); |
| 68 | + auto size = toBlobIndex(blobData.getProperty(rt, "size")); |
| 69 | + if (!blobId.isString() || !offset || !size) { |
| 70 | + return std::nullopt; |
| 71 | + } |
| 72 | + return store.resolve(blobId.getString(rt).utf8(rt), *offset, *size); |
| 73 | +} |
| 74 | + |
| 75 | +BlobModule::BlobModule( |
| 76 | + std::shared_ptr<CallInvoker> jsInvoker, |
| 77 | + std::shared_ptr<BlobStore> store) |
| 78 | + : NativeBlobModuleCxxSpec(std::move(jsInvoker)), store_(std::move(store)) {} |
| 79 | + |
| 80 | +jsi::Object BlobModule::getConstants(jsi::Runtime& rt) { |
| 81 | + auto constants = jsi::Object(rt); |
| 82 | + constants.setProperty( |
| 83 | + rt, "BLOB_URI_SCHEME", jsi::String::createFromAscii(rt, "blob")); |
| 84 | + constants.setProperty(rt, "BLOB_URI_HOST", jsi::Value::null()); |
| 85 | + return constants; |
| 86 | +} |
| 87 | + |
| 88 | +void BlobModule::addNetworkingHandler(jsi::Runtime& /*rt*/) {} |
| 89 | + |
| 90 | +void BlobModule::addWebSocketHandler(jsi::Runtime& rt, double /*id*/) { |
| 91 | + throw jsi::JSError(rt, "BlobModule: WebSocket blobs are not supported"); |
| 92 | +} |
| 93 | + |
| 94 | +void BlobModule::removeWebSocketHandler(jsi::Runtime& /*rt*/, double /*id*/) {} |
| 95 | + |
| 96 | +void BlobModule::sendOverSocket( |
| 97 | + jsi::Runtime& rt, |
| 98 | + jsi::Object /*blob*/, |
| 99 | + double /*socketID*/) { |
| 100 | + throw jsi::JSError(rt, "BlobModule: WebSocket blobs are not supported"); |
| 101 | +} |
| 102 | + |
| 103 | +void BlobModule::createFromParts( |
| 104 | + jsi::Runtime& rt, |
| 105 | + jsi::Array parts, |
| 106 | + const std::string& withId) { |
| 107 | + std::vector<uint8_t> bytes; |
| 108 | + const auto count = parts.size(rt); |
| 109 | + for (size_t i = 0; i < count; ++i) { |
| 110 | + auto part = parts.getValueAtIndex(rt, i).asObject(rt); |
| 111 | + auto type = part.getProperty(rt, "type").asString(rt).utf8(rt); |
| 112 | + if (type == "string") { |
| 113 | + auto text = part.getProperty(rt, "data").asString(rt).utf8(rt); |
| 114 | + bytes.insert(bytes.end(), text.begin(), text.end()); |
| 115 | + } else if (type == "blob") { |
| 116 | + auto data = resolveBlobData( |
| 117 | + rt, part.getProperty(rt, "data").asObject(rt), *store_); |
| 118 | + if (!data) { |
| 119 | + throw jsi::JSError( |
| 120 | + rt, "BlobModule: createFromParts references an invalid blob"); |
| 121 | + } |
| 122 | + bytes.insert(bytes.end(), data->begin(), data->end()); |
| 123 | + } else { |
| 124 | + throw jsi::JSError(rt, "BlobModule: invalid type for blob: " + type); |
| 125 | + } |
| 126 | + } |
| 127 | + store_->store(withId, std::move(bytes)); |
| 128 | +} |
| 129 | + |
| 130 | +void BlobModule::release(jsi::Runtime& /*rt*/, const std::string& blobId) { |
| 131 | + store_->remove(blobId); |
| 132 | +} |
| 133 | + |
| 134 | +void BlobModule::installJSIBindingsWithRuntime(jsi::Runtime& rt) { |
| 135 | + auto global = rt.global(); |
| 136 | + if (global.hasProperty(rt, kBlobCollectorProvider)) { |
| 137 | + return; |
| 138 | + } |
| 139 | + auto name = jsi::PropNameID::forAscii(rt, kBlobCollectorProvider); |
| 140 | + global.setProperty( |
| 141 | + rt, |
| 142 | + name, |
| 143 | + jsi::Function::createFromHostFunction( |
| 144 | + rt, |
| 145 | + name, |
| 146 | + 1, |
| 147 | + [store = std::weak_ptr<BlobStore>(store_)]( |
| 148 | + jsi::Runtime& rt, |
| 149 | + const jsi::Value& /*thisVal*/, |
| 150 | + const jsi::Value* args, |
| 151 | + size_t count) -> jsi::Value { |
| 152 | + if (count < 1 || !args->isString()) { |
| 153 | + throw jsi::JSError( |
| 154 | + rt, "__blobCollectorProvider expects a blob id"); |
| 155 | + } |
| 156 | + return jsi::Object::createFromHostObject( |
| 157 | + rt, |
| 158 | + std::make_shared<BlobCollector>( |
| 159 | + store, args->getString(rt).utf8(rt))); |
| 160 | + })); |
| 161 | +} |
| 162 | + |
| 163 | +BlobNetworkingResponseHandler::BlobNetworkingResponseHandler( |
| 164 | + std::shared_ptr<BlobStore> store) |
| 165 | + : store_(std::move(store)) {} |
| 166 | + |
| 167 | +bool BlobNetworkingResponseHandler::supports( |
| 168 | + const std::string& responseType) const { |
| 169 | + return responseType == "blob"; |
| 170 | +} |
| 171 | + |
| 172 | +jsi::Value BlobNetworkingResponseHandler::toResponseData( |
| 173 | + jsi::Runtime& rt, |
| 174 | + std::vector<uint8_t> body) { |
| 175 | + const auto size = static_cast<double>(body.size()); |
| 176 | + auto blobId = store_->store(std::move(body)); |
| 177 | + auto blobData = jsi::Object(rt); |
| 178 | + blobData.setProperty(rt, "blobId", jsi::String::createFromUtf8(rt, blobId)); |
| 179 | + blobData.setProperty(rt, "offset", 0); |
| 180 | + blobData.setProperty(rt, "size", size); |
| 181 | + return blobData; |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace facebook::react |
0 commit comments