initial import
[vuplus_webkit] / Source / WebCore / fileapi / FileReader.h
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 FileReader_h
32 #define FileReader_h
33
34 #if ENABLE(BLOB)
35
36 #include "ActiveDOMObject.h"
37 #include "EventTarget.h"
38 #include "ExceptionCode.h"
39 #include "FileError.h"
40 #include "FileReaderLoader.h"
41 #include "FileReaderLoaderClient.h"
42 #include <wtf/Forward.h>
43 #include <wtf/RefCounted.h>
44 #include <wtf/text/WTFString.h>
45
46 namespace WebCore {
47
48 class ArrayBuffer;
49 class Blob;
50 class ScriptExecutionContext;
51
52 class FileReader : public RefCounted<FileReader>, public ActiveDOMObject, public EventTarget, public FileReaderLoaderClient {
53 public:
54     static PassRefPtr<FileReader> create(ScriptExecutionContext* context)
55     {
56         return adoptRef(new FileReader(context));
57     }
58
59     virtual ~FileReader();
60
61     enum ReadyState {
62         EMPTY = 0,
63         LOADING = 1,
64         DONE = 2
65     };
66
67     void readAsArrayBuffer(Blob*, ExceptionCode&);
68     void readAsBinaryString(Blob*, ExceptionCode&);
69     void readAsText(Blob*, const String& encoding, ExceptionCode&);
70     void readAsText(Blob*, ExceptionCode&);
71     void readAsDataURL(Blob*, ExceptionCode&);
72     void abort();
73
74     void doAbort();
75
76     ReadyState readyState() const { return m_state; }
77     PassRefPtr<FileError> error() { return m_error; }
78     FileReaderLoader::ReadType readType() const { return m_readType; }
79     PassRefPtr<ArrayBuffer> arrayBufferResult() const;
80     String stringResult();
81
82     // ActiveDOMObject
83     virtual bool canSuspend() const;
84     virtual void stop();
85     virtual bool hasPendingActivity() const;
86
87     // EventTarget
88     virtual FileReader* toFileReader() { return this; }
89     virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
90
91     // FileReaderLoaderClient
92     virtual void didStartLoading();
93     virtual void didReceiveData();
94     virtual void didFinishLoading();
95     virtual void didFail(int errorCode);
96
97     using RefCounted<FileReader>::ref;
98     using RefCounted<FileReader>::deref;
99
100     DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
101     DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
102     DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
103     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
104     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
105     DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend);
106
107 private:
108     FileReader(ScriptExecutionContext*);
109
110     // EventTarget
111     virtual void refEventTarget() { ref(); }
112     virtual void derefEventTarget() { deref(); }
113     virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
114     virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
115
116     void terminate();
117     void readInternal(Blob*, FileReaderLoader::ReadType, ExceptionCode&);
118     void fireErrorEvent(int httpStatusCode);
119     void fireEvent(const AtomicString& type);
120
121     ReadyState m_state;
122     bool m_aborting;
123     EventTargetData m_eventTargetData;
124
125     RefPtr<Blob> m_blob;
126     FileReaderLoader::ReadType m_readType;
127     String m_encoding;
128
129     OwnPtr<FileReaderLoader> m_loader;
130     RefPtr<FileError> m_error;
131     double m_lastProgressNotificationTimeMS;
132 };
133
134 } // namespace WebCore
135
136 #endif // ENABLE(BLOB)
137
138 #endif // FileReader_h