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