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