• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "platform/PlatformExport.h"
35 #include "wtf/Assertions.h"
36 #include "wtf/Forward.h"
37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/PassRefPtr.h"
39 #include "wtf/RefPtr.h"
40 #include "wtf/ThreadSafeRefCounted.h"
41 #include "wtf/TypeTraits.h"
42 
43 namespace WebCore {
44 
45     class IntRect;
46     class IntSize;
47     class KURL;
48     class ResourceError;
49     class ResourceRequest;
50     class ResourceResponse;
51     struct CrossThreadResourceResponseData;
52     struct CrossThreadResourceRequestData;
53     struct ThreadableLoaderOptions;
54 
55     template<typename T> struct CrossThreadCopierPassThrough {
56         typedef T Type;
copyCrossThreadCopierPassThrough57         static Type copy(const T& parameter)
58         {
59             return parameter;
60         }
61     };
62 
63     template<bool isConvertibleToInteger, bool isThreadSafeRefCounted, typename T> struct CrossThreadCopierBase;
64 
65     // Integers get passed through without any changes.
66     template<typename T> struct CrossThreadCopierBase<true, false, T> : public CrossThreadCopierPassThrough<T> {
67     };
68 
69     // To allow a type to be passed across threads using its copy constructor, add a forward declaration of the type and
70     // a CopyThreadCopierBase<false, false, TypeName> : public CrossThreadCopierPassThrough<TypeName> { }; to this file.
71     template<> struct CrossThreadCopierBase<false, false, ThreadableLoaderOptions> : public CrossThreadCopierPassThrough<ThreadableLoaderOptions> {
72     };
73 
74     template<> struct CrossThreadCopierBase<false, false, IntRect> : public CrossThreadCopierPassThrough<IntRect> {
75     };
76 
77     template<> struct CrossThreadCopierBase<false, false, IntSize> : public CrossThreadCopierPassThrough<IntSize> {
78     };
79 
80     // Custom copy methods.
81     template<typename T> struct CrossThreadCopierBase<false, true, T> {
82         typedef typename WTF::RemoveTemplate<T, RefPtr>::Type TypeWithoutRefPtr;
83         typedef typename WTF::RemoveTemplate<TypeWithoutRefPtr, PassRefPtr>::Type TypeWithoutPassRefPtr;
84         typedef typename WTF::RemovePointer<TypeWithoutPassRefPtr>::Type RefCountedType;
85 
86         // Verify that only one of the above did a change.
87         COMPILE_ASSERT((WTF::IsSameType<RefPtr<RefCountedType>, T>::value
88                         || WTF::IsSameType<PassRefPtr<RefCountedType>, T>::value
89                         || WTF::IsSameType<RefCountedType*, T>::value),
90                        OnlyAllowOneTypeModification);
91 
92         typedef PassRefPtr<RefCountedType> Type;
93         static Type copy(const T& refPtr)
94         {
95             return refPtr;
96         }
97     };
98 
99     template<typename T> struct CrossThreadCopierBase<false, false, PassOwnPtr<T> > {
100         typedef PassOwnPtr<T> Type;
101         static Type copy(Type ownPtr)
102         {
103             return ownPtr;
104         }
105     };
106 
107     template<> struct CrossThreadCopierBase<false, false, KURL> {
108         typedef KURL Type;
109         PLATFORM_EXPORT static Type copy(const KURL&);
110     };
111 
112     template<> struct CrossThreadCopierBase<false, false, String> {
113         typedef String Type;
114         PLATFORM_EXPORT static Type copy(const String&);
115     };
116 
117     template<> struct CrossThreadCopierBase<false, false, ResourceError> {
118         typedef ResourceError Type;
119         PLATFORM_EXPORT static Type copy(const ResourceError&);
120     };
121 
122     template<> struct CrossThreadCopierBase<false, false, ResourceRequest> {
123         typedef PassOwnPtr<CrossThreadResourceRequestData> Type;
124         PLATFORM_EXPORT static Type copy(const ResourceRequest&);
125     };
126 
127     template<> struct CrossThreadCopierBase<false, false, ResourceResponse> {
128         typedef PassOwnPtr<CrossThreadResourceResponseData> Type;
129         PLATFORM_EXPORT static Type copy(const ResourceResponse&);
130     };
131 
132     template<typename T> struct CrossThreadCopier : public CrossThreadCopierBase<WTF::IsConvertibleToInteger<T>::value,
133                                                                                  WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, RefPtr>::Type, ThreadSafeRefCounted>::value
134                                                                                      || WTF::IsSubclassOfTemplate<typename WTF::RemovePointer<T>::Type, ThreadSafeRefCounted>::value
135                                                                                      || WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, PassRefPtr>::Type, ThreadSafeRefCounted>::value,
136                                                                                  T> {
137     };
138 
139     template<typename T> struct AllowCrossThreadAccessWrapper {
140     public:
141         explicit AllowCrossThreadAccessWrapper(T* value) : m_value(value) { }
142         T* value() const { return m_value; }
143     private:
144         T* m_value;
145     };
146 
147     template<typename T> struct CrossThreadCopierBase<false, false, AllowCrossThreadAccessWrapper<T> > {
148         typedef T* Type;
149         static Type copy(const AllowCrossThreadAccessWrapper<T>& wrapper) { return wrapper.value(); }
150     };
151 
152     template<typename T> AllowCrossThreadAccessWrapper<T> AllowCrossThreadAccess(T* value)
153     {
154         return AllowCrossThreadAccessWrapper<T>(value);
155     }
156 
157     // FIXME: Move to a different header file. AllowAccessLater is for cross-thread access
158     // that is not cross-thread (tasks posted to a queue guaranteed to run on the same thread).
159     template<typename T> struct AllowAccessLaterWrapper {
160     public:
161         explicit AllowAccessLaterWrapper(T* value) : m_value(value) { }
162         T* value() const { return m_value; }
163     private:
164         T* m_value;
165     };
166 
167     template<typename T> struct CrossThreadCopierBase<false, false, AllowAccessLaterWrapper<T> > {
168         typedef T* Type;
169         static Type copy(const AllowAccessLaterWrapper<T>& wrapper) { return wrapper.value(); }
170     };
171 
172     template<typename T> AllowAccessLaterWrapper<T> AllowAccessLater(T* value)
173     {
174         return AllowAccessLaterWrapper<T>(value);
175     }
176 
177 
178 } // namespace WebCore
179 
180 #endif // CrossThreadCopier_h
181