• 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/events/EventTarget.h"
38 #include "core/fileapi/FileError.h"
39 #include "modules/filesystem/FileWriterBase.h"
40 #include "public/platform/WebFileWriterClient.h"
41 #include "wtf/PassRefPtr.h"
42 #include "wtf/RefPtr.h"
43 
44 namespace WebCore {
45 
46 class Blob;
47 class ExceptionState;
48 class ExecutionContext;
49 
50 class FileWriter : public ScriptWrappable, public FileWriterBase, public ActiveDOMObject, public EventTargetWithInlineData, public blink::WebFileWriterClient {
51     DEFINE_EVENT_TARGET_REFCOUNTING(FileWriterBase);
52 public:
53     static PassRefPtr<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&);
readyState()65     ReadyState readyState() const { return m_readyState; }
error()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(blink::WebFileError) OVERRIDE;
72 
73     // ActiveDOMObject
74     virtual void stop();
75 
76     // EventTarget
77     virtual const AtomicString& interfaceName() const OVERRIDE;
executionContext()78     virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
79 
80     DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
81     DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
82     DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
83     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
84     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
85     DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
86 
87 private:
88     enum Operation {
89         OperationNone,
90         OperationWrite,
91         OperationTruncate,
92         OperationAbort
93     };
94 
95     FileWriter(ExecutionContext*);
96 
97     virtual ~FileWriter();
98 
99     void completeAbort();
100 
101     void doOperation(Operation);
102 
103     void signalCompletion(FileError::ErrorCode);
104 
105     void fireEvent(const AtomicString& type);
106 
107     void setError(FileError::ErrorCode, ExceptionState&);
108 
109     RefPtr<FileError> m_error;
110     ReadyState m_readyState;
111     Operation m_operationInProgress;
112     Operation m_queuedOperation;
113     long long m_bytesWritten;
114     long long m_bytesToWrite;
115     long long m_truncateLength;
116     long long m_numAborts;
117     long long m_recursionDepth;
118     double m_lastProgressNotificationTimeMS;
119     RefPtr<Blob> m_blobBeingWritten;
120 };
121 
122 } // namespace WebCore
123 
124 #endif // FileWriter_h
125