OLD | NEW |
| (Empty) |
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/Functional.h" // FunctionThreadAffinity | |
38 #include "wtf/PassRefPtr.h" | |
39 #include "wtf/RefPtr.h" | |
40 #include "wtf/ThreadSafeRefCounted.h" | |
41 #include "wtf/TypeTraits.h" | |
42 #include <memory> | |
43 | |
44 class SkRefCnt; | |
45 | |
46 namespace WTF { | |
47 | |
48 template <typename T> | |
49 class PassedWrapper; | |
50 | |
51 } | |
52 | |
53 namespace blink { | |
54 | |
55 class IntRect; | |
56 class IntSize; | |
57 class KURL; | |
58 class ResourceError; | |
59 class ResourceRequest; | |
60 class ResourceResponse; | |
61 struct CrossThreadResourceResponseData; | |
62 struct CrossThreadResourceRequestData; | |
63 template<typename T>class CrossThreadPersistent; | |
64 template<typename T>class CrossThreadWeakPersistent; | |
65 | |
66 template <typename T> | |
67 struct CrossThreadCopierPassThrough { | |
68 STATIC_ONLY(CrossThreadCopierPassThrough); | |
69 typedef T Type; | |
70 static Type copy(const T& parameter) | |
71 { | |
72 return parameter; | |
73 } | |
74 }; | |
75 | |
76 template <typename T, bool isArithmeticOrEnum> | |
77 struct CrossThreadCopierBase; | |
78 | |
79 // Arithmetic values (integers or floats) and enums can be safely copied. | |
80 template <typename T> | |
81 struct CrossThreadCopierBase<T, true> : public CrossThreadCopierPassThrough<T> { | |
82 STATIC_ONLY(CrossThreadCopierBase); | |
83 }; | |
84 | |
85 template <typename T> | |
86 struct CrossThreadCopier : public CrossThreadCopierBase<T, std::is_arithmetic<T>
::value || std::is_enum<T>::value> { | |
87 STATIC_ONLY(CrossThreadCopier); | |
88 }; | |
89 | |
90 // CrossThreadCopier specializations follow. | |
91 template <typename T> | |
92 struct CrossThreadCopier<PassRefPtr<T>> : public CrossThreadCopierPassThrough<Pa
ssRefPtr<T>> { | |
93 STATIC_ONLY(CrossThreadCopier); | |
94 static_assert(WTF::IsSubclassOfTemplate<T, ThreadSafeRefCounted>::value || s
td::is_base_of<SkRefCnt, T>::value, "PassRefPtr<T> can be passed across threads
only if T is ThreadSafeRefCounted or SkRefCnt."); | |
95 }; | |
96 template <typename T> | |
97 struct CrossThreadCopier<RefPtr<T>> : public CrossThreadCopierPassThrough<RefPtr
<T>> { | |
98 STATIC_ONLY(CrossThreadCopier); | |
99 static_assert(WTF::IsSubclassOfTemplate<T, ThreadSafeRefCounted>::value || s
td::is_base_of<SkRefCnt, T>::value, "RefPtr<T> can be passed across threads only
if T is ThreadSafeRefCounted or SkRefCnt."); | |
100 }; | |
101 | |
102 // nullptr_t can be passed through without any changes. | |
103 template <> | |
104 struct CrossThreadCopier<std::nullptr_t> : public CrossThreadCopierPassThrough<s
td::nullptr_t> { | |
105 STATIC_ONLY(CrossThreadCopier); | |
106 }; | |
107 | |
108 // To allow a type to be passed across threads using its copy constructor, add a
forward declaration of the type and | |
109 // provide a specialization of CrossThreadCopier<T> in this file, like IntRect b
elow. | |
110 template <> | |
111 struct CrossThreadCopier<IntRect> : public CrossThreadCopierPassThrough<IntRect>
{ | |
112 STATIC_ONLY(CrossThreadCopier); | |
113 }; | |
114 | |
115 template <> | |
116 struct CrossThreadCopier<IntSize> : public CrossThreadCopierPassThrough<IntSize>
{ | |
117 STATIC_ONLY(CrossThreadCopier); | |
118 }; | |
119 | |
120 template <typename T, typename Deleter> | |
121 struct CrossThreadCopier<std::unique_ptr<T, Deleter>> { | |
122 STATIC_ONLY(CrossThreadCopier); | |
123 using Type = std::unique_ptr<T, Deleter>; | |
124 static std::unique_ptr<T, Deleter> copy(std::unique_ptr<T, Deleter> pointer) | |
125 { | |
126 return pointer; // This is in fact a move. | |
127 } | |
128 }; | |
129 | |
130 template<typename T> | |
131 struct CrossThreadCopier<CrossThreadPersistent<T>> : public CrossThreadCopierPas
sThrough<CrossThreadPersistent<T>> { | |
132 STATIC_ONLY(CrossThreadCopier); | |
133 }; | |
134 | |
135 template<typename T> | |
136 struct CrossThreadCopier<CrossThreadWeakPersistent<T>> : public CrossThreadCopie
rPassThrough<CrossThreadWeakPersistent<T>> { | |
137 STATIC_ONLY(CrossThreadCopier); | |
138 }; | |
139 | |
140 template<typename T> | |
141 struct CrossThreadCopier<WTF::UnretainedWrapper<T, WTF::CrossThreadAffinity>> :
public CrossThreadCopierPassThrough<WTF::UnretainedWrapper<T, WTF::CrossThreadAf
finity>> { | |
142 STATIC_ONLY(CrossThreadCopier); | |
143 }; | |
144 | |
145 template<typename T> | |
146 struct CrossThreadCopier<WeakPtr<T>> : public CrossThreadCopierPassThrough<WeakP
tr<T>> { | |
147 STATIC_ONLY(CrossThreadCopier); | |
148 }; | |
149 | |
150 template <typename T> | |
151 struct CrossThreadCopier<WTF::PassedWrapper<T>> { | |
152 STATIC_ONLY(CrossThreadCopier); | |
153 using Type = WTF::PassedWrapper<typename CrossThreadCopier<T>::Type>; | |
154 static Type copy(WTF::PassedWrapper<T>&& value) { return passed(CrossThreadC
opier<T>::copy(value.moveOut())); } | |
155 }; | |
156 | |
157 template <> | |
158 struct CrossThreadCopier<KURL> { | |
159 STATIC_ONLY(CrossThreadCopier); | |
160 typedef KURL Type; | |
161 PLATFORM_EXPORT static Type copy(const KURL&); | |
162 }; | |
163 | |
164 template <> | |
165 struct CrossThreadCopier<String> { | |
166 STATIC_ONLY(CrossThreadCopier); | |
167 typedef String Type; | |
168 PLATFORM_EXPORT static Type copy(const String&); | |
169 }; | |
170 | |
171 template <> | |
172 struct CrossThreadCopier<ResourceError> { | |
173 STATIC_ONLY(CrossThreadCopier); | |
174 typedef ResourceError Type; | |
175 PLATFORM_EXPORT static Type copy(const ResourceError&); | |
176 }; | |
177 | |
178 template <> | |
179 struct CrossThreadCopier<ResourceRequest> { | |
180 STATIC_ONLY(CrossThreadCopier); | |
181 typedef WTF::PassedWrapper<std::unique_ptr<CrossThreadResourceRequestData>>
Type; | |
182 PLATFORM_EXPORT static Type copy(const ResourceRequest&); | |
183 }; | |
184 | |
185 template <> | |
186 struct CrossThreadCopier<ResourceResponse> { | |
187 STATIC_ONLY(CrossThreadCopier); | |
188 typedef WTF::PassedWrapper<std::unique_ptr<CrossThreadResourceResponseData>>
Type; | |
189 PLATFORM_EXPORT static Type copy(const ResourceResponse&); | |
190 }; | |
191 | |
192 } // namespace blink | |
193 | |
194 #endif // CrossThreadCopier_h | |
OLD | NEW |