121939df44de1705786c545cd1bf519d47250322dBen Murdoch/*
221939df44de1705786c545cd1bf519d47250322dBen Murdoch * Copyright (C) 2010 Google Inc.  All rights reserved.
321939df44de1705786c545cd1bf519d47250322dBen Murdoch *
421939df44de1705786c545cd1bf519d47250322dBen Murdoch * Redistribution and use in source and binary forms, with or without
521939df44de1705786c545cd1bf519d47250322dBen Murdoch * modification, are permitted provided that the following conditions are
621939df44de1705786c545cd1bf519d47250322dBen Murdoch * met:
721939df44de1705786c545cd1bf519d47250322dBen Murdoch *
821939df44de1705786c545cd1bf519d47250322dBen Murdoch *     * Redistributions of source code must retain the above copyright
921939df44de1705786c545cd1bf519d47250322dBen Murdoch * notice, this list of conditions and the following disclaimer.
1021939df44de1705786c545cd1bf519d47250322dBen Murdoch *     * Redistributions in binary form must reproduce the above
1121939df44de1705786c545cd1bf519d47250322dBen Murdoch * copyright notice, this list of conditions and the following disclaimer
1221939df44de1705786c545cd1bf519d47250322dBen Murdoch * in the documentation and/or other materials provided with the
1321939df44de1705786c545cd1bf519d47250322dBen Murdoch * distribution.
1421939df44de1705786c545cd1bf519d47250322dBen Murdoch *     * Neither the name of Google Inc. nor the names of its
1521939df44de1705786c545cd1bf519d47250322dBen Murdoch * contributors may be used to endorse or promote products derived from
1621939df44de1705786c545cd1bf519d47250322dBen Murdoch * this software without specific prior written permission.
1721939df44de1705786c545cd1bf519d47250322dBen Murdoch *
1821939df44de1705786c545cd1bf519d47250322dBen Murdoch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1921939df44de1705786c545cd1bf519d47250322dBen Murdoch * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2021939df44de1705786c545cd1bf519d47250322dBen Murdoch * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2121939df44de1705786c545cd1bf519d47250322dBen Murdoch * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2221939df44de1705786c545cd1bf519d47250322dBen Murdoch * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2321939df44de1705786c545cd1bf519d47250322dBen Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2421939df44de1705786c545cd1bf519d47250322dBen Murdoch * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2521939df44de1705786c545cd1bf519d47250322dBen Murdoch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2621939df44de1705786c545cd1bf519d47250322dBen Murdoch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2721939df44de1705786c545cd1bf519d47250322dBen Murdoch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2821939df44de1705786c545cd1bf519d47250322dBen Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2921939df44de1705786c545cd1bf519d47250322dBen Murdoch */
3021939df44de1705786c545cd1bf519d47250322dBen Murdoch
3121939df44de1705786c545cd1bf519d47250322dBen Murdoch#ifndef FileReader_h
3221939df44de1705786c545cd1bf519d47250322dBen Murdoch#define FileReader_h
3321939df44de1705786c545cd1bf519d47250322dBen Murdoch
34dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#if ENABLE(BLOB)
3521939df44de1705786c545cd1bf519d47250322dBen Murdoch
3621939df44de1705786c545cd1bf519d47250322dBen Murdoch#include "ActiveDOMObject.h"
3721939df44de1705786c545cd1bf519d47250322dBen Murdoch#include "EventTarget.h"
3821939df44de1705786c545cd1bf519d47250322dBen Murdoch#include "FileError.h"
39e14391e94c850b8bd03680c23b38978db68687a8John Reck#include "FileReaderLoader.h"
40e14391e94c850b8bd03680c23b38978db68687a8John Reck#include "FileReaderLoaderClient.h"
41e14391e94c850b8bd03680c23b38978db68687a8John Reck#include <wtf/Forward.h>
4221939df44de1705786c545cd1bf519d47250322dBen Murdoch#include <wtf/RefCounted.h>
43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <wtf/text/WTFString.h>
4421939df44de1705786c545cd1bf519d47250322dBen Murdoch
4521939df44de1705786c545cd1bf519d47250322dBen Murdochnamespace WebCore {
4621939df44de1705786c545cd1bf519d47250322dBen Murdoch
47e14391e94c850b8bd03680c23b38978db68687a8John Reckclass ArrayBuffer;
4821939df44de1705786c545cd1bf519d47250322dBen Murdochclass Blob;
4921939df44de1705786c545cd1bf519d47250322dBen Murdochclass ScriptExecutionContext;
5021939df44de1705786c545cd1bf519d47250322dBen Murdoch
51e14391e94c850b8bd03680c23b38978db68687a8John Reckclass FileReader : public RefCounted<FileReader>, public ActiveDOMObject, public EventTarget, public FileReaderLoaderClient {
5221939df44de1705786c545cd1bf519d47250322dBen Murdochpublic:
5321939df44de1705786c545cd1bf519d47250322dBen Murdoch    static PassRefPtr<FileReader> create(ScriptExecutionContext* context)
5421939df44de1705786c545cd1bf519d47250322dBen Murdoch    {
5521939df44de1705786c545cd1bf519d47250322dBen Murdoch        return adoptRef(new FileReader(context));
5621939df44de1705786c545cd1bf519d47250322dBen Murdoch    }
5721939df44de1705786c545cd1bf519d47250322dBen Murdoch
5821939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual ~FileReader();
5921939df44de1705786c545cd1bf519d47250322dBen Murdoch
6021939df44de1705786c545cd1bf519d47250322dBen Murdoch    enum ReadyState {
61f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick        EMPTY = 0,
62f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick        LOADING = 1,
63f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick        DONE = 2
6421939df44de1705786c545cd1bf519d47250322dBen Murdoch    };
6521939df44de1705786c545cd1bf519d47250322dBen Murdoch
66e14391e94c850b8bd03680c23b38978db68687a8John Reck    void readAsArrayBuffer(Blob*);
6721939df44de1705786c545cd1bf519d47250322dBen Murdoch    void readAsBinaryString(Blob*);
6821939df44de1705786c545cd1bf519d47250322dBen Murdoch    void readAsText(Blob*, const String& encoding = "");
695ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen    void readAsDataURL(Blob*);
7021939df44de1705786c545cd1bf519d47250322dBen Murdoch    void abort();
7121939df44de1705786c545cd1bf519d47250322dBen Murdoch
725ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen    void start();
73e14391e94c850b8bd03680c23b38978db68687a8John Reck    void doAbort();
745ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen
75e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    ReadyState readyState() const;
7621939df44de1705786c545cd1bf519d47250322dBen Murdoch    PassRefPtr<FileError> error() { return m_error; }
77e14391e94c850b8bd03680c23b38978db68687a8John Reck    FileReaderLoader::ReadType readType() const { return m_readType; }
78e14391e94c850b8bd03680c23b38978db68687a8John Reck    PassRefPtr<ArrayBuffer> arrayBufferResult() const;
79e14391e94c850b8bd03680c23b38978db68687a8John Reck    String stringResult();
805ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen
8121939df44de1705786c545cd1bf519d47250322dBen Murdoch    // ActiveDOMObject
8221939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual bool canSuspend() const;
8321939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual void stop();
8421939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual bool hasPendingActivity() const;
8521939df44de1705786c545cd1bf519d47250322dBen Murdoch
8621939df44de1705786c545cd1bf519d47250322dBen Murdoch    // EventTarget
8721939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual FileReader* toFileReader() { return this; }
8821939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
8921939df44de1705786c545cd1bf519d47250322dBen Murdoch
90e14391e94c850b8bd03680c23b38978db68687a8John Reck    // FileReaderLoaderClient
91e14391e94c850b8bd03680c23b38978db68687a8John Reck    virtual void didStartLoading();
92e14391e94c850b8bd03680c23b38978db68687a8John Reck    virtual void didReceiveData();
93e14391e94c850b8bd03680c23b38978db68687a8John Reck    virtual void didFinishLoading();
94e14391e94c850b8bd03680c23b38978db68687a8John Reck    virtual void didFail(int errorCode);
9521939df44de1705786c545cd1bf519d47250322dBen Murdoch
9621939df44de1705786c545cd1bf519d47250322dBen Murdoch    using RefCounted<FileReader>::ref;
9721939df44de1705786c545cd1bf519d47250322dBen Murdoch    using RefCounted<FileReader>::deref;
9821939df44de1705786c545cd1bf519d47250322dBen Murdoch
9921939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
10021939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
10121939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
10221939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
10321939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
10421939df44de1705786c545cd1bf519d47250322dBen Murdoch    DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend);
10521939df44de1705786c545cd1bf519d47250322dBen Murdoch
10621939df44de1705786c545cd1bf519d47250322dBen Murdochprivate:
107e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    enum InternalState {
108e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        None,
109e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        Starting,
110e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        Opening,
111e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        Reading,
112e14391e94c850b8bd03680c23b38978db68687a8John Reck        Aborting,
113e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        Completed
114e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    };
11521939df44de1705786c545cd1bf519d47250322dBen Murdoch
11621939df44de1705786c545cd1bf519d47250322dBen Murdoch    FileReader(ScriptExecutionContext*);
11721939df44de1705786c545cd1bf519d47250322dBen Murdoch
11821939df44de1705786c545cd1bf519d47250322dBen Murdoch    // EventTarget
11921939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual void refEventTarget() { ref(); }
12021939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual void derefEventTarget() { deref(); }
12121939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
12221939df44de1705786c545cd1bf519d47250322dBen Murdoch    virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
12321939df44de1705786c545cd1bf519d47250322dBen Murdoch
12421939df44de1705786c545cd1bf519d47250322dBen Murdoch    void terminate();
125e14391e94c850b8bd03680c23b38978db68687a8John Reck    void readInternal(Blob*, FileReaderLoader::ReadType);
1265ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen    void fireErrorEvent(int httpStatusCode);
12721939df44de1705786c545cd1bf519d47250322dBen Murdoch    void fireEvent(const AtomicString& type);
12821939df44de1705786c545cd1bf519d47250322dBen Murdoch
129e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    InternalState m_state;
13021939df44de1705786c545cd1bf519d47250322dBen Murdoch    EventTargetData m_eventTargetData;
13121939df44de1705786c545cd1bf519d47250322dBen Murdoch
1325ddde30071f639962dd557c453f2ad01f8f0fd00Kristian Monsen    RefPtr<Blob> m_blob;
133e14391e94c850b8bd03680c23b38978db68687a8John Reck    FileReaderLoader::ReadType m_readType;
134e14391e94c850b8bd03680c23b38978db68687a8John Reck    String m_encoding;
13521939df44de1705786c545cd1bf519d47250322dBen Murdoch
136e14391e94c850b8bd03680c23b38978db68687a8John Reck    OwnPtr<FileReaderLoader> m_loader;
13721939df44de1705786c545cd1bf519d47250322dBen Murdoch    RefPtr<FileError> m_error;
13821939df44de1705786c545cd1bf519d47250322dBen Murdoch    double m_lastProgressNotificationTimeMS;
13921939df44de1705786c545cd1bf519d47250322dBen Murdoch};
14021939df44de1705786c545cd1bf519d47250322dBen Murdoch
14121939df44de1705786c545cd1bf519d47250322dBen Murdoch} // namespace WebCore
14221939df44de1705786c545cd1bf519d47250322dBen Murdoch
143dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#endif // ENABLE(BLOB)
14421939df44de1705786c545cd1bf519d47250322dBen Murdoch
14521939df44de1705786c545cd1bf519d47250322dBen Murdoch#endif // FileReader_h
146