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 DOMFileSystemBase_h
32#define DOMFileSystemBase_h
33
34#include "modules/filesystem/FileSystemFlags.h"
35#include "platform/FileSystemType.h"
36#include "platform/heap/Handle.h"
37#include "platform/weborigin/KURL.h"
38#include "wtf/text/WTFString.h"
39
40namespace blink {
41class WebFileSystem;
42}
43
44namespace blink {
45
46class DirectoryEntry;
47class DirectoryReaderBase;
48class EntriesCallback;
49class EntryBase;
50class EntryCallback;
51class ErrorCallback;
52class File;
53class FileError;
54struct FileMetadata;
55class MetadataCallback;
56class ExecutionContext;
57class SecurityOrigin;
58class VoidCallback;
59
60// A common base class for DOMFileSystem and DOMFileSystemSync.
61class DOMFileSystemBase : public GarbageCollectedFinalized<DOMFileSystemBase> {
62public:
63    enum SynchronousType {
64        Synchronous,
65        Asynchronous,
66    };
67
68    // Path prefixes that are used in the filesystem URLs (that can be obtained by toURL()).
69    // http://www.w3.org/TR/file-system-api/#widl-Entry-toURL
70    static const char persistentPathPrefix[];
71    static const char temporaryPathPrefix[];
72    static const char isolatedPathPrefix[];
73    static const char externalPathPrefix[];
74
75    virtual ~DOMFileSystemBase();
76
77    // These are called when a new callback is created and resolved in
78    // FileSystem API, so that subclasses can track the number of pending
79    // callbacks if necessary.
80    virtual void addPendingCallbacks() { }
81    virtual void removePendingCallbacks() { }
82
83    // Overridden by subclasses to handle sync vs async error-handling.
84    virtual void reportError(ErrorCallback*, PassRefPtrWillBeRawPtr<FileError>) = 0;
85
86    const String& name() const { return m_name; }
87    FileSystemType type() const { return m_type; }
88    KURL rootURL() const { return m_filesystemRootURL; }
89    blink::WebFileSystem* fileSystem() const;
90    SecurityOrigin* securityOrigin() const;
91
92    // The clonable flag is used in the structured clone algorithm to test
93    // whether the FileSystem API object is permitted to be cloned. It defaults
94    // to false, and must be explicitly set by internal code permit cloning.
95    void makeClonable() { m_clonable = true; }
96    bool clonable() const { return m_clonable; }
97
98    static bool isValidType(FileSystemType);
99    static bool crackFileSystemURL(const KURL&, FileSystemType&, String& filePath);
100    static KURL createFileSystemRootURL(const String& origin, FileSystemType);
101    bool supportsToURL() const;
102    KURL createFileSystemURL(const EntryBase*) const;
103    KURL createFileSystemURL(const String& fullPath) const;
104    static bool pathToAbsolutePath(FileSystemType, const EntryBase*, String path, String& absolutePath);
105    static bool pathPrefixToFileSystemType(const String& pathPrefix, FileSystemType&);
106    static PassRefPtrWillBeRawPtr<File> createFile(const FileMetadata&, const KURL& fileSystemURL, FileSystemType, const String name);
107
108    // Actual FileSystem API implementations. All the validity checks on virtual paths are done at this level.
109    void getMetadata(const EntryBase*, MetadataCallback*, ErrorCallback*, SynchronousType = Asynchronous);
110    void move(const EntryBase* source, EntryBase* parent, const String& name, EntryCallback*, ErrorCallback*, SynchronousType = Asynchronous);
111    void copy(const EntryBase* source, EntryBase* parent, const String& name, EntryCallback*, ErrorCallback*, SynchronousType = Asynchronous);
112    void remove(const EntryBase*, VoidCallback*, ErrorCallback*, SynchronousType = Asynchronous);
113    void removeRecursively(const EntryBase*, VoidCallback*, ErrorCallback*, SynchronousType = Asynchronous);
114    void getParent(const EntryBase*, EntryCallback*, ErrorCallback*);
115    void getFile(const EntryBase*, const String& path, const FileSystemFlags&, EntryCallback*, ErrorCallback*, SynchronousType = Asynchronous);
116    void getDirectory(const EntryBase*, const String& path, const FileSystemFlags&, EntryCallback*, ErrorCallback*, SynchronousType = Asynchronous);
117    int readDirectory(DirectoryReaderBase*, const String& path, EntriesCallback*, ErrorCallback*, SynchronousType = Asynchronous);
118    bool waitForAdditionalResult(int callbacksId);
119
120    virtual void trace(Visitor*) { }
121
122protected:
123    DOMFileSystemBase(ExecutionContext*, const String& name, FileSystemType, const KURL& rootURL);
124    friend class DOMFileSystemSync;
125
126    ExecutionContext* m_context;
127    String m_name;
128    FileSystemType m_type;
129    KURL m_filesystemRootURL;
130    bool m_clonable;
131};
132
133inline bool operator==(const DOMFileSystemBase& a, const DOMFileSystemBase& b) { return a.name() == b.name() && a.type() == b.type() && a.rootURL() == b.rootURL(); }
134
135} // namespace blink
136
137#endif // DOMFileSystemBase_h
138