Home | History | Annotate | Download | only in exported
      1 /*
      2  * Copyright (C) 2013 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 "public/platform/WebFileSystemCallbacks.h"
     33 
     34 #include "platform/AsyncFileSystemCallbacks.h"
     35 #include "platform/FileMetadata.h"
     36 #include "public/platform/WebFileInfo.h"
     37 #include "public/platform/WebFileSystem.h"
     38 #include "public/platform/WebFileSystemEntry.h"
     39 #include "public/platform/WebFileWriter.h"
     40 #include "public/platform/WebString.h"
     41 #include "wtf/PassOwnPtr.h"
     42 #include "wtf/PassRefPtr.h"
     43 #include "wtf/RefCounted.h"
     44 
     45 using namespace WebCore;
     46 
     47 namespace blink {
     48 
     49 class WebFileSystemCallbacksPrivate : public RefCounted<WebFileSystemCallbacksPrivate> {
     50 public:
     51     static PassRefPtr<WebFileSystemCallbacksPrivate> create(const PassOwnPtr<AsyncFileSystemCallbacks>& callbacks)
     52     {
     53         return adoptRef(new WebFileSystemCallbacksPrivate(callbacks));
     54     }
     55 
     56     AsyncFileSystemCallbacks* callbacks() { return m_callbacks.get(); }
     57 
     58 private:
     59     WebFileSystemCallbacksPrivate(const PassOwnPtr<AsyncFileSystemCallbacks>& callbacks) : m_callbacks(callbacks) { }
     60     OwnPtr<AsyncFileSystemCallbacks> m_callbacks;
     61 };
     62 
     63 WebFileSystemCallbacks::WebFileSystemCallbacks(const PassOwnPtr<AsyncFileSystemCallbacks>& callbacks)
     64 {
     65     m_private = WebFileSystemCallbacksPrivate::create(callbacks);
     66 }
     67 
     68 void WebFileSystemCallbacks::reset()
     69 {
     70     m_private.reset();
     71 }
     72 
     73 void WebFileSystemCallbacks::assign(const WebFileSystemCallbacks& other)
     74 {
     75     m_private = other.m_private;
     76 }
     77 
     78 void WebFileSystemCallbacks::didSucceed()
     79 {
     80     ASSERT(!m_private.isNull());
     81     m_private->callbacks()->didSucceed();
     82     m_private.reset();
     83 }
     84 
     85 void WebFileSystemCallbacks::didReadMetadata(const WebFileInfo& webFileInfo)
     86 {
     87     ASSERT(!m_private.isNull());
     88     FileMetadata fileMetadata;
     89     fileMetadata.modificationTime = webFileInfo.modificationTime;
     90     fileMetadata.length = webFileInfo.length;
     91     fileMetadata.type = static_cast<FileMetadata::Type>(webFileInfo.type);
     92     fileMetadata.platformPath = webFileInfo.platformPath;
     93     m_private->callbacks()->didReadMetadata(fileMetadata);
     94     m_private.reset();
     95 }
     96 
     97 void WebFileSystemCallbacks::didCreateSnapshotFile(const WebFileInfo& webFileInfo)
     98 {
     99     ASSERT(!m_private.isNull());
    100     // It's important to create a BlobDataHandle that refers to the platform file path prior
    101     // to return from this method so the underlying file will not be deleted.
    102     OwnPtr<BlobData> blobData = BlobData::create();
    103     blobData->appendFile(webFileInfo.platformPath);
    104     RefPtr<BlobDataHandle> snapshotBlob = BlobDataHandle::create(blobData.release(), webFileInfo.length);
    105 
    106     FileMetadata fileMetadata;
    107     fileMetadata.modificationTime = webFileInfo.modificationTime;
    108     fileMetadata.length = webFileInfo.length;
    109     fileMetadata.type = static_cast<FileMetadata::Type>(webFileInfo.type);
    110     fileMetadata.platformPath = webFileInfo.platformPath;
    111     m_private->callbacks()->didCreateSnapshotFile(fileMetadata, snapshotBlob);
    112     m_private.reset();
    113 }
    114 
    115 void WebFileSystemCallbacks::didReadDirectory(const WebVector<WebFileSystemEntry>& entries, bool hasMore)
    116 {
    117     ASSERT(!m_private.isNull());
    118     for (size_t i = 0; i < entries.size(); ++i)
    119         m_private->callbacks()->didReadDirectoryEntry(entries[i].name, entries[i].isDirectory);
    120     m_private->callbacks()->didReadDirectoryEntries(hasMore);
    121     m_private.reset();
    122 }
    123 
    124 void WebFileSystemCallbacks::didOpenFileSystem(const WebString& name, const WebURL& rootURL)
    125 {
    126     ASSERT(!m_private.isNull());
    127     m_private->callbacks()->didOpenFileSystem(name, rootURL);
    128     m_private.reset();
    129 }
    130 
    131 void WebFileSystemCallbacks::didResolveURL(const WebString& name, const WebURL& rootURL, WebFileSystemType type, const WebString& filePath, bool isDirectory)
    132 {
    133     ASSERT(!m_private.isNull());
    134     m_private->callbacks()->didResolveURL(name, rootURL, static_cast<WebCore::FileSystemType>(type), filePath, isDirectory);
    135     m_private.reset();
    136 }
    137 
    138 void WebFileSystemCallbacks::didCreateFileWriter(WebFileWriter* webFileWriter, long long length)
    139 {
    140     ASSERT(!m_private.isNull());
    141     m_private->callbacks()->didCreateFileWriter(adoptPtr(webFileWriter), length);
    142     m_private.reset();
    143 }
    144 
    145 void WebFileSystemCallbacks::didFail(WebFileError error)
    146 {
    147     ASSERT(!m_private.isNull());
    148     m_private->callbacks()->didFail(error);
    149     m_private.reset();
    150 }
    151 
    152 bool WebFileSystemCallbacks::shouldBlockUntilCompletion() const
    153 {
    154     ASSERT(!m_private.isNull());
    155     return m_private->callbacks()->shouldBlockUntilCompletion();
    156 }
    157 
    158 } // namespace blink
    159