~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/Modules/filesystem/DOMFileSystemBase.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "DOMFileSystemBase.h"
 
33
 
 
34
#if ENABLE(FILE_SYSTEM)
 
35
 
 
36
#include "DOMFilePath.h"
 
37
#include "DirectoryEntry.h"
 
38
#include "DirectoryReaderBase.h"
 
39
#include "EntriesCallback.h"
 
40
#include "EntryArray.h"
 
41
#include "EntryBase.h"
 
42
#include "EntryCallback.h"
 
43
#include "ErrorCallback.h"
 
44
#include "FileError.h"
 
45
#include "FileSystemCallbacks.h"
 
46
#include "MetadataCallback.h"
 
47
#include "ScriptExecutionContext.h"
 
48
#include "VoidCallback.h"
 
49
#include <wtf/OwnPtr.h>
 
50
 
 
51
namespace WebCore {
 
52
 
 
53
const char DOMFileSystemBase::persistentPathPrefix[] = "persistent";
 
54
const size_t DOMFileSystemBase::persistentPathPrefixLength = sizeof(DOMFileSystemBase::persistentPathPrefix) - 1;
 
55
const char DOMFileSystemBase::temporaryPathPrefix[] = "temporary";
 
56
const size_t DOMFileSystemBase::temporaryPathPrefixLength = sizeof(DOMFileSystemBase::temporaryPathPrefix) - 1;
 
57
const char DOMFileSystemBase::isolatedPathPrefix[] = "isolated";
 
58
const size_t DOMFileSystemBase::isolatedPathPrefixLength = sizeof(DOMFileSystemBase::isolatedPathPrefix) - 1;
 
59
 
 
60
DOMFileSystemBase::DOMFileSystemBase(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
 
61
    : m_context(context)
 
62
    , m_name(name)
 
63
    , m_type(type)
 
64
    , m_filesystemRootURL(rootURL)
 
65
    , m_clonable(false)
 
66
    , m_asyncFileSystem(asyncFileSystem)
 
67
{
 
68
}
 
69
 
 
70
DOMFileSystemBase::~DOMFileSystemBase()
 
71
{
 
72
}
 
73
 
 
74
#if !PLATFORM(CHROMIUM)
 
75
// static
 
76
bool DOMFileSystemBase::isValidType(FileSystemType type)
 
77
{
 
78
    return type == FileSystemTypeTemporary || type == FileSystemTypePersistent;
 
79
}
 
80
 
 
81
// static
 
82
bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath)
 
83
{
 
84
    if (!url.protocolIs("filesystem"))
 
85
        return false;
 
86
 
 
87
    if (!url.innerURL())
 
88
        return false;
 
89
 
 
90
    String typeString = url.innerURL()->path().substring(1);
 
91
    if (typeString == temporaryPathPrefix)
 
92
        type = FileSystemTypeTemporary;
 
93
    else if (typeString == persistentPathPrefix)
 
94
        type = FileSystemTypePersistent;
 
95
    else
 
96
        return false;
 
97
 
 
98
    filePath = decodeURLEscapeSequences(url.path());
 
99
    return true;
 
100
}
 
101
 
 
102
bool DOMFileSystemBase::supportsToURL() const
 
103
{
 
104
    ASSERT(isValidType(m_type));
 
105
    return true;
 
106
}
 
107
 
 
108
KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const
 
109
{
 
110
    ASSERT(DOMFilePath::isAbsolute(fullPath));
 
111
    KURL url = m_filesystemRootURL;
 
112
    // Remove the extra leading slash.
 
113
    url.setPath(url.path() + encodeWithURLEscapeSequences(fullPath.substring(1)));
 
114
    return url;
 
115
}
 
116
#endif
 
117
 
 
118
SecurityOrigin* DOMFileSystemBase::securityOrigin() const
 
119
{
 
120
    return m_context->securityOrigin();
 
121
}
 
122
 
 
123
KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const
 
124
{
 
125
    return createFileSystemURL(entry->fullPath());
 
126
}
 
127
 
 
128
bool DOMFileSystemBase::getMetadata(const EntryBase* entry, PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
129
{
 
130
    m_asyncFileSystem->readMetadata(createFileSystemURL(entry), MetadataCallbacks::create(successCallback, errorCallback));
 
131
    return true;
 
132
}
 
133
 
 
134
static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
 
135
{
 
136
    ASSERT(source);
 
137
 
 
138
    if (!parent || !parent->isDirectory())
 
139
        return false;
 
140
 
 
141
    if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
 
142
        return false;
 
143
 
 
144
    const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
 
145
 
 
146
    // It is an error to try to copy or move an entry inside itself at any depth if it is a directory.
 
147
    if (source->isDirectory() && isSameFileSystem && DOMFilePath::isParentOf(source->fullPath(), parent->fullPath()))
 
148
        return false;
 
149
 
 
150
    // It is an error to copy or move an entry into its parent if a name different from its current one isn't provided.
 
151
    if (isSameFileSystem && (newName.isEmpty() || source->name() == newName) && DOMFilePath::getDirectory(source->fullPath()) == parent->fullPath())
 
152
        return false;
 
153
 
 
154
    destinationPath = parent->fullPath();
 
155
    if (!newName.isEmpty())
 
156
        destinationPath = DOMFilePath::append(destinationPath, newName);
 
157
    else
 
158
        destinationPath = DOMFilePath::append(destinationPath, source->name());
 
159
 
 
160
    return true;
 
161
}
 
162
 
 
163
static bool pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath)
 
164
{
 
165
    ASSERT(base);
 
166
 
 
167
    if (!DOMFilePath::isAbsolute(path))
 
168
        path = DOMFilePath::append(base->fullPath(), path);
 
169
    absolutePath = DOMFilePath::removeExtraParentReferences(path);
 
170
 
 
171
    if ((type == FileSystemTypeTemporary || type == FileSystemTypePersistent) && !DOMFilePath::isValidPath(absolutePath))
 
172
        return false;
 
173
    return true;
 
174
}
 
175
 
 
176
bool DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
177
{
 
178
    String destinationPath;
 
179
    if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
 
180
        return false;
 
181
 
 
182
    m_asyncFileSystem->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
 
183
    return true;
 
184
}
 
185
 
 
186
bool DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
187
{
 
188
    String destinationPath;
 
189
    if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
 
190
        return false;
 
191
 
 
192
    m_asyncFileSystem->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
 
193
    return true;
 
194
}
 
195
 
 
196
bool DOMFileSystemBase::remove(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
197
{
 
198
    ASSERT(entry);
 
199
    // We don't allow calling remove() on the root directory.
 
200
    if (entry->fullPath() == String(DOMFilePath::root))
 
201
        return false;
 
202
    m_asyncFileSystem->remove(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
 
203
    return true;
 
204
}
 
205
 
 
206
bool DOMFileSystemBase::removeRecursively(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
207
{
 
208
    ASSERT(entry && entry->isDirectory());
 
209
    // We don't allow calling remove() on the root directory.
 
210
    if (entry->fullPath() == String(DOMFilePath::root))
 
211
        return false;
 
212
    m_asyncFileSystem->removeRecursively(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
 
213
    return true;
 
214
}
 
215
 
 
216
bool DOMFileSystemBase::getParent(const EntryBase* entry, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
217
{
 
218
    ASSERT(entry);
 
219
    String path = DOMFilePath::getDirectory(entry->fullPath());
 
220
 
 
221
    m_asyncFileSystem->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, this, path, true));
 
222
    return true;
 
223
}
 
224
 
 
225
bool DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
226
{
 
227
    String absolutePath;
 
228
    if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
 
229
        return false;
 
230
 
 
231
    OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, false);
 
232
    if (flags.create)
 
233
        m_asyncFileSystem->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
 
234
    else
 
235
        m_asyncFileSystem->fileExists(createFileSystemURL(absolutePath), callbacks.release());
 
236
    return true;
 
237
}
 
238
 
 
239
bool DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
240
{
 
241
    String absolutePath;
 
242
    if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
 
243
        return false;
 
244
 
 
245
    OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, true);
 
246
    if (flags.create)
 
247
        m_asyncFileSystem->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
 
248
    else
 
249
        m_asyncFileSystem->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
 
250
    return true;
 
251
}
 
252
 
 
253
bool DOMFileSystemBase::readDirectory(PassRefPtr<DirectoryReaderBase> reader, const String& path, PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
 
254
{
 
255
    ASSERT(DOMFilePath::isAbsolute(path));
 
256
    m_asyncFileSystem->readDirectory(createFileSystemURL(path), EntriesCallbacks::create(successCallback, errorCallback, reader, path));
 
257
    return true;
 
258
}
 
259
 
 
260
} // namespace WebCore
 
261
 
 
262
#endif // ENABLE(FILE_SYSTEM)