Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: Source/modules/filesystem/SyncCallbackHelper.h

Issue 22673003: Replace EntryArraySync type by an EntrySync[] (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add copyright Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/filesystem/EntryArraySync.idl ('k') | Source/modules/modules.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
3 * 4 *
4 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
6 * met: 7 * met:
7 * 8 *
8 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 11 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 12 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 13 * in the documentation and/or other materials provided with the
(...skipping 15 matching lines...) Expand all
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 30 */
30 31
31 #ifndef SyncCallbackHelper_h 32 #ifndef SyncCallbackHelper_h
32 #define SyncCallbackHelper_h 33 #define SyncCallbackHelper_h
33 34
34 #include "bindings/v8/ExceptionState.h" 35 #include "bindings/v8/ExceptionState.h"
35 #include "core/fileapi/FileError.h" 36 #include "core/fileapi/FileError.h"
36 #include "core/html/VoidCallback.h" 37 #include "core/html/VoidCallback.h"
37 #include "modules/filesystem/DirectoryEntry.h" 38 #include "modules/filesystem/DirectoryEntry.h"
39 #include "modules/filesystem/DirectoryReaderSync.h"
38 #include "modules/filesystem/EntriesCallback.h" 40 #include "modules/filesystem/EntriesCallback.h"
39 #include "modules/filesystem/EntryArraySync.h"
40 #include "modules/filesystem/EntryCallback.h" 41 #include "modules/filesystem/EntryCallback.h"
42 #include "modules/filesystem/EntrySync.h"
41 #include "modules/filesystem/ErrorCallback.h" 43 #include "modules/filesystem/ErrorCallback.h"
42 #include "modules/filesystem/FileEntry.h" 44 #include "modules/filesystem/FileEntry.h"
43 #include "modules/filesystem/FileSystemCallback.h" 45 #include "modules/filesystem/FileSystemCallback.h"
44 #include "modules/filesystem/MetadataCallback.h" 46 #include "modules/filesystem/MetadataCallback.h"
45 #include "wtf/PassRefPtr.h" 47 #include "wtf/PassRefPtr.h"
46 #include "wtf/RefCounted.h" 48 #include "wtf/RefCounted.h"
47 49
48 namespace WebCore { 50 namespace WebCore {
49 51
50 class AsyncFileSystem; 52 class AsyncFileSystem;
51 class DirectoryEntrySync; 53 class DirectoryEntrySync;
52 class EntryArraySync; 54 class EntryArraySync;
kinuko 2013/08/08 11:39:19 nit: obsolete
53 class EntrySync; 55 class EntrySync;
54 class FileEntrySync; 56 class FileEntrySync;
kinuko 2013/08/08 11:39:19 I guess actually we can remove all these forward d
55 57
56 // A helper template for FileSystemSync implementation. 58 // A helper template for FileSystemSync implementation.
57 template <typename SuccessCallback, typename ObserverType, typename CallbackArg, typename ResultType> 59 template <typename SuccessCallback, typename ObserverType, typename CallbackArg>
58 class SyncCallbackHelper { 60 class SyncCallbackHelperBase {
59 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper); 61 WTF_MAKE_NONCOPYABLE(SyncCallbackHelperBase);
60 public: 62 public:
61 typedef SyncCallbackHelper<SuccessCallback, ObserverType, CallbackArg, Resul tType> HelperType; 63 typedef SyncCallbackHelperBase<SuccessCallback, ObserverType, CallbackArg> H elperType;
62 SyncCallbackHelper(ObserverType* observer = 0) 64 SyncCallbackHelperBase(ObserverType* observer)
63 : m_observer(observer) 65 : m_observer(observer)
64 , m_successCallback(SuccessCallbackImpl::create(this)) 66 , m_successCallback(SuccessCallbackImpl::create(this))
65 , m_errorCallback(ErrorCallbackImpl::create(this)) 67 , m_errorCallback(ErrorCallbackImpl::create(this))
66 , m_errorCode(FileError::OK) 68 , m_errorCode(FileError::OK)
67 , m_completed(false) 69 , m_completed(false)
68 { 70 {
69 } 71 }
70 72
71 PassRefPtr<ResultType> getResult(ExceptionState& es)
72 {
73 if (m_observer) {
74 while (!m_completed) {
75 if (!m_observer->waitForOperationToComplete()) {
76 m_errorCode = FileError::ABORT_ERR;
77 break;
78 }
79 }
80 }
81 if (m_errorCode)
82 FileError::throwDOMException(es, m_errorCode);
83
84 return m_result.release();
85 }
86
87 PassRefPtr<SuccessCallback> successCallback() { return m_successCallback; } 73 PassRefPtr<SuccessCallback> successCallback() { return m_successCallback; }
88 PassRefPtr<ErrorCallback> errorCallback() { return m_errorCallback; } 74 PassRefPtr<ErrorCallback> errorCallback() { return m_errorCallback; }
89 75
90 private: 76 protected:
91 class SuccessCallbackImpl : public SuccessCallback { 77 class SuccessCallbackImpl : public SuccessCallback {
92 public: 78 public:
93 static PassRefPtr<SuccessCallbackImpl> create(HelperType* helper) 79 static PassRefPtr<SuccessCallbackImpl> create(HelperType* helper)
94 { 80 {
95 return adoptRef(new SuccessCallbackImpl(helper)); 81 return adoptRef(new SuccessCallbackImpl(helper));
96 } 82 }
97 83
98 virtual bool handleEvent() 84 virtual bool handleEvent()
99 { 85 {
100 m_helper->setError(FileError::OK); 86 m_helper->setError(FileError::OK);
101 return true; 87 return true;
102 } 88 }
103 89
104 virtual bool handleEvent(CallbackArg arg) 90 virtual bool handleEvent(CallbackArg arg)
105 { 91 {
106 m_helper->setResult(ResultType::create(arg)); 92 m_helper->setResult(arg);
107 return true; 93 return true;
108 } 94 }
109 95
110 private: 96 private:
111 explicit SuccessCallbackImpl(HelperType* helper) 97 explicit SuccessCallbackImpl(HelperType* helper)
112 : m_helper(helper) 98 : m_helper(helper)
113 { 99 {
114 } 100 }
115 HelperType* m_helper; 101 HelperType* m_helper;
116 }; 102 };
(...skipping 22 matching lines...) Expand all
139 125
140 friend class SuccessCallbackImpl; 126 friend class SuccessCallbackImpl;
141 friend class ErrorCallbackImpl; 127 friend class ErrorCallbackImpl;
142 128
143 void setError(FileError::ErrorCode code) 129 void setError(FileError::ErrorCode code)
144 { 130 {
145 m_errorCode = code; 131 m_errorCode = code;
146 m_completed = true; 132 m_completed = true;
147 } 133 }
148 134
149 void setResult(PassRefPtr<ResultType> result) 135 void waitForCompletion(ExceptionState& es)
150 { 136 {
151 m_result = result; 137 if (m_observer) {
152 m_completed = true; 138 while (!m_completed) {
139 if (!m_observer->waitForOperationToComplete()) {
140 m_errorCode = FileError::ABORT_ERR;
141 break;
142 }
143 }
144 }
145 if (m_errorCode)
146 FileError::throwDOMException(es, m_errorCode);
153 } 147 }
154 148
149 virtual void setResult(CallbackArg) = 0;
150
155 ObserverType* m_observer; 151 ObserverType* m_observer;
156 RefPtr<SuccessCallbackImpl> m_successCallback; 152 RefPtr<SuccessCallbackImpl> m_successCallback;
157 RefPtr<ErrorCallbackImpl> m_errorCallback; 153 RefPtr<ErrorCallbackImpl> m_errorCallback;
158 RefPtr<ResultType> m_result;
159 FileError::ErrorCode m_errorCode; 154 FileError::ErrorCode m_errorCode;
160 bool m_completed; 155 bool m_completed;
161 }; 156 };
162 157
158 template <typename SuccessCallback, typename ObserverType, typename CallbackArg, typename ResultType>
kinuko 2013/08/08 11:39:19 Could this change become simpler if we define trai
159 class SyncCallbackHelper : public SyncCallbackHelperBase<SuccessCallback, Observ erType, CallbackArg> {
160 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper);
161 public:
162 SyncCallbackHelper(ObserverType* observer = 0)
163 : SyncCallbackHelperBase<SuccessCallback, ObserverType, CallbackArg>(obs erver)
164 {
165 }
166
167 PassRefPtr<ResultType> getResult(ExceptionState& es)
168 {
169 SyncCallbackHelperBase<SuccessCallback, ObserverType, CallbackArg>::wait ForCompletion(es);
170
171 return m_result.release();
172 }
173
174 private:
175 virtual void setResult(CallbackArg result)
176 {
177 m_result = ResultType::create(result);
178 SyncCallbackHelperBase<SuccessCallback, ObserverType, CallbackArg>::m_co mpleted = true;
179 }
180
181 RefPtr<ResultType> m_result;
182 };
183
184 // Partial specialization for EntryVector / EntrySyncVector.
185 template <typename SuccessCallback, typename ObserverType>
186 class SyncCallbackHelper<SuccessCallback, ObserverType, const EntryVector&, Entr ySyncVector> : public SyncCallbackHelperBase<SuccessCallback, ObserverType, cons t EntryVector&> {
187 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper);
188 public:
189 SyncCallbackHelper(ObserverType* observer = 0)
190 : SyncCallbackHelperBase<SuccessCallback, ObserverType, const EntryVecto r&>(observer)
191 {
192 }
193
194 EntrySyncVector getResult(ExceptionState& es)
195 {
196 SyncCallbackHelperBase<SuccessCallback, ObserverType, const EntryVector& >::waitForCompletion(es);
197
198 return m_result;
199 }
200
201 private:
202 virtual void setResult(const EntryVector& result)
203 {
204 size_t entryCount = result.size();
205 m_result.resize(entryCount);
206 for (size_t i = 0; i < entryCount; ++i)
207 m_result[i] = EntrySync::create(result[i].get());
208 SyncCallbackHelperBase<SuccessCallback, ObserverType, const EntryVector& >::m_completed = true;
209 }
210
211 EntrySyncVector m_result;
212 };
213
163 struct EmptyType : public RefCounted<EmptyType> { 214 struct EmptyType : public RefCounted<EmptyType> {
164 static PassRefPtr<EmptyType> create(EmptyType*) 215 static PassRefPtr<EmptyType> create(EmptyType*)
165 { 216 {
166 return 0; 217 return 0;
167 } 218 }
168 }; 219 };
169 220
170 struct EmptyObserverType { 221 struct EmptyObserverType {
171 bool waitForOperationToComplete() 222 bool waitForOperationToComplete()
172 { 223 {
173 return false; 224 return false;
174 } 225 }
175 }; 226 };
176 227
177 typedef SyncCallbackHelper<EntryCallback, AsyncFileSystem, Entry*, EntrySync> En trySyncCallbackHelper; 228 typedef SyncCallbackHelper<EntryCallback, AsyncFileSystem, Entry*, EntrySync> En trySyncCallbackHelper;
178 typedef SyncCallbackHelper<EntriesCallback, AsyncFileSystem, const EntryVector&, EntryArraySync> EntriesSyncCallbackHelper; 229 typedef SyncCallbackHelper<EntriesCallback, AsyncFileSystem, const EntryVector&, EntrySyncVector> EntriesSyncCallbackHelper;
179 typedef SyncCallbackHelper<MetadataCallback, AsyncFileSystem, Metadata*, Metadat a> MetadataSyncCallbackHelper; 230 typedef SyncCallbackHelper<MetadataCallback, AsyncFileSystem, Metadata*, Metadat a> MetadataSyncCallbackHelper;
180 typedef SyncCallbackHelper<VoidCallback, AsyncFileSystem, EmptyType*, EmptyType> VoidSyncCallbackHelper; 231 typedef SyncCallbackHelper<VoidCallback, AsyncFileSystem, EmptyType*, EmptyType> VoidSyncCallbackHelper;
181 typedef SyncCallbackHelper<FileSystemCallback, EmptyObserverType, DOMFileSystem* , DOMFileSystemSync> FileSystemSyncCallbackHelper; 232 typedef SyncCallbackHelper<FileSystemCallback, EmptyObserverType, DOMFileSystem* , DOMFileSystemSync> FileSystemSyncCallbackHelper;
182 233
183 } // namespace WebCore 234 } // namespace WebCore
184 235
185 #endif // SyncCallbackHelper_h 236 #endif // SyncCallbackHelper_h
OLDNEW
« no previous file with comments | « Source/modules/filesystem/EntryArraySync.idl ('k') | Source/modules/modules.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698