Home | History | Annotate | Download | only in fileapi
      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 FileSystemCallbacks_h
     32 #define FileSystemCallbacks_h
     33 
     34 #if ENABLE(FILE_SYSTEM)
     35 
     36 #include "AsyncFileSystemCallbacks.h"
     37 #include "PlatformString.h"
     38 #include <wtf/PassRefPtr.h>
     39 #include <wtf/Vector.h>
     40 
     41 namespace WebCore {
     42 
     43 class AsyncFileWriter;
     44 class DOMFileSystemBase;
     45 class DirectoryReaderBase;
     46 class EntriesCallback;
     47 class EntryArray;
     48 class EntryCallback;
     49 class ErrorCallback;
     50 struct FileMetadata;
     51 class FileSystemCallback;
     52 class FileWriterBase;
     53 class FileWriterBaseCallback;
     54 class MetadataCallback;
     55 class ScriptExecutionContext;
     56 class VoidCallback;
     57 
     58 class FileSystemCallbacksBase : public AsyncFileSystemCallbacks {
     59 public:
     60     virtual ~FileSystemCallbacksBase();
     61 
     62     // For EntryCallbacks and VoidCallbacks.
     63     virtual void didSucceed();
     64 
     65     // For FileSystemCallbacks.
     66     virtual void didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem>);
     67 
     68     // For MetadataCallbacks.
     69     virtual void didReadMetadata(const FileMetadata&);
     70 
     71     // For EntriesCallbacks. didReadDirectoryEntry is called each time the API reads an entry, and didReadDirectoryDone is called when a chunk of entries have been read (i.e. good time to call back to the application).  If hasMore is true there can be more chunks.
     72     virtual void didReadDirectoryEntry(const String& name, bool isDirectory);
     73     virtual void didReadDirectoryEntries(bool hasMore);
     74 
     75     // For createFileWriter.
     76     virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long length);
     77 
     78     // For ErrorCallback.
     79     virtual void didFail(int code);
     80 
     81 protected:
     82     FileSystemCallbacksBase(PassRefPtr<ErrorCallback> errorCallback);
     83     RefPtr<ErrorCallback> m_errorCallback;
     84 };
     85 
     86 // Subclasses ----------------------------------------------------------------
     87 
     88 class EntryCallbacks : public FileSystemCallbacksBase {
     89 public:
     90     static PassOwnPtr<EntryCallbacks> create(PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, PassRefPtr<DOMFileSystemBase>, const String& expectedPath, bool isDirectory);
     91     virtual void didSucceed();
     92 
     93 private:
     94     EntryCallbacks(PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, PassRefPtr<DOMFileSystemBase>, const String& expectedPath, bool isDirectory);
     95     RefPtr<EntryCallback> m_successCallback;
     96     RefPtr<DOMFileSystemBase> m_fileSystem;
     97     String m_expectedPath;
     98     bool m_isDirectory;
     99 };
    100 
    101 class EntriesCallbacks : public FileSystemCallbacksBase {
    102 public:
    103     static PassOwnPtr<EntriesCallbacks> create(PassRefPtr<EntriesCallback>, PassRefPtr<ErrorCallback>, PassRefPtr<DirectoryReaderBase>, const String& basePath);
    104     virtual void didReadDirectoryEntry(const String& name, bool isDirectory);
    105     virtual void didReadDirectoryEntries(bool hasMore);
    106 
    107 private:
    108     EntriesCallbacks(PassRefPtr<EntriesCallback>, PassRefPtr<ErrorCallback>, PassRefPtr<DirectoryReaderBase>, const String& basePath);
    109     RefPtr<EntriesCallback> m_successCallback;
    110     RefPtr<DirectoryReaderBase> m_directoryReader;
    111     String m_basePath;
    112     RefPtr<EntryArray> m_entries;
    113 };
    114 
    115 class FileSystemCallbacks : public FileSystemCallbacksBase {
    116 public:
    117     static PassOwnPtr<FileSystemCallbacks> create(PassRefPtr<FileSystemCallback>, PassRefPtr<ErrorCallback>, ScriptExecutionContext*);
    118     virtual void didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem>);
    119 
    120 private:
    121     FileSystemCallbacks(PassRefPtr<FileSystemCallback>, PassRefPtr<ErrorCallback>, ScriptExecutionContext*);
    122     RefPtr<FileSystemCallback> m_successCallback;
    123     RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
    124 };
    125 
    126 class ResolveURICallbacks : public FileSystemCallbacksBase {
    127 public:
    128     static PassOwnPtr<ResolveURICallbacks> create(PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, ScriptExecutionContext*, const String& filePath);
    129     virtual void didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem>);
    130 
    131 private:
    132     ResolveURICallbacks(PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, ScriptExecutionContext*, const String& filePath);
    133     RefPtr<EntryCallback> m_successCallback;
    134     RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
    135     String m_filePath;
    136 };
    137 
    138 class MetadataCallbacks : public FileSystemCallbacksBase {
    139 public:
    140     static PassOwnPtr<MetadataCallbacks> create(PassRefPtr<MetadataCallback>, PassRefPtr<ErrorCallback>);
    141     virtual void didReadMetadata(const FileMetadata&);
    142 
    143 private:
    144     MetadataCallbacks(PassRefPtr<MetadataCallback>, PassRefPtr<ErrorCallback>);
    145     RefPtr<MetadataCallback> m_successCallback;
    146 };
    147 
    148 class FileWriterBaseCallbacks : public FileSystemCallbacksBase {
    149 public:
    150     static PassOwnPtr<FileWriterBaseCallbacks> create(PassRefPtr<FileWriterBase>, PassRefPtr<FileWriterBaseCallback>, PassRefPtr<ErrorCallback>);
    151     virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long length);
    152 
    153 private:
    154     FileWriterBaseCallbacks(PassRefPtr<FileWriterBase>, PassRefPtr<FileWriterBaseCallback>, PassRefPtr<ErrorCallback>);
    155     RefPtr<FileWriterBase> m_fileWriter;
    156     RefPtr<FileWriterBaseCallback> m_successCallback;
    157 };
    158 
    159 class VoidCallbacks : public FileSystemCallbacksBase {
    160 public:
    161     static PassOwnPtr<VoidCallbacks> create(PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>);
    162     virtual void didSucceed();
    163 
    164 private:
    165     VoidCallbacks(PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>);
    166     RefPtr<VoidCallback> m_successCallback;
    167 };
    168 
    169 } // namespace
    170 
    171 #endif // ENABLE(FILE_SYSTEM)
    172 
    173 #endif // FileSystemCallbacks_h
    174