Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / filesystem / 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 #include "core/dom/ActiveDOMObject.h"
35 #include "core/dom/ExecutionContext.h"
36 #include "core/fileapi/FileError.h"
37 #include "modules/EventTargetModules.h"
38 #include "modules/filesystem/FileWriterBase.h"
39 #include "platform/heap/Handle.h"
40 #include "public/platform/WebFileWriterClient.h"
41 #include "wtf/RefPtr.h"
42
43 namespace blink {
44
45 class Blob;
46 class ExceptionState;
47 class ExecutionContext;
48
49 class FileWriter FINAL : public FileWriterBase, public ActiveDOMObject, public EventTargetWithInlineData, public WebFileWriterClient {
50     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<FileWriterBase>);
51     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FileWriter);
52 public:
53     static FileWriter* create(ExecutionContext*);
54
55     enum ReadyState {
56         INIT = 0,
57         WRITING = 1,
58         DONE = 2
59     };
60
61     void write(Blob*, ExceptionState&);
62     void seek(long long position, ExceptionState&);
63     void truncate(long long length, ExceptionState&);
64     void abort(ExceptionState&);
65     ReadyState readyState() const { return m_readyState; }
66     FileError* error() const { return m_error.get(); }
67
68     // WebFileWriterClient
69     virtual void didWrite(long long bytes, bool complete) OVERRIDE;
70     virtual void didTruncate() OVERRIDE;
71     virtual void didFail(WebFileError) OVERRIDE;
72
73     // ActiveDOMObject
74     virtual void stop() OVERRIDE;
75     virtual bool hasPendingActivity() const OVERRIDE;
76
77     // EventTarget
78     virtual const AtomicString& interfaceName() const OVERRIDE;
79     virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
80
81     DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
82     DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
83     DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
84     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
85     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
86     DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
87
88     virtual void trace(Visitor*) OVERRIDE;
89
90 private:
91     enum Operation {
92         OperationNone,
93         OperationWrite,
94         OperationTruncate,
95         OperationAbort
96     };
97
98     FileWriter(ExecutionContext*);
99
100     virtual ~FileWriter();
101
102     void completeAbort();
103
104     void doOperation(Operation);
105
106     void signalCompletion(FileError::ErrorCode);
107
108     void fireEvent(const AtomicString& type);
109
110     void setError(FileError::ErrorCode, ExceptionState&);
111
112     RefPtrWillBeMember<FileError> m_error;
113     ReadyState m_readyState;
114     Operation m_operationInProgress;
115     Operation m_queuedOperation;
116     long long m_bytesWritten;
117     long long m_bytesToWrite;
118     long long m_truncateLength;
119     long long m_numAborts;
120     long long m_recursionDepth;
121     double m_lastProgressNotificationTimeMS;
122     RefPtrWillBeMember<Blob> m_blobBeingWritten;
123     int m_asyncOperationId;
124 };
125
126 } // namespace blink
127
128 #endif // FileWriter_h