2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #if ENABLE(FILE_SYSTEM)
36 #include "ActiveDOMObject.h"
37 #include "AsyncFileWriterClient.h"
38 #include "EventTarget.h"
39 #include "FileWriterBase.h"
40 #include "ScriptExecutionContext.h"
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/RefPtr.h>
46 class ScriptExecutionContext;
48 class FileWriter : public FileWriterBase, public ActiveDOMObject, public EventTarget, public AsyncFileWriterClient {
50 static PassRefPtr<FileWriter> create(ScriptExecutionContext* context)
52 return adoptRef(new FileWriter(context));
61 void write(Blob*, ExceptionCode&);
62 void seek(long long position, ExceptionCode&);
63 void truncate(long long length, ExceptionCode&);
64 void abort(ExceptionCode&);
65 ReadyState readyState() const { return m_readyState; }
66 FileError* error() const { return m_error.get(); }
68 // AsyncFileWriterClient
69 void didWrite(long long bytes, bool complete);
71 void didFail(FileError::ErrorCode);
74 virtual bool canSuspend() const;
75 virtual bool hasPendingActivity() const;
79 virtual FileWriter* toFileWriter() { return this; }
80 virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
82 using RefCounted<FileWriterBase>::ref;
83 using RefCounted<FileWriterBase>::deref;
85 DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
86 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
87 DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
88 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
89 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
90 DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
93 FileWriter(ScriptExecutionContext*);
95 virtual ~FileWriter();
98 virtual void refEventTarget() { ref(); }
99 virtual void derefEventTarget() { deref(); }
100 virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
101 virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
103 void fireEvent(const AtomicString& type);
105 void setError(FileError::ErrorCode, ExceptionCode&);
107 void signalCompletion(FileError::ErrorCode);
109 class FileWriterCompletionEventTask : public ScriptExecutionContext::Task {
111 static PassOwnPtr<FileWriterCompletionEventTask> create(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
113 return adoptPtr(new FileWriterCompletionEventTask(fileWriter, code));
117 virtual void performTask(ScriptExecutionContext*)
119 m_fileWriter->signalCompletion(m_code);
122 FileWriterCompletionEventTask(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
123 : m_fileWriter(fileWriter)
128 RefPtr<FileWriter> m_fileWriter;
129 FileError::ErrorCode m_code;
132 RefPtr<FileError> m_error;
133 EventTargetData m_eventTargetData;
134 ReadyState m_readyState;
135 bool m_startedWriting;
136 long long m_bytesWritten;
137 long long m_bytesToWrite;
138 long long m_truncateLength;
139 RefPtr<Blob> m_blobBeingWritten;
142 } // namespace WebCore
144 #endif // ENABLE(FILE_SYSTEM)
146 #endif // FileWriter_h