Home | History | Annotate | Download | only in fileapi
      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 "bindings/v8/ScriptWrappable.h"
     35 #include "core/dom/ActiveDOMObject.h"
     36 #include "core/dom/EventTarget.h"
     37 #include "core/fileapi/FileError.h"
     38 #include "core/fileapi/FileReaderLoader.h"
     39 #include "core/fileapi/FileReaderLoaderClient.h"
     40 #include "wtf/Forward.h"
     41 #include "wtf/RefCounted.h"
     42 #include "wtf/text/WTFString.h"
     43 
     44 namespace WebCore {
     45 
     46 class Blob;
     47 class ExceptionState;
     48 class ScriptExecutionContext;
     49 
     50 class FileReader : public RefCounted<FileReader>, public ScriptWrappable, public ActiveDOMObject, public EventTarget, public FileReaderLoaderClient {
     51 public:
     52     static PassRefPtr<FileReader> create(ScriptExecutionContext*);
     53 
     54     virtual ~FileReader();
     55 
     56     enum ReadyState {
     57         EMPTY = 0,
     58         LOADING = 1,
     59         DONE = 2
     60     };
     61 
     62     void readAsArrayBuffer(Blob*, ExceptionState&);
     63     void readAsBinaryString(Blob*, ExceptionState&);
     64     void readAsText(Blob*, const String& encoding, ExceptionState&);
     65     void readAsText(Blob*, ExceptionState&);
     66     void readAsDataURL(Blob*, ExceptionState&);
     67     void abort();
     68 
     69     void doAbort();
     70 
     71     ReadyState readyState() const { return m_state; }
     72     PassRefPtr<FileError> error() { return m_error; }
     73     FileReaderLoader::ReadType readType() const { return m_readType; }
     74     PassRefPtr<ArrayBuffer> arrayBufferResult() const;
     75     String stringResult();
     76 
     77     // ActiveDOMObject
     78     virtual bool canSuspend() const;
     79     virtual void stop();
     80 
     81     // EventTarget
     82     virtual const AtomicString& interfaceName() const;
     83     virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
     84 
     85     // FileReaderLoaderClient
     86     virtual void didStartLoading();
     87     virtual void didReceiveData();
     88     virtual void didFinishLoading();
     89     virtual void didFail(FileError::ErrorCode);
     90 
     91     using RefCounted<FileReader>::ref;
     92     using RefCounted<FileReader>::deref;
     93 
     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);
    100 
    101 private:
    102     FileReader(ScriptExecutionContext*);
    103 
    104     // EventTarget
    105     virtual void refEventTarget() { ref(); }
    106     virtual void derefEventTarget() { deref(); }
    107     virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
    108     virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
    109 
    110     void terminate();
    111     void readInternal(Blob*, FileReaderLoader::ReadType, ExceptionState&);
    112     void fireErrorEvent(int httpStatusCode);
    113     void fireEvent(const AtomicString& type);
    114 
    115     ReadyState m_state;
    116 
    117     // Internal loading state, which could differ from ReadyState as it's
    118     // for script-visible state while this one's for internal state.
    119     enum LoadingState {
    120         LoadingStateNone,
    121         LoadingStateLoading,
    122         LoadingStateAborted
    123     };
    124     LoadingState m_loadingState;
    125 
    126     EventTargetData m_eventTargetData;
    127 
    128     RefPtr<Blob> m_blob;
    129     FileReaderLoader::ReadType m_readType;
    130     String m_encoding;
    131 
    132     OwnPtr<FileReaderLoader> m_loader;
    133     RefPtr<FileError> m_error;
    134     double m_lastProgressNotificationTimeMS;
    135 };
    136 
    137 } // namespace WebCore
    138 
    139 #endif // FileReader_h
    140