DOMFileSystemBase.cpp [plain text]
#include "config.h"
#include "DOMFileSystemBase.h"
#if ENABLE(FILE_SYSTEM)
#include "DOMFilePath.h"
#include "DirectoryEntry.h"
#include "DirectoryReaderBase.h"
#include "EntriesCallback.h"
#include "EntryArray.h"
#include "EntryBase.h"
#include "EntryCallback.h"
#include "ErrorCallback.h"
#include "FileError.h"
#include "FileSystemCallbacks.h"
#include "MetadataCallback.h"
#include "ScriptExecutionContext.h"
#include "VoidCallback.h"
#include <wtf/OwnPtr.h>
namespace WebCore {
const char DOMFileSystemBase::persistentPathPrefix[] = "persistent";
const size_t DOMFileSystemBase::persistentPathPrefixLength = sizeof(DOMFileSystemBase::persistentPathPrefix) - 1;
const char DOMFileSystemBase::temporaryPathPrefix[] = "temporary";
const size_t DOMFileSystemBase::temporaryPathPrefixLength = sizeof(DOMFileSystemBase::temporaryPathPrefix) - 1;
const char DOMFileSystemBase::isolatedPathPrefix[] = "isolated";
const size_t DOMFileSystemBase::isolatedPathPrefixLength = sizeof(DOMFileSystemBase::isolatedPathPrefix) - 1;
DOMFileSystemBase::DOMFileSystemBase(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
: m_context(context)
, m_name(name)
, m_type(type)
, m_filesystemRootURL(rootURL)
, m_clonable(false)
, m_asyncFileSystem(asyncFileSystem)
{
}
DOMFileSystemBase::~DOMFileSystemBase()
{
}
#if !PLATFORM(BLACKBERRY)
bool DOMFileSystemBase::isValidType(FileSystemType type)
{
return type == FileSystemTypeTemporary || type == FileSystemTypePersistent;
}
bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath)
{
if (!url.protocolIs("filesystem"))
return false;
if (!url.innerURL())
return false;
String typeString = url.innerURL()->path().substring(1);
if (typeString == temporaryPathPrefix)
type = FileSystemTypeTemporary;
else if (typeString == persistentPathPrefix)
type = FileSystemTypePersistent;
else
return false;
filePath = decodeURLEscapeSequences(url.path());
return true;
}
bool DOMFileSystemBase::supportsToURL() const
{
ASSERT(isValidType(m_type));
return true;
}
KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const
{
ASSERT(DOMFilePath::isAbsolute(fullPath));
KURL url = m_filesystemRootURL;
url.setPath(url.path() + encodeWithURLEscapeSequences(fullPath.substring(1)));
return url;
}
#endif
SecurityOrigin* DOMFileSystemBase::securityOrigin() const
{
return m_context->securityOrigin();
}
KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const
{
return createFileSystemURL(entry->fullPath());
}
bool DOMFileSystemBase::getMetadata(const EntryBase* entry, PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
m_asyncFileSystem->readMetadata(createFileSystemURL(entry), MetadataCallbacks::create(successCallback, errorCallback));
return true;
}
static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
{
ASSERT(source);
if (!parent || !parent->isDirectory())
return false;
if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
return false;
const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
if (source->isDirectory() && isSameFileSystem && DOMFilePath::isParentOf(source->fullPath(), parent->fullPath()))
return false;
if (isSameFileSystem && (newName.isEmpty() || source->name() == newName) && DOMFilePath::getDirectory(source->fullPath()) == parent->fullPath())
return false;
destinationPath = parent->fullPath();
if (!newName.isEmpty())
destinationPath = DOMFilePath::append(destinationPath, newName);
else
destinationPath = DOMFilePath::append(destinationPath, source->name());
return true;
}
static bool pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath)
{
ASSERT(base);
if (!DOMFilePath::isAbsolute(path))
path = DOMFilePath::append(base->fullPath(), path);
absolutePath = DOMFilePath::removeExtraParentReferences(path);
if ((type == FileSystemTypeTemporary || type == FileSystemTypePersistent) && !DOMFilePath::isValidPath(absolutePath))
return false;
return true;
}
bool DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
String destinationPath;
if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
return false;
m_asyncFileSystem->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
return true;
}
bool DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
String destinationPath;
if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath))
return false;
m_asyncFileSystem->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
return true;
}
bool DOMFileSystemBase::remove(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
ASSERT(entry);
if (entry->fullPath() == String(DOMFilePath::root))
return false;
m_asyncFileSystem->remove(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
return true;
}
bool DOMFileSystemBase::removeRecursively(const EntryBase* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
ASSERT(entry && entry->isDirectory());
if (entry->fullPath() == String(DOMFilePath::root))
return false;
m_asyncFileSystem->removeRecursively(createFileSystemURL(entry), VoidCallbacks::create(successCallback, errorCallback));
return true;
}
bool DOMFileSystemBase::getParent(const EntryBase* entry, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
ASSERT(entry);
String path = DOMFilePath::getDirectory(entry->fullPath());
m_asyncFileSystem->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, this, path, true));
return true;
}
bool DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
String absolutePath;
if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
return false;
OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, false);
if (flags.create)
m_asyncFileSystem->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
else
m_asyncFileSystem->fileExists(createFileSystemURL(absolutePath), callbacks.release());
return true;
}
bool DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
String absolutePath;
if (!pathToAbsolutePath(m_type, entry, path, absolutePath))
return false;
OwnPtr<EntryCallbacks> callbacks = EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, true);
if (flags.create)
m_asyncFileSystem->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
else
m_asyncFileSystem->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
return true;
}
bool DOMFileSystemBase::readDirectory(PassRefPtr<DirectoryReaderBase> reader, const String& path, PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
{
ASSERT(DOMFilePath::isAbsolute(path));
m_asyncFileSystem->readDirectory(createFileSystemURL(path), EntriesCallbacks::create(successCallback, errorCallback, reader, path));
return true;
}
}
#endif // ENABLE(FILE_SYSTEM)