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 | #pragma once |
32 | |
33 | #include "BlobResourceHandle.h" |
34 | #include "FileError.h" |
35 | #include <wtf/URL.h> |
36 | #include "TextEncoding.h" |
37 | #include "ThreadableLoaderClient.h" |
38 | #include <wtf/Forward.h> |
39 | #include <wtf/text/WTFString.h> |
40 | |
41 | namespace JSC { |
42 | class ArrayBuffer; |
43 | } |
44 | |
45 | namespace WebCore { |
46 | |
47 | class Blob; |
48 | class FileReaderLoaderClient; |
49 | class ScriptExecutionContext; |
50 | class TextResourceDecoder; |
51 | class ThreadableLoader; |
52 | |
53 | class FileReaderLoader : public ThreadableLoaderClient { |
54 | public: |
55 | enum ReadType { |
56 | ReadAsArrayBuffer, |
57 | ReadAsBinaryString, |
58 | ReadAsBlob, |
59 | ReadAsText, |
60 | ReadAsDataURL |
61 | }; |
62 | |
63 | // If client is given, do the loading asynchronously. Otherwise, load synchronously. |
64 | FileReaderLoader(ReadType, FileReaderLoaderClient*); |
65 | ~FileReaderLoader(); |
66 | |
67 | void start(ScriptExecutionContext*, Blob&); |
68 | void cancel(); |
69 | |
70 | // ThreadableLoaderClient |
71 | void didReceiveResponse(unsigned long, const ResourceResponse&) override; |
72 | void didReceiveData(const char*, int) override; |
73 | void didFinishLoading(unsigned long) override; |
74 | void didFail(const ResourceError&) override; |
75 | |
76 | String stringResult(); |
77 | RefPtr<JSC::ArrayBuffer> arrayBufferResult() const; |
78 | unsigned bytesLoaded() const { return m_bytesLoaded; } |
79 | unsigned totalBytes() const { return m_totalBytes; } |
80 | FileError::ErrorCode errorCode() const { return m_errorCode; } |
81 | |
82 | void setEncoding(const String&); |
83 | void setDataType(const String& dataType) { m_dataType = dataType; } |
84 | |
85 | const URL& url() { return m_urlForReading; } |
86 | |
87 | private: |
88 | void terminate(); |
89 | void cleanup(); |
90 | void failed(FileError::ErrorCode); |
91 | void convertToText(); |
92 | void convertToDataURL(); |
93 | |
94 | bool isCompleted() const; |
95 | |
96 | static FileError::ErrorCode httpStatusCodeToErrorCode(int); |
97 | static FileError::ErrorCode toErrorCode(BlobResourceHandle::Error); |
98 | |
99 | ReadType m_readType; |
100 | FileReaderLoaderClient* m_client; |
101 | TextEncoding m_encoding; |
102 | String m_dataType; |
103 | |
104 | URL m_urlForReading; |
105 | RefPtr<ThreadableLoader> m_loader; |
106 | |
107 | RefPtr<JSC::ArrayBuffer> m_rawData; |
108 | bool m_isRawDataConverted; |
109 | |
110 | String m_stringResult; |
111 | RefPtr<Blob> m_blobResult; |
112 | |
113 | // The decoder used to decode the text data. |
114 | RefPtr<TextResourceDecoder> m_decoder; |
115 | |
116 | bool m_variableLength; |
117 | unsigned m_bytesLoaded; |
118 | unsigned m_totalBytes; |
119 | |
120 | FileError::ErrorCode m_errorCode { FileError::OK }; |
121 | }; |
122 | |
123 | } // namespace WebCore |
124 | |