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 PassRefPtrWillBeRawPtr<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 if (!metadata.platformPath.isEmpty()) {
203 // If the platformPath in the returned metadata is given, we create a File object for the path.
204 File::UserVisibility userVisibility = (type == FileSystemTypeExternal) ? File::IsUserVisible : File::IsNotUserVisible;
205 return File::createForFileSystemFile(name, metadata, userVisibility);
208 return File::createForFileSystemFile(fileSystemURL, metadata);
211 void DOMFileSystemBase::getMetadata(const EntryBase* entry, MetadataCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
214 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
218 OwnPtr<AsyncFileSystemCallbacks> callbacks(MetadataCallbacks::create(successCallback, errorCallback, m_context, this));
219 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
220 fileSystem()->readMetadata(createFileSystemURL(entry), callbacks.release());
223 static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath)
227 if (!parent || !parent->isDirectory())
230 if (!newName.isEmpty() && !DOMFilePath::isValidName(newName))
233 const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem());
235 // It is an error to try to copy or move an entry inside itself at any depth if it is a directory.
236 if (source->isDirectory() && isSameFileSystem && DOMFilePath::isParentOf(source->fullPath(), parent->fullPath()))
239 // It is an error to copy or move an entry into its parent if a name different from its current one isn't provided.
240 if (isSameFileSystem && (newName.isEmpty() || source->name() == newName) && DOMFilePath::getDirectory(source->fullPath()) == parent->fullPath())
243 destinationPath = parent->fullPath();
244 if (!newName.isEmpty())
245 destinationPath = DOMFilePath::append(destinationPath, newName);
247 destinationPath = DOMFilePath::append(destinationPath, source->name());
252 void DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
255 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
259 String destinationPath;
260 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
261 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
265 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory()));
266 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
268 fileSystem()->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
271 void DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
274 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
278 String destinationPath;
279 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) {
280 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
284 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory()));
285 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
287 fileSystem()->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release());
290 void DOMFileSystemBase::remove(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
293 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
298 // We don't allow calling remove() on the root directory.
299 if (entry->fullPath() == String(DOMFilePath::root)) {
300 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
304 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this));
305 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
307 fileSystem()->remove(createFileSystemURL(entry), callbacks.release());
310 void DOMFileSystemBase::removeRecursively(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
313 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
317 ASSERT(entry && entry->isDirectory());
318 // We don't allow calling remove() on the root directory.
319 if (entry->fullPath() == String(DOMFilePath::root)) {
320 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
324 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this));
325 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
327 fileSystem()->removeRecursively(createFileSystemURL(entry), callbacks.release());
330 void DOMFileSystemBase::getParent(const EntryBase* entry, EntryCallback* successCallback, ErrorCallback* errorCallback)
333 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
338 String path = DOMFilePath::getDirectory(entry->fullPath());
340 fileSystem()->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, m_context, this, path, true));
343 void DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
346 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
351 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
352 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
356 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, false));
357 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
360 fileSystem()->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
362 fileSystem()->fileExists(createFileSystemURL(absolutePath), callbacks.release());
365 void DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
368 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
373 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) {
374 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
378 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, true));
379 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
382 fileSystem()->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release());
384 fileSystem()->directoryExists(createFileSystemURL(absolutePath), callbacks.release());
387 int DOMFileSystemBase::readDirectory(DirectoryReaderBase* reader, const String& path, EntriesCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType)
390 reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
394 ASSERT(DOMFilePath::isAbsolute(path));
396 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntriesCallbacks::create(successCallback, errorCallback, m_context, reader, path));
397 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous);
399 return fileSystem()->readDirectory(createFileSystemURL(path), callbacks.release());
402 bool DOMFileSystemBase::waitForAdditionalResult(int callbacksId)
406 return fileSystem()->waitForAdditionalResult(callbacksId);