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 #include "config.h"
     32 #include "DOMFileSystem.h"
     33 
     34 #if ENABLE(FILE_SYSTEM)
     35 
     36 #include "AsyncFileSystem.h"
     37 #include "DOMFilePath.h"
     38 #include "DirectoryEntry.h"
     39 #include "ErrorCallback.h"
     40 #include "File.h"
     41 #include "FileEntry.h"
     42 #include "FileMetadata.h"
     43 #include "FileSystemCallbacks.h"
     44 #include "FileWriter.h"
     45 #include "FileWriterBaseCallback.h"
     46 #include "FileWriterCallback.h"
     47 #include "MetadataCallback.h"
     48 #include "ScriptExecutionContext.h"
     49 #include <wtf/OwnPtr.h>
     50 
     51 namespace WebCore {
     52 
     53 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
     54     : DOMFileSystemBase(context, name, asyncFileSystem)
     55     , ActiveDOMObject(context, this)
     56 {
     57 }
     58 
     59 PassRefPtr<DirectoryEntry> DOMFileSystem::root()
     60 {
     61     return DirectoryEntry::create(this, DOMFilePath::root);
     62 }
     63 
     64 void DOMFileSystem::stop()
     65 {
     66     m_asyncFileSystem->stop();
     67 }
     68 
     69 bool DOMFileSystem::hasPendingActivity() const
     70 {
     71     return m_asyncFileSystem->hasPendingActivity();
     72 }
     73 
     74 void DOMFileSystem::contextDestroyed()
     75 {
     76     m_asyncFileSystem->stop();
     77     ActiveDOMObject::contextDestroyed();
     78 }
     79 
     80 namespace {
     81 
     82 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
     83 public:
     84     static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterCallback> callback)
     85     {
     86         return adoptRef(new ConvertToFileWriterCallback(callback));
     87     }
     88 
     89     bool handleEvent(FileWriterBase* fileWriterBase)
     90     {
     91         return m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
     92     }
     93 private:
     94     ConvertToFileWriterCallback(PassRefPtr<FileWriterCallback> callback)
     95         : m_callback(callback)
     96     {
     97     }
     98     RefPtr<FileWriterCallback> m_callback;
     99 };
    100 
    101 }
    102 
    103 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWriterCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
    104 {
    105     ASSERT(fileEntry);
    106 
    107     String platformPath = m_asyncFileSystem->virtualToPlatformPath(fileEntry->fullPath());
    108 
    109     RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext());
    110     RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback);
    111     OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback, errorCallback);
    112     m_asyncFileSystem->createWriter(fileWriter.get(), platformPath, callbacks.release());
    113 }
    114 
    115 namespace {
    116 
    117 class GetPathCallback : public FileSystemCallbacksBase {
    118 public:
    119     static PassOwnPtr<GetPathCallback> create(PassRefPtr<DOMFileSystem> filesystem, const String& path, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
    120     {
    121         return adoptPtr(new GetPathCallback(filesystem, path, successCallback, errorCallback));
    122     }
    123 
    124     virtual void didReadMetadata(const FileMetadata& metadata)
    125     {
    126         if (!metadata.platformPath.isEmpty())
    127             m_path = metadata.platformPath;
    128 
    129         m_filesystem->scheduleCallback(m_successCallback.release(), File::create(m_path));
    130     }
    131 
    132 private:
    133     GetPathCallback(PassRefPtr<DOMFileSystem> filesystem, const String& path, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
    134         : FileSystemCallbacksBase(errorCallback)
    135         , m_filesystem(filesystem)
    136         , m_path(path)
    137         , m_successCallback(successCallback)
    138     {
    139     }
    140 
    141     RefPtr<DOMFileSystem> m_filesystem;
    142     String m_path;
    143     RefPtr<FileCallback> m_successCallback;
    144 };
    145 
    146 } // namespace
    147 
    148 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
    149 {
    150     String platformPath = m_asyncFileSystem->virtualToPlatformPath(fileEntry->fullPath());
    151 
    152     m_asyncFileSystem->readMetadata(platformPath, GetPathCallback::create(this, platformPath, successCallback, errorCallback));
    153 }
    154 
    155 } // namespace WebCore
    156 
    157 #endif // ENABLE(FILE_SYSTEM)
    158