Home | History | Annotate | Download | only in memory
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
      6 #define BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
      7 #pragma once
      8 
      9 #include "base/memory/ref_counted.h"
     10 #include "base/template_util.h"
     11 #include "base/tuple.h"
     12 #include "build/build_config.h"
     13 
     14 // It is dangerous to post a task with a T* argument where T is a subtype of
     15 // RefCounted(Base|ThreadSafeBase), since by the time the parameter is used, the
     16 // object may already have been deleted since it was not held with a
     17 // scoped_refptr. Example: http://crbug.com/27191
     18 // The following set of traits are designed to generate a compile error
     19 // whenever this antipattern is attempted.
     20 
     21 namespace base {
     22 
     23 // This is a base internal implementation file used by task.h and callback.h.
     24 // Not for public consumption, so we wrap it in namespace internal.
     25 namespace internal {
     26 
     27 template <typename T>
     28 struct NeedsScopedRefptrButGetsRawPtr {
     29 #if defined(OS_WIN)
     30   enum {
     31     value = base::false_type::value
     32   };
     33 #else
     34   enum {
     35     // Human readable translation: you needed to be a scoped_refptr if you are a
     36     // raw pointer type and are convertible to a RefCounted(Base|ThreadSafeBase)
     37     // type.
     38     value = (is_pointer<T>::value &&
     39              (is_convertible<T, subtle::RefCountedBase*>::value ||
     40               is_convertible<T, subtle::RefCountedThreadSafeBase*>::value))
     41   };
     42 #endif
     43 };
     44 
     45 template <typename Params>
     46 struct ParamsUseScopedRefptrCorrectly {
     47   enum { value = 0 };
     48 };
     49 
     50 template <>
     51 struct ParamsUseScopedRefptrCorrectly<Tuple0> {
     52   enum { value = 1 };
     53 };
     54 
     55 template <typename A>
     56 struct ParamsUseScopedRefptrCorrectly<Tuple1<A> > {
     57   enum { value = !NeedsScopedRefptrButGetsRawPtr<A>::value };
     58 };
     59 
     60 template <typename A, typename B>
     61 struct ParamsUseScopedRefptrCorrectly<Tuple2<A, B> > {
     62   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
     63                    NeedsScopedRefptrButGetsRawPtr<B>::value) };
     64 };
     65 
     66 template <typename A, typename B, typename C>
     67 struct ParamsUseScopedRefptrCorrectly<Tuple3<A, B, C> > {
     68   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
     69                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
     70                    NeedsScopedRefptrButGetsRawPtr<C>::value) };
     71 };
     72 
     73 template <typename A, typename B, typename C, typename D>
     74 struct ParamsUseScopedRefptrCorrectly<Tuple4<A, B, C, D> > {
     75   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
     76                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
     77                    NeedsScopedRefptrButGetsRawPtr<C>::value ||
     78                    NeedsScopedRefptrButGetsRawPtr<D>::value) };
     79 };
     80 
     81 template <typename A, typename B, typename C, typename D, typename E>
     82 struct ParamsUseScopedRefptrCorrectly<Tuple5<A, B, C, D, E> > {
     83   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
     84                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
     85                    NeedsScopedRefptrButGetsRawPtr<C>::value ||
     86                    NeedsScopedRefptrButGetsRawPtr<D>::value ||
     87                    NeedsScopedRefptrButGetsRawPtr<E>::value) };
     88 };
     89 
     90 template <typename A, typename B, typename C, typename D, typename E,
     91           typename F>
     92 struct ParamsUseScopedRefptrCorrectly<Tuple6<A, B, C, D, E, F> > {
     93   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
     94                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
     95                    NeedsScopedRefptrButGetsRawPtr<C>::value ||
     96                    NeedsScopedRefptrButGetsRawPtr<D>::value ||
     97                    NeedsScopedRefptrButGetsRawPtr<E>::value ||
     98                    NeedsScopedRefptrButGetsRawPtr<F>::value) };
     99 };
    100 
    101 template <typename A, typename B, typename C, typename D, typename E,
    102           typename F, typename G>
    103 struct ParamsUseScopedRefptrCorrectly<Tuple7<A, B, C, D, E, F, G> > {
    104   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
    105                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
    106                    NeedsScopedRefptrButGetsRawPtr<C>::value ||
    107                    NeedsScopedRefptrButGetsRawPtr<D>::value ||
    108                    NeedsScopedRefptrButGetsRawPtr<E>::value ||
    109                    NeedsScopedRefptrButGetsRawPtr<F>::value ||
    110                    NeedsScopedRefptrButGetsRawPtr<G>::value) };
    111 };
    112 
    113 template <typename A, typename B, typename C, typename D, typename E,
    114           typename F, typename G, typename H>
    115 struct ParamsUseScopedRefptrCorrectly<Tuple8<A, B, C, D, E, F, G, H> > {
    116   enum { value = !(NeedsScopedRefptrButGetsRawPtr<A>::value ||
    117                    NeedsScopedRefptrButGetsRawPtr<B>::value ||
    118                    NeedsScopedRefptrButGetsRawPtr<C>::value ||
    119                    NeedsScopedRefptrButGetsRawPtr<D>::value ||
    120                    NeedsScopedRefptrButGetsRawPtr<E>::value ||
    121                    NeedsScopedRefptrButGetsRawPtr<F>::value ||
    122                    NeedsScopedRefptrButGetsRawPtr<G>::value ||
    123                    NeedsScopedRefptrButGetsRawPtr<H>::value) };
    124 };
    125 
    126 }  // namespace internal
    127 
    128 }  // namespace base
    129 
    130 #endif  // BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
    131