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