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/File.h"
36 #include "core/fileapi/FileError.h"
37 #include "core/html/VoidCallback.h"
38 #include "modules/filesystem/DOMFilePath.h"
39 #include "modules/filesystem/DirectoryEntry.h"
40 #include "modules/filesystem/DirectoryReaderBase.h"
41 #include "modules/filesystem/EntriesCallback.h"
42 #include "modules/filesystem/Entry.h"
43 #include "modules/filesystem/EntryBase.h"
44 #include "modules/filesystem/EntryCallback.h"
45 #include "modules/filesystem/ErrorCallback.h"
46 #include "modules/filesystem/FileSystemCallbacks.h"
47 #include "modules/filesystem/MetadataCallback.h"
48 #include "platform/weborigin/SecurityOrigin.h"
49 #include "public/platform/Platform.h"
50 #include "public/platform/WebFileSystem.h"
51 #include "public/platform/WebFileSystemCallbacks.h"
52 #include "wtf/OwnPtr.h"
53 #include "wtf/text/StringBuilder.h"
57 const char DOMFileSystemBase::persistentPathPrefix[] = "persistent";
58 const char DOMFileSystemBase::temporaryPathPrefix[] = "temporary";
59 const char DOMFileSystemBase::isolatedPathPrefix[] = "isolated";
60 const char DOMFileSystemBase::externalPathPrefix[] = "external";
62 DOMFileSystemBase::DOMFileSystemBase(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
66 , m_filesystemRootURL(rootURL)
71 DOMFileSystemBase::~DOMFileSystemBase()
75 WebFileSystem* DOMFileSystemBase::fileSystem() const
77 Platform* platform = Platform::current();
80 return platform->fileSystem();
83 SecurityOrigin* DOMFileSystemBase::securityOrigin() const
85 return m_context->securityOrigin();
88 bool DOMFileSystemBase::isValidType(FileSystemType type)
90 return type == FileSystemTypeTemporary || type == FileSystemTypePersistent || type == FileSystemTypeIsolated || type == FileSystemTypeExternal;
93 bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath)
95 if (!url.protocolIs("filesystem"))
101 String typeString = url.innerURL()->path().substring(1);
102 if (!pathPrefixToFileSystemType(typeString, type))
105 filePath = decodeURLEscapeSequences(url.path());
109 KURL DOMFileSystemBase::createFileSystemRootURL(const String& origin, FileSystemType type)
112 if (type == FileSystemTypeTemporary)
113 typeString = temporaryPathPrefix;
114 else if (type == FileSystemTypePersistent)
115 typeString = persistentPathPrefix;
116 else if (type == FileSystemTypeExternal)
117 typeString = externalPathPrefix;
121 String result = "filesystem:" + origin + "/" + typeString + "/";
122 return KURL(ParsedURLString, result);
125 bool DOMFileSystemBase::supportsToURL() const
127 ASSERT(isValidType(m_type));
128 return m_type != FileSystemTypeIsolated;
131 KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const
133 return createFileSystemURL(entry->fullPath());
136 KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const
138 ASSERT(DOMFilePath::isAbsolute(fullPath));
140 if (type() == FileSystemTypeExternal) {
141 // For external filesystem originString could be different from what we have in m_filesystemRootURL.
142 StringBuilder result;
143 result.appendLiteral("filesystem:");
144 result.append(securityOrigin()->toString());
146 result.append(externalPathPrefix);
147 result.append(m_filesystemRootURL.path());
148 // Remove the extra leading slash.
149 result.append(encodeWithURLEscapeSequences(fullPath.substring(1)));
150 return KURL(ParsedURLString, result.toString());
153 // For regular types we can just append the entry's fullPath to the m_filesystemRootURL that should look like 'filesystem:<origin>/<typePrefix>'.
154 ASSERT(!m_filesystemRootURL.isEmpty());
155 KURL url = m_filesystemRootURL;
156 // Remove the extra leading slash.
157 url.setPath(url.path() + encodeWithURLEscapeSequences(fullPath.substring(1)));
161 bool DOMFileSystemBase::pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath)
165 if (!DOMFilePath::isAbsolute(path))
166 path = DOMFilePath::append(base->fullPath(), path);
167 absolutePath = DOMFilePath::removeExtraParentReferences(path);
169 return (type != FileSystemTypeTemporary && type != FileSystemTypePersistent) || DOMFilePath::isValidPath(absolutePath);
172 bool DOMFileSystemBase::pathPrefixToFileSystemType(const String& pathPrefix, FileSystemType& type)
174 if (pathPrefix == temporaryPathPrefix) {
175 type = FileSystemTypeTemporary;
179 if (pathPrefix == persistentPathPrefix) {
180 type = FileSystemTypePersistent;
184 if (pathPrefix == externalPathPrefix) {
185 type = FileSystemTypeExternal;
192 File* DOMFileSystemBase::createFile(const FileMetadata& metadata, const KURL& fileSystemURL, FileSystemType type, const String name)
194 // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics.
195 // For other filesystem types (which could be platform-specific ones), there's a chance that the files are on remote filesystem.
196 // If the port has returned metadata just pass it to File constructor (so we may cache the metadata).
197 // FIXME: We should use the snapshot metadata for all files.
198 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746
199 if (type == FileSystemTypeTemporary || type == FileSystemTypePersistent)
200 return File::createForFileSystemFile(metadata.platformPath, name);
202 const File::UserVisibility userVisibility = (type == FileSystemTypeExternal) ? File::IsUserVisible : File::IsNotUserVisible;
204 if (!metadata.platformPath.isEmpty()) {
205 // If the platformPath in the returned metadata is given, we create a File object for the snapshot path.
206 return File::createForFileSystemFile(name, metadata, userVisibility);
208 // Otherwise we create a File object for the fileSystemURL.
209 return File::createForFileSystemFile(fileSystemURL, metadata, userVisibility);
213 void DOMFileSystemBase::getMetadata(const EntryBase* entry, MetadataCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
216 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
220 OwnPtr<AsyncFileSystemCallbacks> callbacks(MetadataCallbacks::create(successCallback, errorCallback, m_context, this));
221 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
222 fileSystem()->readMetadata(createFileSystemURL(entry), callbacks.release());
225 static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
229 if (!parent || !parent->isDirectory())
232 if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
235 const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
237 // It is an error to try to copy or move an entry inside itself at any depth if it is a directory.
238 if (source->isDirectory() && isSameFileSystem && DOMFilePath::isParentOf(source->fullPath(), parent->fullPath()))
241 // It is an error to copy or move an entry into its parent if a name different from its current one isn't provided.
242 if (isSameFileSystem && (newName.isEmpty() || source->name() == newName) && DOMFilePath::getDirectory(source->fullPath()) == parent->fullPath())
245 destinationPath = parent->fullPath();
246 if (!newName.isEmpty())
247 destinationPath = DOMFilePath::append(destinationPath, newName);
249 destinationPath = DOMFilePath::append(destinationPath, source->name());
254 void DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
257 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
261 String destinationPath;
262 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
263 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
267 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory()));
268 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
270 fileSystem()->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
273 void DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
276 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
280 String destinationPath;
281 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
282 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
286 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory()));
287 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
289 fileSystem()->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
292 void DOMFileSystemBase::remove(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
295 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
300 // We don't allow calling remove() on the root directory.
301 if (entry->fullPath() == String(DOMFilePath::root)) {
302 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
306 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this));
307 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
309 fileSystem()->remove(createFileSystemURL(entry), callbacks.release());
312 void DOMFileSystemBase::removeRecursively(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
315 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
319 ASSERT(entry && entry->isDirectory());
320 // We don't allow calling remove() on the root directory.
321 if (entry->fullPath() == String(DOMFilePath::root)) {
322 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
326 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this));
327 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
329 fileSystem()->removeRecursively(createFileSystemURL(entry), callbacks.release());
332 void DOMFileSystemBase::getParent(const EntryBase* entry, EntryCallback* successCallback, ErrorCallback* errorCallback)
335 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
340 String path = DOMFilePath::getDirectory(entry->fullPath());
342 fileSystem()->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, m_context, this, path, true));
345 void DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
348 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
353 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
354 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
358 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, false));
359 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
361 if (flags.createFlag())
362 fileSystem()->createFile(createFileSystemURL(absolutePath), flags.exclusive(), callbacks.release());
364 fileSystem()->fileExists(createFileSystemURL(absolutePath), callbacks.release());
367 void DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
370 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
375 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
376 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
380 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, true));
381 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
383 if (flags.createFlag())
384 fileSystem()->createDirectory(createFileSystemURL(absolutePath), flags.exclusive(), callbacks.release());
386 fileSystem()->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
389 int DOMFileSystemBase::readDirectory(DirectoryReaderBase* reader, const String& path, EntriesCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
392 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
396 ASSERT(DOMFilePath::isAbsolute(path));
398 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntriesCallbacks::create(successCallback, errorCallback, m_context, reader, path));
399 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
401 return fileSystem()->readDirectory(createFileSystemURL(path), callbacks.release());
404 bool DOMFileSystemBase::waitForAdditionalResult(int callbacksId)
408 return fileSystem()->waitForAdditionalResult(callbacksId);