Home | History | Annotate | Download | only in src
      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 #include "config.h"
     31 #include "AsyncFileSystemChromium.h"
     32 
     33 #if ENABLE(FILE_SYSTEM)
     34 
     35 #include "AsyncFileSystemCallbacks.h"
     36 #include "AsyncFileWriterChromium.h"
     37 #include "WebFileInfo.h"
     38 #include "WebFileSystem.h"
     39 #include "WebFileSystemCallbacksImpl.h"
     40 #include "WebFileWriter.h"
     41 #include "WebKit.h"
     42 #include "WebKitClient.h"
     43 
     44 #include <wtf/text/CString.h>
     45 
     46 namespace WebCore {
     47 
     48 bool AsyncFileSystem::isAvailable()
     49 {
     50     return true;
     51 }
     52 
     53 AsyncFileSystemChromium::AsyncFileSystemChromium(AsyncFileSystem::Type type, const String& rootPath)
     54     : AsyncFileSystem(type, rootPath)
     55     , m_webFileSystem(WebKit::webKitClient()->fileSystem())
     56 {
     57     ASSERT(m_webFileSystem);
     58 }
     59 
     60 AsyncFileSystemChromium::~AsyncFileSystemChromium()
     61 {
     62 }
     63 
     64 void AsyncFileSystemChromium::move(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     65 {
     66     m_webFileSystem->move(sourcePath, destinationPath, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     67 }
     68 
     69 void AsyncFileSystemChromium::copy(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     70 {
     71     m_webFileSystem->copy(sourcePath, destinationPath, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     72 }
     73 
     74 void AsyncFileSystemChromium::remove(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     75 {
     76     m_webFileSystem->remove(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     77 }
     78 
     79 void AsyncFileSystemChromium::removeRecursively(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     80 {
     81     m_webFileSystem->removeRecursively(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     82 }
     83 
     84 void AsyncFileSystemChromium::readMetadata(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     85 {
     86     m_webFileSystem->readMetadata(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     87 }
     88 
     89 void AsyncFileSystemChromium::createFile(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     90 {
     91     m_webFileSystem->createFile(path, exclusive, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     92 }
     93 
     94 void AsyncFileSystemChromium::createDirectory(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
     95 {
     96     m_webFileSystem->createDirectory(path, exclusive, new WebKit::WebFileSystemCallbacksImpl(callbacks));
     97 }
     98 
     99 void AsyncFileSystemChromium::fileExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
    100 {
    101     m_webFileSystem->fileExists(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
    102 }
    103 
    104 void AsyncFileSystemChromium::directoryExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
    105 {
    106     m_webFileSystem->directoryExists(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
    107 }
    108 
    109 void AsyncFileSystemChromium::readDirectory(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
    110 {
    111     m_webFileSystem->readDirectory(path, new WebKit::WebFileSystemCallbacksImpl(callbacks));
    112 }
    113 
    114 class FileWriterHelperCallbacks : public WebKit::WebFileSystemCallbacks {
    115 public:
    116     FileWriterHelperCallbacks(AsyncFileWriterClient* client, const String& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks)
    117         : m_client(client)
    118         , m_path(path)
    119         , m_webFileSystem(webFileSystem)
    120         , m_callbacks(callbacks)
    121     {
    122     }
    123 
    124     virtual void didSucceed()
    125     {
    126         ASSERT_NOT_REACHED();
    127         delete this;
    128     }
    129     virtual void didReadMetadata(const WebKit::WebFileInfo& info)
    130     {
    131         ASSERT(m_callbacks);
    132         if (info.type != WebKit::WebFileInfo::TypeFile || info.length < 0)
    133             m_callbacks->didFail(WebKit::WebFileErrorInvalidState);
    134         else {
    135             OwnPtr<AsyncFileWriterChromium> asyncFileWriterChromium = adoptPtr(new AsyncFileWriterChromium(m_client));
    136             OwnPtr<WebKit::WebFileWriter> webFileWriter = adoptPtr(m_webFileSystem->createFileWriter(m_path, asyncFileWriterChromium.get()));
    137             asyncFileWriterChromium->setWebFileWriter(webFileWriter.release());
    138             m_callbacks->didCreateFileWriter(asyncFileWriterChromium.release(), info.length);
    139         }
    140         delete this;
    141     }
    142 
    143     virtual void didReadDirectory(const WebKit::WebVector<WebKit::WebFileSystemEntry>& entries, bool hasMore)
    144     {
    145         ASSERT_NOT_REACHED();
    146         delete this;
    147     }
    148     virtual void didOpenFileSystem(const WebKit::WebString& name, const WebKit::WebString& rootPath)
    149     {
    150         ASSERT_NOT_REACHED();
    151         delete this;
    152     }
    153 
    154     virtual void didFail(WebKit::WebFileError error)
    155     {
    156         ASSERT(m_callbacks);
    157         m_callbacks->didFail(error);
    158         delete this;
    159     }
    160 
    161 private:
    162     AsyncFileWriterClient* m_client;
    163     String m_path;
    164     WebKit::WebFileSystem* m_webFileSystem;
    165     OwnPtr<WebCore::AsyncFileSystemCallbacks> m_callbacks;
    166 };
    167 
    168 void AsyncFileSystemChromium::createWriter(AsyncFileWriterClient* client, const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
    169 {
    170     m_webFileSystem->readMetadata(path, new FileWriterHelperCallbacks(client, path, m_webFileSystem, callbacks));
    171 }
    172 
    173 } // namespace WebCore
    174 
    175 #endif
    176