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 ArrayBuffer; 48class Blob; 49class ScriptExecutionContext; 50 51class FileReader : public RefCounted<FileReader>, public ActiveDOMObject, public EventTarget, public FileReaderLoaderClient { 52public: 53 static PassRefPtr<FileReader> create(ScriptExecutionContext* context) 54 { 55 return adoptRef(new FileReader(context)); 56 } 57 58 virtual ~FileReader(); 59 60 enum ReadyState { 61 EMPTY = 0, 62 LOADING = 1, 63 DONE = 2 64 }; 65 66 void readAsArrayBuffer(Blob*); 67 void readAsBinaryString(Blob*); 68 void readAsText(Blob*, const String& encoding = ""); 69 void readAsDataURL(Blob*); 70 void abort(); 71 72 void start(); 73 void doAbort(); 74 75 ReadyState readyState() const; 76 PassRefPtr<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 bool canSuspend() const; 83 virtual void stop(); 84 virtual bool hasPendingActivity() const; 85 86 // EventTarget 87 virtual FileReader* toFileReader() { return this; } 88 virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); } 89 90 // FileReaderLoaderClient 91 virtual void didStartLoading(); 92 virtual void didReceiveData(); 93 virtual void didFinishLoading(); 94 virtual void didFail(int errorCode); 95 96 using RefCounted<FileReader>::ref; 97 using RefCounted<FileReader>::deref; 98 99 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart); 100 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); 101 DEFINE_ATTRIBUTE_EVENT_LISTENER(load); 102 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 103 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 104 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend); 105 106private: 107 enum InternalState { 108 None, 109 Starting, 110 Opening, 111 Reading, 112 Aborting, 113 Completed 114 }; 115 116 FileReader(ScriptExecutionContext*); 117 118 // EventTarget 119 virtual void refEventTarget() { ref(); } 120 virtual void derefEventTarget() { deref(); } 121 virtual EventTargetData* eventTargetData() { return &m_eventTargetData; } 122 virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; } 123 124 void terminate(); 125 void readInternal(Blob*, FileReaderLoader::ReadType); 126 void fireErrorEvent(int httpStatusCode); 127 void fireEvent(const AtomicString& type); 128 129 InternalState m_state; 130 EventTargetData m_eventTargetData; 131 132 RefPtr<Blob> m_blob; 133 FileReaderLoader::ReadType m_readType; 134 String m_encoding; 135 136 OwnPtr<FileReaderLoader> m_loader; 137 RefPtr<FileError> m_error; 138 double m_lastProgressNotificationTimeMS; 139}; 140 141} // namespace WebCore 142 143#endif // ENABLE(BLOB) 144 145#endif // FileReader_h 146