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/dom/ScriptExecutionContext.h" 35 #include "core/fileapi/File.h" 36 #include "core/platform/AsyncFileSystem.h" 37 #include "core/platform/FileMetadata.h" 38 #include "modules/filesystem/DOMFilePath.h" 39 #include "modules/filesystem/DirectoryEntry.h" 40 #include "modules/filesystem/ErrorCallback.h" 41 #include "modules/filesystem/FileCallback.h" 42 #include "modules/filesystem/FileEntry.h" 43 #include "modules/filesystem/FileSystemCallbacks.h" 44 #include "modules/filesystem/FileWriter.h" 45 #include "modules/filesystem/FileWriterBaseCallback.h" 46 #include "modules/filesystem/FileWriterCallback.h" 47 #include "modules/filesystem/MetadataCallback.h" 48 #include "weborigin/DatabaseIdentifier.h" 49 #include "weborigin/SecurityOrigin.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(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem) 58 { 59 RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, type, rootURL, asyncFileSystem))); 60 fileSystem->suspendIfNeeded(); 61 return fileSystem.release(); 62 } 63 64 PassRefPtr<DOMFileSystem> DOMFileSystem::createIsolatedFileSystem(ScriptExecutionContext* 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()), AsyncFileSystem::create()); 86 } 87 88 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFileSystem) 89 : DOMFileSystemBase(context, name, type, rootURL, asyncFileSystem) 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::stop() 101 { 102 m_asyncFileSystem->stop(); 103 } 104 105 bool DOMFileSystem::hasPendingActivity() const 106 { 107 return m_asyncFileSystem->hasPendingActivity(); 108 } 109 110 void DOMFileSystem::contextDestroyed() 111 { 112 m_asyncFileSystem->stop(); 113 ActiveDOMObject::contextDestroyed(); 114 } 115 116 namespace { 117 118 class ConvertToFileWriterCallback : public FileWriterBaseCallback { 119 public: 120 static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterCallback> callback) 121 { 122 return adoptRef(new ConvertToFileWriterCallback(callback)); 123 } 124 125 bool handleEvent(FileWriterBase* fileWriterBase) 126 { 127 return m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase)); 128 } 129 private: 130 ConvertToFileWriterCallback(PassRefPtr<FileWriterCallback> callback) 131 : m_callback(callback) 132 { 133 } 134 RefPtr<FileWriterCallback> m_callback; 135 }; 136 137 } 138 139 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWriterCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 140 { 141 ASSERT(fileEntry); 142 143 RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext()); 144 RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback); 145 OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback, errorCallback); 146 m_asyncFileSystem->createWriter(fileWriter.get(), createFileSystemURL(fileEntry), callbacks.release()); 147 } 148 149 namespace { 150 151 class SnapshotFileCallback : public FileSystemCallbacksBase { 152 public: 153 static PassOwnPtr<SnapshotFileCallback> create(PassRefPtr<DOMFileSystem> filesystem, const String& name, const KURL& url, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 154 { 155 return adoptPtr(new SnapshotFileCallback(filesystem, name, url, successCallback, errorCallback)); 156 } 157 158 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot) 159 { 160 ASSERT(!metadata.platformPath.isEmpty()); 161 if (!m_successCallback) 162 return; 163 164 // We can't directly use the snapshot blob data handle because the content type on it hasn't been set. 165 // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until 166 // *after* we've coined a File with a new handle that has the correct type set on it. This allows the 167 // blob storage system to track when a temp file can and can't be safely deleted. 168 169 // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics. 170 // 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). 171 // FIXME: We should use the snapshot metadata for all files. 172 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746 173 if (m_filesystem->type() == FileSystemTypeTemporary || m_filesystem->type() == FileSystemTypePersistent) { 174 m_successCallback->handleEvent(File::createWithName(metadata.platformPath, m_name).get()); 175 } else if (!metadata.platformPath.isEmpty()) { 176 // If the platformPath in the returned metadata is given, we create a File object for the path. 177 m_successCallback->handleEvent(File::createForFileSystemFile(m_name, metadata).get()); 178 } else { 179 // Otherwise create a File from the FileSystem URL. 180 m_successCallback->handleEvent(File::createForFileSystemFile(m_url, metadata).get()); 181 } 182 183 m_successCallback.release(); 184 } 185 186 private: 187 SnapshotFileCallback(PassRefPtr<DOMFileSystem> filesystem, const String& name, const KURL& url, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 188 : FileSystemCallbacksBase(errorCallback) 189 , m_filesystem(filesystem) 190 , m_name(name) 191 , m_url(url) 192 , m_successCallback(successCallback) 193 { 194 } 195 196 RefPtr<DOMFileSystem> m_filesystem; 197 String m_name; 198 KURL m_url; 199 RefPtr<FileCallback> m_successCallback; 200 }; 201 202 } // namespace 203 204 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) 205 { 206 KURL fileSystemURL = createFileSystemURL(fileEntry); 207 m_asyncFileSystem->createSnapshotFileAndReadMetadata(fileSystemURL, SnapshotFileCallback::create(this, fileEntry->name(), fileSystemURL, successCallback, errorCallback)); 208 } 209 210 } // namespace WebCore 211