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