1/* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31#include "config.h" 32#include "modules/filesystem/DOMFileSystemBase.h" 33 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" 54 55namespace blink { 56 57const char DOMFileSystemBase::persistentPathPrefix[] = "persistent"; 58const char DOMFileSystemBase::temporaryPathPrefix[] = "temporary"; 59const char DOMFileSystemBase::isolatedPathPrefix[] = "isolated"; 60const char DOMFileSystemBase::externalPathPrefix[] = "external"; 61 62DOMFileSystemBase::DOMFileSystemBase(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL) 63 : m_context(context) 64 , m_name(name) 65 , m_type(type) 66 , m_filesystemRootURL(rootURL) 67 , m_clonable(false) 68{ 69} 70 71DOMFileSystemBase::~DOMFileSystemBase() 72{ 73} 74 75WebFileSystem* DOMFileSystemBase::fileSystem() const 76{ 77 Platform* platform = Platform::current(); 78 if (!platform) 79 return nullptr; 80 return platform->fileSystem(); 81} 82 83SecurityOrigin* DOMFileSystemBase::securityOrigin() const 84{ 85 return m_context->securityOrigin(); 86} 87 88bool DOMFileSystemBase::isValidType(FileSystemType type) 89{ 90 return type == FileSystemTypeTemporary || type == FileSystemTypePersistent || type == FileSystemTypeIsolated || type == FileSystemTypeExternal; 91} 92 93bool DOMFileSystemBase::crackFileSystemURL(const KURL& url, FileSystemType& type, String& filePath) 94{ 95 if (!url.protocolIs("filesystem")) 96 return false; 97 98 if (!url.innerURL()) 99 return false; 100 101 String typeString = url.innerURL()->path().substring(1); 102 if (!pathPrefixToFileSystemType(typeString, type)) 103 return false; 104 105 filePath = decodeURLEscapeSequences(url.path()); 106 return true; 107} 108 109KURL DOMFileSystemBase::createFileSystemRootURL(const String& origin, FileSystemType type) 110{ 111 String typeString; 112 if (type == FileSystemTypeTemporary) 113 typeString = temporaryPathPrefix; 114 else if (type == FileSystemTypePersistent) 115 typeString = persistentPathPrefix; 116 else if (type == FileSystemTypeExternal) 117 typeString = externalPathPrefix; 118 else 119 return KURL(); 120 121 String result = "filesystem:" + origin + "/" + typeString + "/"; 122 return KURL(ParsedURLString, result); 123} 124 125bool DOMFileSystemBase::supportsToURL() const 126{ 127 ASSERT(isValidType(m_type)); 128 return m_type != FileSystemTypeIsolated; 129} 130 131KURL DOMFileSystemBase::createFileSystemURL(const EntryBase* entry) const 132{ 133 return createFileSystemURL(entry->fullPath()); 134} 135 136KURL DOMFileSystemBase::createFileSystemURL(const String& fullPath) const 137{ 138 ASSERT(DOMFilePath::isAbsolute(fullPath)); 139 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()); 145 result.append('/'); 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()); 151 } 152 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))); 158 return url; 159} 160 161bool DOMFileSystemBase::pathToAbsolutePath(FileSystemType type, const EntryBase* base, String path, String& absolutePath) 162{ 163 ASSERT(base); 164 165 if (!DOMFilePath::isAbsolute(path)) 166 path = DOMFilePath::append(base->fullPath(), path); 167 absolutePath = DOMFilePath::removeExtraParentReferences(path); 168 169 return (type != FileSystemTypeTemporary && type != FileSystemTypePersistent) || DOMFilePath::isValidPath(absolutePath); 170} 171 172bool DOMFileSystemBase::pathPrefixToFileSystemType(const String& pathPrefix, FileSystemType& type) 173{ 174 if (pathPrefix == temporaryPathPrefix) { 175 type = FileSystemTypeTemporary; 176 return true; 177 } 178 179 if (pathPrefix == persistentPathPrefix) { 180 type = FileSystemTypePersistent; 181 return true; 182 } 183 184 if (pathPrefix == externalPathPrefix) { 185 type = FileSystemTypeExternal; 186 return true; 187 } 188 189 return false; 190} 191 192PassRefPtrWillBeRawPtr<File> DOMFileSystemBase::createFile(const FileMetadata& metadata, const KURL& fileSystemURL, FileSystemType type, const String name) 193{ 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); 201 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); 206 } 207 208 return File::createForFileSystemFile(fileSystemURL, metadata); 209} 210 211void DOMFileSystemBase::getMetadata(const EntryBase* entry, MetadataCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 212{ 213 if (!fileSystem()) { 214 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 215 return; 216 } 217 218 OwnPtr<AsyncFileSystemCallbacks> callbacks(MetadataCallbacks::create(successCallback, errorCallback, m_context, this)); 219 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 220 fileSystem()->readMetadata(createFileSystemURL(entry), callbacks.release()); 221} 222 223static bool verifyAndGetDestinationPathForCopyOrMove(const EntryBase* source, EntryBase* parent, const String& newName, String& destinationPath) 224{ 225 ASSERT(source); 226 227 if (!parent || !parent->isDirectory()) 228 return false; 229 230 if (!newName.isEmpty() && !DOMFilePath::isValidName(newName)) 231 return false; 232 233 const bool isSameFileSystem = (*source->filesystem() == *parent->filesystem()); 234 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())) 237 return false; 238 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()) 241 return false; 242 243 destinationPath = parent->fullPath(); 244 if (!newName.isEmpty()) 245 destinationPath = DOMFilePath::append(destinationPath, newName); 246 else 247 destinationPath = DOMFilePath::append(destinationPath, source->name()); 248 249 return true; 250} 251 252void DOMFileSystemBase::move(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 253{ 254 if (!fileSystem()) { 255 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 256 return; 257 } 258 259 String destinationPath; 260 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) { 261 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR)); 262 return; 263 } 264 265 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory())); 266 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 267 268 fileSystem()->move(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release()); 269} 270 271void DOMFileSystemBase::copy(const EntryBase* source, EntryBase* parent, const String& newName, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 272{ 273 if (!fileSystem()) { 274 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 275 return; 276 } 277 278 String destinationPath; 279 if (!verifyAndGetDestinationPathForCopyOrMove(source, parent, newName, destinationPath)) { 280 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR)); 281 return; 282 } 283 284 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, parent->filesystem(), destinationPath, source->isDirectory())); 285 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 286 287 fileSystem()->copy(createFileSystemURL(source), parent->filesystem()->createFileSystemURL(destinationPath), callbacks.release()); 288} 289 290void DOMFileSystemBase::remove(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 291{ 292 if (!fileSystem()) { 293 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 294 return; 295 } 296 297 ASSERT(entry); 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)); 301 return; 302 } 303 304 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this)); 305 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 306 307 fileSystem()->remove(createFileSystemURL(entry), callbacks.release()); 308} 309 310void DOMFileSystemBase::removeRecursively(const EntryBase* entry, VoidCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 311{ 312 if (!fileSystem()) { 313 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 314 return; 315 } 316 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)); 321 return; 322 } 323 324 OwnPtr<AsyncFileSystemCallbacks> callbacks(VoidCallbacks::create(successCallback, errorCallback, m_context, this)); 325 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 326 327 fileSystem()->removeRecursively(createFileSystemURL(entry), callbacks.release()); 328} 329 330void DOMFileSystemBase::getParent(const EntryBase* entry, EntryCallback* successCallback, ErrorCallback* errorCallback) 331{ 332 if (!fileSystem()) { 333 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 334 return; 335 } 336 337 ASSERT(entry); 338 String path = DOMFilePath::getDirectory(entry->fullPath()); 339 340 fileSystem()->directoryExists(createFileSystemURL(path), EntryCallbacks::create(successCallback, errorCallback, m_context, this, path, true)); 341} 342 343void DOMFileSystemBase::getFile(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 344{ 345 if (!fileSystem()) { 346 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 347 return; 348 } 349 350 String absolutePath; 351 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) { 352 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR)); 353 return; 354 } 355 356 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, false)); 357 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 358 359 if (flags.create) 360 fileSystem()->createFile(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release()); 361 else 362 fileSystem()->fileExists(createFileSystemURL(absolutePath), callbacks.release()); 363} 364 365void DOMFileSystemBase::getDirectory(const EntryBase* entry, const String& path, const FileSystemFlags& flags, EntryCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 366{ 367 if (!fileSystem()) { 368 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 369 return; 370 } 371 372 String absolutePath; 373 if (!pathToAbsolutePath(m_type, entry, path, absolutePath)) { 374 reportError(errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR)); 375 return; 376 } 377 378 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntryCallbacks::create(successCallback, errorCallback, m_context, this, absolutePath, true)); 379 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 380 381 if (flags.create) 382 fileSystem()->createDirectory(createFileSystemURL(absolutePath), flags.exclusive, callbacks.release()); 383 else 384 fileSystem()->directoryExists(createFileSystemURL(absolutePath), callbacks.release()); 385} 386 387int DOMFileSystemBase::readDirectory(DirectoryReaderBase* reader, const String& path, EntriesCallback* successCallback, ErrorCallback* errorCallback, SynchronousType synchronousType) 388{ 389 if (!fileSystem()) { 390 reportError(errorCallback, FileError::create(FileError::ABORT_ERR)); 391 return 0; 392 } 393 394 ASSERT(DOMFilePath::isAbsolute(path)); 395 396 OwnPtr<AsyncFileSystemCallbacks> callbacks(EntriesCallbacks::create(successCallback, errorCallback, m_context, reader, path)); 397 callbacks->setShouldBlockUntilCompletion(synchronousType == Synchronous); 398 399 return fileSystem()->readDirectory(createFileSystemURL(path), callbacks.release()); 400} 401 402bool DOMFileSystemBase::waitForAdditionalResult(int callbacksId) 403{ 404 if (!fileSystem()) 405 return false; 406 return fileSystem()->waitForAdditionalResult(callbacksId); 407} 408 409} // namespace blink 410