0454fae173cabeff04a3c4e350e0d41b2610f936
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventTarget.h"
37 #include "core/fileapi/FileError.h"
38 #include "core/fileapi/FileReaderLoader.h"
39 #include "core/fileapi/FileReaderLoaderClient.h"
40 #include "platform/heap/Handle.h"
41 #include "wtf/Forward.h"
42 #include "wtf/RefCounted.h"
43 #include "wtf/ThreadSpecific.h"
44 #include "wtf/text/WTFString.h"
45
46 namespace WebCore {
47
48 class Blob;
49 class ExceptionState;
50 class ExecutionContext;
51
52 class FileReader FINAL : public RefCountedWillBeRefCountedGarbageCollected<FileReader>, public ScriptWrappable, public ActiveDOMObject, public FileReaderLoaderClient, public EventTargetWithInlineData {
53     REFCOUNTED_EVENT_TARGET(FileReader);
54     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FileReader);
55 public:
56     static PassRefPtrWillBeRawPtr<FileReader> create(ExecutionContext*);
57
58     virtual ~FileReader();
59
60     enum ReadyState {
61         EMPTY = 0,
62         LOADING = 1,
63         DONE = 2
64     };
65
66     void readAsArrayBuffer(Blob*, ExceptionState&);
67     void readAsBinaryString(Blob*, ExceptionState&);
68     void readAsText(Blob*, const String& encoding, ExceptionState&);
69     void readAsText(Blob*, ExceptionState&);
70     void readAsDataURL(Blob*, ExceptionState&);
71     void abort();
72
73     void doAbort();
74
75     ReadyState readyState() const { return m_state; }
76     PassRefPtrWillBeRawPtr<FileError> error() { return m_error; }
77     FileReaderLoader::ReadType readType() const { return m_readType; }
78     PassRefPtr<ArrayBuffer> arrayBufferResult() const;
79     String stringResult();
80
81     // ActiveDOMObject
82     virtual void stop() OVERRIDE;
83
84     // EventTarget
85     virtual const AtomicString& interfaceName() const OVERRIDE;
86     virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
87
88     // FileReaderLoaderClient
89     virtual void didStartLoading() OVERRIDE;
90     virtual void didReceiveData() OVERRIDE;
91     virtual void didFinishLoading() OVERRIDE;
92     virtual void didFail(FileError::ErrorCode) OVERRIDE;
93
94     DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
95     DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
96     DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
97     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
98     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
99     DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend);
100
101     virtual void trace(Visitor*) OVERRIDE;
102
103 private:
104     class ThrottlingController;
105
106     FileReader(ExecutionContext*);
107
108     void terminate();
109     void readInternal(Blob*, FileReaderLoader::ReadType, ExceptionState&);
110     void fireErrorEvent(int httpStatusCode);
111     void fireEvent(const AtomicString& type);
112
113     static ThreadSpecific<ThrottlingController>& throttlingController();
114     void executePendingRead();
115
116     ReadyState m_state;
117
118     // Internal loading state, which could differ from ReadyState as it's
119     // for script-visible state while this one's for internal state.
120     enum LoadingState {
121         LoadingStateNone,
122         LoadingStatePending,
123         LoadingStateLoading,
124         LoadingStateAborted
125     };
126     LoadingState m_loadingState;
127
128     String m_blobType;
129     RefPtr<BlobDataHandle> m_blobDataHandle;
130     FileReaderLoader::ReadType m_readType;
131     String m_encoding;
132
133     OwnPtr<FileReaderLoader> m_loader;
134     RefPtrWillBeMember<FileError> m_error;
135     double m_lastProgressNotificationTimeMS;
136 };
137
138 } // namespace WebCore
139
140 #endif // FileReader_h