Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 template <typename ResultType, typename CallbackArg> |
| 51 class DirectoryEntrySync; | 53 struct HelperResultType { |
|
kinuko
2013/08/09 02:15:54
nit: these helper classes can be in an anonymous n
do-not-use
2013/08/09 06:39:21
This would apparently be against coding style:
"So
| |
| 52 class EntryArraySync; | 54 typedef PassRefPtr<ResultType> ReturnType; |
| 53 class EntrySync; | 55 typedef RefPtr<ResultType> StorageType; |
| 54 class FileEntrySync; | 56 |
| 57 static ReturnType createFromCallbackArg(CallbackArg argument) | |
| 58 { | |
| 59 return ResultType::create(argument); | |
| 60 } | |
| 61 }; | |
| 62 | |
| 63 template <> | |
| 64 struct HelperResultType<EntrySyncVector, const EntryVector&> { | |
| 65 typedef EntrySyncVector ReturnType; | |
| 66 typedef EntrySyncVector StorageType; | |
| 67 | |
| 68 static EntrySyncVector createFromCallbackArg(const EntryVector& entries) | |
| 69 { | |
| 70 EntrySyncVector result; | |
| 71 size_t entryCount = entries.size(); | |
| 72 result.reserveInitialCapacity(entryCount); | |
| 73 for (size_t i = 0; i < entryCount; ++i) | |
| 74 result.uncheckedAppend(EntrySync::create(entries[i].get())); | |
| 75 return result; | |
| 76 } | |
| 77 }; | |
| 55 | 78 |
| 56 // A helper template for FileSystemSync implementation. | 79 // A helper template for FileSystemSync implementation. |
| 57 template <typename SuccessCallback, typename ObserverType, typename CallbackArg, typename ResultType> | 80 template <typename SuccessCallback, typename ObserverType, typename CallbackArg, typename ResultType> |
| 58 class SyncCallbackHelper { | 81 class SyncCallbackHelper { |
| 59 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper); | 82 WTF_MAKE_NONCOPYABLE(SyncCallbackHelper); |
| 60 public: | 83 public: |
| 61 typedef SyncCallbackHelper<SuccessCallback, ObserverType, CallbackArg, Resul tType> HelperType; | 84 typedef SyncCallbackHelper<SuccessCallback, ObserverType, CallbackArg, Resul tType> HelperType; |
| 85 typedef HelperResultType<ResultType, CallbackArg> ResultTypeTrait; | |
| 86 typedef typename ResultTypeTrait::StorageType ResultStorageType; | |
| 87 typedef typename ResultTypeTrait::ReturnType ResultReturnType; | |
| 88 | |
| 62 SyncCallbackHelper(ObserverType* observer = 0) | 89 SyncCallbackHelper(ObserverType* observer = 0) |
| 63 : m_observer(observer) | 90 : m_observer(observer) |
| 64 , m_successCallback(SuccessCallbackImpl::create(this)) | 91 , m_successCallback(SuccessCallbackImpl::create(this)) |
| 65 , m_errorCallback(ErrorCallbackImpl::create(this)) | 92 , m_errorCallback(ErrorCallbackImpl::create(this)) |
| 66 , m_errorCode(FileError::OK) | 93 , m_errorCode(FileError::OK) |
| 67 , m_completed(false) | 94 , m_completed(false) |
| 68 { | 95 { |
| 69 } | 96 } |
| 70 | 97 |
| 71 PassRefPtr<ResultType> getResult(ExceptionState& es) | 98 ResultReturnType getResult(ExceptionState& es) |
| 72 { | 99 { |
| 73 if (m_observer) { | 100 if (m_observer) { |
| 74 while (!m_completed) { | 101 while (!m_completed) { |
| 75 if (!m_observer->waitForOperationToComplete()) { | 102 if (!m_observer->waitForOperationToComplete()) { |
| 76 m_errorCode = FileError::ABORT_ERR; | 103 m_errorCode = FileError::ABORT_ERR; |
| 77 break; | 104 break; |
| 78 } | 105 } |
| 79 } | 106 } |
| 80 } | 107 } |
| 81 if (m_errorCode) | 108 if (m_errorCode) |
| 82 FileError::throwDOMException(es, m_errorCode); | 109 FileError::throwDOMException(es, m_errorCode); |
| 83 | 110 |
| 84 return m_result.release(); | 111 return m_result; |
| 85 } | 112 } |
| 86 | 113 |
| 87 PassRefPtr<SuccessCallback> successCallback() { return m_successCallback; } | 114 PassRefPtr<SuccessCallback> successCallback() { return m_successCallback; } |
| 88 PassRefPtr<ErrorCallback> errorCallback() { return m_errorCallback; } | 115 PassRefPtr<ErrorCallback> errorCallback() { return m_errorCallback; } |
| 89 | 116 |
| 90 private: | 117 private: |
| 91 class SuccessCallbackImpl : public SuccessCallback { | 118 class SuccessCallbackImpl : public SuccessCallback { |
| 92 public: | 119 public: |
| 93 static PassRefPtr<SuccessCallbackImpl> create(HelperType* helper) | 120 static PassRefPtr<SuccessCallbackImpl> create(HelperType* helper) |
| 94 { | 121 { |
| 95 return adoptRef(new SuccessCallbackImpl(helper)); | 122 return adoptRef(new SuccessCallbackImpl(helper)); |
| 96 } | 123 } |
| 97 | 124 |
| 98 virtual bool handleEvent() | 125 virtual bool handleEvent() |
| 99 { | 126 { |
| 100 m_helper->setError(FileError::OK); | 127 m_helper->setError(FileError::OK); |
| 101 return true; | 128 return true; |
| 102 } | 129 } |
| 103 | 130 |
| 104 virtual bool handleEvent(CallbackArg arg) | 131 virtual bool handleEvent(CallbackArg arg) |
| 105 { | 132 { |
| 106 m_helper->setResult(ResultType::create(arg)); | 133 m_helper->setResult(arg); |
| 107 return true; | 134 return true; |
| 108 } | 135 } |
| 109 | 136 |
| 110 private: | 137 private: |
| 111 explicit SuccessCallbackImpl(HelperType* helper) | 138 explicit SuccessCallbackImpl(HelperType* helper) |
| 112 : m_helper(helper) | 139 : m_helper(helper) |
| 113 { | 140 { |
| 114 } | 141 } |
| 115 HelperType* m_helper; | 142 HelperType* m_helper; |
| 116 }; | 143 }; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 139 | 166 |
| 140 friend class SuccessCallbackImpl; | 167 friend class SuccessCallbackImpl; |
| 141 friend class ErrorCallbackImpl; | 168 friend class ErrorCallbackImpl; |
| 142 | 169 |
| 143 void setError(FileError::ErrorCode code) | 170 void setError(FileError::ErrorCode code) |
| 144 { | 171 { |
| 145 m_errorCode = code; | 172 m_errorCode = code; |
| 146 m_completed = true; | 173 m_completed = true; |
| 147 } | 174 } |
| 148 | 175 |
| 149 void setResult(PassRefPtr<ResultType> result) | 176 void setResult(CallbackArg result) |
| 150 { | 177 { |
| 151 m_result = result; | 178 m_result = ResultTypeTrait::createFromCallbackArg(result); |
| 152 m_completed = true; | 179 m_completed = true; |
| 153 } | 180 } |
| 154 | 181 |
| 155 ObserverType* m_observer; | 182 ObserverType* m_observer; |
| 156 RefPtr<SuccessCallbackImpl> m_successCallback; | 183 RefPtr<SuccessCallbackImpl> m_successCallback; |
| 157 RefPtr<ErrorCallbackImpl> m_errorCallback; | 184 RefPtr<ErrorCallbackImpl> m_errorCallback; |
| 158 RefPtr<ResultType> m_result; | 185 ResultStorageType m_result; |
| 159 FileError::ErrorCode m_errorCode; | 186 FileError::ErrorCode m_errorCode; |
| 160 bool m_completed; | 187 bool m_completed; |
| 161 }; | 188 }; |
| 162 | 189 |
| 163 struct EmptyType : public RefCounted<EmptyType> { | 190 struct EmptyType : public RefCounted<EmptyType> { |
| 164 static PassRefPtr<EmptyType> create(EmptyType*) | 191 static PassRefPtr<EmptyType> create(EmptyType*) |
| 165 { | 192 { |
| 166 return 0; | 193 return 0; |
| 167 } | 194 } |
| 168 }; | 195 }; |
| 169 | 196 |
| 170 struct EmptyObserverType { | 197 struct EmptyObserverType { |
| 171 bool waitForOperationToComplete() | 198 bool waitForOperationToComplete() |
| 172 { | 199 { |
| 173 return false; | 200 return false; |
| 174 } | 201 } |
| 175 }; | 202 }; |
| 176 | 203 |
| 177 typedef SyncCallbackHelper<EntryCallback, AsyncFileSystem, Entry*, EntrySync> En trySyncCallbackHelper; | 204 typedef SyncCallbackHelper<EntryCallback, AsyncFileSystem, Entry*, EntrySync> En trySyncCallbackHelper; |
| 178 typedef SyncCallbackHelper<EntriesCallback, AsyncFileSystem, const EntryVector&, EntryArraySync> EntriesSyncCallbackHelper; | 205 typedef SyncCallbackHelper<EntriesCallback, AsyncFileSystem, const EntryVector&, EntrySyncVector> EntriesSyncCallbackHelper; |
| 179 typedef SyncCallbackHelper<MetadataCallback, AsyncFileSystem, Metadata*, Metadat a> MetadataSyncCallbackHelper; | 206 typedef SyncCallbackHelper<MetadataCallback, AsyncFileSystem, Metadata*, Metadat a> MetadataSyncCallbackHelper; |
| 180 typedef SyncCallbackHelper<VoidCallback, AsyncFileSystem, EmptyType*, EmptyType> VoidSyncCallbackHelper; | 207 typedef SyncCallbackHelper<VoidCallback, AsyncFileSystem, EmptyType*, EmptyType> VoidSyncCallbackHelper; |
| 181 typedef SyncCallbackHelper<FileSystemCallback, EmptyObserverType, DOMFileSystem* , DOMFileSystemSync> FileSystemSyncCallbackHelper; | 208 typedef SyncCallbackHelper<FileSystemCallback, EmptyObserverType, DOMFileSystem* , DOMFileSystemSync> FileSystemSyncCallbackHelper; |
| 182 | 209 |
| 183 } // namespace WebCore | 210 } // namespace WebCore |
| 184 | 211 |
| 185 #endif // SyncCallbackHelper_h | 212 #endif // SyncCallbackHelper_h |
| OLD | NEW |