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 "modules/filesystem/DOMFileSystemBase.h"
34 #include "core/dom/ExecutionContext.h"
35 #include "core/fileapi/FileError.h"
36 #include "core/html/VoidCallback.h"
37 #include "modules/filesystem/DOMFilePath.h"
38 #include "modules/filesystem/DirectoryEntry.h"
39 #include "modules/filesystem/DirectoryReaderBase.h"
40 #include "modules/filesystem/EntriesCallback.h"
41 #include "modules/filesystem/Entry.h"
42 #include "modules/filesystem/EntryBase.h"
43 #include "modules/filesystem/EntryCallback.h"
44 #include "modules/filesystem/ErrorCallback.h"
45 #include "modules/filesystem/FileSystemCallbacks.h"
46 #include "modules/filesystem/MetadataCallback.h"
47 #include "platform/weborigin/SecurityOrigin.h"
48 #include "public/platform/Platform.h"
49 #include "public/platform/WebFileSystem.h"
50 #include "public/platform/WebFileSystemCallbacks.h"
51 #include "wtf/OwnPtr.h"
52 #include "wtf/text/StringBuilder.h"
56 const char DOMFileSystemBase::persistentPathPrefix[] = "persistent";
57 const char DOMFileSystemBase::temporaryPathPrefix[] = "temporary";
58 const char DOMFileSystemBase::isolatedPathPrefix[] = "isolated";
59 const char DOMFileSystemBase::externalPathPrefix[] = "external";
61 DOMFileSystemBase::DOMFileSystemBase(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
65 , m_filesystemRootURL(rootURL)
70 DOMFileSystemBase::~DOMFileSystemBase()
74 blink::WebFileSystem* DOMFileSystemBase::fileSystem() const
76 return blink::Platform::current()->fileSystem();
79 SecurityOrigin* DOMFileSystemBase::securityOrigin() const
81 return m_context->securityOrigin();
84 bool DOMFileSystemBase::isValidType(FileSystemType type)
86 return type == FileSystemTypeTemporary || type == FileSystemTypePersistent || type == FileSystemTypeIsolated || type == FileSystemTypeExternal;
89 bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath)
91 if (!url.protocolIs("filesystem"))
97 String typeString = url.innerURL()->path().substring(1);
98 if (!pathPrefixToFileSystemType(typeString, type))
101 filePath = decodeURLEscapeSequences(url.path());
105 KURL DOMFileSystemBase::createFileSystemRootURL(const String& origin, FileSystemType type)
108 if (type == FileSystemTypeTemporary)
109 typeString = temporaryPathPrefix;
110 else if (type == FileSystemTypePersistent)
111 typeString = persistentPathPrefix;
112 else if (type == FileSystemTypeExternal)
113 typeString = externalPathPrefix;
117 String result = "filesystem:" + origin + "/" + typeString + "/";
118 return KURL(ParsedURLString, result);
121 bool DOMFileSystemBase::supportsToURL() const
123 ASSERT(isValidType(m_type));
124 return m_type != FileSystemTypeIsolated;
127 KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const
129 return createFileSystemURL(entry->fullPath());
132 KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const
134 ASSERT(DOMFilePath::isAbsolute(fullPath));
136 if (type() == FileSystemTypeExternal) {
137 // For external filesystem originString could be different from what we have in m_filesystemRootURL.
138 StringBuilder result;
139 result.append("filesystem:");
140 result.append(securityOrigin()->toString());
142 result.append(externalPathPrefix);
143 result.append(m_filesystemRootURL.path());
144 // Remove the extra leading slash.
145 result.append(encodeWithURLEscapeSequences(fullPath.substring(1)));
146 return KURL(ParsedURLString, result.toString());
149 // For regular types we can just append the entry's fullPath to the m_filesystemRootURL that should look like 'filesystem:<origin>/<typePrefix>'.
150 ASSERT(!m_filesystemRootURL.isEmpty());
151 KURL url = m_filesystemRootURL;
152 // Remove the extra leading slash.
153 url.setPath(url.path() + encodeWithURLEscapeSequences(fullPath.substring(1)));
157 bool DOMFileSystemBase::pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath)
161 if (!DOMFilePath::isAbsolute(path))
162 path = DOMFilePath::append(base->fullPath(), path);
163 absolutePath = DOMFilePath::removeExtraParentReferences(path);
165 return (type != FileSystemTypeTemporary && type != FileSystemTypePersistent) || DOMFilePath::isValidPath(absolutePath);
168 bool DOMFileSystemBase::pathPrefixToFileSystemType(const String& pathPrefix, FileSystemType& type)
170 if (pathPrefix == temporaryPathPrefix) {
171 type = FileSystemTypeTemporary;
175 if (pathPrefix == persistentPathPrefix) {
176 type = FileSystemTypePersistent;
180 if (pathPrefix == externalPathPrefix) {
181 type = FileSystemTypeExternal;
188 void DOMFileSystemBase::getMetadata(const EntryBase* entry, PassOwnPtr<MetadataCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
190 OwnPtr<AsyncFileSystemCallbacks> callbacks(MetadataCallbacks::create(successCallback, errorCallback, this));
191 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
192 fileSystem()->readMetadata(createFileSystemURL(entry), callbacks.release());
195 static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
199 if (!parent || !parent->isDirectory())
202 if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
205 const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
207 // It is an error to try to copy or move an entry inside itself at any depth if it is a directory.
208 if (source->isDirectory() && isSameFileSystem && DOMFilePath::isParentOf(source->fullPath(), parent->fullPath()))
211 // It is an error to copy or move an entry into its parent if a name different from its current one isn't provided.
212 if (isSameFileSystem && (newName.isEmpty() || source->name() == newName) && DOMFilePath::getDirectory(source->fullPath()) == parent->fullPath())
215 destinationPath = parent->fullPath();
216 if (!newName.isEmpty())
217 destinationPath = DOMFilePath::append(destinationPath, newName);
219 destinationPath = DOMFilePath::append(destinationPath, source->name());
224 void DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
226 String destinationPath;
227 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
228 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
232 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
233 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
235 fileSystem()->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
238 void DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
240 String destinationPath;
241 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
242 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
246 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, parent->filesystem(), destinationPath, source->isDirectory()));
247 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
249 fileSystem()->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
252 void DOMFileSystemBase::remove(const EntryBase* entry, PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
255 // We don't allow calling remove() on the root directory.
256 if (entry->fullPath() == String(DOMFilePath::root)) {
257 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
261 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, this));
262 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
264 fileSystem()->remove(createFileSystemURL(entry), callbacks.release());
267 void DOMFileSystemBase::removeRecursively(const EntryBase* entry, PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
269 ASSERT(entry && entry->isDirectory());
270 // We don't allow calling remove() on the root directory.
271 if (entry->fullPath() == String(DOMFilePath::root)) {
272 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
276 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, this));
277 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
279 fileSystem()->removeRecursively(createFileSystemURL(entry), callbacks.release());
282 void DOMFileSystemBase::getParent(const EntryBase* entry, PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback)
285 String path = DOMFilePath::getDirectory(entry->fullPath());
286 fileSystem()->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, this, path, true));
289 void DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
292 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
293 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
297 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, false));
298 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
301 fileSystem()->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
303 fileSystem()->fileExists(createFileSystemURL(absolutePath), callbacks.release());
306 void DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
309 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
310 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
314 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, this, absolutePath, true));
315 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
318 fileSystem()->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
320 fileSystem()->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
323 bool DOMFileSystemBase::readDirectory(PassRefPtr<DirectoryReaderBase> reader, const String& path, PassOwnPtr<EntriesCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, SynchronousType synchronousType)
325 ASSERT(DOMFilePath::isAbsolute(path));
327 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntriesCallbacks::create(successCallback, errorCallback, reader, path));
328 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
330 fileSystem()->readDirectory(createFileSystemURL(path), callbacks.release());
334 } // namespace WebCore