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 AsyncFileSystem_h
32#define AsyncFileSystem_h
33
34#if ENABLE(FILE_SYSTEM)
35
36#include "PlatformString.h"
37#include "Timer.h"
38#include <wtf/PassOwnPtr.h>
39
40namespace WebCore {
41
42class AsyncFileSystem;
43class AsyncFileSystemCallbacks;
44class AsyncFileWriterClient;
45
46// This class provides async interface for platform-specific file system implementation.  Note that all the methods take platform paths.
47class AsyncFileSystem {
48    WTF_MAKE_NONCOPYABLE(AsyncFileSystem);
49public:
50    virtual ~AsyncFileSystem() { }
51
52    // FileSystem type
53    enum Type {
54        Temporary,
55        Persistent,
56        External,
57    };
58
59    virtual void stop() { }
60    virtual bool hasPendingActivity() { return false; }
61
62    static bool isAvailable();
63
64    // Subclass must implement this if it supports synchronous operations.
65    // This should return false if there are no pending operations.
66    virtual bool waitForOperationToComplete() { return false; }
67
68    // Creates and returns a new platform-specific AsyncFileSystem instance if the platform has its own implementation.
69    static PassOwnPtr<AsyncFileSystem> create(Type, const String& rootPath);
70
71    // Opens a new file system. The create parameter specifies whether or not to create the path if it does not already exists.
72    static void openFileSystem(const String& basePath, const String& storageIdentifier, Type, bool create, PassOwnPtr<AsyncFileSystemCallbacks>);
73
74    // Moves a file or directory from srcPath to destPath.
75    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
76    // AsyncFileSystemCallbacks::didFail() is called otherwise.
77    virtual void move(const String& srcPath, const String& destPath, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
78
79    // Copies a file or directory from srcPath to destPath.
80    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
81    // AsyncFileSystemCallbacks::didFail() is called otherwise.
82    virtual void copy(const String& srcPath, const String& destPath, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
83
84    // Deletes a file or directory at a given path.
85    // It is an error to try to remove a directory that is not empty.
86    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
87    // AsyncFileSystemCallbacks::didFail() is called otherwise.
88    virtual void remove(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
89
90    // Recursively deletes a directory at a given path.
91    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
92    // AsyncFileSystemCallbacks::didFail() is called otherwise.
93    virtual void removeRecursively(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
94
95    // Retrieves the metadata information of the file or directory at a given path.
96    // AsyncFileSystemCallbacks::didReadMetadata() is called when the operation is completed successfully.
97    // AsyncFileSystemCallbacks::didFail() is called otherwise.
98    virtual void readMetadata(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
99
100    // Creates a file at a given path.  If exclusive flag is true, it fails if the path already exists.
101    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
102    // AsyncFileSystemCallbacks::didFail() is called otherwise.
103    virtual void createFile(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
104
105    // Creates a directory at a given path.  If exclusive flag is true, it fails if the path already exists.
106    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
107    // AsyncFileSystemCallbacks::didFail() is called otherwise.
108    virtual void createDirectory(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
109
110    // Checks if a file exists at a given path.
111    // AsyncFileSystemCallbacks::didSucceed() is called if the file exists.
112    // AsyncFileSystemCallbacks::didFail() is called otherwise.
113    virtual void fileExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
114
115    // Checks if a directory exists at a given path.
116    // AsyncFileSystemCallbacks::didSucceed() is called if the directory exists.
117    // AsyncFileSystemCallbacks::didFail() is called otherwise.
118    virtual void directoryExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
119
120    // Reads directory entries of a given directory at path.
121    // AsyncFileSystemCallbacks::didReadDirectoryEntry() is called when each directory entry is called. AsyncFileSystemCallbacks::didReadDirectoryEntries() is called after a chunk of directory entries have been read.
122    // AsyncFileSystemCallbacks::didFail() is when there is an error.
123    virtual void readDirectory(const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
124
125    // Creates an AsyncFileWriter for a given file path.
126    // AsyncFileSystemCallbacks::didCreateFileWriter() is called when an AsyncFileWriter is created successfully.
127    // AsyncFileSystemCallbacks::didFail() is called otherwise.
128    virtual void createWriter(AsyncFileWriterClient* client, const String& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
129
130    // Converts a given absolute virtual path to a platform path that starts with the platform root path of this file system.
131    virtual String virtualToPlatformPath(const String& path) const;
132
133    // Getter for this file system's root path.
134    String root() const { return m_platformRootPath; }
135
136    Type type() const { return m_type; }
137
138protected:
139    AsyncFileSystem(Type type, const String& platformRootPath)
140        : m_type(type)
141        , m_platformRootPath(platformRootPath)
142    {
143    }
144
145    Type m_type;
146    String m_platformRootPath;
147};
148
149} // namespace WebCore
150
151#endif // ENABLE(FILE_SYSTEM)
152
153#endif // AsyncFileSystem_h
154