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 "ActiveDOMObject.h" 35#include "EventTarget.h" 36#include "FileError.h" 37#include "FileReaderLoader.h" 38#include "FileReaderLoaderClient.h" 39#include <chrono> 40#include <wtf/Forward.h> 41#include <wtf/RefCounted.h> 42#include <wtf/text/WTFString.h> 43 44namespace JSC { 45class ArrayBuffer; 46} 47 48namespace WebCore { 49 50class Blob; 51class ScriptExecutionContext; 52 53typedef int ExceptionCode; 54 55class FileReader final : public RefCounted<FileReader>, public ActiveDOMObject, public EventTargetWithInlineData, public FileReaderLoaderClient { 56public: 57 static PassRefPtr<FileReader> create(ScriptExecutionContext&); 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<JSC::ArrayBuffer> arrayBufferResult() const; 80 String stringResult(); 81 82 // EventTarget 83 virtual EventTargetInterface eventTargetInterface() const override { return FileReaderEventTargetInterfaceType; } 84 virtual ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); } 85 86 // FileReaderLoaderClient 87 virtual void didStartLoading() override; 88 virtual void didReceiveData() override; 89 virtual void didFinishLoading() override; 90 virtual void didFail(int errorCode) override; 91 92 using RefCounted<FileReader>::ref; 93 using RefCounted<FileReader>::deref; 94 95 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart); 96 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); 97 DEFINE_ATTRIBUTE_EVENT_LISTENER(load); 98 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 99 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 100 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend); 101 102private: 103 explicit FileReader(ScriptExecutionContext&); 104 105 // ActiveDOMObject 106 virtual bool canSuspend() const override; 107 virtual void stop() override; 108 109 // EventTarget 110 virtual void refEventTarget() override { ref(); } 111 virtual void derefEventTarget() override { deref(); } 112 113 void terminate(); 114 void readInternal(Blob*, FileReaderLoader::ReadType, ExceptionCode&); 115 void fireErrorEvent(int httpStatusCode); 116 void fireEvent(const AtomicString& type); 117 118 ReadyState m_state; 119 bool m_aborting; 120 RefPtr<Blob> m_blob; 121 FileReaderLoader::ReadType m_readType; 122 String m_encoding; 123 std::unique_ptr<FileReaderLoader> m_loader; 124 RefPtr<FileError> m_error; 125 std::chrono::steady_clock::time_point m_lastProgressNotificationTime; 126}; 127 128} // namespace WebCore 129 130#endif // FileReader_h 131