Home | History | Annotate | Download | only in platform
      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 <wtf/Forward.h>
     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     struct CrossThreadResourceResponseData;
     48     struct CrossThreadResourceRequestData;
     49     struct ThreadableLoaderOptions;
     50 
     51     template<typename T> struct CrossThreadCopierPassThrough {
     52         typedef T Type;
     53         static Type copy(const T& parameter)
     54         {
     55             return parameter;
     56         }
     57     };
     58 
     59     template<bool isConvertibleToInteger, bool isThreadSafeRefCounted, typename T> struct CrossThreadCopierBase;
     60 
     61     // Integers get passed through without any changes.
     62     template<typename T> struct CrossThreadCopierBase<true, false, T> : public CrossThreadCopierPassThrough<T> {
     63     };
     64 
     65     // Pointers get passed through without any significant changes.
     66     template<typename T> struct CrossThreadCopierBase<false, false, T*> : public CrossThreadCopierPassThrough<T*> {
     67     };
     68 
     69     template<> struct CrossThreadCopierBase<false, false, ThreadableLoaderOptions> : public CrossThreadCopierPassThrough<ThreadableLoaderOptions> {
     70     };
     71 
     72     // Custom copy methods.
     73     template<typename T> struct CrossThreadCopierBase<false, true, T> {
     74         typedef typename WTF::RemoveTemplate<T, RefPtr>::Type TypeWithoutRefPtr;
     75         typedef typename WTF::RemoveTemplate<TypeWithoutRefPtr, PassRefPtr>::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(Type ownPtr)
     86         {
     87             return ownPtr;
     88         }
     89     };
     90 
     91     template<> struct CrossThreadCopierBase<false, false, KURL> {
     92         typedef KURL Type;
     93         static Type copy(const KURL&);
     94     };
     95 
     96     template<> struct CrossThreadCopierBase<false, false, String> {
     97         typedef String Type;
     98         static Type copy(const String&);
     99     };
    100 
    101     template<> struct CrossThreadCopierBase<false, false, ResourceError> {
    102         typedef ResourceError Type;
    103         static Type copy(const ResourceError&);
    104     };
    105 
    106     template<> struct CrossThreadCopierBase<false, false, ResourceRequest> {
    107         typedef PassOwnPtr<CrossThreadResourceRequestData> Type;
    108         static Type copy(const ResourceRequest&);
    109     };
    110 
    111     template<> struct CrossThreadCopierBase<false, false, ResourceResponse> {
    112         typedef PassOwnPtr<CrossThreadResourceResponseData> Type;
    113         static Type copy(const ResourceResponse&);
    114     };
    115 
    116     template<typename T> struct CrossThreadCopier : public CrossThreadCopierBase<WTF::IsConvertibleToInteger<T>::value,
    117                                                                                  WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, RefPtr>::Type, ThreadSafeRefCounted>::value
    118                                                                                      || WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, PassRefPtr>::Type, ThreadSafeRefCounted>::value,
    119                                                                                  T> {
    120     };
    121 
    122 } // namespace WebCore
    123 
    124 #endif // CrossThreadCopier_h
    125