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 FileStream_h
32#define FileStream_h
33
34#if ENABLE(BLOB)
35
36#include "FileSystem.h"
37#include <wtf/Forward.h>
38#include <wtf/PassRefPtr.h>
39#include <wtf/RefCounted.h>
40
41namespace WebCore {
42
43class KURL;
44
45// All methods are synchronous.
46class FileStream : public RefCounted<FileStream> {
47public:
48    static PassRefPtr<FileStream> create()
49    {
50        return adoptRef(new FileStream());
51    }
52    ~FileStream();
53
54    // FIXME: To be removed when we switch to using BlobData.
55    void start();
56
57    // Aborts the operation.
58    void stop();
59
60    // Gets the size of a file. Also validates if the file has been changed or not if the expected modification time is provided, i.e. non-zero.
61    // Returns total number of bytes if successful. -1 otherwise.
62    long long getSize(const String& path, double expectedModificationTime);
63
64    // Opens a file for reading. The reading starts at the specified offset and lasts till the specified length.
65    // Returns true on success. False otherwise.
66    bool openForRead(const String& path, long long offset, long long length);
67
68    // Opens a file for writing.
69    // Returns true on success. False otherwise.
70    bool openForWrite(const String& path);
71
72    // Closes the file.
73    void close();
74
75    // Reads a file into the provided data buffer.
76    // Returns number of bytes being read on success. -1 otherwise.
77    // If 0 is returned, it means that the reading is completed.
78    int read(char* buffer, int length);
79
80    // Writes a blob to the file.
81    // Returns number of bytes being written on success. -1 otherwise.
82    int write(const KURL& blobURL, long long position, int length);
83
84    // Truncates the file to the specified position.
85    // Returns true on success. False otherwise.
86    bool truncate(long long position);
87
88private:
89    FileStream();
90
91    PlatformFileHandle m_handle;
92    long long m_bytesProcessed;
93    long long m_totalBytesToRead;
94};
95
96} // namespace WebCore
97
98#endif // ENABLE(BLOB)
99
100#endif // FileStream_h
101