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 "KURL.h"
36#include "ResourceError.h"
37#include "ResourceRequest.h"
38#include "ResourceResponse.h"
39#include "SerializedScriptValue.h"
40#include <wtf/Assertions.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45CrossThreadCopierBase<false, false, KURL>::Type CrossThreadCopierBase<false, false, KURL>::copy(const KURL& url)
46{
47    return url.copy();
48}
49
50CrossThreadCopierBase<false, false, String>::Type CrossThreadCopierBase<false, false, String>::copy(const String& str)
51{
52    return str.isolatedCopy();
53}
54
55CrossThreadCopierBase<false, false, ResourceError>::Type CrossThreadCopierBase<false, false, ResourceError>::copy(const ResourceError& error)
56{
57    return error.copy();
58}
59
60CrossThreadCopierBase<false, false, ResourceRequest>::Type CrossThreadCopierBase<false, false, ResourceRequest>::copy(const ResourceRequest& request)
61{
62    return request.copyData();
63}
64
65CrossThreadCopierBase<false, false, ResourceResponse>::Type CrossThreadCopierBase<false, false, ResourceResponse>::copy(const ResourceResponse& response)
66{
67    return response.copyData();
68}
69
70// Test CrossThreadCopier using COMPILE_ASSERT.
71
72// Verify that ThreadSafeRefCounted objects get handled correctly.
73class CopierThreadSafeRefCountedTest : public ThreadSafeRefCounted<CopierThreadSafeRefCountedTest> {
74};
75
76COMPILE_ASSERT((WTF::IsSameType<
77                  PassRefPtr<CopierThreadSafeRefCountedTest>,
78                  CrossThreadCopier<PassRefPtr<CopierThreadSafeRefCountedTest> >::Type
79                  >::value),
80               PassRefPtrTest);
81COMPILE_ASSERT((WTF::IsSameType<
82                  PassRefPtr<CopierThreadSafeRefCountedTest>,
83                  CrossThreadCopier<RefPtr<CopierThreadSafeRefCountedTest> >::Type
84                  >::value),
85               RefPtrTest);
86COMPILE_ASSERT((WTF::IsSameType<
87                  PassRefPtr<CopierThreadSafeRefCountedTest>,
88                  CrossThreadCopier<CopierThreadSafeRefCountedTest*>::Type
89                  >::value),
90               RawPointerTest);
91
92
93// Add a generic specialization which will let's us verify that no other template matches.
94template<typename T> struct CrossThreadCopierBase<false, false, T> {
95    typedef int Type;
96};
97
98// Verify that RefCounted objects only match our generic template which exposes Type as int.
99class CopierRefCountedTest : public RefCounted<CopierRefCountedTest> {
100};
101
102COMPILE_ASSERT((WTF::IsSameType<
103                  int,
104                  CrossThreadCopier<PassRefPtr<CopierRefCountedTest> >::Type
105                  >::value),
106               PassRefPtrRefCountedTest);
107
108COMPILE_ASSERT((WTF::IsSameType<
109                  int,
110                  CrossThreadCopier<RefPtr<CopierRefCountedTest> >::Type
111                  >::value),
112               RefPtrRefCountedTest);
113
114COMPILE_ASSERT((WTF::IsSameType<
115                  int,
116                  CrossThreadCopier<CopierRefCountedTest*>::Type
117                  >::value),
118               RawPointerRefCountedTest);
119
120// Verify that PassOwnPtr gets passed through.
121COMPILE_ASSERT((WTF::IsSameType<
122                  PassOwnPtr<float>,
123                  CrossThreadCopier<PassOwnPtr<float> >::Type
124                  >::value),
125               PassOwnPtrTest);
126
127// Verify that PassOwnPtr does not get passed through.
128COMPILE_ASSERT((WTF::IsSameType<
129                  int,
130                  CrossThreadCopier<OwnPtr<float> >::Type
131                  >::value),
132               OwnPtrTest);
133
134} // namespace WebCore
135