1 /* 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef CrossThreadCopier_h 32 #define CrossThreadCopier_h 33 34 #include <memory> 35 #include <wtf/PassOwnPtr.h> 36 #include <wtf/PassRefPtr.h> 37 #include <wtf/RefPtr.h> 38 #include <wtf/Threading.h> 39 #include <wtf/TypeTraits.h> 40 41 namespace WebCore { 42 43 class KURL; 44 class ResourceError; 45 class ResourceRequest; 46 class ResourceResponse; 47 class String; 48 struct CrossThreadResourceResponseData; 49 struct CrossThreadResourceRequestData; 50 struct ThreadableLoaderOptions; 51 52 template<typename T> struct CrossThreadCopierPassThrough { 53 typedef T Type; copyCrossThreadCopierPassThrough54 static Type copy(const T& parameter) 55 { 56 return parameter; 57 } 58 }; 59 60 template<bool isConvertibleToInteger, bool isThreadsafeShared, typename T> struct CrossThreadCopierBase; 61 62 // Integers get passed through without any changes. 63 template<typename T> struct CrossThreadCopierBase<true, false, T> : public CrossThreadCopierPassThrough<T> { 64 }; 65 66 // Pointers get passed through without any significant changes. 67 template<typename T> struct CrossThreadCopierBase<false, false, T*> : public CrossThreadCopierPassThrough<T*> { 68 }; 69 70 template<> struct CrossThreadCopierBase<false, false, ThreadableLoaderOptions> : public CrossThreadCopierPassThrough<ThreadableLoaderOptions> { 71 }; 72 73 // Custom copy methods. 74 template<typename T> struct CrossThreadCopierBase<false, true, T> { 75 typedef typename WTF::RemoveTemplate<T, RefPtr>::Type RefCountedType; 76 typedef PassRefPtr<RefCountedType> Type; 77 static Type copy(const T& refPtr) 78 { 79 return refPtr.get(); 80 } 81 }; 82 83 template<typename T> struct CrossThreadCopierBase<false, false, PassOwnPtr<T> > { 84 typedef PassOwnPtr<T> Type; 85 static Type copy(const PassOwnPtr<T>& ownPtr) 86 { 87 return PassOwnPtr<T>(static_cast<T*>(ownPtr.release())); 88 } 89 }; 90 91 template<typename T> struct CrossThreadCopierBase<false, false, std::auto_ptr<T> > { 92 typedef std::auto_ptr<T> Type; 93 static Type copy(const std::auto_ptr<T>& autoPtr) 94 { 95 return std::auto_ptr<T>(*const_cast<std::auto_ptr<T>*>(&autoPtr)); 96 } 97 }; 98 99 template<> struct CrossThreadCopierBase<false, false, KURL> { 100 typedef KURL Type; 101 static Type copy(const KURL&); 102 }; 103 104 template<> struct CrossThreadCopierBase<false, false, String> { 105 typedef String Type; 106 static Type copy(const String&); 107 }; 108 109 template<> struct CrossThreadCopierBase<false, false, ResourceError> { 110 typedef ResourceError Type; 111 static Type copy(const ResourceError&); 112 }; 113 114 template<> struct CrossThreadCopierBase<false, false, ResourceRequest> { 115 typedef std::auto_ptr<CrossThreadResourceRequestData> Type; 116 static Type copy(const ResourceRequest&); 117 }; 118 119 template<> struct CrossThreadCopierBase<false, false, ResourceResponse> { 120 typedef std::auto_ptr<CrossThreadResourceResponseData> Type; 121 static Type copy(const ResourceResponse&); 122 }; 123 124 template<typename T> struct CrossThreadCopier : public CrossThreadCopierBase<WTF::IsConvertibleToInteger<T>::value, 125 WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, RefPtr>::Type, ThreadSafeShared>::value, 126 T> { 127 }; 128 129 } // namespace WebCore 130 131 #endif // CrossThreadCopier_h 132