1/* 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28#include "config.h" 29#include "WorkerContextFileSystem.h" 30 31#if ENABLE(FILE_SYSTEM) 32 33#include "AsyncFileSystem.h" 34#include "DOMFileSystemBase.h" 35#include "DOMFileSystemSync.h" 36#include "DirectoryEntrySync.h" 37#include "ErrorCallback.h" 38#include "FileEntrySync.h" 39#include "FileError.h" 40#include "FileException.h" 41#include "FileSystemCallback.h" 42#include "FileSystemCallbacks.h" 43#include "FileSystemType.h" 44#include "LocalFileSystem.h" 45#include "SecurityOrigin.h" 46#include "SyncCallbackHelper.h" 47#include "WorkerContext.h" 48 49namespace WebCore { 50 51void WorkerContextFileSystem::webkitRequestFileSystem(WorkerContext* worker, int type, long long size, PassRefPtr<FileSystemCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 52{ 53 ScriptExecutionContext* secureContext = worker->scriptExecutionContext(); 54 if (!AsyncFileSystem::isAvailable() || !secureContext->securityOrigin()->canAccessFileSystem()) { 55 DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::SECURITY_ERR)); 56 return; 57 } 58 59 FileSystemType fileSystemType = static_cast<FileSystemType>(type); 60 if (!DOMFileSystemBase::isValidType(fileSystemType)) { 61 DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR)); 62 return; 63 } 64 65 LocalFileSystem::localFileSystem().requestFileSystem(worker, fileSystemType, size, FileSystemCallbacks::create(successCallback, errorCallback, worker, fileSystemType), AsynchronousFileSystem); 66} 67 68PassRefPtr<DOMFileSystemSync> WorkerContextFileSystem::webkitRequestFileSystemSync(WorkerContext* worker, int type, long long size, ExceptionCode& ec) 69{ 70 ec = 0; 71 ScriptExecutionContext* secureContext = worker->scriptExecutionContext(); 72 if (!AsyncFileSystem::isAvailable() || !secureContext->securityOrigin()->canAccessFileSystem()) { 73 ec = FileException::SECURITY_ERR; 74 return 0; 75 } 76 77 FileSystemType fileSystemType = static_cast<FileSystemType>(type); 78 if (!DOMFileSystemBase::isValidType(fileSystemType)) { 79 ec = FileException::INVALID_MODIFICATION_ERR; 80 return 0; 81 } 82 83 FileSystemSyncCallbackHelper helper; 84 LocalFileSystem::localFileSystem().requestFileSystem(worker, fileSystemType, size, FileSystemCallbacks::create(helper.successCallback(), helper.errorCallback(), worker, fileSystemType), SynchronousFileSystem); 85 return helper.getResult(ec); 86} 87 88void WorkerContextFileSystem::webkitResolveLocalFileSystemURL(WorkerContext* worker, const String& url, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 89{ 90 KURL completedURL = worker->completeURL(url); 91 ScriptExecutionContext* secureContext = worker->scriptExecutionContext(); 92 if (!AsyncFileSystem::isAvailable() || !secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) { 93 DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::SECURITY_ERR)); 94 return; 95 } 96 97 FileSystemType type; 98 String filePath; 99 if (!completedURL.isValid() || !DOMFileSystemBase::crackFileSystemURL(completedURL, type, filePath)) { 100 DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::ENCODING_ERR)); 101 return; 102 } 103 104 LocalFileSystem::localFileSystem().readFileSystem(worker, type, ResolveURICallbacks::create(successCallback, errorCallback, worker, type, filePath)); 105} 106 107PassRefPtr<EntrySync> WorkerContextFileSystem::webkitResolveLocalFileSystemSyncURL(WorkerContext* worker, const String& url, ExceptionCode& ec) 108{ 109 ec = 0; 110 KURL completedURL = worker->completeURL(url); 111 ScriptExecutionContext* secureContext = worker->scriptExecutionContext(); 112 if (!AsyncFileSystem::isAvailable() || !secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) { 113 ec = FileException::SECURITY_ERR; 114 return 0; 115 } 116 117 FileSystemType type; 118 String filePath; 119 if (!completedURL.isValid() || !DOMFileSystemBase::crackFileSystemURL(completedURL, type, filePath)) { 120 ec = FileException::ENCODING_ERR; 121 return 0; 122 } 123 124 FileSystemSyncCallbackHelper readFileSystemHelper; 125 LocalFileSystem::localFileSystem().readFileSystem(worker, type, FileSystemCallbacks::create(readFileSystemHelper.successCallback(), readFileSystemHelper.errorCallback(), worker, type), SynchronousFileSystem); 126 RefPtr<DOMFileSystemSync> fileSystem = readFileSystemHelper.getResult(ec); 127 if (!fileSystem) 128 return 0; 129 130 RefPtr<EntrySync> entry = fileSystem->root()->getDirectory(filePath, Dictionary(), ec); 131 if (ec == FileException::TYPE_MISMATCH_ERR) 132 return fileSystem->root()->getFile(filePath, Dictionary(), ec); 133 134 return entry.release(); 135} 136 137COMPILE_ASSERT(static_cast<int>(WorkerContextFileSystem::TEMPORARY) == static_cast<int>(FileSystemTypeTemporary), enum_mismatch); 138COMPILE_ASSERT(static_cast<int>(WorkerContextFileSystem::PERSISTENT) == static_cast<int>(FileSystemTypePersistent), enum_mismatch); 139 140} // namespace WebCore 141 142#endif // ENABLE(FILE_SYSTEM) 143