#ifndef FileWriter_h
#define FileWriter_h
#if ENABLE(FILE_SYSTEM)
#include "ActiveDOMObject.h"
#include "AsyncFileWriterClient.h"
#include "EventTarget.h"
#include "FileWriterBase.h"
#include "ScriptExecutionContext.h"
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class ScriptExecutionContext;
class FileWriter : public FileWriterBase, public ActiveDOMObject, public EventTarget, public AsyncFileWriterClient {
public:
static PassRefPtr<FileWriter> create(ScriptExecutionContext* context)
{
return adoptRef(new FileWriter(context));
}
enum ReadyState {
INIT = 0,
WRITING = 1,
DONE = 2
};
void write(Blob*, ExceptionCode&);
void seek(long long position, ExceptionCode&);
void truncate(long long length, ExceptionCode&);
void abort(ExceptionCode&);
ReadyState readyState() const { return m_readyState; }
FileError* error() const { return m_error.get(); }
void didWrite(long long bytes, bool complete);
void didTruncate();
void didFail(FileError::ErrorCode);
virtual bool canSuspend() const;
virtual bool hasPendingActivity() const;
virtual void stop();
virtual FileWriter* toFileWriter() { return this; }
virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
using RefCounted<FileWriterBase>::ref;
using RefCounted<FileWriterBase>::deref;
DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
private:
FileWriter(ScriptExecutionContext*);
virtual ~FileWriter();
virtual void refEventTarget() { ref(); }
virtual void derefEventTarget() { deref(); }
virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
void fireEvent(const AtomicString& type);
void setError(FileError::ErrorCode, ExceptionCode&);
void signalCompletion(FileError::ErrorCode);
class FileWriterCompletionEventTask : public ScriptExecutionContext::Task {
public:
static PassOwnPtr<FileWriterCompletionEventTask> create(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
{
return adoptPtr(new FileWriterCompletionEventTask(fileWriter, code));
}
virtual void performTask(ScriptExecutionContext*)
{
m_fileWriter->signalCompletion(m_code);
}
private:
FileWriterCompletionEventTask(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
: m_fileWriter(fileWriter)
, m_code(code)
{
}
RefPtr<FileWriter> m_fileWriter;
FileError::ErrorCode m_code;
};
RefPtr<FileError> m_error;
EventTargetData m_eventTargetData;
ReadyState m_readyState;
bool m_startedWriting;
long long m_bytesWritten;
long long m_bytesToWrite;
long long m_truncateLength;
RefPtr<Blob> m_blobBeingWritten;
};
}
#endif // ENABLE(FILE_SYSTEM)
#endif // FileWriter_h