1 | /*
|
---|
2 | * Copyright (C) 2010 Google Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions are
|
---|
6 | * met:
|
---|
7 | *
|
---|
8 | * * Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * * Redistributions in binary form must reproduce the above
|
---|
11 | * copyright notice, this list of conditions and the following disclaimer
|
---|
12 | * in the documentation and/or other materials provided with the
|
---|
13 | * distribution.
|
---|
14 | * * Neither the name of Google Inc. nor the names of its
|
---|
15 | * contributors may be used to endorse or promote products derived from
|
---|
16 | * this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include "config.h"
|
---|
32 | #include "FileReader.h"
|
---|
33 |
|
---|
34 | #include "DOMException.h"
|
---|
35 | #include "EventLoop.h"
|
---|
36 | #include "EventNames.h"
|
---|
37 | #include "Exception.h"
|
---|
38 | #include "ExceptionCode.h"
|
---|
39 | #include "File.h"
|
---|
40 | #include "Logging.h"
|
---|
41 | #include "ProgressEvent.h"
|
---|
42 | #include "ScriptExecutionContext.h"
|
---|
43 | #include <JavaScriptCore/ArrayBuffer.h>
|
---|
44 | #include <wtf/IsoMallocInlines.h>
|
---|
45 | #include <wtf/text/CString.h>
|
---|
46 |
|
---|
47 | namespace WebCore {
|
---|
48 |
|
---|
49 | WTF_MAKE_ISO_ALLOCATED_IMPL(FileReader);
|
---|
50 |
|
---|
51 | // Fire the progress event at least every 50ms.
|
---|
52 | static const auto progressNotificationInterval = 50_ms;
|
---|
53 |
|
---|
54 | Ref<FileReader> FileReader::create(ScriptExecutionContext& context)
|
---|
55 | {
|
---|
56 | auto fileReader = adoptRef(*new FileReader(context));
|
---|
57 | fileReader->suspendIfNeeded();
|
---|
58 | return fileReader;
|
---|
59 | }
|
---|
60 |
|
---|
61 | FileReader::FileReader(ScriptExecutionContext& context)
|
---|
62 | : ActiveDOMObject(&context)
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | FileReader::~FileReader()
|
---|
67 | {
|
---|
68 | if (m_loader)
|
---|
69 | m_loader->cancel();
|
---|
70 | }
|
---|
71 |
|
---|
72 | const char* FileReader::activeDOMObjectName() const
|
---|
73 | {
|
---|
74 | return "FileReader";
|
---|
75 | }
|
---|
76 |
|
---|
77 | void FileReader::stop()
|
---|
78 | {
|
---|
79 | m_pendingTasks.clear();
|
---|
80 | if (m_loader) {
|
---|
81 | m_loader->cancel();
|
---|
82 | m_loader = nullptr;
|
---|
83 | }
|
---|
84 | m_state = DONE;
|
---|
85 | }
|
---|
86 |
|
---|
87 | bool FileReader::virtualHasPendingActivity() const
|
---|
88 | {
|
---|
89 | return m_state == LOADING;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ExceptionOr<void> FileReader::readAsArrayBuffer(Blob& blob)
|
---|
93 | {
|
---|
94 | LOG(FileAPI, "FileReader: reading as array buffer: %s %s\n", blob.url().string().utf8().data(), is<File>(blob) ? downcast<File>(blob).path().utf8().data() : "");
|
---|
95 |
|
---|
96 | return readInternal(blob, FileReaderLoader::ReadAsArrayBuffer);
|
---|
97 | }
|
---|
98 |
|
---|
99 | ExceptionOr<void> FileReader::readAsBinaryString(Blob& blob)
|
---|
100 | {
|
---|
101 | LOG(FileAPI, "FileReader: reading as binary: %s %s\n", blob.url().string().utf8().data(), is<File>(blob) ? downcast<File>(blob).path().utf8().data() : "");
|
---|
102 |
|
---|
103 | return readInternal(blob, FileReaderLoader::ReadAsBinaryString);
|
---|
104 | }
|
---|
105 |
|
---|
106 | ExceptionOr<void> FileReader::readAsText(Blob& blob, const String& encoding)
|
---|
107 | {
|
---|
108 | LOG(FileAPI, "FileReader: reading as text: %s %s\n", blob.url().string().utf8().data(), is<File>(blob) ? downcast<File>(blob).path().utf8().data() : "");
|
---|
109 |
|
---|
110 | m_encoding = encoding;
|
---|
111 | return readInternal(blob, FileReaderLoader::ReadAsText);
|
---|
112 | }
|
---|
113 |
|
---|
114 | ExceptionOr<void> FileReader::readAsDataURL(Blob& blob)
|
---|
115 | {
|
---|
116 | LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", blob.url().string().utf8().data(), is<File>(blob) ? downcast<File>(blob).path().utf8().data() : "");
|
---|
117 |
|
---|
118 | return readInternal(blob, FileReaderLoader::ReadAsDataURL);
|
---|
119 | }
|
---|
120 |
|
---|
121 | ExceptionOr<void> FileReader::readInternal(Blob& blob, FileReaderLoader::ReadType type)
|
---|
122 | {
|
---|
123 | // If multiple concurrent read methods are called on the same FileReader, InvalidStateError should be thrown when the state is LOADING.
|
---|
124 | if (m_state == LOADING)
|
---|
125 | return Exception { InvalidStateError };
|
---|
126 |
|
---|
127 | m_blob = &blob;
|
---|
128 | m_readType = type;
|
---|
129 | m_state = LOADING;
|
---|
130 | m_error = nullptr;
|
---|
131 |
|
---|
132 | m_loader = makeUnique<FileReaderLoader>(m_readType, static_cast<FileReaderLoaderClient*>(this));
|
---|
133 | m_loader->setEncoding(m_encoding);
|
---|
134 | m_loader->setDataType(m_blob->type());
|
---|
135 | m_loader->start(scriptExecutionContext(), blob);
|
---|
136 |
|
---|
137 | return { };
|
---|
138 | }
|
---|
139 |
|
---|
140 | void FileReader::abort()
|
---|
141 | {
|
---|
142 | LOG(FileAPI, "FileReader: aborting\n");
|
---|
143 | if (m_state != LOADING || m_finishedLoading)
|
---|
144 | return;
|
---|
145 |
|
---|
146 | m_pendingTasks.clear();
|
---|
147 | stop();
|
---|
148 | m_error = DOMException::create(Exception { AbortError });
|
---|
149 |
|
---|
150 | Ref protectedThis { *this };
|
---|
151 | fireEvent(eventNames().abortEvent);
|
---|
152 | fireEvent(eventNames().loadendEvent);
|
---|
153 | }
|
---|
154 |
|
---|
155 | void FileReader::didStartLoading()
|
---|
156 | {
|
---|
157 | enqueueTask([this] {
|
---|
158 | fireEvent(eventNames().loadstartEvent);
|
---|
159 | });
|
---|
160 | }
|
---|
161 |
|
---|
162 | void FileReader::didReceiveData()
|
---|
163 | {
|
---|
164 | enqueueTask([this] {
|
---|
165 | auto now = MonotonicTime::now();
|
---|
166 | if (std::isnan(m_lastProgressNotificationTime)) {
|
---|
167 | m_lastProgressNotificationTime = now;
|
---|
168 | return;
|
---|
169 | }
|
---|
170 | if (now - m_lastProgressNotificationTime > progressNotificationInterval) {
|
---|
171 | fireEvent(eventNames().progressEvent);
|
---|
172 | m_lastProgressNotificationTime = now;
|
---|
173 | }
|
---|
174 | });
|
---|
175 | }
|
---|
176 |
|
---|
177 | void FileReader::didFinishLoading()
|
---|
178 | {
|
---|
179 | enqueueTask([this] {
|
---|
180 | if (m_state == DONE)
|
---|
181 | return;
|
---|
182 | m_finishedLoading = true;
|
---|
183 | fireEvent(eventNames().progressEvent);
|
---|
184 | if (m_state == DONE)
|
---|
185 | return;
|
---|
186 | m_state = DONE;
|
---|
187 | fireEvent(eventNames().loadEvent);
|
---|
188 | fireEvent(eventNames().loadendEvent);
|
---|
189 | });
|
---|
190 | }
|
---|
191 |
|
---|
192 | void FileReader::didFail(ExceptionCode errorCode)
|
---|
193 | {
|
---|
194 | enqueueTask([this, errorCode] {
|
---|
195 | if (m_state == DONE)
|
---|
196 | return;
|
---|
197 | m_state = DONE;
|
---|
198 |
|
---|
199 | m_error = DOMException::create(Exception { errorCode });
|
---|
200 |
|
---|
201 | fireEvent(eventNames().errorEvent);
|
---|
202 | fireEvent(eventNames().loadendEvent);
|
---|
203 | });
|
---|
204 | }
|
---|
205 |
|
---|
206 | void FileReader::fireEvent(const AtomString& type)
|
---|
207 | {
|
---|
208 | ASSERT(isAllowedToRunScript());
|
---|
209 | dispatchEvent(ProgressEvent::create(type, true, m_loader ? m_loader->bytesLoaded() : 0, m_loader ? m_loader->totalBytes() : 0));
|
---|
210 | }
|
---|
211 |
|
---|
212 | std::optional<std::variant<String, RefPtr<JSC::ArrayBuffer>>> FileReader::result() const
|
---|
213 | {
|
---|
214 | if (!m_loader || m_error || m_state != DONE)
|
---|
215 | return std::nullopt;
|
---|
216 | if (m_readType == FileReaderLoader::ReadAsArrayBuffer) {
|
---|
217 | auto result = m_loader->arrayBufferResult();
|
---|
218 | if (!result)
|
---|
219 | return std::nullopt;
|
---|
220 | return { result };
|
---|
221 | }
|
---|
222 | String result = m_loader->stringResult();
|
---|
223 | if (result.isNull())
|
---|
224 | return std::nullopt;
|
---|
225 | return { WTFMove(result) };
|
---|
226 | }
|
---|
227 |
|
---|
228 | void FileReader::enqueueTask(Function<void()>&& task)
|
---|
229 | {
|
---|
230 | if (!scriptExecutionContext())
|
---|
231 | return;
|
---|
232 |
|
---|
233 | static uint64_t taskIdentifierSeed = 0;
|
---|
234 | uint64_t taskIdentifier = ++taskIdentifierSeed;
|
---|
235 | m_pendingTasks.add(taskIdentifier, WTFMove(task));
|
---|
236 | queueTaskKeepingObjectAlive(*this, TaskSource::FileReading, [this, pendingActivity = makePendingActivity(*this), taskIdentifier] {
|
---|
237 | auto task = m_pendingTasks.take(taskIdentifier);
|
---|
238 | if (task && !isContextStopped())
|
---|
239 | task();
|
---|
240 | });
|
---|
241 | }
|
---|
242 |
|
---|
243 | } // namespace WebCore
|
---|