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/DOMFileSystem.h" 33 34 #include "core/fileapi/File.h" 35 #include "modules/filesystem/DOMFilePath.h" 36 #include "modules/filesystem/DirectoryEntry.h" 37 #include "modules/filesystem/ErrorCallback.h" 38 #include "modules/filesystem/FileCallback.h" 39 #include "modules/filesystem/FileEntry.h" 40 #include "modules/filesystem/FileSystemCallbacks.h" 41 #include "modules/filesystem/FileWriter.h" 42 #include "modules/filesystem/FileWriterBaseCallback.h" 43 #include "modules/filesystem/FileWriterCallback.h" 44 #include "modules/filesystem/MetadataCallback.h" 45 #include "platform/FileMetadata.h" 46 #include "platform/weborigin/DatabaseIdentifier.h" 47 #include "platform/weborigin/SecurityOrigin.h" 48 #include "public/platform/WebFileSystem.h" 49 #include "public/platform/WebFileSystemCallbacks.h" 50 #include "wtf/OwnPtr.h" 51 #include "wtf/text/StringBuilder.h" 52 #include "wtf/text/WTFString.h" 53 54 namespace WebCore { 55 56 // static 57 PassRefPtr<DOMFileSystem> DOMFileSystem::create(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL) 58 { 59 RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, type, rootURL))); 60 fileSystem->suspendIfNeeded(); 61 return fileSystem.release(); 62 } 63 64 PassRefPtr<DOMFileSystem> DOMFileSystem::createIsolatedFileSystem(ExecutionContext* context, const String& filesystemId) 65 { 66 if (filesystemId.isEmpty()) 67 return 0; 68 69 StringBuilder filesystemName; 70 filesystemName.append(createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin())); 71 filesystemName.append(":Isolated_"); 72 filesystemName.append(filesystemId); 73 74 // The rootURL created here is going to be attached to each filesystem request and 75 // is to be validated each time the request is being handled. 76 StringBuilder rootURL; 77 rootURL.append("filesystem:"); 78 rootURL.append(context->securityOrigin()->toString()); 79 rootURL.append("/"); 80 rootURL.append(isolatedPathPrefix); 81 rootURL.append("/"); 82 rootURL.append(filesystemId); 83 rootURL.append("/"); 84 85 return DOMFileSystem::create(context, filesystemName.toString(), FileSystemTypeIsolated, KURL(ParsedURLString, rootURL.toString())); 86 } 87 88 DOMFileSystem::DOMFileSystem(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL) 89 : DOMFileSystemBase(context, name, type, rootURL) 90 , ActiveDOMObject(context) 91 { 92 ScriptWrappable::init(this); 93 } 94 95 PassRefPtr<DirectoryEntry> DOMFileSystem::root() 96 { 97 return DirectoryEntry::create(this, DOMFilePath::root); 98 } 99 100 void DOMFileSystem::addPendingCallbacks() 101 { 102 setPendingActivity(this); 103 } 104 105 void DOMFileSystem::removePendingCallbacks() 106 { 107 unsetPendingActivity(this); 108 } 109 110 void DOMFileSystem::reportError(PassOwnPtr<ErrorCallback> errorCallback, PassRefPtr<FileError> fileError) 111 { 112 scheduleCallback(errorCallback, fileError); 113 } 114 115 namespace { 116 117 class ConvertToFileWriterCallback : public FileWriterBaseCallback { 118 public: 119 static PassOwnPtr<ConvertToFileWriterCallback> create(PassOwnPtr<FileWriterCallback> callback) 120 { 121 return adoptPtr(new ConvertToFileWriterCallback(callback)); 122 } 123 124 void handleEvent(FileWriterBase* fileWriterBase) 125 { 126 m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase)); 127 } 128 private: 129 ConvertToFileWriterCallback(PassOwnPtr<FileWriterCallback> callback) 130 : m_callback(callback) 131 { 132 } 133 OwnPtr<FileWriterCallback> m_callback; 134 }; 135 136 } 137 138 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassOwnPtr<FileWriterCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 139 { 140 ASSERT(fileEntry); 141 142 RefPtr<FileWriter> fileWriter = FileWriter::create(executionContext()); 143 OwnPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback); 144 OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback.release(), errorCallback); 145 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter.get(), callbacks.release()); 146 } 147 148 namespace { 149 150 class SnapshotFileCallback : public FileSystemCallbacksBase { 151 public: 152 static PassOwnPtr<AsyncFileSystemCallbacks> create(PassRefPtr<DOMFileSystem> filesystem, const String& name, const KURL& url, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 153 { 154 return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new SnapshotFileCallback(filesystem, name, url, successCallback, errorCallback))); 155 } 156 157 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot) 158 { 159 if (!m_successCallback) 160 return; 161 162 // We can't directly use the snapshot blob data handle because the content type on it hasn't been set. 163 // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until 164 // *after* we've coined a File with a new handle that has the correct type set on it. This allows the 165 // blob storage system to track when a temp file can and can't be safely deleted. 166 167 // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics. 168 // For other filesystem types (which could be platform-specific ones), there's a chance that the files are on remote filesystem. If the port has returned metadata just pass it to File constructor (so we may cache the metadata). 169 // FIXME: We should use the snapshot metadata for all files. 170 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746 171 if (m_fileSystem->type() == FileSystemTypeTemporary || m_fileSystem->type() == FileSystemTypePersistent) { 172 m_successCallback->handleEvent(File::createWithName(metadata.platformPath, m_name).get()); 173 } else if (!metadata.platformPath.isEmpty()) { 174 // If the platformPath in the returned metadata is given, we create a File object for the path. 175 m_successCallback->handleEvent(File::createForFileSystemFile(m_name, metadata).get()); 176 } else { 177 // Otherwise create a File from the FileSystem URL. 178 m_successCallback->handleEvent(File::createForFileSystemFile(m_url, metadata).get()); 179 } 180 181 m_successCallback.release(); 182 } 183 184 private: 185 SnapshotFileCallback(PassRefPtr<DOMFileSystem> filesystem, const String& name, const KURL& url, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 186 : FileSystemCallbacksBase(errorCallback, filesystem.get()) 187 , m_name(name) 188 , m_url(url) 189 , m_successCallback(successCallback) 190 { 191 } 192 193 String m_name; 194 KURL m_url; 195 OwnPtr<FileCallback> m_successCallback; 196 }; 197 198 } // namespace 199 200 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback) 201 { 202 KURL fileSystemURL = createFileSystemURL(fileEntry); 203 fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, SnapshotFileCallback::create(this, fileEntry->name(), fileSystemURL, successCallback, errorCallback)); 204 } 205 206 } // namespace WebCore 207