2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "core/dom/ActiveDOMObject.h"
35 #include "core/events/EventTarget.h"
36 #include "core/fileapi/FileError.h"
37 #include "core/fileapi/FileReaderLoader.h"
38 #include "core/fileapi/FileReaderLoaderClient.h"
39 #include "platform/heap/Handle.h"
40 #include "wtf/Forward.h"
41 #include "wtf/RefCounted.h"
42 #include "wtf/text/WTFString.h"
48 class ExecutionContext;
50 class FileReader FINAL : public RefCountedWillBeGarbageCollectedFinalized<FileReader>, public ActiveDOMObject, public FileReaderLoaderClient, public EventTargetWithInlineData {
51 DEFINE_WRAPPERTYPEINFO();
52 REFCOUNTED_EVENT_TARGET(FileReader);
53 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FileReader);
55 static PassRefPtrWillBeRawPtr<FileReader> create(ExecutionContext*);
57 virtual ~FileReader();
65 void readAsArrayBuffer(Blob*, ExceptionState&);
66 void readAsBinaryString(Blob*, ExceptionState&);
67 void readAsText(Blob*, const String& encoding, ExceptionState&);
68 void readAsText(Blob*, ExceptionState&);
69 void readAsDataURL(Blob*, ExceptionState&);
74 ReadyState readyState() const { return m_state; }
75 PassRefPtrWillBeRawPtr<FileError> error() { return m_error; }
76 FileReaderLoader::ReadType readType() const { return m_readType; }
77 PassRefPtr<ArrayBuffer> arrayBufferResult() const;
78 String stringResult();
81 virtual void stop() OVERRIDE;
82 virtual bool hasPendingActivity() const OVERRIDE;
85 virtual const AtomicString& interfaceName() const OVERRIDE;
86 virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
88 // FileReaderLoaderClient
89 virtual void didStartLoading() OVERRIDE;
90 virtual void didReceiveData() OVERRIDE;
91 virtual void didFinishLoading() OVERRIDE;
92 virtual void didFail(FileError::ErrorCode) OVERRIDE;
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);
101 virtual void trace(Visitor*) OVERRIDE;
104 explicit FileReader(ExecutionContext*);
106 class ThrottlingController;
109 void readInternal(Blob*, FileReaderLoader::ReadType, ExceptionState&);
110 void fireEvent(const AtomicString& type);
112 void executePendingRead();
116 // Internal loading state, which could differ from ReadyState as it's
117 // for script-visible state while this one's for internal state.
124 LoadingState m_loadingState;
127 RefPtr<BlobDataHandle> m_blobDataHandle;
128 FileReaderLoader::ReadType m_readType;
131 OwnPtr<FileReaderLoader> m_loader;
132 RefPtrWillBeMember<FileError> m_error;
133 double m_lastProgressNotificationTimeMS;
134 int m_asyncOperationId;
139 #endif // FileReader_h