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)
63 DOMFileSystemSync::~DOMFileSystemSync()
67 void DOMFileSystemSync::reportError(ErrorCallback* errorCallback, PassRefPtrWillBeRawPtr<FileError> fileError)
69 errorCallback->handleEvent(fileError.get());
72 DirectoryEntrySync* DOMFileSystemSync::root()
74 return DirectoryEntrySync::create(this, DOMFilePath::root);
79 class CreateFileHelper FINAL : public AsyncFileSystemCallbacks {
81 class CreateFileResult : public GarbageCollectedFinalized<CreateFileResult> {
83 static CreateFileResult* create()
85 return new CreateFileResult();
90 RefPtrWillBeMember<File> m_file;
92 void trace(Visitor* visitor)
94 visitor->trace(m_file);
105 static PassOwnPtr<AsyncFileSystemCallbacks> create(CreateFileResult* result, const String& name, const KURL& url, FileSystemType type)
107 return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new CreateFileHelper(result, name, url, type)));
110 virtual void didFail(int code) OVERRIDE
112 m_result->m_failed = true;
113 m_result->m_code = code;
116 virtual ~CreateFileHelper()
120 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot) OVERRIDE
122 // We can't directly use the snapshot blob data handle because the content type on it hasn't been set.
123 // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until
124 // *after* we've coined a File with a new handle that has the correct type set on it. This allows the
125 // blob storage system to track when a temp file can and can't be safely deleted.
127 m_result->m_file = DOMFileSystemBase::createFile(metadata, m_url, m_type, m_name);
130 virtual bool shouldBlockUntilCompletion() const OVERRIDE
136 CreateFileHelper(CreateFileResult* result, const String& name, const KURL& url, FileSystemType type)
144 Persistent<CreateFileResult> m_result;
147 FileSystemType m_type;
152 PassRefPtrWillBeRawPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
154 KURL fileSystemURL = createFileSystemURL(fileEntry);
155 CreateFileHelper::CreateFileResult* result(CreateFileHelper::CreateFileResult::create());
156 fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, CreateFileHelper::create(result, fileEntry->name(), fileSystemURL, type()));
157 if (result->m_failed) {
158 exceptionState.throwDOMException(result->m_code, "Could not create '" + fileEntry->name() + "'.");
161 return result->m_file.get();
166 class ReceiveFileWriterCallback FINAL : public FileWriterBaseCallback {
168 static ReceiveFileWriterCallback* create()
170 return new ReceiveFileWriterCallback();
173 virtual void handleEvent(FileWriterBase*) OVERRIDE
178 ReceiveFileWriterCallback()
183 class LocalErrorCallback FINAL : public ErrorCallback {
185 static LocalErrorCallback* create(FileError::ErrorCode& errorCode)
187 return new LocalErrorCallback(errorCode);
190 virtual void handleEvent(FileError* error) OVERRIDE
192 ASSERT(error->code() != FileError::OK);
193 m_errorCode = error->code();
197 explicit LocalErrorCallback(FileError::ErrorCode& errorCode)
198 : m_errorCode(errorCode)
202 FileError::ErrorCode& m_errorCode;
207 FileWriterSync* DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
211 FileWriterSync* fileWriter = FileWriterSync::create();
212 ReceiveFileWriterCallback* successCallback = ReceiveFileWriterCallback::create();
213 FileError::ErrorCode errorCode = FileError::OK;
214 LocalErrorCallback* errorCallback = LocalErrorCallback::create(errorCode);
216 OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback, m_context);
217 callbacks->setShouldBlockUntilCompletion(true);
219 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, callbacks.release());
220 if (errorCode != FileError::OK) {
221 FileError::throwDOMException(exceptionState, errorCode);