2
* Copyright (C) 2010 Google Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are
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
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.
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.
32
#include "DOMFileSystemBase.h"
34
#if ENABLE(FILE_SYSTEM)
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>
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;
60
DOMFileSystemBase::DOMFileSystemBase(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
64
, m_filesystemRootURL(rootURL)
66
, m_asyncFileSystem(asyncFileSystem)
70
DOMFileSystemBase::~DOMFileSystemBase()
74
#if !PLATFORM(CHROMIUM)
76
bool DOMFileSystemBase::isValidType(FileSystemType type)
78
return type == FileSystemTypeTemporary || type == FileSystemTypePersistent;
82
bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath)
84
if (!url.protocolIs("filesystem"))
90
String typeString = url.innerURL()->path().substring(1);
91
if (typeString == temporaryPathPrefix)
92
type = FileSystemTypeTemporary;
93
else if (typeString == persistentPathPrefix)
94
type = FileSystemTypePersistent;
98
filePath = decodeURLEscapeSequences(url.path());
102
bool DOMFileSystemBase::supportsToURL() const
104
ASSERT(isValidType(m_type));
108
KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const
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)));
118
SecurityOrigin* DOMFileSystemBase::securityOrigin() const
120
return m_context->securityOrigin();
123
KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const
125
return createFileSystemURL(entry->fullPath());
128
bool DOMFileSystemBase::getMetadata(const EntryBase* entry, PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
130
m_asyncFileSystem->readMetadata(createFileSystemURL(entry), MetadataCallbacks::create(successCallback, errorCallback));
134
static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
138
if (!parent || !parent->isDirectory())
141
if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
144
const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
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()))
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())
154
destinationPath = parent->fullPath();
155
if (!newName.isEmpty())
156
destinationPath = DOMFilePath::append(destinationPath, newName);
158
destinationPath = DOMFilePath::append(destinationPath, source->name());
163
static bool pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath)
167
if (!DOMFilePath::isAbsolute(path))
168
path = DOMFilePath::append(base->fullPath(), path);
169
absolutePath = DOMFilePath::removeExtraParentReferences(path);
171
if ((type == FileSystemTypeTemporary || type == FileSystemTypePersistent) && !DOMFilePath::isValidPath(absolutePath))
176
bool DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
178
String destinationPath;
179
if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
182
m_asyncFileSystem->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
186
bool DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
188
String destinationPath;
189
if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
192
m_asyncFileSystem->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
196
bool DOMFileSystemBase::remove(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
199
// We don't allow calling remove() on the root directory.
200
if (entry->fullPath() == String(DOMFilePath::root))
202
m_asyncFileSystem->remove(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
206
bool DOMFileSystemBase::removeRecursively(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
208
ASSERT(entry && entry->isDirectory());
209
// We don't allow calling remove() on the root directory.
210
if (entry->fullPath() == String(DOMFilePath::root))
212
m_asyncFileSystem->removeRecursively(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
216
bool DOMFileSystemBase::getParent(const EntryBase* entry, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
219
String path = DOMFilePath::getDirectory(entry->fullPath());
221
m_asyncFileSystem->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, this, path, true));
225
bool DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
228
if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
231
OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, false);
233
m_asyncFileSystem->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
235
m_asyncFileSystem->fileExists(createFileSystemURL(absolutePath), callbacks.release());
239
bool DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
242
if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
245
OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, true);
247
m_asyncFileSystem->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
249
m_asyncFileSystem->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
253
bool DOMFileSystemBase::readDirectory(PassRefPtr<DirectoryReaderBase> reader, const String& path, PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
255
ASSERT(DOMFilePath::isAbsolute(path));
256
m_asyncFileSystem->readDirectory(createFileSystemURL(path), EntriesCallbacks::create(successCallback, errorCallback, reader, path));
260
} // namespace WebCore
262
#endif // ENABLE(FILE_SYSTEM)