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 "DOMFileSystemSync.h" 33 34#if ENABLE(FILE_SYSTEM) 35 36#include "AsyncFileSystem.h" 37#include "AsyncFileWriter.h" 38#include "DOMFilePath.h" 39#include "DirectoryEntrySync.h" 40#include "ErrorCallback.h" 41#include "File.h" 42#include "FileEntrySync.h" 43#include "FileError.h" 44#include "FileException.h" 45#include "FileMetadata.h" 46#include "FileSystemCallbacks.h" 47#include "FileWriterBaseCallback.h" 48#include "FileWriterSync.h" 49 50namespace WebCore { 51 52class FileWriterBase; 53 54PassRefPtr<DOMFileSystemSync> DOMFileSystemSync::create(DOMFileSystemBase* fileSystem) 55{ 56 return adoptRef(new DOMFileSystemSync(fileSystem->m_context, fileSystem->name(), fileSystem->type(), fileSystem->rootURL(), fileSystem->m_asyncFileSystem.release())); 57} 58 59DOMFileSystemSync::DOMFileSystemSync(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem) 60 : DOMFileSystemBase(context, name, type, rootURL, asyncFileSystem) 61{ 62} 63 64DOMFileSystemSync::~DOMFileSystemSync() 65{ 66} 67 68PassRefPtr<DirectoryEntrySync> DOMFileSystemSync::root() 69{ 70 return DirectoryEntrySync::create(this, DOMFilePath::root); 71} 72 73namespace { 74 75class CreateFileHelper : public AsyncFileSystemCallbacks { 76public: 77 class CreateFileResult : public RefCounted<CreateFileResult> { 78 public: 79 static PassRefPtr<CreateFileResult> create() 80 { 81 return adoptRef(new CreateFileResult()); 82 } 83 84 bool m_failed; 85 int m_code; 86 RefPtr<File> m_file; 87 88 private: 89 CreateFileResult() 90 : m_failed(false) 91 , m_code(0) 92 { 93 } 94 95 ~CreateFileResult() 96 { 97 } 98 friend class WTF::RefCounted<CreateFileResult>; 99 }; 100 101 static PassOwnPtr<CreateFileHelper> create(PassRefPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type) 102 { 103 return adoptPtr(new CreateFileHelper(result, name, url, type)); 104 } 105 106 virtual void didFail(int code) 107 { 108 m_result->m_failed = true; 109 m_result->m_code = code; 110 } 111 112 virtual ~CreateFileHelper() 113 { 114 } 115 116 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> /* snapshot */) 117 { 118 // We can't directly use the snapshot blob data handle because the content type on it hasn't been set. 119 // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until 120 // *after* we've coined a File with a new handle that has the correct type set on it. This allows the 121 // blob storage system to track when a temp file can and can't be safely deleted. 122 123 // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics. 124 // For other filesystem types (which could be platform-specific ones), there's a chance that the files are on remote filesystem. 125 // If the port has returned metadata just pass it to File constructor (so we may cache the metadata). 126 // FIXME: We should use the snapshot metadata for all files. 127 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746 128 if (m_type == FileSystemTypeTemporary || m_type == FileSystemTypePersistent) { 129 m_result->m_file = File::createWithName(metadata.platformPath, m_name); 130 } else if (!metadata.platformPath.isEmpty()) { 131 // If the platformPath in the returned metadata is given, we create a File object for the path. 132 m_result->m_file = File::createForFileSystemFile(m_name, metadata).get(); 133 } else { 134 // Otherwise create a File from the FileSystem URL. 135 m_result->m_file = File::createForFileSystemFile(m_url, metadata).get(); 136 } 137 } 138private: 139 CreateFileHelper(PassRefPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type) 140 : m_result(result) 141 , m_name(name) 142 , m_url(url) 143 , m_type(type) 144 { 145 } 146 147 RefPtr<CreateFileResult> m_result; 148 String m_name; 149 KURL m_url; 150 FileSystemType m_type; 151}; 152 153} // namespace 154 155PassRefPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionCode& ec) 156{ 157 ec = 0; 158 KURL fileSystemURL = createFileSystemURL(fileEntry); 159 RefPtr<CreateFileHelper::CreateFileResult> result(CreateFileHelper::CreateFileResult::create()); 160 m_asyncFileSystem->createSnapshotFileAndReadMetadata(fileSystemURL, CreateFileHelper::create(result, fileEntry->name(), fileSystemURL, type())); 161 if (!m_asyncFileSystem->waitForOperationToComplete()) { 162 ec = FileException::ABORT_ERR; 163 return 0; 164 } 165 if (result->m_failed) { 166 ec = result->m_code; 167 return 0; 168 } 169 return result->m_file; 170} 171 172namespace { 173 174class ReceiveFileWriterCallback : public FileWriterBaseCallback { 175public: 176 static PassRefPtr<ReceiveFileWriterCallback> create() 177 { 178 return adoptRef(new ReceiveFileWriterCallback()); 179 } 180 181 bool handleEvent(FileWriterBase* fileWriterBase) 182 { 183#ifndef NDEBUG 184 m_fileWriterBase = fileWriterBase; 185#else 186 ASSERT_UNUSED(fileWriterBase, fileWriterBase); 187#endif 188 return true; 189 } 190 191#ifndef NDEBUG 192 FileWriterBase* fileWriterBase() 193 { 194 return m_fileWriterBase; 195 } 196#endif 197 198private: 199 ReceiveFileWriterCallback() 200#ifndef NDEBUG 201 : m_fileWriterBase(0) 202#endif 203 { 204 } 205 206#ifndef NDEBUG 207 FileWriterBase* m_fileWriterBase; 208#endif 209}; 210 211class LocalErrorCallback : public ErrorCallback { 212public: 213 static PassRefPtr<LocalErrorCallback> create() 214 { 215 return adoptRef(new LocalErrorCallback()); 216 } 217 218 bool handleEvent(FileError* error) 219 { 220 m_error = error; 221 return true; 222 } 223 224 FileError* error() 225 { 226 return m_error.get(); 227 } 228 229private: 230 LocalErrorCallback() 231 { 232 } 233 RefPtr<FileError> m_error; 234}; 235 236} 237 238PassRefPtr<FileWriterSync> DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionCode& ec) 239{ 240 ASSERT(fileEntry); 241 ec = 0; 242 243 244 RefPtr<FileWriterSync> fileWriter = FileWriterSync::create(); 245 RefPtr<ReceiveFileWriterCallback> successCallback = ReceiveFileWriterCallback::create(); 246 RefPtr<LocalErrorCallback> errorCallback = LocalErrorCallback::create(); 247 248 OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback); 249 m_asyncFileSystem->createWriter(fileWriter.get(), createFileSystemURL(fileEntry), callbacks.release()); 250 if (!m_asyncFileSystem->waitForOperationToComplete()) { 251 ec = FileException::ABORT_ERR; 252 return 0; 253 } 254 if (errorCallback->error()) { 255 ASSERT(!successCallback->fileWriterBase()); 256 ec = FileException::ErrorCodeToExceptionCode(errorCallback->error()->code()); 257 return 0; 258 } 259 ASSERT(successCallback->fileWriterBase()); 260 ASSERT(static_cast<FileWriterSync*>(successCallback->fileWriterBase()) == fileWriter.get()); 261 return fileWriter; 262} 263 264} 265 266#endif // ENABLE(FILE_SYSTEM) 267