Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / filesystem / SyncCallbackHelper.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Samsung Electronics. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
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
14  * distribution.
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.
18  *
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.
30  */
31
32 #ifndef SyncCallbackHelper_h
33 #define SyncCallbackHelper_h
34
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"
49
50 namespace WebCore {
51
52 template <typename ResultType, typename CallbackArg>
53 struct HelperResultType {
54     DISALLOW_ALLOCATION();
55 public:
56     typedef PassRefPtrWillBeRawPtr<ResultType> ReturnType;
57     typedef RefPtrWillBePersistent<ResultType> StorageType;
58
59     static ReturnType createFromCallbackArg(CallbackArg argument)
60     {
61         return ResultType::create(argument);
62     }
63 };
64
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> > {
68 public:
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;
73
74     static PassRefPtr<HelperType> create()
75     {
76         return adoptRef(new SyncCallbackHelper());
77     }
78
79     ResultReturnType getResult(ExceptionState& exceptionState)
80     {
81         if (m_errorCode)
82             FileError::throwDOMException(exceptionState, m_errorCode);
83
84         return m_result;
85     }
86
87     PassOwnPtr<SuccessCallback> successCallback() { return SuccessCallbackImpl::create(this); }
88     PassOwnPtr<ErrorCallback> errorCallback() { return ErrorCallbackImpl::create(this); }
89
90 private:
91     SyncCallbackHelper()
92         : m_errorCode(FileError::OK)
93         , m_completed(false)
94     {
95     }
96
97     class SuccessCallbackImpl FINAL : public SuccessCallback {
98     public:
99         static PassOwnPtr<SuccessCallbackImpl> create(PassRefPtr<HelperType> helper)
100         {
101             return adoptPtr(new SuccessCallbackImpl(helper));
102         }
103
104         virtual void handleEvent()
105         {
106             m_helper->setError(FileError::OK);
107         }
108
109         virtual void handleEvent(CallbackArg arg)
110         {
111             m_helper->setResult(arg);
112         }
113
114     private:
115         explicit SuccessCallbackImpl(PassRefPtr<HelperType> helper)
116             : m_helper(helper)
117         {
118         }
119         RefPtr<HelperType> m_helper;
120     };
121
122     class ErrorCallbackImpl FINAL : public ErrorCallback {
123     public:
124         static PassOwnPtr<ErrorCallbackImpl> create(PassRefPtr<HelperType> helper)
125         {
126             return adoptPtr(new ErrorCallbackImpl(helper));
127         }
128
129         virtual void handleEvent(FileError* error) OVERRIDE
130         {
131             ASSERT(error);
132             m_helper->setError(error->code());
133         }
134
135     private:
136         explicit ErrorCallbackImpl(PassRefPtr<HelperType> helper)
137             : m_helper(helper)
138         {
139         }
140         RefPtr<HelperType> m_helper;
141     };
142
143     void setError(FileError::ErrorCode code)
144     {
145         m_errorCode = code;
146         m_completed = true;
147     }
148
149     void setResult(CallbackArg result)
150     {
151         m_result = ResultTypeTrait::createFromCallbackArg(result);
152         m_completed = true;
153     }
154
155     ResultStorageType m_result;
156     FileError::ErrorCode m_errorCode;
157     bool m_completed;
158 };
159
160 struct EmptyType : public RefCountedWillBeGarbageCollected<EmptyType> {
161     static PassRefPtrWillBeRawPtr<EmptyType> create(EmptyType*)
162     {
163         return nullptr;
164     }
165
166     void trace(Visitor*) { }
167 };
168
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;
173
174 } // namespace WebCore
175
176 #endif // SyncCallbackHelper_h