OSDN Git Service

am d7ab5cca: (-s ours) am e3a33d1e: Cherry-pick security fix in WebKit change 63048...
[android-x86/external-webkit.git] / 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_WRITER)
35
36 #include "ActiveDOMObject.h"
37 #include "EventTarget.h"
38 #include "FileWriterClient.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>
44
45 namespace WebCore {
46
47 class AsyncFileWriter;
48 class Blob;
49 class FileError;
50 class ScriptExecutionContext;
51
52 class FileWriter : public RefCounted<FileWriter>, public ActiveDOMObject, public EventTarget, public FileWriterClient {
53 public:
54     static PassRefPtr<FileWriter> create(ScriptExecutionContext* context)
55     {
56         return adoptRef(new FileWriter(context));
57     }
58
59     void initialize(PassOwnPtr<AsyncFileWriter> writer, long long length);
60
61     enum ReadyState {
62         INIT = 0,
63         WRITING = 1,
64         DONE = 2
65     };
66
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);
71
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; }
76
77     // FileWriterClient
78     void didWrite(long long bytes, bool complete);
79     void didTruncate(long long length);
80     void didFail(ExceptionCode ec);
81
82     // ActiveDOMObject
83     virtual bool canSuspend() const;
84     virtual bool hasPendingActivity() const;
85     virtual void stop();
86
87     // EventTarget
88     virtual FileWriter* toFileWriter() { return this; }
89     virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
90
91     using RefCounted<FileWriter>::ref;
92     using RefCounted<FileWriter>::deref;
93
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);
100     
101 private:
102     FileWriter(ScriptExecutionContext*);
103
104     virtual ~FileWriter();
105
106     friend class RefCounted<FileWriter>;
107
108     // EventTarget
109     virtual void refEventTarget() { ref(); }
110     virtual void derefEventTarget() { deref(); }
111     virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
112     virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
113
114     void fireEvent(const AtomicString& type);
115
116     RefPtr<FileError> m_error;
117     EventTargetData m_eventTargetData;
118     OwnPtr<AsyncFileWriter> m_writer;
119     ReadyState m_readyState;
120     long long m_position;
121     long long m_length;
122     long long m_bytesWritten;
123     long long m_bytesToWrite;
124 };
125
126 } // namespace WebCore
127
128 #endif // ENABLE(FILE_WRITER)
129
130 #endif // FileWriter_h