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