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 | #pragma once |
32 | |
33 | #include "ActiveDOMObject.h" |
34 | #include "EventTarget.h" |
35 | #include "ExceptionOr.h" |
36 | #include "FileError.h" |
37 | #include "FileReaderLoader.h" |
38 | #include "FileReaderLoaderClient.h" |
39 | |
40 | namespace JSC { |
41 | class ArrayBuffer; |
42 | } |
43 | |
44 | namespace WebCore { |
45 | |
46 | class Blob; |
47 | |
48 | class FileReader final : public RefCounted<FileReader>, public ActiveDOMObject, public EventTargetWithInlineData, private FileReaderLoaderClient { |
49 | WTF_MAKE_ISO_ALLOCATED(FileReader); |
50 | public: |
51 | static Ref<FileReader> create(ScriptExecutionContext&); |
52 | |
53 | virtual ~FileReader(); |
54 | |
55 | enum ReadyState { |
56 | EMPTY = 0, |
57 | LOADING = 1, |
58 | DONE = 2 |
59 | }; |
60 | |
61 | ExceptionOr<void> readAsArrayBuffer(Blob*); |
62 | ExceptionOr<void> readAsBinaryString(Blob*); |
63 | ExceptionOr<void> readAsText(Blob*, const String& encoding); |
64 | ExceptionOr<void> readAsDataURL(Blob*); |
65 | void abort(); |
66 | |
67 | void doAbort(); |
68 | |
69 | ReadyState readyState() const { return m_state; } |
70 | RefPtr<FileError> error() { return m_error; } |
71 | FileReaderLoader::ReadType readType() const { return m_readType; } |
72 | Optional<Variant<String, RefPtr<JSC::ArrayBuffer>>> result() const; |
73 | |
74 | using RefCounted::ref; |
75 | using RefCounted::deref; |
76 | |
77 | private: |
78 | explicit FileReader(ScriptExecutionContext&); |
79 | |
80 | const char* activeDOMObjectName() const final; |
81 | bool canSuspendForDocumentSuspension() const final; |
82 | void stop() final; |
83 | |
84 | EventTargetInterface eventTargetInterface() const final { return FileReaderEventTargetInterfaceType; } |
85 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
86 | void refEventTarget() final { ref(); } |
87 | void derefEventTarget() final { deref(); } |
88 | |
89 | void didStartLoading() final; |
90 | void didReceiveData() final; |
91 | void didFinishLoading() final; |
92 | void didFail(int errorCode) final; |
93 | |
94 | ExceptionOr<void> readInternal(Blob&, FileReaderLoader::ReadType); |
95 | void fireErrorEvent(int httpStatusCode); |
96 | void fireEvent(const AtomicString& type); |
97 | |
98 | ReadyState m_state { EMPTY }; |
99 | bool m_aborting { false }; |
100 | RefPtr<Blob> m_blob; |
101 | FileReaderLoader::ReadType m_readType { FileReaderLoader::ReadAsBinaryString }; |
102 | String m_encoding; |
103 | std::unique_ptr<FileReaderLoader> m_loader; |
104 | RefPtr<FileError> m_error; |
105 | MonotonicTime m_lastProgressNotificationTime { MonotonicTime::nan() }; |
106 | RefPtr<PendingActivity<FileReader>> m_loadingActivity; |
107 | }; |
108 | |
109 | } // namespace WebCore |
110 | |