/src/node/src/node_blob.h
Line  | Count  | Source  | 
1  |  | #ifndef SRC_NODE_BLOB_H_  | 
2  |  | #define SRC_NODE_BLOB_H_  | 
3  |  |  | 
4  |  | #include "v8-function-callback.h"  | 
5  |  | #include "v8-template.h"  | 
6  |  | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS  | 
7  |  |  | 
8  |  | #include "async_wrap.h"  | 
9  |  | #include "base_object.h"  | 
10  |  | #include "dataqueue/queue.h"  | 
11  |  | #include "env.h"  | 
12  |  | #include "memory_tracker.h"  | 
13  |  | #include "node_internals.h"  | 
14  |  | #include "node_snapshotable.h"  | 
15  |  | #include "node_worker.h"  | 
16  |  | #include "v8.h"  | 
17  |  |  | 
18  |  | #include <string>  | 
19  |  | #include <unordered_map>  | 
20  |  | #include <vector>  | 
21  |  |  | 
22  |  | namespace node { | 
23  |  |  | 
24  |  | class Blob : public BaseObject { | 
25  |  |  public:  | 
26  |  |   static void RegisterExternalReferences(  | 
27  |  |       ExternalReferenceRegistry* registry);  | 
28  |  |  | 
29  |  |   static void CreatePerIsolateProperties(IsolateData* isolate_data,  | 
30  |  |                                          v8::Local<v8::ObjectTemplate> target);  | 
31  |  |   static void CreatePerContextProperties(v8::Local<v8::Object> target,  | 
32  |  |                                          v8::Local<v8::Value> unused,  | 
33  |  |                                          v8::Local<v8::Context> context,  | 
34  |  |                                          void* priv);  | 
35  |  |  | 
36  |  |   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
37  |  |   static void GetReader(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
38  |  |   static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
39  |  |   static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
40  |  |   static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
41  |  |   static void RevokeObjectURL(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
42  |  |  | 
43  |  |   static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(  | 
44  |  |       Environment* env);  | 
45  |  |  | 
46  |  |   static BaseObjectPtr<Blob> Create(Environment* env,  | 
47  |  |                                     std::shared_ptr<DataQueue> data_queue);  | 
48  |  |  | 
49  |  |   static bool HasInstance(Environment* env, v8::Local<v8::Value> object);  | 
50  |  |  | 
51  |  |   void MemoryInfo(MemoryTracker* tracker) const override;  | 
52  |  |   SET_MEMORY_INFO_NAME(Blob)  | 
53  |  |   SET_SELF_SIZE(Blob)  | 
54  |  |  | 
55  |  |   BaseObjectPtr<Blob> Slice(Environment* env, size_t start, size_t end);  | 
56  |  |  | 
57  | 0  |   inline size_t length() const { return this->data_queue_->size().value(); } | 
58  |  |  | 
59  |  |   class BlobTransferData : public worker::TransferData { | 
60  |  |    public:  | 
61  |  |     explicit BlobTransferData(std::shared_ptr<DataQueue> data_queue)  | 
62  | 0  |         : data_queue(data_queue) {} | 
63  |  |  | 
64  |  |     BaseObjectPtr<BaseObject> Deserialize(  | 
65  |  |         Environment* env,  | 
66  |  |         v8::Local<v8::Context> context,  | 
67  |  |         std::unique_ptr<worker::TransferData> self) override;  | 
68  |  |  | 
69  |  |     SET_MEMORY_INFO_NAME(BlobTransferData)  | 
70  |  |     SET_SELF_SIZE(BlobTransferData)  | 
71  |  |     SET_NO_MEMORY_INFO()  | 
72  |  |  | 
73  |  |    private:  | 
74  |  |     std::shared_ptr<DataQueue> data_queue;  | 
75  |  |   };  | 
76  |  |  | 
77  |  |   class Reader final : public AsyncWrap { | 
78  |  |    public:  | 
79  |  |     static bool HasInstance(Environment* env, v8::Local<v8::Value> value);  | 
80  |  |     static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(  | 
81  |  |         Environment* env);  | 
82  |  |     static BaseObjectPtr<Reader> Create(Environment* env,  | 
83  |  |                                         BaseObjectPtr<Blob> blob);  | 
84  |  |     static void Pull(const v8::FunctionCallbackInfo<v8::Value>& args);  | 
85  |  |  | 
86  |  |     explicit Reader(Environment* env,  | 
87  |  |                     v8::Local<v8::Object> obj,  | 
88  |  |                     BaseObjectPtr<Blob> strong_ptr);  | 
89  |  |  | 
90  |  |     SET_NO_MEMORY_INFO()  | 
91  |  |     SET_MEMORY_INFO_NAME(Blob::Reader)  | 
92  |  |     SET_SELF_SIZE(Reader)  | 
93  |  |  | 
94  |  |    private:  | 
95  |  |     std::shared_ptr<DataQueue::Reader> inner_;  | 
96  |  |     BaseObjectPtr<Blob> strong_ptr_;  | 
97  |  |     bool eos_ = false;  | 
98  |  |   };  | 
99  |  |  | 
100  |  |   BaseObject::TransferMode GetTransferMode() const override;  | 
101  |  |   std::unique_ptr<worker::TransferData> CloneForMessaging() const override;  | 
102  |  |  | 
103  |  |   Blob(Environment* env,  | 
104  |  |        v8::Local<v8::Object> obj,  | 
105  |  |        std::shared_ptr<DataQueue> data_queue);  | 
106  |  |  | 
107  | 0  |   DataQueue& getDataQueue() const { return *data_queue_; } | 
108  |  |  | 
109  |  |  private:  | 
110  |  |   std::shared_ptr<DataQueue> data_queue_;  | 
111  |  | };  | 
112  |  |  | 
113  |  | class BlobBindingData : public SnapshotableObject { | 
114  |  |  public:  | 
115  |  |   explicit BlobBindingData(Realm* realm, v8::Local<v8::Object> wrap);  | 
116  |  |  | 
117  |  |   using InternalFieldInfo = InternalFieldInfoBase;  | 
118  |  |  | 
119  |  |   SERIALIZABLE_OBJECT_METHODS()  | 
120  |  |  | 
121  |  |   SET_BINDING_ID(blob_binding_data)  | 
122  |  |  | 
123  |  |   void MemoryInfo(MemoryTracker* tracker) const override;  | 
124  |  |   SET_SELF_SIZE(BlobBindingData)  | 
125  |  |   SET_MEMORY_INFO_NAME(BlobBindingData)  | 
126  |  |  | 
127  |  |   struct StoredDataObject : public MemoryRetainer { | 
128  |  |     BaseObjectPtr<Blob> blob;  | 
129  |  |     size_t length;  | 
130  |  |     std::string type;  | 
131  |  |  | 
132  | 0  |     StoredDataObject() = default;  | 
133  |  |  | 
134  |  |     StoredDataObject(  | 
135  |  |         const BaseObjectPtr<Blob>& blob_,  | 
136  |  |         size_t length_,  | 
137  |  |         const std::string& type_);  | 
138  |  |  | 
139  |  |     void MemoryInfo(MemoryTracker* tracker) const override;  | 
140  |  |     SET_SELF_SIZE(StoredDataObject)  | 
141  |  |     SET_MEMORY_INFO_NAME(StoredDataObject)  | 
142  |  |   };  | 
143  |  |  | 
144  |  |   void store_data_object(  | 
145  |  |       const std::string& uuid,  | 
146  |  |       const StoredDataObject& object);  | 
147  |  |  | 
148  |  |   void revoke_data_object(const std::string& uuid);  | 
149  |  |  | 
150  |  |   StoredDataObject get_data_object(const std::string& uuid);  | 
151  |  |  | 
152  |  |  private:  | 
153  |  |   std::unordered_map<std::string, StoredDataObject> data_objects_;  | 
154  |  | };  | 
155  |  |  | 
156  |  | }  // namespace node  | 
157  |  |  | 
158  |  | #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS  | 
159  |  | #endif  // SRC_NODE_BLOB_H_  |