jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 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 | #ifndef FileReaderLoader_h |
| 32 | #define FileReaderLoader_h |
| 33 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 34 | #include "FileError.h" |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 35 | #include "URL.h" |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 36 | #include "TextEncoding.h" |
| 37 | #include "ThreadableLoaderClient.h" |
| 38 | #include <wtf/Forward.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
oliver@apple.com | df60608 | 2013-08-05 22:11:50 +0000 | [diff] [blame] | 41 | namespace JSC { |
| 42 | class ArrayBuffer; |
| 43 | } |
| 44 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 45 | namespace WebCore { |
| 46 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 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, |
commit-queue@webkit.org | 5b69f17 | 2013-02-22 10:03:40 +0000 | [diff] [blame] | 58 | ReadAsBlob, |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 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 |
pfeldman@chromium.org | c2baad0 | 2011-06-16 16:53:03 +0000 | [diff] [blame] | 71 | virtual void didReceiveResponse(unsigned long, const ResourceResponse&); |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 72 | virtual void didReceiveData(const char*, int); |
commit-queue@webkit.org | 7c4021c | 2011-02-17 04:11:38 +0000 | [diff] [blame] | 73 | virtual void didFinishLoading(unsigned long, double); |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 74 | virtual void didFail(const ResourceError&); |
| 75 | |
| 76 | String stringResult(); |
gyuyoung.kim@webkit.org | 862a586 | 2015-09-08 10:50:42 +0000 | [diff] [blame] | 77 | RefPtr<JSC::ArrayBuffer> arrayBufferResult() const; |
darin@apple.com | f581c55 | 2010-12-01 01:53:21 +0000 | [diff] [blame] | 78 | unsigned bytesLoaded() const { return m_bytesLoaded; } |
| 79 | unsigned totalBytes() const { return m_totalBytes; } |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 80 | int errorCode() const { return m_errorCode; } |
| 81 | |
| 82 | void setEncoding(const String&); |
| 83 | void setDataType(const String& dataType) { m_dataType = dataType; } |
commit-queue@webkit.org | 5b69f17 | 2013-02-22 10:03:40 +0000 | [diff] [blame] | 84 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 85 | private: |
| 86 | void terminate(); |
| 87 | void cleanup(); |
| 88 | void failed(int errorCode); |
| 89 | void convertToText(); |
| 90 | void convertToDataURL(); |
| 91 | |
| 92 | bool isCompleted() const; |
| 93 | |
| 94 | static FileError::ErrorCode httpStatusCodeToErrorCode(int); |
| 95 | |
| 96 | ReadType m_readType; |
| 97 | FileReaderLoaderClient* m_client; |
| 98 | TextEncoding m_encoding; |
| 99 | String m_dataType; |
| 100 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 101 | URL m_urlForReading; |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 102 | RefPtr<ThreadableLoader> m_loader; |
| 103 | |
oliver@apple.com | df60608 | 2013-08-05 22:11:50 +0000 | [diff] [blame] | 104 | RefPtr<JSC::ArrayBuffer> m_rawData; |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 105 | bool m_isRawDataConverted; |
| 106 | |
| 107 | String m_stringResult; |
commit-queue@webkit.org | 5b69f17 | 2013-02-22 10:03:40 +0000 | [diff] [blame] | 108 | RefPtr<Blob> m_blobResult; |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 109 | |
| 110 | // The decoder used to decode the text data. |
| 111 | RefPtr<TextResourceDecoder> m_decoder; |
| 112 | |
commit-queue@webkit.org | 5b69f17 | 2013-02-22 10:03:40 +0000 | [diff] [blame] | 113 | bool m_variableLength; |
darin@apple.com | f581c55 | 2010-12-01 01:53:21 +0000 | [diff] [blame] | 114 | unsigned m_bytesLoaded; |
| 115 | unsigned m_totalBytes; |
commit-queue@webkit.org | 5b69f17 | 2013-02-22 10:03:40 +0000 | [diff] [blame] | 116 | |
| 117 | bool m_hasRange; |
| 118 | unsigned m_rangeStart; |
| 119 | unsigned m_rangeEnd; |
| 120 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 121 | int m_errorCode; |
| 122 | }; |
| 123 | |
| 124 | } // namespace WebCore |
| 125 | |
jianli@chromium.org | a8c47de | 2010-10-29 18:23:28 +0000 | [diff] [blame] | 126 | #endif // FileReaderLoader_h |