-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
blob.cc
182 lines (153 loc) · 5.65 KB
/
blob.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "blob.h"
#include <modp_b64/modp_b64.h>
#include <string>
#include "bindings/qjs/script_promise_resolver.h"
#include "built_in_string.h"
#include "core/executing_context.h"
namespace webf {
class BlobReaderClient {
public:
enum ReadType { kReadAsText, kReadAsArrayBuffer, kReadAsBase64 };
BlobReaderClient(ExecutingContext* context,
Blob* blob,
std::shared_ptr<ScriptPromiseResolver> resolver,
ReadType read_type)
: context_(context), blob_(blob), resolver_(std::move(resolver)), read_type_(read_type) {
Start();
};
void Start();
void DidFinishLoading();
private:
ExecutingContext* context_;
Blob* blob_;
std::shared_ptr<ScriptPromiseResolver> resolver_;
ReadType read_type_;
};
void BlobReaderClient::Start() {
DidFinishLoading();
}
void BlobReaderClient::DidFinishLoading() {
if (read_type_ == ReadType::kReadAsText) {
resolver_->Resolve<std::string>(blob_->StringResult());
} else if (read_type_ == ReadType::kReadAsArrayBuffer) {
resolver_->Resolve<ArrayBufferData>(blob_->ArrayBufferResult());
} else if (read_type_ == ReadType::kReadAsBase64) {
resolver_->Resolve<std::string>(blob_->Base64Result());
}
delete this;
}
Blob* Blob::Create(ExecutingContext* context, ExceptionState& exception_state) {
return MakeGarbageCollected<Blob>(context->ctx());
}
Blob* Blob::Create(ExecutingContext* context) {
return MakeGarbageCollected<Blob>(context->ctx());
}
Blob* Blob::Create(ExecutingContext* context,
std::vector<std::shared_ptr<BlobPart>>& data,
ExceptionState& exception_state) {
return MakeGarbageCollected<Blob>(context->ctx(), data);
}
Blob* Blob::Create(ExecutingContext* context,
std::vector<std::shared_ptr<BlobPart>>& data,
std::shared_ptr<BlobPropertyBag> property,
ExceptionState& exception_state) {
return MakeGarbageCollected<Blob>(context->ctx(), data, property);
}
int32_t Blob::size() {
return _data.size();
}
uint8_t* Blob::bytes() {
return _data.data();
}
void Blob::Trace(GCVisitor* visitor) const {}
Blob* Blob::slice(ExceptionState& exception_state) {
return slice(0, _data.size(), exception_state);
}
Blob* Blob::slice(int64_t start, ExceptionState& exception_state) {
return slice(start, _data.size(), exception_state);
}
Blob* Blob::slice(int64_t start, int64_t end, ExceptionState& exception_state) {
return slice(start, end, AtomicString::Empty(), exception_state);
}
Blob* Blob::slice(int64_t start, int64_t end, const AtomicString& content_type, ExceptionState& exception_state) {
auto* newBlob = MakeGarbageCollected<Blob>(ctx());
std::vector<uint8_t> newData;
newData.reserve(_data.size() - (end - start));
newData.insert(newData.begin(), _data.begin() + start, _data.end() - (_data.size() - end));
newBlob->_data = newData;
newBlob->mime_type_ = content_type != built_in_string::kempty_string ? content_type.ToStdString(ctx()) : mime_type_;
return newBlob;
}
std::string Blob::StringResult() {
return std::string(bytes(), bytes() + size());
}
std::string Blob::Base64Result() {
size_t encode_len = modp_b64_encode_data_len(size());
std::string buffer;
buffer.resize(encode_len);
const size_t output_size =
modp_b64_encode_data(reinterpret_cast<char*>(buffer.data()), reinterpret_cast<const char*>(bytes()), size());
assert(output_size == encode_len);
return "data:" + mime_type_ + ";base64," + buffer;
}
ArrayBufferData Blob::ArrayBufferResult() {
return ArrayBufferData{bytes(), size()};
}
std::string Blob::type() {
return mime_type_;
}
void Blob::SetMineType(const std::string& mine_type) {
mime_type_ = mine_type;
}
ScriptPromise Blob::arrayBuffer(ExceptionState& exception_state) {
auto resolver = ScriptPromiseResolver::Create(GetExecutingContext());
new BlobReaderClient(GetExecutingContext(), this, resolver, BlobReaderClient::ReadType::kReadAsArrayBuffer);
return resolver->Promise();
}
ScriptPromise Blob::text(ExceptionState& exception_state) {
auto resolver = ScriptPromiseResolver::Create(GetExecutingContext());
new BlobReaderClient(GetExecutingContext(), this, resolver, BlobReaderClient::ReadType::kReadAsText);
return resolver->Promise();
}
ScriptPromise Blob::base64(ExceptionState& exception_state) {
auto resolver = ScriptPromiseResolver::Create(GetExecutingContext());
new BlobReaderClient(GetExecutingContext(), this, resolver, BlobReaderClient::ReadType::kReadAsBase64);
return resolver->Promise();
}
void Blob::PopulateBlobData(const std::vector<std::shared_ptr<BlobPart>>& data) {
for (auto& item : data) {
switch (item->GetContentType()) {
case BlobPart::ContentType::kString: {
AppendText(item->GetString());
break;
}
case BlobPart::ContentType::kArrayBuffer:
case BlobPart::ContentType::kArrayBufferView: {
uint32_t length;
uint8_t* buffer = item->GetBytes(&length);
AppendBytes(buffer, length);
break;
}
case BlobPart::ContentType::kBlob: {
AppendBytes(item->GetBlob()->bytes(), item->GetBlob()->size());
break;
}
}
}
}
void Blob::AppendText(const std::string& string) {
std::vector<uint8_t> strArr(string.begin(), string.end());
_data.reserve(_data.size() + strArr.size());
_data.insert(_data.end(), strArr.begin(), strArr.end());
}
void Blob::AppendBytes(uint8_t* buffer, uint32_t length) {
_data.reserve(_data.size() + length);
for (size_t i = 0; i < length; i++) {
_data.emplace_back(buffer[i]);
}
}
} // namespace webf