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.
31 #ifndef SyncCallbackHelper_h
32 #define SyncCallbackHelper_h
34 #if ENABLE(FILE_SYSTEM)
36 #include "DirectoryEntry.h"
37 #include "EntriesCallback.h"
38 #include "EntryArraySync.h"
39 #include "EntryCallback.h"
40 #include "ErrorCallback.h"
41 #include "ExceptionCode.h"
42 #include "FileEntry.h"
43 #include "FileError.h"
44 #include "FileException.h"
45 #include "FileSystemCallback.h"
46 #include "MetadataCallback.h"
47 #include "VoidCallback.h"
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/RefCounted.h>
53 class AsyncFileSystem;
54 class DirectoryEntrySync;
59 // A helper template for FileSystemSync implementation.
60 template <typename SuccessCallback, typename ObserverType, typename CallbackArg, typename ResultType>
61 class SyncCallbackHelper {
62 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper);
64 typedef SyncCallbackHelper<SuccessCallback, ObserverType, CallbackArg, ResultType> HelperType;
65 SyncCallbackHelper(ObserverType* observer = 0)
66 : m_observer(observer)
67 , m_successCallback(SuccessCallbackImpl::create(this))
68 , m_errorCallback(ErrorCallbackImpl::create(this))
74 PassRefPtr<ResultType> getResult(ExceptionCode& ec)
77 while (!m_completed) {
78 if (!m_observer->waitForOperationToComplete()) {
79 m_exceptionCode = FileException::ABORT_ERR;
85 return m_result.release();
88 PassRefPtr<SuccessCallback> successCallback() { return m_successCallback; }
89 PassRefPtr<ErrorCallback> errorCallback() { return m_errorCallback; }
92 class SuccessCallbackImpl : public SuccessCallback {
94 static PassRefPtr<SuccessCallbackImpl> create(HelperType* helper)
96 return adoptRef(new SuccessCallbackImpl(helper));
99 virtual void handleEvent()
101 m_helper->setError(0);
104 virtual bool handleEvent(CallbackArg* arg)
106 m_helper->setResult(ResultType::create(arg));
111 SuccessCallbackImpl(HelperType* helper)
115 HelperType* m_helper;
118 class ErrorCallbackImpl : public ErrorCallback {
120 static PassRefPtr<ErrorCallbackImpl> create(HelperType* helper)
122 return adoptRef(new ErrorCallbackImpl(helper));
125 virtual bool handleEvent(FileError* error)
128 m_helper->setError(error->code());
133 ErrorCallbackImpl(HelperType* helper)
137 HelperType* m_helper;
140 friend class SuccessCallbackImpl;
141 friend class ErrorCallbackImpl;
143 void setError(int code)
145 m_exceptionCode = FileException::ErrorCodeToExceptionCode(code);
149 void setResult(PassRefPtr<ResultType> result)
155 ObserverType* m_observer;
156 RefPtr<SuccessCallbackImpl> m_successCallback;
157 RefPtr<ErrorCallbackImpl> m_errorCallback;
158 RefPtr<ResultType> m_result;
159 ExceptionCode m_exceptionCode;
163 struct EmptyType : public RefCounted<EmptyType> {
164 static PassRefPtr<EmptyType> create(EmptyType*)
170 struct EmptyObserverType {
171 bool waitForOperationToComplete()
177 typedef SyncCallbackHelper<EntryCallback, AsyncFileSystem, Entry, EntrySync> EntrySyncCallbackHelper;
178 typedef SyncCallbackHelper<EntriesCallback, AsyncFileSystem, EntryArray, EntryArraySync> EntriesSyncCallbackHelper;
179 typedef SyncCallbackHelper<MetadataCallback, AsyncFileSystem, Metadata, Metadata> MetadataSyncCallbackHelper;
180 typedef SyncCallbackHelper<VoidCallback, AsyncFileSystem, EmptyType, EmptyType> VoidSyncCallbackHelper;
181 typedef SyncCallbackHelper<FileSystemCallback, EmptyObserverType, DOMFileSystem, DOMFileSystemSync> FileSystemSyncCallbackHelper;
183 } // namespace WebCore
185 #endif // ENABLE(FILE_SYSTEM)
187 #endif // SyncCallbackHelper_h