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 DOMFileSystem_h
     32 #define DOMFileSystem_h
     33 
     34 #include "bindings/v8/ScriptWrappable.h"
     35 #include "core/dom/ActiveDOMObject.h"
     36 #include "core/dom/ScriptExecutionContext.h"
     37 #include "modules/filesystem/DOMFileSystemBase.h"
     38 #include "modules/filesystem/EntriesCallback.h"
     39 
     40 namespace WebCore {
     41 
     42 class DirectoryEntry;
     43 class File;
     44 class FileCallback;
     45 class FileEntry;
     46 class FileWriterCallback;
     47 
     48 class DOMFileSystem : public DOMFileSystemBase, public ScriptWrappable, public ActiveDOMObject {
     49 public:
     50     static PassRefPtr<DOMFileSystem> create(ScriptExecutionContext*, const String& name, FileSystemType, const KURL& rootURL, PassOwnPtr<AsyncFileSystem>);
     51 
     52     // Creates a new isolated file system for the given filesystemId.
     53     static PassRefPtr<DOMFileSystem> createIsolatedFileSystem(ScriptExecutionContext*, const String& filesystemId);
     54 
     55     PassRefPtr<DirectoryEntry> root();
     56 
     57     // ActiveDOMObject methods.
     58     virtual void stop();
     59     virtual bool hasPendingActivity() const;
     60     virtual void contextDestroyed();
     61 
     62     void createWriter(const FileEntry*, PassRefPtr<FileWriterCallback>, PassRefPtr<ErrorCallback>);
     63     void createFile(const FileEntry*, PassRefPtr<FileCallback>, PassRefPtr<ErrorCallback>);
     64 
     65     // Schedule a callback. This should not cross threads (should be called on the same context thread).
     66     // FIXME: move this to a more generic place.
     67     template <typename CB, typename CBArg>
     68     static void scheduleCallback(ScriptExecutionContext*, PassRefPtr<CB>, PassRefPtr<CBArg>);
     69 
     70     template <typename CB, typename CBArg>
     71     static void scheduleCallback(ScriptExecutionContext*, PassRefPtr<CB>, const CBArg&);
     72 
     73     template <typename CB, typename CBArg>
     74     void scheduleCallback(PassRefPtr<CB> callback, PassRefPtr<CBArg> callbackArg)
     75     {
     76         scheduleCallback(scriptExecutionContext(), callback, callbackArg);
     77     }
     78 
     79     template <typename CB, typename CBArg>
     80     void scheduleCallback(PassRefPtr<CB> callback,  const CBArg& callbackArg)
     81     {
     82         scheduleCallback(scriptExecutionContext(), callback, callbackArg);
     83     }
     84 
     85 private:
     86     DOMFileSystem(ScriptExecutionContext*, const String& name, FileSystemType, const KURL& rootURL, PassOwnPtr<AsyncFileSystem>);
     87 
     88     // A helper template to schedule a callback task.
     89     template <typename CB, typename CBArg>
     90     class DispatchCallbacRefPtrArgTask : public ScriptExecutionContext::Task {
     91     public:
     92         DispatchCallbacRefPtrArgTask(PassRefPtr<CB> callback, PassRefPtr<CBArg> arg)
     93             : m_callback(callback)
     94             , m_callbackArg(arg)
     95         {
     96         }
     97 
     98         virtual void performTask(ScriptExecutionContext*)
     99         {
    100             m_callback->handleEvent(m_callbackArg.get());
    101         }
    102 
    103     private:
    104         RefPtr<CB> m_callback;
    105         RefPtr<CBArg> m_callbackArg;
    106     };
    107 
    108     template <typename CB, typename CBArg>
    109     class DispatchCallbackNonPtrArgTask : public ScriptExecutionContext::Task {
    110     public:
    111         DispatchCallbackNonPtrArgTask(PassRefPtr<CB> callback, const CBArg& arg)
    112             : m_callback(callback)
    113             , m_callbackArg(arg)
    114         {
    115         }
    116 
    117         virtual void performTask(ScriptExecutionContext*)
    118         {
    119             m_callback->handleEvent(m_callbackArg);
    120         }
    121 
    122     private:
    123         RefPtr<CB> m_callback;
    124         CBArg m_callbackArg;
    125     };
    126 };
    127 
    128 template <typename CB, typename CBArg>
    129 void DOMFileSystem::scheduleCallback(ScriptExecutionContext* scriptExecutionContext, PassRefPtr<CB> callback, PassRefPtr<CBArg> arg)
    130 {
    131     ASSERT(scriptExecutionContext->isContextThread());
    132     if (callback)
    133         scriptExecutionContext->postTask(adoptPtr(new DispatchCallbacRefPtrArgTask<CB, CBArg>(callback, arg)));
    134 }
    135 
    136 template <typename CB, typename CBArg>
    137 void DOMFileSystem::scheduleCallback(ScriptExecutionContext* scriptExecutionContext, PassRefPtr<CB> callback, const CBArg& arg)
    138 {
    139     ASSERT(scriptExecutionContext->isContextThread());
    140     if (callback)
    141         scriptExecutionContext->postTask(adoptPtr(new DispatchCallbackNonPtrArgTask<CB, CBArg>(callback, arg)));
    142 }
    143 
    144 } // namespace
    145 
    146 #endif // DOMFileSystem_h
    147