2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef FileReaderLoader_h
32 #define FileReaderLoader_h
34 #include "core/fileapi/FileError.h"
35 #include "core/loader/ThreadableLoaderClient.h"
36 #include "platform/weborigin/KURL.h"
37 #include "wtf/ArrayBuffer.h"
38 #include "wtf/ArrayBufferBuilder.h"
39 #include "wtf/Forward.h"
40 #include "wtf/OwnPtr.h"
41 #include "wtf/text/TextEncoding.h"
42 #include "wtf/text/WTFString.h"
47 class FileReaderLoaderClient;
48 class ExecutionContext;
50 class TextResourceDecoder;
51 class ThreadableLoader;
53 class FileReaderLoader final : public ThreadableLoaderClient {
64 // If client is given, do the loading asynchronously. Otherwise, load synchronously.
65 FileReaderLoader(ReadType, FileReaderLoaderClient*);
66 virtual ~FileReaderLoader();
68 void start(ExecutionContext*, PassRefPtr<BlobDataHandle>);
69 void start(ExecutionContext*, const Stream&, unsigned readSize);
72 // ThreadableLoaderClient
73 virtual void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<WebDataConsumerHandle>) override;
74 virtual void didReceiveData(const char*, unsigned) override;
75 virtual void didFinishLoading(unsigned long, double) override;
76 virtual void didFail(const ResourceError&) override;
78 String stringResult();
79 PassRefPtr<ArrayBuffer> arrayBufferResult() const;
81 // Returns the total bytes received. Bytes ignored by m_rawData won't be
84 // This value doesn't grow more than numeric_limits<unsigned> when
85 // m_readType is not set to ReadByClient.
86 long long bytesLoaded() const { return m_bytesLoaded; }
88 // Before didReceiveResponse() is called: Returns -1.
89 // After didReceiveResponse() is called:
90 // - If the size of the resource is known (from
91 // m_response.expectedContentLength() or once didFinishLoading() is
92 // called), returns it.
93 // - Otherwise, returns -1.
94 long long totalBytes() const { return m_totalBytes; }
96 FileError::ErrorCode errorCode() const { return m_errorCode; }
98 void setEncoding(const String&);
99 void setDataType(const String& dataType) { m_dataType = dataType; }
102 void startInternal(ExecutionContext&, const Stream*, PassRefPtr<BlobDataHandle>);
106 void failed(FileError::ErrorCode);
107 void convertToText();
108 void convertToDataURL();
110 static FileError::ErrorCode httpStatusCodeToErrorCode(int);
113 FileReaderLoaderClient* m_client;
114 WTF::TextEncoding m_encoding;
117 KURL m_urlForReading;
118 bool m_urlForReadingIsStream;
119 RefPtr<ThreadableLoader> m_loader;
121 OwnPtr<ArrayBufferBuilder> m_rawData;
122 bool m_isRawDataConverted;
124 String m_stringResult;
126 // The decoder used to decode the text data.
127 OwnPtr<TextResourceDecoder> m_decoder;
129 bool m_finishedLoading;
130 long long m_bytesLoaded;
131 // If the total size of the resource is unknown, m_totalBytes is set to -1
132 // until completion of loading, and the buffer for receiving data is set to
133 // dynamically grow. Otherwise, m_totalBytes is set to the total size and
134 // the buffer for receiving data of m_totalBytes is allocated and never grow
135 // even when extra data is appeneded.
136 long long m_totalBytes;
139 unsigned m_rangeStart;
142 FileError::ErrorCode m_errorCode;
147 #endif // FileReaderLoader_h