2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/filesystem/DOMFileSystemSync.h"
34 #include "bindings/core/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"
53 DOMFileSystemSync* DOMFileSystemSync::create(DOMFileSystemBase* fileSystem)
55 return new DOMFileSystemSync(fileSystem->m_context, fileSystem->name(), fileSystem->type(), fileSystem->rootURL());
58 DOMFileSystemSync::DOMFileSystemSync(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
59 : DOMFileSystemBase(context, name, type, rootURL)
61 ScriptWrappable::init(this);
64 DOMFileSystemSync::~DOMFileSystemSync()
68 void DOMFileSystemSync::reportError(PassOwnPtr<ErrorCallback> errorCallback, PassRefPtrWillBeRawPtr<FileError> fileError)
70 errorCallback->handleEvent(fileError.get());
73 DirectoryEntrySync* DOMFileSystemSync::root()
75 return DirectoryEntrySync::create(this, DOMFilePath::root);
80 class CreateFileHelper FINAL : public AsyncFileSystemCallbacks {
82 class CreateFileResult : public RefCountedWillBeGarbageCollected<CreateFileResult> {
84 static PassRefPtrWillBeRawPtr<CreateFileResult> create()
86 return adoptRefWillBeNoop(new CreateFileResult());
91 RefPtrWillBeMember<File> m_file;
93 void trace(Visitor* visitor)
95 visitor->trace(m_file);
111 friend class RefCountedWillBeGarbageCollected<CreateFileResult>;
114 static PassOwnPtr<AsyncFileSystemCallbacks> create(PassRefPtrWillBeRawPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type)
116 return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new CreateFileHelper(result, name, url, type)));
119 virtual void didFail(int code) OVERRIDE
121 m_result->m_failed = true;
122 m_result->m_code = code;
125 virtual ~CreateFileHelper()
129 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot) OVERRIDE
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.
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::createForFileSystemFile(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();
147 // Otherwise create a File from the FileSystem URL.
148 m_result->m_file = File::createForFileSystemFile(m_url, metadata).get();
152 virtual bool shouldBlockUntilCompletion() const OVERRIDE
158 CreateFileHelper(PassRefPtrWillBeRawPtr<CreateFileResult> result, const String& name, const KURL& url, FileSystemType type)
166 RefPtrWillBePersistent<CreateFileResult> m_result;
169 FileSystemType m_type;
174 PassRefPtrWillBeRawPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
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() + "'.");
183 return result->m_file.get();
188 class ReceiveFileWriterCallback FINAL : public FileWriterBaseCallback {
190 static PassOwnPtr<ReceiveFileWriterCallback> create()
192 return adoptPtr(new ReceiveFileWriterCallback());
195 virtual void handleEvent(FileWriterBase*) OVERRIDE
200 ReceiveFileWriterCallback()
205 class LocalErrorCallback FINAL : public ErrorCallback {
207 static PassOwnPtr<LocalErrorCallback> create(FileError::ErrorCode& errorCode)
209 return adoptPtr(new LocalErrorCallback(errorCode));
212 virtual void handleEvent(FileError* error) OVERRIDE
214 ASSERT(error->code() != FileError::OK);
215 m_errorCode = error->code();
219 explicit LocalErrorCallback(FileError::ErrorCode& errorCode)
220 : m_errorCode(errorCode)
224 FileError::ErrorCode& m_errorCode;
229 FileWriterSync* DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
233 FileWriterSync* fileWriter = FileWriterSync::create();
234 OwnPtr<ReceiveFileWriterCallback> successCallback = ReceiveFileWriterCallback::create();
235 FileError::ErrorCode errorCode = FileError::OK;
236 OwnPtr<LocalErrorCallback> errorCallback = LocalErrorCallback::create(errorCode);
238 OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback.release(), errorCallback.release(), m_context);
239 callbacks->setShouldBlockUntilCompletion(true);
241 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, callbacks.release());
242 if (errorCode != FileError::OK) {
243 FileError::throwDOMException(exceptionState, errorCode);