11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_BLOB_H_ 21cb0ef41Sopenharmony_ci#define SRC_NODE_BLOB_H_ 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci#include "async_wrap.h" 71cb0ef41Sopenharmony_ci#include "base_object.h" 81cb0ef41Sopenharmony_ci#include "env.h" 91cb0ef41Sopenharmony_ci#include "memory_tracker.h" 101cb0ef41Sopenharmony_ci#include "node_internals.h" 111cb0ef41Sopenharmony_ci#include "node_snapshotable.h" 121cb0ef41Sopenharmony_ci#include "node_worker.h" 131cb0ef41Sopenharmony_ci#include "v8.h" 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#include <string> 161cb0ef41Sopenharmony_ci#include <unordered_map> 171cb0ef41Sopenharmony_ci#include <vector> 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cinamespace node { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cistruct BlobEntry { 221cb0ef41Sopenharmony_ci std::shared_ptr<v8::BackingStore> store; 231cb0ef41Sopenharmony_ci size_t length; 241cb0ef41Sopenharmony_ci size_t offset; 251cb0ef41Sopenharmony_ci}; 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciclass Blob : public BaseObject { 281cb0ef41Sopenharmony_ci public: 291cb0ef41Sopenharmony_ci static void RegisterExternalReferences( 301cb0ef41Sopenharmony_ci ExternalReferenceRegistry* registry); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci static void Initialize( 331cb0ef41Sopenharmony_ci v8::Local<v8::Object> target, 341cb0ef41Sopenharmony_ci v8::Local<v8::Value> unused, 351cb0ef41Sopenharmony_ci v8::Local<v8::Context> context, 361cb0ef41Sopenharmony_ci void* priv); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 391cb0ef41Sopenharmony_ci static void ToArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args); 401cb0ef41Sopenharmony_ci static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args); 411cb0ef41Sopenharmony_ci static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args); 421cb0ef41Sopenharmony_ci static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args); 431cb0ef41Sopenharmony_ci static void RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 461cb0ef41Sopenharmony_ci Environment* env); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci static BaseObjectPtr<Blob> Create(Environment* env, 491cb0ef41Sopenharmony_ci const std::vector<BlobEntry>& store, 501cb0ef41Sopenharmony_ci size_t length); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci static bool HasInstance(Environment* env, v8::Local<v8::Value> object); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci const std::vector<BlobEntry>& entries() const { return store_; } 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 571cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(Blob) 581cb0ef41Sopenharmony_ci SET_SELF_SIZE(Blob) 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci // Copies the contents of the Blob into an ArrayBuffer. 611cb0ef41Sopenharmony_ci v8::MaybeLocal<v8::Value> GetArrayBuffer(Environment* env); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci BaseObjectPtr<Blob> Slice(Environment* env, size_t start, size_t end); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci inline size_t length() const { return length_; } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci class BlobTransferData : public worker::TransferData { 681cb0ef41Sopenharmony_ci public: 691cb0ef41Sopenharmony_ci explicit BlobTransferData( 701cb0ef41Sopenharmony_ci const std::vector<BlobEntry>& store, 711cb0ef41Sopenharmony_ci size_t length) 721cb0ef41Sopenharmony_ci : store_(store), 731cb0ef41Sopenharmony_ci length_(length) {} 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci BaseObjectPtr<BaseObject> Deserialize( 761cb0ef41Sopenharmony_ci Environment* env, 771cb0ef41Sopenharmony_ci v8::Local<v8::Context> context, 781cb0ef41Sopenharmony_ci std::unique_ptr<worker::TransferData> self) override; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(BlobTransferData) 811cb0ef41Sopenharmony_ci SET_SELF_SIZE(BlobTransferData) 821cb0ef41Sopenharmony_ci SET_NO_MEMORY_INFO() 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci private: 851cb0ef41Sopenharmony_ci std::vector<BlobEntry> store_; 861cb0ef41Sopenharmony_ci size_t length_ = 0; 871cb0ef41Sopenharmony_ci }; 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci BaseObject::TransferMode GetTransferMode() const override; 901cb0ef41Sopenharmony_ci std::unique_ptr<worker::TransferData> CloneForMessaging() const override; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci Blob( 931cb0ef41Sopenharmony_ci Environment* env, 941cb0ef41Sopenharmony_ci v8::Local<v8::Object> obj, 951cb0ef41Sopenharmony_ci const std::vector<BlobEntry>& store, 961cb0ef41Sopenharmony_ci size_t length); 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci private: 991cb0ef41Sopenharmony_ci std::vector<BlobEntry> store_; 1001cb0ef41Sopenharmony_ci size_t length_ = 0; 1011cb0ef41Sopenharmony_ci}; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciclass FixedSizeBlobCopyJob : public AsyncWrap, public ThreadPoolWork { 1041cb0ef41Sopenharmony_ci public: 1051cb0ef41Sopenharmony_ci enum class Mode { 1061cb0ef41Sopenharmony_ci SYNC, 1071cb0ef41Sopenharmony_ci ASYNC 1081cb0ef41Sopenharmony_ci }; 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci static void RegisterExternalReferences( 1111cb0ef41Sopenharmony_ci ExternalReferenceRegistry* registry); 1121cb0ef41Sopenharmony_ci static void Initialize(Environment* env, v8::Local<v8::Object> target); 1131cb0ef41Sopenharmony_ci static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 1141cb0ef41Sopenharmony_ci static void Run(const v8::FunctionCallbackInfo<v8::Value>& args); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci bool IsNotIndicativeOfMemoryLeakAtExit() const override { 1171cb0ef41Sopenharmony_ci return true; 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci void DoThreadPoolWork() override; 1211cb0ef41Sopenharmony_ci void AfterThreadPoolWork(int status) override; 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci Mode mode() const { return mode_; } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 1261cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(FixedSizeBlobCopyJob) 1271cb0ef41Sopenharmony_ci SET_SELF_SIZE(FixedSizeBlobCopyJob) 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci private: 1301cb0ef41Sopenharmony_ci FixedSizeBlobCopyJob( 1311cb0ef41Sopenharmony_ci Environment* env, 1321cb0ef41Sopenharmony_ci v8::Local<v8::Object> object, 1331cb0ef41Sopenharmony_ci Blob* blob, 1341cb0ef41Sopenharmony_ci Mode mode = Mode::ASYNC); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci Mode mode_; 1371cb0ef41Sopenharmony_ci std::vector<BlobEntry> source_; 1381cb0ef41Sopenharmony_ci std::shared_ptr<v8::BackingStore> destination_; 1391cb0ef41Sopenharmony_ci size_t length_ = 0; 1401cb0ef41Sopenharmony_ci}; 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ciclass BlobBindingData : public SnapshotableObject { 1431cb0ef41Sopenharmony_ci public: 1441cb0ef41Sopenharmony_ci explicit BlobBindingData(Realm* realm, v8::Local<v8::Object> wrap); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci using InternalFieldInfo = InternalFieldInfoBase; 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci SERIALIZABLE_OBJECT_METHODS() 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci SET_BINDING_ID(blob_binding_data) 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 1531cb0ef41Sopenharmony_ci SET_SELF_SIZE(BlobBindingData) 1541cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(BlobBindingData) 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci struct StoredDataObject : public MemoryRetainer { 1571cb0ef41Sopenharmony_ci BaseObjectPtr<Blob> blob; 1581cb0ef41Sopenharmony_ci size_t length; 1591cb0ef41Sopenharmony_ci std::string type; 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci StoredDataObject() = default; 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci StoredDataObject( 1641cb0ef41Sopenharmony_ci const BaseObjectPtr<Blob>& blob_, 1651cb0ef41Sopenharmony_ci size_t length_, 1661cb0ef41Sopenharmony_ci const std::string& type_); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 1691cb0ef41Sopenharmony_ci SET_SELF_SIZE(StoredDataObject) 1701cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(StoredDataObject) 1711cb0ef41Sopenharmony_ci }; 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci void store_data_object( 1741cb0ef41Sopenharmony_ci const std::string& uuid, 1751cb0ef41Sopenharmony_ci const StoredDataObject& object); 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci void revoke_data_object(const std::string& uuid); 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci StoredDataObject get_data_object(const std::string& uuid); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci private: 1821cb0ef41Sopenharmony_ci std::unordered_map<std::string, StoredDataObject> data_objects_; 1831cb0ef41Sopenharmony_ci}; 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci} // namespace node 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 1881cb0ef41Sopenharmony_ci#endif // SRC_NODE_BLOB_H_ 189