Home | History | Annotate | Download | only in filesystem
      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 "core/platform/AsyncFileSystem.h"
     35 #include "modules/filesystem/FileSystemFlags.h"
     36 #include "modules/filesystem/FileSystemType.h"
     37 #include "weborigin/KURL.h"
     38 #include "wtf/PassRefPtr.h"
     39 #include "wtf/RefCounted.h"
     40 #include "wtf/text/WTFString.h"
     41 
     42 namespace WebCore {
     43 
     44 class DirectoryEntry;
     45 class DirectoryReaderBase;
     46 class EntriesCallback;
     47 class EntryBase;
     48 class EntryCallback;
     49 class ErrorCallback;
     50 class MetadataCallback;
     51 class ScriptExecutionContext;
     52 class SecurityOrigin;
     53 class VoidCallback;
     54 
     55 // A common base class for DOMFileSystem and DOMFileSystemSync.
     56 class DOMFileSystemBase : public RefCounted<DOMFileSystemBase> {
     57 public:
     58     enum SynchronousType {
     59         Synchronous,
     60         Asynchronous,
     61     };
     62 
     63     // Path prefixes that are used in the filesystem URLs (that can be obtained by toURL()).
     64     // http://www.w3.org/TR/file-system-api/#widl-Entry-toURL
     65     static const char persistentPathPrefix[];
     66     static const char temporaryPathPrefix[];
     67     static const char isolatedPathPrefix[];
     68     static const char externalPathPrefix[];
     69 
     70     static PassRefPtr<DOMFileSystemBase> create(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
     71     {
     72         return adoptRef(new DOMFileSystemBase(context, name, type, rootURL, asyncFileSystem));
     73     }
     74     virtual ~DOMFileSystemBase();
     75 
     76     const String& name() const { return m_name; }
     77     FileSystemType type() const { return m_type; }
     78     KURL rootURL() const { return m_filesystemRootURL; }
     79     AsyncFileSystem* asyncFileSystem() const { return m_asyncFileSystem.get(); }
     80     SecurityOrigin* securityOrigin() const;
     81 
     82     // The clonable flag is used in the structured clone algorithm to test
     83     // whether the FileSystem API object is permitted to be cloned. It defaults
     84     // to false, and must be explicitly set by internal code permit cloning.
     85     void makeClonable() { m_clonable = true; }
     86     bool clonable() const { return m_clonable; }
     87 
     88     static bool isValidType(FileSystemType);
     89     static bool crackFileSystemURL(const KURL&, FileSystemType&, String& filePath);
     90     bool supportsToURL() const;
     91     KURL createFileSystemURL(const EntryBase*) const;
     92     KURL createFileSystemURL(const String& fullPath) const;
     93 
     94     // Actual FileSystem API implementations. All the validity checks on virtual paths are done at this level.
     95     // They return false for immediate errors that don't involve lower AsyncFileSystem layer (e.g. for name validation errors). Otherwise they return true (but later may call back with an runtime error).
     96     bool getMetadata(const EntryBase*, PassRefPtr<MetadataCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
     97     bool move(const EntryBase* source, EntryBase* parent, const String& name, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
     98     bool copy(const EntryBase* source, EntryBase* parent, const String& name, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
     99     bool remove(const EntryBase*, PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
    100     bool removeRecursively(const EntryBase*, PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
    101     bool getParent(const EntryBase*, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>);
    102     bool getFile(const EntryBase*, const String& path, const FileSystemFlags&, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
    103     bool getDirectory(const EntryBase*, const String& path, const FileSystemFlags&, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
    104     bool readDirectory(PassRefPtr<DirectoryReaderBase>, const String& path, PassRefPtr<EntriesCallback>, PassRefPtr<ErrorCallback>, SynchronousType = Asynchronous);
    105 
    106 protected:
    107     DOMFileSystemBase(ScriptExecutionContext*, const String& name, FileSystemType, const KURL& rootURL, PassOwnPtr<AsyncFileSystem>);
    108     friend class DOMFileSystemSync;
    109 
    110     ScriptExecutionContext* m_context;
    111     String m_name;
    112     FileSystemType m_type;
    113     KURL m_filesystemRootURL;
    114     bool m_clonable;
    115 
    116     mutable OwnPtr<AsyncFileSystem> m_asyncFileSystem;
    117 };
    118 
    119 inline bool operator==(const DOMFileSystemBase& a, const DOMFileSystemBase& b) { return a.name() == b.name() && a.type() == b.type() && a.rootURL() == b.rootURL(); }
    120 
    121 } // namespace WebCore
    122 
    123 #endif // DOMFileSystemBase_h
    124