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
43namespace blink {
44
45class Blob;
46class ExceptionState;
47class ExecutionContext;
48
49class FileWriter FINAL : public FileWriterBase, public ActiveDOMObject, public EventTargetWithInlineData, public WebFileWriterClient {
50    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<FileWriterBase>);
51    DEFINE_WRAPPERTYPEINFO();
52    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FileWriter);
53public:
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&);
66    ReadyState readyState() const { return m_readyState; }
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(WebFileError) OVERRIDE;
73
74    // ActiveDOMObject
75    virtual void stop() OVERRIDE;
76    virtual bool hasPendingActivity() const OVERRIDE;
77
78    // EventTarget
79    virtual const AtomicString& interfaceName() const OVERRIDE;
80    virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
81
82    DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart);
83    DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
84    DEFINE_ATTRIBUTE_EVENT_LISTENER(write);
85    DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
86    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
87    DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend);
88
89    virtual void trace(Visitor*) OVERRIDE;
90
91private:
92    enum Operation {
93        OperationNone,
94        OperationWrite,
95        OperationTruncate,
96        OperationAbort
97    };
98
99    FileWriter(ExecutionContext*);
100
101    virtual ~FileWriter();
102
103    void completeAbort();
104
105    void doOperation(Operation);
106
107    void signalCompletion(FileError::ErrorCode);
108
109    void fireEvent(const AtomicString& type);
110
111    void setError(FileError::ErrorCode, ExceptionState&);
112
113    RefPtrWillBeMember<FileError> m_error;
114    ReadyState m_readyState;
115    Operation m_operationInProgress;
116    Operation m_queuedOperation;
117    long long m_bytesWritten;
118    long long m_bytesToWrite;
119    long long m_truncateLength;
120    long long m_numAborts;
121    long long m_recursionDepth;
122    double m_lastProgressNotificationTimeMS;
123    RefPtrWillBeMember<Blob> m_blobBeingWritten;
124    int m_asyncOperationId;
125};
126
127} // namespace blink
128
129#endif // FileWriter_h
130