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/FileSystemCallbacks.h" 33 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/DOMFileSystemBase.h" 39#include "modules/filesystem/DirectoryEntry.h" 40#include "modules/filesystem/DirectoryReader.h" 41#include "modules/filesystem/Entry.h" 42#include "modules/filesystem/EntryCallback.h" 43#include "modules/filesystem/ErrorCallback.h" 44#include "modules/filesystem/FileEntry.h" 45#include "modules/filesystem/FileSystemCallback.h" 46#include "modules/filesystem/FileWriterBase.h" 47#include "modules/filesystem/FileWriterBaseCallback.h" 48#include "modules/filesystem/Metadata.h" 49#include "modules/filesystem/MetadataCallback.h" 50#include "platform/FileMetadata.h" 51#include "public/platform/WebFileWriter.h" 52 53namespace WebCore { 54 55FileSystemCallbacksBase::FileSystemCallbacksBase(PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem) 56 : m_errorCallback(errorCallback) 57 , m_fileSystem(fileSystem) 58{ 59 if (m_fileSystem) 60 m_fileSystem->addPendingCallbacks(); 61} 62 63FileSystemCallbacksBase::~FileSystemCallbacksBase() 64{ 65 if (m_fileSystem) 66 m_fileSystem->removePendingCallbacks(); 67} 68 69void FileSystemCallbacksBase::didFail(int code) 70{ 71 if (m_errorCallback) { 72 m_errorCallback->handleEvent(FileError::create(static_cast<FileError::ErrorCode>(code)).get()); 73 m_errorCallback.clear(); 74 } 75} 76 77// EntryCallbacks ------------------------------------------------------------- 78 79PassOwnPtr<AsyncFileSystemCallbacks> EntryCallbacks::create(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, PassRefPtr<DOMFileSystemBase> fileSystem, const String& expectedPath, bool isDirectory) 80{ 81 return adoptPtr(new EntryCallbacks(successCallback, errorCallback, fileSystem, expectedPath, isDirectory)); 82} 83 84EntryCallbacks::EntryCallbacks(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, PassRefPtr<DOMFileSystemBase> fileSystem, const String& expectedPath, bool isDirectory) 85 : FileSystemCallbacksBase(errorCallback, fileSystem.get()) 86 , m_successCallback(successCallback) 87 , m_expectedPath(expectedPath) 88 , m_isDirectory(isDirectory) 89{ 90} 91 92void EntryCallbacks::didSucceed() 93{ 94 if (m_successCallback) { 95 if (m_isDirectory) 96 m_successCallback->handleEvent(DirectoryEntry::create(m_fileSystem, m_expectedPath).get()); 97 else 98 m_successCallback->handleEvent(FileEntry::create(m_fileSystem, m_expectedPath).get()); 99 } 100 m_successCallback.clear(); 101} 102 103// EntriesCallbacks ----------------------------------------------------------- 104 105PassOwnPtr<AsyncFileSystemCallbacks> EntriesCallbacks::create(PassOwnPtr<EntriesCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, PassRefPtr<DirectoryReaderBase> directoryReader, const String& basePath) 106{ 107 return adoptPtr(new EntriesCallbacks(successCallback, errorCallback, directoryReader, basePath)); 108} 109 110EntriesCallbacks::EntriesCallbacks(PassOwnPtr<EntriesCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, PassRefPtr<DirectoryReaderBase> directoryReader, const String& basePath) 111 : FileSystemCallbacksBase(errorCallback, directoryReader->filesystem()) 112 , m_successCallback(successCallback) 113 , m_directoryReader(directoryReader) 114 , m_basePath(basePath) 115{ 116 ASSERT(m_directoryReader); 117} 118 119void EntriesCallbacks::didReadDirectoryEntry(const String& name, bool isDirectory) 120{ 121 if (isDirectory) 122 m_entries.append(DirectoryEntry::create(m_directoryReader->filesystem(), DOMFilePath::append(m_basePath, name))); 123 else 124 m_entries.append(FileEntry::create(m_directoryReader->filesystem(), DOMFilePath::append(m_basePath, name))); 125} 126 127void EntriesCallbacks::didReadDirectoryEntries(bool hasMore) 128{ 129 m_directoryReader->setHasMoreEntries(hasMore); 130 if (m_successCallback) 131 m_successCallback->handleEvent(m_entries); 132} 133 134// FileSystemCallbacks -------------------------------------------------------- 135 136PassOwnPtr<AsyncFileSystemCallbacks> FileSystemCallbacks::create(PassOwnPtr<FileSystemCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* executionContext, FileSystemType type) 137{ 138 return adoptPtr(new FileSystemCallbacks(successCallback, errorCallback, executionContext, type)); 139} 140 141FileSystemCallbacks::FileSystemCallbacks(PassOwnPtr<FileSystemCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, FileSystemType type) 142 : FileSystemCallbacksBase(errorCallback, 0) 143 , m_successCallback(successCallback) 144 , m_executionContext(context) 145 , m_type(type) 146{ 147} 148 149void FileSystemCallbacks::didOpenFileSystem(const String& name, const KURL& rootURL) 150{ 151 if (m_successCallback) { 152 RefPtr<DOMFileSystem> fileSystem = DOMFileSystem::create(m_executionContext.get(), name, m_type, rootURL); 153 m_successCallback->handleEvent(fileSystem.get()); 154 m_executionContext.clear(); 155 } 156 m_successCallback.clear(); 157} 158 159// ResolveURICallbacks -------------------------------------------------------- 160 161PassOwnPtr<AsyncFileSystemCallbacks> ResolveURICallbacks::create(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* executionContext) 162{ 163 return adoptPtr(new ResolveURICallbacks(successCallback, errorCallback, executionContext)); 164} 165 166ResolveURICallbacks::ResolveURICallbacks(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context) 167 : FileSystemCallbacksBase(errorCallback, 0) 168 , m_successCallback(successCallback) 169 , m_executionContext(context) 170{ 171} 172 173void ResolveURICallbacks::didResolveURL(const String& name, const KURL& rootURL, FileSystemType type, const String& filePath, bool isDirectory) 174{ 175 RefPtr<DOMFileSystem> filesystem = DOMFileSystem::create(m_executionContext.get(), name, type, rootURL); 176 RefPtr<DirectoryEntry> root = filesystem->root(); 177 178 String absolutePath; 179 if (!DOMFileSystemBase::pathToAbsolutePath(type, root.get(), filePath, absolutePath)) { 180 m_errorCallback->handleEvent(FileError::create(FileError::INVALID_MODIFICATION_ERR).get()); 181 m_errorCallback.clear(); 182 return; 183 } 184 185 if (isDirectory) 186 m_successCallback->handleEvent(DirectoryEntry::create(filesystem, absolutePath).get()); 187 else 188 m_successCallback->handleEvent(FileEntry::create(filesystem, absolutePath).get()); 189 m_successCallback.clear(); 190} 191 192// MetadataCallbacks ---------------------------------------------------------- 193 194PassOwnPtr<AsyncFileSystemCallbacks> MetadataCallbacks::create(PassOwnPtr<MetadataCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem) 195{ 196 return adoptPtr(new MetadataCallbacks(successCallback, errorCallback, fileSystem)); 197} 198 199MetadataCallbacks::MetadataCallbacks(PassOwnPtr<MetadataCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem) 200 : FileSystemCallbacksBase(errorCallback, fileSystem) 201 , m_successCallback(successCallback) 202{ 203} 204 205void MetadataCallbacks::didReadMetadata(const FileMetadata& metadata) 206{ 207 if (m_successCallback) 208 m_successCallback->handleEvent(Metadata::create(metadata).get()); 209 m_successCallback.clear(); 210} 211 212// FileWriterBaseCallbacks ---------------------------------------------------------- 213 214PassOwnPtr<AsyncFileSystemCallbacks> FileWriterBaseCallbacks::create(PassRefPtr<FileWriterBase> fileWriter, PassOwnPtr<FileWriterBaseCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 215{ 216 return adoptPtr(new FileWriterBaseCallbacks(fileWriter, successCallback, errorCallback)); 217} 218 219FileWriterBaseCallbacks::FileWriterBaseCallbacks(PassRefPtr<FileWriterBase> fileWriter, PassOwnPtr<FileWriterBaseCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 220 : FileSystemCallbacksBase(errorCallback, 0) 221 , m_fileWriter(fileWriter) 222 , m_successCallback(successCallback) 223{ 224} 225 226void FileWriterBaseCallbacks::didCreateFileWriter(PassOwnPtr<blink::WebFileWriter> fileWriter, long long length) 227{ 228 m_fileWriter->initialize(fileWriter, length); 229 if (m_successCallback) 230 m_successCallback->handleEvent(m_fileWriter.release().get()); 231 m_successCallback.clear(); 232} 233 234// VoidCallbacks -------------------------------------------------------------- 235 236PassOwnPtr<AsyncFileSystemCallbacks> VoidCallbacks::create(PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem) 237{ 238 return adoptPtr(new VoidCallbacks(successCallback, errorCallback, fileSystem)); 239} 240 241VoidCallbacks::VoidCallbacks(PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem) 242 : FileSystemCallbacksBase(errorCallback, fileSystem) 243 , m_successCallback(successCallback) 244{ 245} 246 247void VoidCallbacks::didSucceed() 248{ 249 if (m_successCallback) 250 m_successCallback->handleEvent(); 251 m_successCallback.clear(); 252} 253 254} // namespace 255