• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/fileapi/FileError.h"
38 #include "modules/EventTargetModules.h"
39 #include "modules/filesystem/FileWriterBase.h"
40 #include "platform/heap/Handle.h"
41 #include "public/platform/WebFileWriterClient.h"
42 #include "wtf/RefPtr.h"
43 
44 namespace WebCore {
45 
46 class Blob;
47 class ExceptionState;
48 class ExecutionContext;
49 
50 class FileWriter FINAL : public FileWriterBase, public ScriptWrappable, public ActiveDOMObject, public EventTargetWithInlineData, public blink::WebFileWriterClient {
51     DEFINE_EVENT_TARGET_REFCOUNTING(RefCountedGarbageCollected<FileWriterBase>);
52     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FileWriter);
53 public:
54     static FileWriter* create(ExecutionContext*);
55 
56     enum ReadyState {
57         INIT = 0,
58         WRITING = 1,
59         DONE = 2
60     };
61 
62     void write(Blob*, ExceptionState&);
63     void seek(long long position, ExceptionState&);
64     void truncate(long long length, ExceptionState&);
65     void abort(ExceptionState&);
readyState()66     ReadyState readyState() const { return m_readyState; }
error()67     FileError* error() const { return m_error.get(); }
68 
69     // WebFileWriterClient
70     virtual void didWrite(long long bytes, bool complete) OVERRIDE;
71     virtual void didTruncate() OVERRIDE;
72     virtual void didFail(blink::WebFileError) OVERRIDE;
73 
74     // ActiveDOMObject
75     virtual void stop() OVERRIDE;
76 
77     // EventTarget
78     virtual const AtomicString& interfaceName() const OVERRIDE;
executionContext()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 };
124 
125 } // namespace WebCore
126 
127 #endif // FileWriter_h
128