initial import
[vuplus_webkit] / Source / WebCore / fileapi / FileWriter.h
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 FileWriter_h
32 #define FileWriter_h
33
34 #if ENABLE(FILE_SYSTEM)
35
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>
43
44 namespace WebCore {
45
46 class ScriptExecutionContext;
47
48 class FileWriter : public FileWriterBase, public ActiveDOMObject, public EventTarget, public AsyncFileWriterClient {
49 public:
50     static PassRefPtr<FileWriter> create(ScriptExecutionContext* context)
51     {
52         return adoptRef(new FileWriter(context));
53     }
54
55     enum ReadyState {
56         INIT = 0,
57         WRITING = 1,
58         DONE = 2
59     };
60
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(); }
67
68     // AsyncFileWriterClient
69     void didWrite(long long bytes, bool complete);
70     void didTruncate();
71     void didFail(FileError::ErrorCode);
72
73     // ActiveDOMObject
74     virtual bool canSuspend() const;
75     virtual bool hasPendingActivity() const;
76     virtual void stop();
77
78     // EventTarget
79     virtual FileWriter* toFileWriter() { return this; }
80     virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
81
82     using RefCounted<FileWriterBase>::ref;
83     using RefCounted<FileWriterBase>::deref;
84
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);
91
92 private:
93     FileWriter(ScriptExecutionContext*);
94
95     virtual ~FileWriter();
96
97     // EventTarget
98     virtual void refEventTarget() { ref(); }
99     virtual void derefEventTarget() { deref(); }
100     virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
101     virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
102
103     void fireEvent(const AtomicString& type);
104
105     void setError(FileError::ErrorCode, ExceptionCode&);
106
107     void signalCompletion(FileError::ErrorCode);
108
109     class FileWriterCompletionEventTask : public ScriptExecutionContext::Task {
110     public:
111         static PassOwnPtr<FileWriterCompletionEventTask> create(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
112         {
113             return adoptPtr(new FileWriterCompletionEventTask(fileWriter, code));
114         }
115
116
117         virtual void performTask(ScriptExecutionContext*)
118         {
119             m_fileWriter->signalCompletion(m_code);
120         }
121     private:
122         FileWriterCompletionEventTask(PassRefPtr<FileWriter> fileWriter, FileError::ErrorCode code)
123             : m_fileWriter(fileWriter)
124             , m_code(code)
125         {
126         }
127
128         RefPtr<FileWriter> m_fileWriter;
129         FileError::ErrorCode m_code;
130     };
131
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;
140 };
141
142 } // namespace WebCore
143
144 #endif // ENABLE(FILE_SYSTEM)
145
146 #endif // FileWriter_h