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 #include "config.h"
     32 #include "modules/filesystem/DOMFileSystemSync.h"
     33 
     34 #include "bindings/v8/ExceptionState.h"
     35 #include "core/dom/ExceptionCode.h"
     36 #include "core/fileapi/File.h"
     37 #include "core/fileapi/FileError.h"
     38 #include "core/platform/AsyncFileSystem.h"
     39 #include "core/platform/FileMetadata.h"
     40 #include "modules/filesystem/AsyncFileWriter.h"
     41 #include "modules/filesystem/DOMFilePath.h"
     42 #include "modules/filesystem/DirectoryEntrySync.h"
     43 #include "modules/filesystem/ErrorCallback.h"
     44 #include "modules/filesystem/FileEntrySync.h"
     45 #include "modules/filesystem/FileSystemCallbacks.h"
     46 #include "modules/filesystem/FileWriterBaseCallback.h"
     47 #include "modules/filesystem/FileWriterSync.h"
     48 
     49 namespace WebCore {
     50 
     51 class FileWriterBase;
     52 
     53 PassRefPtr<DOMFileSystemSync> DOMFileSystemSync::create(DOMFileSystemBase* fileSystem)
     54 {
     55     return adoptRef(new DOMFileSystemSync(fileSystem->m_context, fileSystem->name(), fileSystem->type(), fileSystem->rootURL(), fileSystem->m_asyncFileSystem.release()));
     56 }
     57 
     58 DOMFileSystemSync::DOMFileSystemSync(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
     59     : DOMFileSystemBase(context, name, type, rootURL, asyncFileSystem)
     60 {
     61     ScriptWrappable::init(this);
     62 }
     63 
     64 DOMFileSystemSync::~DOMFileSystemSync()
     65 {
     66 }
     67 
     68 PassRefPtr<DirectoryEntrySync> DOMFileSystemSync::root()
     69 {
     70     return DirectoryEntrySync::create(this, DOMFilePath::root);
     71 }
     72 
     73 namespace {
     74 
     75 class CreateFileHelper : public AsyncFileSystemCallbacks {
     76 public:
     77     class CreateFileResult : public RefCounted<CreateFileResult> {
     78       public:
     79         static PassRefPtr<CreateFileResult> create()
     80         {
     81             return adoptRef(new CreateFileResult());
     82         }
     83 
     84         bool m_failed;
     85         int m_code;
     86         RefPtr<File> m_file;
     87 
     88       private:
     89         CreateFileResult()
     90             : m_failed(false)
     91             , m_code(0)
     92         {
     93         }
     94 
     95         ~CreateFileResult()
     96         {
     97         }
     98         friend class WTF::RefCounted<CreateFileResult>;
     99     };
    100 
    101     static PassOwnPtr<CreateFileHelper> create(PassRefPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type)
    102     {
    103         return adoptPtr(new CreateFileHelper(result, name, url, type));
    104     }
    105 
    106     virtual void didFail(int code)
    107     {
    108         m_result->m_failed = true;
    109         m_result->m_code = code;
    110     }
    111 
    112     virtual ~CreateFileHelper()
    113     {
    114     }
    115 
    116     virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot)
    117     {
    118         // We can't directly use the snapshot blob data handle because the content type on it hasn't been set.
    119         // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until
    120         // *after* we've coined a File with a new handle that has the correct type set on it. This allows the
    121         // blob storage system to track when a temp file can and can't be safely deleted.
    122 
    123         // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics.
    124         // For other filesystem types (which could be platform-specific ones), there's a chance that the files are on remote filesystem.
    125         // If the port has returned metadata just pass it to File constructor (so we may cache the metadata).
    126         // FIXME: We should use the snapshot metadata for all files.
    127         // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746
    128         if (m_type == FileSystemTypeTemporary || m_type == FileSystemTypePersistent) {
    129             m_result->m_file = File::createWithName(metadata.platformPath, m_name);
    130         } else if (!metadata.platformPath.isEmpty()) {
    131             // If the platformPath in the returned metadata is given, we create a File object for the path.
    132             m_result->m_file = File::createForFileSystemFile(m_name, metadata).get();
    133         } else {
    134             // Otherwise create a File from the FileSystem URL.
    135             m_result->m_file = File::createForFileSystemFile(m_url, metadata).get();
    136         }
    137     }
    138 private:
    139     CreateFileHelper(PassRefPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type)
    140         : m_result(result)
    141         , m_name(name)
    142         , m_url(url)
    143         , m_type(type)
    144     {
    145     }
    146 
    147     RefPtr<CreateFileResult> m_result;
    148     String m_name;
    149     KURL m_url;
    150     FileSystemType m_type;
    151 };
    152 
    153 } // namespace
    154 
    155 PassRefPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionState& es)
    156 {
    157     KURL fileSystemURL = createFileSystemURL(fileEntry);
    158     RefPtr<CreateFileHelper::CreateFileResult> result(CreateFileHelper::CreateFileResult::create());
    159     m_asyncFileSystem->createSnapshotFileAndReadMetadata(fileSystemURL, CreateFileHelper::create(result, fileEntry->name(), fileSystemURL, type()));
    160     if (!m_asyncFileSystem->waitForOperationToComplete()) {
    161         es.throwDOMException(AbortError, FileError::abortErrorMessage);
    162         return 0;
    163     }
    164     if (result->m_failed) {
    165         es.throwDOMException(result->m_code);
    166         return 0;
    167     }
    168     return result->m_file;
    169 }
    170 
    171 namespace {
    172 
    173 class ReceiveFileWriterCallback : public FileWriterBaseCallback {
    174 public:
    175     static PassRefPtr<ReceiveFileWriterCallback> create()
    176     {
    177         return adoptRef(new ReceiveFileWriterCallback());
    178     }
    179 
    180     bool handleEvent(FileWriterBase* fileWriterBase)
    181     {
    182 #ifndef NDEBUG
    183         m_fileWriterBase = fileWriterBase;
    184 #else
    185         ASSERT_UNUSED(fileWriterBase, fileWriterBase);
    186 #endif
    187         return true;
    188     }
    189 
    190 #ifndef NDEBUG
    191     FileWriterBase* fileWriterBase()
    192     {
    193         return m_fileWriterBase;
    194     }
    195 #endif
    196 
    197 private:
    198     ReceiveFileWriterCallback()
    199 #ifndef NDEBUG
    200         : m_fileWriterBase(0)
    201 #endif
    202     {
    203     }
    204 
    205 #ifndef NDEBUG
    206     FileWriterBase* m_fileWriterBase;
    207 #endif
    208 };
    209 
    210 class LocalErrorCallback : public ErrorCallback {
    211 public:
    212     static PassRefPtr<LocalErrorCallback> create()
    213     {
    214         return adoptRef(new LocalErrorCallback());
    215     }
    216 
    217     bool handleEvent(FileError* error)
    218     {
    219         m_error = error;
    220         return true;
    221     }
    222 
    223     FileError* error()
    224     {
    225         return m_error.get();
    226     }
    227 
    228 private:
    229     LocalErrorCallback()
    230     {
    231     }
    232     RefPtr<FileError> m_error;
    233 };
    234 
    235 }
    236 
    237 PassRefPtr<FileWriterSync> DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionState& es)
    238 {
    239     ASSERT(fileEntry);
    240 
    241     RefPtr<FileWriterSync> fileWriter = FileWriterSync::create();
    242     RefPtr<ReceiveFileWriterCallback> successCallback = ReceiveFileWriterCallback::create();
    243     RefPtr<LocalErrorCallback> errorCallback = LocalErrorCallback::create();
    244 
    245     OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback);
    246     callbacks->setShouldBlockUntilCompletion(true);
    247 
    248     m_asyncFileSystem->createWriter(fileWriter.get(), createFileSystemURL(fileEntry), callbacks.release());
    249     if (!m_asyncFileSystem->waitForOperationToComplete()) {
    250         es.throwDOMException(AbortError, FileError::abortErrorMessage);
    251         return 0;
    252     }
    253     if (errorCallback->error()) {
    254         ASSERT(!successCallback->fileWriterBase());
    255         FileError::ErrorCode errorCode = errorCallback->error()->code();
    256         if (errorCode)
    257             FileError::throwDOMException(es, errorCode);
    258         return 0;
    259     }
    260     ASSERT(successCallback->fileWriterBase());
    261     ASSERT(static_cast<FileWriterSync*>(successCallback->fileWriterBase()) == fileWriter.get());
    262     return fileWriter;
    263 }
    264 
    265 }
    266