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 "FileSystemType.h"
37#include "KURL.h"
38#include "Timer.h"
39#include <wtf/PassOwnPtr.h>
40#include <wtf/text/WTFString.h>
41
42namespace WebCore {
43
44class AsyncFileSystem;
45class AsyncFileSystemCallbacks;
46class AsyncFileWriterClient;
47
48// This class provides async interface for platform-specific file system implementation.  Note that all the methods take platform paths.
49class AsyncFileSystem {
50    WTF_MAKE_NONCOPYABLE(AsyncFileSystem);
51public:
52    virtual ~AsyncFileSystem() { }
53
54    virtual void stop() { }
55    virtual bool hasPendingActivity() { return false; }
56
57    static bool isAvailable();
58
59    // Subclass must implement this if it supports synchronous operations.
60    // This should return false if there are no pending operations.
61    virtual bool waitForOperationToComplete() { return false; }
62
63    // Creates and returns a new platform-specific AsyncFileSystem instance if the platform has its own implementation.
64    static PassOwnPtr<AsyncFileSystem> create();
65
66    // Opens a new file system. The create parameter specifies whether or not to create the path if it does not already exists.
67    static void openFileSystem(const String& basePath, const String& storageIdentifier, FileSystemType, bool create, PassOwnPtr<AsyncFileSystemCallbacks>);
68
69    // Deletes the file system.
70    static void deleteFileSystem(const String& basePath, const String& storageIdentifier, FileSystemType, PassOwnPtr<AsyncFileSystemCallbacks>);
71
72    // Moves a file or directory from srcPath to destPath.
73    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
74    // AsyncFileSystemCallbacks::didFail() is called otherwise.
75    virtual void move(const KURL& srcPath, const KURL& destPath, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
76
77    // Copies a file or directory from srcPath to destPath.
78    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
79    // AsyncFileSystemCallbacks::didFail() is called otherwise.
80    virtual void copy(const KURL& srcPath, const KURL& destPath, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
81
82    // Deletes a file or directory at a given path.
83    // It is an error to try to remove a directory that is not empty.
84    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
85    // AsyncFileSystemCallbacks::didFail() is called otherwise.
86    virtual void remove(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
87
88    // Recursively deletes a directory at a given path.
89    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
90    // AsyncFileSystemCallbacks::didFail() is called otherwise.
91    virtual void removeRecursively(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
92
93    // Retrieves the metadata information of the file or directory at a given path.
94    // AsyncFileSystemCallbacks::didReadMetadata() is called when the operation is completed successfully.
95    // AsyncFileSystemCallbacks::didFail() is called otherwise.
96    virtual void readMetadata(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
97
98    // Creates a file at a given path.  If exclusive flag is true, it fails if the path already exists.
99    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
100    // AsyncFileSystemCallbacks::didFail() is called otherwise.
101    virtual void createFile(const KURL& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
102
103    // Creates a directory at a given path.  If exclusive flag is true, it fails if the path already exists.
104    // AsyncFileSystemCallbacks::didSucceed() is called when the operation is completed successfully.
105    // AsyncFileSystemCallbacks::didFail() is called otherwise.
106    virtual void createDirectory(const KURL& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
107
108    // Checks if a file exists at a given path.
109    // AsyncFileSystemCallbacks::didSucceed() is called if the file exists.
110    // AsyncFileSystemCallbacks::didFail() is called otherwise.
111    virtual void fileExists(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
112
113    // Checks if a directory exists at a given path.
114    // AsyncFileSystemCallbacks::didSucceed() is called if the directory exists.
115    // AsyncFileSystemCallbacks::didFail() is called otherwise.
116    virtual void directoryExists(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
117
118    // Reads directory entries of a given directory at path.
119    // AsyncFileSystemCallbacks::didReadDirectoryEntry() is called when each directory entry is called. AsyncFileSystemCallbacks::didReadDirectoryEntries() is called after a chunk of directory entries have been read.
120    // AsyncFileSystemCallbacks::didFail() is when there is an error.
121    virtual void readDirectory(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
122
123    // Creates an AsyncFileWriter for a given file path.
124    // AsyncFileSystemCallbacks::didCreateFileWriter() is called when an AsyncFileWriter is created successfully.
125    // AsyncFileSystemCallbacks::didFail() is called otherwise.
126    virtual void createWriter(AsyncFileWriterClient*, const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
127
128    // Creates a snapshot file and read its metadata for a new File object.
129    // In local filesystem cases the backend may simply return the metadata of the file itself (as well as readMetadata does),
130    // while in remote filesystem case the backend may download the file into a temporary snapshot file and return the metadata of the temporary file.
131    // AsyncFileSystemCallbacks::didReadMetadata() is called when the metadata for the snapshot file is successfully returned.
132    // AsyncFileSystemCallbacks::didFail() is called otherwise.
133    //
134    // Note: the returned metadata info is cached in the File object for non-regular filesystem types (neither Temporary nor Persistent). The port could return valid metadata if it wants File object to cache metadata (e.g. if the file body is on a remote server), but otherwise should NOT return valid metadata.
135    virtual void createSnapshotFileAndReadMetadata(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks>) = 0;
136
137protected:
138    AsyncFileSystem() { }
139};
140
141} // namespace WebCore
142
143#endif // ENABLE(FILE_SYSTEM)
144
145#endif // AsyncFileSystem_h
146