blob: 0bdf08c88e73520f087c1b9d046fbd81b246e965 [file] [log] [blame]
jianli@chromium.orga8c47de2010-10-29 18:23:28 +00001/*
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.orga8c47de2010-10-29 18:23:28 +000034#include "FileError.h"
darin@apple.com5ffbb5c2013-09-27 16:39:41 +000035#include "URL.h"
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000036#include "TextEncoding.h"
37#include "ThreadableLoaderClient.h"
38#include <wtf/Forward.h>
39#include <wtf/text/WTFString.h>
40
oliver@apple.comdf606082013-08-05 22:11:50 +000041namespace JSC {
42class ArrayBuffer;
43}
44
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000045namespace WebCore {
46
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000047class Blob;
48class FileReaderLoaderClient;
49class ScriptExecutionContext;
50class TextResourceDecoder;
51class ThreadableLoader;
52
53class FileReaderLoader : public ThreadableLoaderClient {
54public:
55 enum ReadType {
56 ReadAsArrayBuffer,
57 ReadAsBinaryString,
commit-queue@webkit.org5b69f172013-02-22 10:03:40 +000058 ReadAsBlob,
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000059 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.orgc2baad02011-06-16 16:53:03 +000071 virtual void didReceiveResponse(unsigned long, const ResourceResponse&);
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000072 virtual void didReceiveData(const char*, int);
commit-queue@webkit.org7c4021c2011-02-17 04:11:38 +000073 virtual void didFinishLoading(unsigned long, double);
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000074 virtual void didFail(const ResourceError&);
75
76 String stringResult();
gyuyoung.kim@webkit.org862a5862015-09-08 10:50:42 +000077 RefPtr<JSC::ArrayBuffer> arrayBufferResult() const;
darin@apple.comf581c552010-12-01 01:53:21 +000078 unsigned bytesLoaded() const { return m_bytesLoaded; }
79 unsigned totalBytes() const { return m_totalBytes; }
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000080 int errorCode() const { return m_errorCode; }
81
82 void setEncoding(const String&);
83 void setDataType(const String& dataType) { m_dataType = dataType; }
commit-queue@webkit.org5b69f172013-02-22 10:03:40 +000084
jianli@chromium.orga8c47de2010-10-29 18:23:28 +000085private:
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.com5ffbb5c2013-09-27 16:39:41 +0000101 URL m_urlForReading;
jianli@chromium.orga8c47de2010-10-29 18:23:28 +0000102 RefPtr<ThreadableLoader> m_loader;
103
oliver@apple.comdf606082013-08-05 22:11:50 +0000104 RefPtr<JSC::ArrayBuffer> m_rawData;
jianli@chromium.orga8c47de2010-10-29 18:23:28 +0000105 bool m_isRawDataConverted;
106
107 String m_stringResult;
commit-queue@webkit.org5b69f172013-02-22 10:03:40 +0000108 RefPtr<Blob> m_blobResult;
jianli@chromium.orga8c47de2010-10-29 18:23:28 +0000109
110 // The decoder used to decode the text data.
111 RefPtr<TextResourceDecoder> m_decoder;
112
commit-queue@webkit.org5b69f172013-02-22 10:03:40 +0000113 bool m_variableLength;
darin@apple.comf581c552010-12-01 01:53:21 +0000114 unsigned m_bytesLoaded;
115 unsigned m_totalBytes;
commit-queue@webkit.org5b69f172013-02-22 10:03:40 +0000116
117 bool m_hasRange;
118 unsigned m_rangeStart;
119 unsigned m_rangeEnd;
120
jianli@chromium.orga8c47de2010-10-29 18:23:28 +0000121 int m_errorCode;
122};
123
124} // namespace WebCore
125
jianli@chromium.orga8c47de2010-10-29 18:23:28 +0000126#endif // FileReaderLoader_h