2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef SyncCallbackHelper_h
33 #define SyncCallbackHelper_h
35 #include "bindings/v8/ExceptionState.h"
36 #include "core/fileapi/FileError.h"
37 #include "core/html/VoidCallback.h"
38 #include "modules/filesystem/DirectoryEntry.h"
39 #include "modules/filesystem/EntriesCallback.h"
40 #include "modules/filesystem/EntryCallback.h"
41 #include "modules/filesystem/EntrySync.h"
42 #include "modules/filesystem/ErrorCallback.h"
43 #include "modules/filesystem/FileEntry.h"
44 #include "modules/filesystem/FileSystemCallback.h"
45 #include "modules/filesystem/MetadataCallback.h"
46 #include "platform/heap/Handle.h"
47 #include "wtf/PassRefPtr.h"
48 #include "wtf/RefCounted.h"
52 template <typename ResultType, typename CallbackArg>
53 struct HelperResultType {
54 DISALLOW_ALLOCATION();
56 typedef PassRefPtrWillBeRawPtr<ResultType> ReturnType;
57 typedef RefPtrWillBePersistent<ResultType> StorageType;
59 static ReturnType createFromCallbackArg(CallbackArg argument)
61 return ResultType::create(argument);
65 // A helper template for FileSystemSync implementation.
66 template <typename SuccessCallback, typename CallbackArg, typename ResultType>
67 class SyncCallbackHelper FINAL : public RefCounted<SyncCallbackHelper<SuccessCallback, CallbackArg, ResultType> > {
69 typedef SyncCallbackHelper<SuccessCallback, CallbackArg, ResultType> HelperType;
70 typedef HelperResultType<ResultType, CallbackArg> ResultTypeTrait;
71 typedef typename ResultTypeTrait::StorageType ResultStorageType;
72 typedef typename ResultTypeTrait::ReturnType ResultReturnType;
74 static PassRefPtr<HelperType> create()
76 return adoptRef(new SyncCallbackHelper());
79 ResultReturnType getResult(ExceptionState& exceptionState)
82 FileError::throwDOMException(exceptionState, m_errorCode);
87 PassOwnPtr<SuccessCallback> successCallback() { return SuccessCallbackImpl::create(this); }
88 PassOwnPtr<ErrorCallback> errorCallback() { return ErrorCallbackImpl::create(this); }
92 : m_errorCode(FileError::OK)
97 class SuccessCallbackImpl FINAL : public SuccessCallback {
99 static PassOwnPtr<SuccessCallbackImpl> create(PassRefPtr<HelperType> helper)
101 return adoptPtr(new SuccessCallbackImpl(helper));
104 virtual void handleEvent()
106 m_helper->setError(FileError::OK);
109 virtual void handleEvent(CallbackArg arg)
111 m_helper->setResult(arg);
115 explicit SuccessCallbackImpl(PassRefPtr<HelperType> helper)
119 RefPtr<HelperType> m_helper;
122 class ErrorCallbackImpl FINAL : public ErrorCallback {
124 static PassOwnPtr<ErrorCallbackImpl> create(PassRefPtr<HelperType> helper)
126 return adoptPtr(new ErrorCallbackImpl(helper));
129 virtual void handleEvent(FileError* error) OVERRIDE
132 m_helper->setError(error->code());
136 explicit ErrorCallbackImpl(PassRefPtr<HelperType> helper)
140 RefPtr<HelperType> m_helper;
143 void setError(FileError::ErrorCode code)
149 void setResult(CallbackArg result)
151 m_result = ResultTypeTrait::createFromCallbackArg(result);
155 ResultStorageType m_result;
156 FileError::ErrorCode m_errorCode;
160 struct EmptyType : public RefCountedWillBeGarbageCollected<EmptyType> {
161 static PassRefPtrWillBeRawPtr<EmptyType> create(EmptyType*)
166 void trace(Visitor*) { }
169 typedef SyncCallbackHelper<EntryCallback, Entry*, EntrySync> EntrySyncCallbackHelper;
170 typedef SyncCallbackHelper<MetadataCallback, Metadata*, Metadata> MetadataSyncCallbackHelper;
171 typedef SyncCallbackHelper<VoidCallback, EmptyType*, EmptyType> VoidSyncCallbackHelper;
172 typedef SyncCallbackHelper<FileSystemCallback, DOMFileSystem*, DOMFileSystemSync> FileSystemSyncCallbackHelper;
174 } // namespace WebCore
176 #endif // SyncCallbackHelper_h