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 <wtf/OwnPtr.h>
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/RefCounted.h>
43 #include <wtf/RefPtr.h>
47 class AsyncFileWriter;
50 class ScriptExecutionContext;
52 class FileWriter : public RefCounted<FileWriter>, public ActiveDOMObject, public EventTarget, public AsyncFileWriterClient {
54 static PassRefPtr<FileWriter> create(ScriptExecutionContext* context)
56 return adoptRef(new FileWriter(context));
59 void initialize(PassOwnPtr<AsyncFileWriter> writer, long long length);
67 void write(Blob* data, ExceptionCode& ec);
68 void seek(long long position, ExceptionCode& ec);
69 void truncate(long long length, ExceptionCode& ec);
70 void abort(ExceptionCode& ec);
72 ReadyState readyState() const { return m_readyState; }
73 FileError* error() const { return m_error.get(); }
74 long long position() const { return m_position; }
75 long long length() const { return m_length; }
77 // AsyncFileWriterClient
78 void didWrite(long long bytes, bool complete);
80 void didFail(ExceptionCode ec);
83 virtual bool canSuspend() const;
84 virtual bool hasPendingActivity() const;
88 virtual FileWriter* toFileWriter() { return this; }
89 virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
91 using RefCounted<FileWriter>::ref;
92 using RefCounted<FileWriter>::deref;
94 DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
102 FileWriter(ScriptExecutionContext*);
104 virtual ~FileWriter();
106 friend class WTF::RefCounted<FileWriter>;
109 virtual void refEventTarget() { ref(); }
110 virtual void derefEventTarget() { deref(); }
111 virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
112 virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
114 void fireEvent(const AtomicString& type);
116 RefPtr<FileError> m_error;
117 EventTargetData m_eventTargetData;
118 OwnPtr<AsyncFileWriter> m_writer;
119 ReadyState m_readyState;
120 long long m_position;
122 long long m_bytesWritten;
123 long long m_bytesToWrite;
124 long long m_truncateLength;
127 } // namespace WebCore
129 #endif // ENABLE(FILE_SYSTEM)
131 #endif // FileWriter_h