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/core/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 49 namespace blink { 50 51 template <typename ResultType, typename CallbackArg> 52 struct HelperResultType { 53 DISALLOW_ALLOCATION(); 54 public: 55 typedef ResultType* ReturnType; 56 typedef Member<ResultType> StorageType; 57 58 static ReturnType createFromCallbackArg(CallbackArg argument) 59 { 60 return ResultType::create(argument); 61 } 62 }; 63 64 // A helper template for FileSystemSync implementation. 65 template <typename SuccessCallback, typename CallbackArg, typename ResultType> 66 class SyncCallbackHelper FINAL : public GarbageCollected<SyncCallbackHelper<SuccessCallback, CallbackArg, ResultType> > { 67 public: 68 typedef SyncCallbackHelper<SuccessCallback, CallbackArg, ResultType> HelperType; 69 typedef HelperResultType<ResultType, CallbackArg> ResultTypeTrait; 70 typedef typename ResultTypeTrait::StorageType ResultStorageType; 71 typedef typename ResultTypeTrait::ReturnType ResultReturnType; 72 73 static HelperType* create() 74 { 75 return new SyncCallbackHelper(); 76 } 77 78 ResultReturnType getResult(ExceptionState& exceptionState) 79 { 80 if (m_errorCode) 81 FileError::throwDOMException(exceptionState, m_errorCode); 82 83 return m_result; 84 } 85 86 SuccessCallback* successCallback() { return SuccessCallbackImpl::create(this); } 87 ErrorCallback* errorCallback() { return ErrorCallbackImpl::create(this); } 88 89 void trace(Visitor* visitor) 90 { 91 visitor->trace(m_result); 92 } 93 94 private: 95 SyncCallbackHelper() 96 : m_errorCode(FileError::OK) 97 , m_completed(false) 98 { 99 } 100 101 class SuccessCallbackImpl FINAL : public SuccessCallback { 102 public: 103 static SuccessCallbackImpl* create(HelperType* helper) 104 { 105 return new SuccessCallbackImpl(helper); 106 } 107 108 virtual void handleEvent() 109 { 110 m_helper->setError(FileError::OK); 111 } 112 113 virtual void handleEvent(CallbackArg arg) 114 { 115 m_helper->setResult(arg); 116 } 117 118 private: 119 explicit SuccessCallbackImpl(HelperType* helper) 120 : m_helper(helper) 121 { 122 } 123 Persistent<HelperType> m_helper; 124 }; 125 126 class ErrorCallbackImpl FINAL : public ErrorCallback { 127 public: 128 static ErrorCallbackImpl* create(HelperType* helper) 129 { 130 return new ErrorCallbackImpl(helper); 131 } 132 133 virtual void handleEvent(FileError* error) OVERRIDE 134 { 135 ASSERT(error); 136 m_helper->setError(error->code()); 137 } 138 139 private: 140 explicit ErrorCallbackImpl(HelperType* helper) 141 : m_helper(helper) 142 { 143 } 144 Persistent<HelperType> m_helper; 145 }; 146 147 void setError(FileError::ErrorCode code) 148 { 149 m_errorCode = code; 150 m_completed = true; 151 } 152 153 void setResult(CallbackArg result) 154 { 155 m_result = ResultTypeTrait::createFromCallbackArg(result); 156 m_completed = true; 157 } 158 159 ResultStorageType m_result; 160 FileError::ErrorCode m_errorCode; 161 bool m_completed; 162 }; 163 164 struct EmptyType : public GarbageCollected<EmptyType> { 165 static EmptyType* create(EmptyType*) 166 { 167 return 0; 168 } 169 170 void trace(Visitor*) { } 171 }; 172 173 typedef SyncCallbackHelper<EntryCallback, Entry*, EntrySync> EntrySyncCallbackHelper; 174 typedef SyncCallbackHelper<MetadataCallback, Metadata*, Metadata> MetadataSyncCallbackHelper; 175 typedef SyncCallbackHelper<VoidCallback, EmptyType*, EmptyType> VoidSyncCallbackHelper; 176 typedef SyncCallbackHelper<FileSystemCallback, DOMFileSystem*, DOMFileSystemSync> FileSystemSyncCallbackHelper; 177 178 } // namespace blink 179 180 #endif // SyncCallbackHelper_h 181