summaryrefslogtreecommitdiff
path: root/Source/WebCore/fileapi/FileReaderLoader.cpp
diff options
context:
space:
mode:
authorKonstantin Tokarev <annulen@yandex.ru>2016-08-25 19:20:41 +0300
committerKonstantin Tokarev <annulen@yandex.ru>2017-02-02 12:30:55 +0000
commit6882a04fb36642862b11efe514251d32070c3d65 (patch)
treeb7959826000b061fd5ccc7512035c7478742f7b0 /Source/WebCore/fileapi/FileReaderLoader.cpp
parentab6df191029eeeb0b0f16f127d553265659f739e (diff)
downloadqtwebkit-6882a04fb36642862b11efe514251d32070c3d65.tar.gz
Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443)
Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
Diffstat (limited to 'Source/WebCore/fileapi/FileReaderLoader.cpp')
-rw-r--r--Source/WebCore/fileapi/FileReaderLoader.cpp108
1 files changed, 38 insertions, 70 deletions
diff --git a/Source/WebCore/fileapi/FileReaderLoader.cpp b/Source/WebCore/fileapi/FileReaderLoader.cpp
index 77c05fe81..a80a2ab12 100644
--- a/Source/WebCore/fileapi/FileReaderLoader.cpp
+++ b/Source/WebCore/fileapi/FileReaderLoader.cpp
@@ -30,28 +30,24 @@
#include "config.h"
-#if ENABLE(BLOB)
-
#include "FileReaderLoader.h"
#include "Blob.h"
#include "BlobURL.h"
#include "FileReaderLoaderClient.h"
+#include "HTTPHeaderNames.h"
#include "ResourceRequest.h"
#include "ResourceResponse.h"
#include "ScriptExecutionContext.h"
#include "TextResourceDecoder.h"
#include "ThreadableBlobRegistry.h"
#include "ThreadableLoader.h"
-#include <wtf/ArrayBuffer.h>
-#include <wtf/PassRefPtr.h>
+#include <runtime/ArrayBuffer.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/Base64.h>
#include <wtf/text/StringBuilder.h>
-using namespace std;
-
namespace WebCore {
const int defaultBufferLength = 32768;
@@ -92,14 +88,16 @@ void FileReaderLoader::start(ScriptExecutionContext* scriptExecutionContext, Blo
ResourceRequest request(m_urlForReading);
request.setHTTPMethod("GET");
if (m_hasRange)
- request.setHTTPHeaderField("Range", String::format("bytes=%d-%d", m_rangeStart, m_rangeEnd));
+ request.setHTTPHeaderField(HTTPHeaderName::Range, String::format("bytes=%d-%d", m_rangeStart, m_rangeEnd));
ThreadableLoaderOptions options;
- options.sendLoadCallbacks = SendCallbacks;
- options.sniffContent = DoNotSniffContent;
+ options.setSendLoadCallbacks(SendCallbacks);
+ options.setSniffContent(DoNotSniffContent);
+ options.setDataBufferingPolicy(DoNotBufferData);
options.preflightPolicy = ConsiderPreflight;
- options.allowCredentials = AllowStoredCredentials;
+ options.setAllowCredentials(AllowStoredCredentials);
options.crossOriginRequestPolicy = DenyCrossOriginRequests;
+ options.contentSecurityPolicyEnforcement = ContentSecurityPolicyEnforcement::DoNotEnforce;
if (m_client)
m_loader = ThreadableLoader::create(scriptExecutionContext, this, request, options);
@@ -123,11 +121,11 @@ void FileReaderLoader::terminate()
void FileReaderLoader::cleanup()
{
- m_loader = 0;
+ m_loader = nullptr;
// If we get any error, we do not need to keep a buffer around.
if (m_errorCode) {
- m_rawData = 0;
+ m_rawData = nullptr;
m_stringResult = "";
}
}
@@ -139,11 +137,10 @@ void FileReaderLoader::didReceiveResponse(unsigned long, const ResourceResponse&
return;
}
- unsigned long long length = response.expectedContentLength();
+ long long length = response.expectedContentLength();
- // A value larger than INT_MAX means that the content length wasn't
- // specified, so the buffer will need to be dynamically grown.
- if (length > INT_MAX) {
+ // A negative value means that the content length wasn't specified, so the buffer will need to be dynamically grown.
+ if (length < 0) {
m_variableLength = true;
if (m_hasRange)
length = 1 + m_rangeEnd - m_rangeStart;
@@ -154,7 +151,7 @@ void FileReaderLoader::didReceiveResponse(unsigned long, const ResourceResponse&
// Check that we can cast to unsigned since we have to do
// so to call ArrayBuffer's create function.
// FIXME: Support reading more than the current size limit of ArrayBuffer.
- if (length > numeric_limits<unsigned>::max()) {
+ if (length > std::numeric_limits<unsigned>::max()) {
failed(FileError::NOT_READABLE_ERR);
return;
}
@@ -186,22 +183,31 @@ void FileReaderLoader::didReceiveData(const char* data, int dataLength)
unsigned remainingBufferSpace = m_totalBytes - m_bytesLoaded;
if (length > static_cast<long long>(remainingBufferSpace)) {
// If the buffer has hit maximum size, it can't be grown any more.
- if (m_totalBytes >= numeric_limits<unsigned>::max()) {
+ if (m_totalBytes >= std::numeric_limits<unsigned>::max()) {
failed(FileError::NOT_READABLE_ERR);
return;
}
if (m_variableLength) {
- unsigned long long newLength = m_totalBytes * 2;
- if (newLength > numeric_limits<unsigned>::max())
- newLength = numeric_limits<unsigned>::max();
- RefPtr<ArrayBuffer> newData =
- ArrayBuffer::create(static_cast<unsigned>(newLength), 1);
+ unsigned newLength = m_totalBytes + static_cast<unsigned>(dataLength);
+ if (newLength < m_totalBytes) {
+ failed(FileError::NOT_READABLE_ERR);
+ return;
+ }
+ newLength = std::max(newLength, m_totalBytes + m_totalBytes / 4 + 1);
+ RefPtr<ArrayBuffer> newData = ArrayBuffer::create(newLength, 1);
+ if (!newData) {
+ // Not enough memory.
+ failed(FileError::NOT_READABLE_ERR);
+ return;
+ }
memcpy(static_cast<char*>(newData->data()), static_cast<char*>(m_rawData->data()), m_bytesLoaded);
m_rawData = newData;
m_totalBytes = static_cast<unsigned>(newLength);
- } else
+ } else {
+ // This can only happen if we get more data than indicated in expected content length (i.e. never, unless the networking layer is buggy).
length = remainingBufferSpace;
+ }
}
if (length <= 0)
@@ -258,13 +264,13 @@ FileError::ErrorCode FileReaderLoader::httpStatusCodeToErrorCode(int httpStatusC
}
}
-PassRefPtr<ArrayBuffer> FileReaderLoader::arrayBufferResult() const
+RefPtr<ArrayBuffer> FileReaderLoader::arrayBufferResult() const
{
ASSERT(m_readType == ReadAsArrayBuffer);
// If the loading is not started or an error occurs, return an empty result.
if (!m_rawData || m_errorCode)
- return 0;
+ return nullptr;
// If completed, we can simply return our buffer.
if (isCompleted())
@@ -274,29 +280,6 @@ PassRefPtr<ArrayBuffer> FileReaderLoader::arrayBufferResult() const
return ArrayBuffer::create(m_rawData.get());
}
-#if ENABLE(STREAM)
-PassRefPtr<Blob> FileReaderLoader::blobResult()
-{
- ASSERT(m_readType == ReadAsBlob);
-
- // If the loading is not finished or an error occurs, return an empty result.
- if (!m_rawData || m_errorCode || !isCompleted())
- return 0;
-
- if (!m_blobResult) {
- OwnPtr<BlobData> blobData = BlobData::create();
- size_t size = 0;
- RefPtr<RawData> rawData = RawData::create();
- size = m_rawData->byteLength();
- rawData->mutableData()->append(static_cast<char*>(m_rawData->data()), size);
- blobData->appendData(rawData, 0, size);
- blobData->setContentType(m_dataType);
- m_blobResult = Blob::create(blobData.release(), size);
- }
- return m_blobResult;
-}
-#endif // ENABLE(STREAM)
-
String FileReaderLoader::stringResult()
{
ASSERT(m_readType != ReadAsArrayBuffer && m_readType != ReadAsBlob);
@@ -341,21 +324,18 @@ void FileReaderLoader::convertToText()
// requirement in order to be consistent with how WebKit decodes the web content: always has the BOM override the
// provided encoding.
// FIXME: consider supporting incremental decoding to improve the perf.
- StringBuilder builder;
if (!m_decoder)
m_decoder = TextResourceDecoder::create("text/plain", m_encoding.isValid() ? m_encoding : UTF8Encoding());
- builder.append(m_decoder->decode(static_cast<const char*>(m_rawData->data()), m_bytesLoaded));
-
if (isCompleted())
- builder.append(m_decoder->flush());
-
- m_stringResult = builder.toString();
+ m_stringResult = m_decoder->decodeAndFlush(static_cast<const char*>(m_rawData->data()), m_bytesLoaded);
+ else
+ m_stringResult = m_decoder->decode(static_cast<const char*>(m_rawData->data()), m_bytesLoaded);
}
void FileReaderLoader::convertToDataURL()
{
StringBuilder builder;
- builder.append("data:");
+ builder.appendLiteral("data:");
if (!m_bytesLoaded) {
m_stringResult = builder.toString();
@@ -363,10 +343,10 @@ void FileReaderLoader::convertToDataURL()
}
builder.append(m_dataType);
- builder.append(";base64,");
+ builder.appendLiteral(";base64,");
Vector<char> out;
- base64Encode(static_cast<const char*>(m_rawData->data()), m_bytesLoaded, out);
+ base64Encode(m_rawData->data(), m_bytesLoaded, out);
out.append('\0');
builder.append(out.data());
@@ -384,16 +364,4 @@ void FileReaderLoader::setEncoding(const String& encoding)
m_encoding = TextEncoding(encoding);
}
-#if ENABLE(STREAM)
-void FileReaderLoader::setRange(unsigned start, unsigned length)
-{
- ASSERT(length > 0);
- m_hasRange = true;
- m_rangeStart = start;
- m_rangeEnd = start + length - 1;
-}
-#endif // ENABLE(STREAM)
-
} // namespace WebCore
-
-#endif // ENABLE(BLOB)