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 "DOMFileSystemSync.h"
34 #if ENABLE(FILE_SYSTEM)
36 #include "AsyncFileSystem.h"
37 #include "AsyncFileWriter.h"
38 #include "DOMFilePath.h"
39 #include "DirectoryEntrySync.h"
40 #include "ErrorCallback.h"
42 #include "FileEntrySync.h"
43 #include "FileError.h"
44 #include "FileException.h"
45 #include "FileMetadata.h"
46 #include "FileSystemCallbacks.h"
47 #include "FileWriterBaseCallback.h"
48 #include "FileWriterSync.h"
54 PassRefPtr<DOMFileSystemSync> DOMFileSystemSync::create(DOMFileSystemBase* fileSystem)
56 return adoptRef(new DOMFileSystemSync(fileSystem->m_context, fileSystem->m_name, fileSystem->m_asyncFileSystem.release()));
59 DOMFileSystemSync::DOMFileSystemSync(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
60 : DOMFileSystemBase(context, name, asyncFileSystem)
64 DOMFileSystemSync::~DOMFileSystemSync()
68 PassRefPtr<DirectoryEntrySync> DOMFileSystemSync::root()
70 return DirectoryEntrySync::create(this, DOMFilePath::root);
75 class GetPathHelper : public AsyncFileSystemCallbacks {
77 class GetPathResult : public RefCounted<GetPathResult> {
79 static PassRefPtr<GetPathResult> create()
81 return adoptRef(new GetPathResult());
98 friend class WTF::RefCounted<GetPathResult>;
101 static PassOwnPtr<GetPathHelper> create(PassRefPtr<GetPathResult> result)
103 return adoptPtr(new GetPathHelper(result));
106 virtual void didSucceed()
108 ASSERT_NOT_REACHED();
111 virtual void didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>)
113 ASSERT_NOT_REACHED();
116 virtual void didReadDirectoryEntry(const String&, bool)
118 ASSERT_NOT_REACHED();
121 virtual void didReadDirectoryEntries(bool)
123 ASSERT_NOT_REACHED();
126 virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long)
128 ASSERT_NOT_REACHED();
131 virtual void didFail(int code)
133 m_result->m_failed = true;
134 m_result->m_code = code;
137 virtual ~GetPathHelper()
141 void didReadMetadata(const FileMetadata& metadata)
143 m_result->m_path = metadata.platformPath;
146 GetPathHelper(PassRefPtr<GetPathResult> result)
151 RefPtr<GetPathResult> m_result;
156 PassRefPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionCode& ec)
159 RefPtr<GetPathHelper::GetPathResult> result(GetPathHelper::GetPathResult::create());
160 m_asyncFileSystem->readMetadata(fileEntry->fullPath(), GetPathHelper::create(result));
161 if (!m_asyncFileSystem->waitForOperationToComplete()) {
162 ec = FileException::ABORT_ERR;
165 if (result->m_failed) {
169 ASSERT(!result->m_path.isEmpty());
170 return File::createWithName(result->m_path, fileEntry->name());
175 class ReceiveFileWriterCallback : public FileWriterBaseCallback {
177 static PassRefPtr<ReceiveFileWriterCallback> create()
179 return adoptRef(new ReceiveFileWriterCallback());
182 bool handleEvent(FileWriterBase* fileWriterBase)
185 m_fileWriterBase = fileWriterBase;
187 ASSERT_UNUSED(fileWriterBase, fileWriterBase);
193 FileWriterBase* fileWriterBase()
195 return m_fileWriterBase;
200 ReceiveFileWriterCallback()
202 : m_fileWriterBase(0)
208 FileWriterBase* m_fileWriterBase;
212 class LocalErrorCallback : public ErrorCallback {
214 static PassRefPtr<LocalErrorCallback> create()
216 return adoptRef(new LocalErrorCallback());
219 bool handleEvent(FileError* error)
227 return m_error.get();
234 RefPtr<FileError> m_error;
239 PassRefPtr<FileWriterSync> DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionCode& ec)
245 RefPtr<FileWriterSync> fileWriter = FileWriterSync::create();
246 RefPtr<ReceiveFileWriterCallback> successCallback = ReceiveFileWriterCallback::create();
247 RefPtr<LocalErrorCallback> errorCallback = LocalErrorCallback::create();
249 OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback);
250 m_asyncFileSystem->createWriter(fileWriter.get(), fileEntry->fullPath(), callbacks.release());
251 if (!m_asyncFileSystem->waitForOperationToComplete()) {
252 ec = FileException::ABORT_ERR;
255 if (errorCallback->error()) {
256 ASSERT(!successCallback->fileWriterBase());
257 ec = FileException::ErrorCodeToExceptionCode(errorCallback->error()->code());
260 ASSERT(successCallback->fileWriterBase());
261 ASSERT(static_cast<FileWriterSync*>(successCallback->fileWriterBase()) == fileWriter.get());
267 #endif // ENABLE(FILE_SYSTEM)