1/*
2 * Copyright (C) 2009 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#include "config.h"
32
33#include "CrossThreadCopier.h"
34
35#include "URL.h"
36#include "ResourceError.h"
37#include "ResourceRequest.h"
38#include "ResourceResponse.h"
39#include "SerializedScriptValue.h"
40#include "SessionID.h"
41#include <wtf/Assertions.h>
42#include <wtf/text/WTFString.h>
43
44#if ENABLE(INDEXED_DATABASE)
45#include "IDBDatabaseMetadata.h"
46#include "IDBGetResult.h"
47#include "IDBKeyData.h"
48#include "IDBKeyRangeData.h"
49#endif
50
51namespace WebCore {
52
53CrossThreadCopierBase<false, false, URL>::Type CrossThreadCopierBase<false, false, URL>::copy(const URL& url)
54{
55    return url.copy();
56}
57
58CrossThreadCopierBase<false, false, String>::Type CrossThreadCopierBase<false, false, String>::copy(const String& str)
59{
60    return str.isolatedCopy();
61}
62
63CrossThreadCopierBase<false, false, ResourceError>::Type CrossThreadCopierBase<false, false, ResourceError>::copy(const ResourceError& error)
64{
65    return error.copy();
66}
67
68CrossThreadCopierBase<false, false, ResourceRequest>::Type CrossThreadCopierBase<false, false, ResourceRequest>::copy(const ResourceRequest& request)
69{
70    return request.copyData();
71}
72
73CrossThreadCopierBase<false, false, ResourceResponse>::Type CrossThreadCopierBase<false, false, ResourceResponse>::copy(const ResourceResponse& response)
74{
75    return response.copyData();
76}
77
78CrossThreadCopierBase<false, false, SessionID>::Type CrossThreadCopierBase<false, false, SessionID>::copy(const SessionID& sessionID)
79{
80    return sessionID;
81}
82
83#if ENABLE(INDEXED_DATABASE)
84
85IndexedDB::TransactionMode CrossThreadCopierBase<false, false, IndexedDB::TransactionMode>::copy(const IndexedDB::TransactionMode& mode)
86{
87    return mode;
88}
89
90IndexedDB::CursorDirection CrossThreadCopierBase<false, false, IndexedDB::CursorDirection>::copy(const IndexedDB::CursorDirection& direction)
91{
92    return direction;
93}
94
95IndexedDB::CursorType CrossThreadCopierBase<false, false, IndexedDB::CursorType>::copy(const IndexedDB::CursorType& type)
96{
97    return type;
98}
99
100CrossThreadCopierBase<false, false, IDBDatabaseMetadata>::Type CrossThreadCopierBase<false, false, IDBDatabaseMetadata>::copy(const IDBDatabaseMetadata& metadata)
101{
102    return metadata.isolatedCopy();
103}
104
105CrossThreadCopierBase<false, false, IDBGetResult>::Type CrossThreadCopierBase<false, false, IDBGetResult>::copy(const IDBGetResult& result)
106{
107    return result.isolatedCopy();
108}
109
110CrossThreadCopierBase<false, false, IDBIndexMetadata>::Type CrossThreadCopierBase<false, false, IDBIndexMetadata>::copy(const IDBIndexMetadata& metadata)
111{
112    return metadata.isolatedCopy();
113}
114
115CrossThreadCopierBase<false, false, IDBKeyData>::Type CrossThreadCopierBase<false, false, IDBKeyData>::copy(const IDBKeyData& keyData)
116{
117    return keyData.isolatedCopy();
118}
119
120CrossThreadCopierBase<false, false, IDBKeyRangeData>::Type CrossThreadCopierBase<false, false, IDBKeyRangeData>::copy(const IDBKeyRangeData& keyRangeData)
121{
122    return keyRangeData.isolatedCopy();
123}
124
125CrossThreadCopierBase<false, false, IDBObjectStoreMetadata>::Type CrossThreadCopierBase<false, false, IDBObjectStoreMetadata>::copy(const IDBObjectStoreMetadata& metadata)
126{
127    return metadata.isolatedCopy();
128}
129
130#endif
131
132// Test CrossThreadCopier using COMPILE_ASSERT.
133
134// Verify that ThreadSafeRefCounted objects get handled correctly.
135class CopierThreadSafeRefCountedTest : public ThreadSafeRefCounted<CopierThreadSafeRefCountedTest> {
136};
137
138COMPILE_ASSERT((std::is_same<
139                  PassRefPtr<CopierThreadSafeRefCountedTest>,
140                  CrossThreadCopier<PassRefPtr<CopierThreadSafeRefCountedTest>>::Type
141                  >::value),
142               PassRefPtrTest);
143COMPILE_ASSERT((std::is_same<
144                  PassRefPtr<CopierThreadSafeRefCountedTest>,
145                  CrossThreadCopier<RefPtr<CopierThreadSafeRefCountedTest>>::Type
146                  >::value),
147               RefPtrTest);
148COMPILE_ASSERT((std::is_same<
149                  PassRefPtr<CopierThreadSafeRefCountedTest>,
150                  CrossThreadCopier<CopierThreadSafeRefCountedTest*>::Type
151                  >::value),
152               RawPointerTest);
153
154
155// Add a generic specialization which will let's us verify that no other template matches.
156template<typename T> struct CrossThreadCopierBase<false, false, T> {
157    typedef int Type;
158};
159
160// Verify that RefCounted objects only match our generic template which exposes Type as int.
161class CopierRefCountedTest : public RefCounted<CopierRefCountedTest> {
162};
163
164COMPILE_ASSERT((std::is_same<
165                  int,
166                  CrossThreadCopier<PassRefPtr<CopierRefCountedTest>>::Type
167                  >::value),
168               PassRefPtrRefCountedTest);
169
170COMPILE_ASSERT((std::is_same<
171                  int,
172                  CrossThreadCopier<RefPtr<CopierRefCountedTest>>::Type
173                  >::value),
174               RefPtrRefCountedTest);
175
176COMPILE_ASSERT((std::is_same<
177                  int,
178                  CrossThreadCopier<CopierRefCountedTest*>::Type
179                  >::value),
180               RawPointerRefCountedTest);
181
182// Verify that PassOwnPtr gets passed through.
183COMPILE_ASSERT((std::is_same<
184                  PassOwnPtr<float>,
185                  CrossThreadCopier<PassOwnPtr<float>>::Type
186                  >::value),
187               PassOwnPtrTest);
188
189// Verify that PassOwnPtr does not get passed through.
190COMPILE_ASSERT((std::is_same<
191                  int,
192                  CrossThreadCopier<OwnPtr<float>>::Type
193                  >::value),
194               OwnPtrTest);
195
196} // namespace WebCore
197