1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/callback_internal.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// This file contains utility functions and classes that help the 1.9 +// implementation, and management of the Callback objects. 1.10 + 1.11 +#ifndef BASE_CALLBACK_INTERNAL_H_ 1.12 +#define BASE_CALLBACK_INTERNAL_H_ 1.13 + 1.14 +#include <stddef.h> 1.15 + 1.16 +#include "base/base_export.h" 1.17 +#include "base/memory/ref_counted.h" 1.18 +#include "base/memory/scoped_ptr.h" 1.19 + 1.20 +template <typename T> 1.21 +class ScopedVector; 1.22 + 1.23 +namespace base { 1.24 +namespace internal { 1.25 + 1.26 +// BindStateBase is used to provide an opaque handle that the Callback 1.27 +// class can use to represent a function object with bound arguments. It 1.28 +// behaves as an existential type that is used by a corresponding 1.29 +// DoInvoke function to perform the function execution. This allows 1.30 +// us to shield the Callback class from the types of the bound argument via 1.31 +// "type erasure." 1.32 +class BindStateBase : public RefCountedThreadSafe<BindStateBase> { 1.33 + protected: 1.34 + friend class RefCountedThreadSafe<BindStateBase>; 1.35 + virtual ~BindStateBase() {} 1.36 +}; 1.37 + 1.38 +// Holds the Callback methods that don't require specialization to reduce 1.39 +// template bloat. 1.40 +class BASE_EXPORT CallbackBase { 1.41 + public: 1.42 + // Returns true if Callback is null (doesn't refer to anything). 1.43 + bool is_null() const; 1.44 + 1.45 + // Returns the Callback into an uninitialized state. 1.46 + void Reset(); 1.47 + 1.48 + protected: 1.49 + // In C++, it is safe to cast function pointers to function pointers of 1.50 + // another type. It is not okay to use void*. We create a InvokeFuncStorage 1.51 + // that that can store our function pointer, and then cast it back to 1.52 + // the original type on usage. 1.53 + typedef void(*InvokeFuncStorage)(void); 1.54 + 1.55 + // Returns true if this callback equals |other|. |other| may be null. 1.56 + bool Equals(const CallbackBase& other) const; 1.57 + 1.58 + // Allow initializing of |bind_state_| via the constructor to avoid default 1.59 + // initialization of the scoped_refptr. We do not also initialize 1.60 + // |polymorphic_invoke_| here because doing a normal assignment in the 1.61 + // derived Callback templates makes for much nicer compiler errors. 1.62 + explicit CallbackBase(BindStateBase* bind_state); 1.63 + 1.64 + // Force the destructor to be instantiated inside this translation unit so 1.65 + // that our subclasses will not get inlined versions. Avoids more template 1.66 + // bloat. 1.67 + ~CallbackBase(); 1.68 + 1.69 + scoped_refptr<BindStateBase> bind_state_; 1.70 + InvokeFuncStorage polymorphic_invoke_; 1.71 +}; 1.72 + 1.73 +// This is a typetraits object that's used to take an argument type, and 1.74 +// extract a suitable type for storing and forwarding arguments. 1.75 +// 1.76 +// In particular, it strips off references, and converts arrays to 1.77 +// pointers for storage; and it avoids accidentally trying to create a 1.78 +// "reference of a reference" if the argument is a reference type. 1.79 +// 1.80 +// This array type becomes an issue for storage because we are passing bound 1.81 +// parameters by const reference. In this case, we end up passing an actual 1.82 +// array type in the initializer list which C++ does not allow. This will 1.83 +// break passing of C-string literals. 1.84 +template <typename T> 1.85 +struct CallbackParamTraits { 1.86 + typedef const T& ForwardType; 1.87 + typedef T StorageType; 1.88 +}; 1.89 + 1.90 +// The Storage should almost be impossible to trigger unless someone manually 1.91 +// specifies type of the bind parameters. However, in case they do, 1.92 +// this will guard against us accidentally storing a reference parameter. 1.93 +// 1.94 +// The ForwardType should only be used for unbound arguments. 1.95 +template <typename T> 1.96 +struct CallbackParamTraits<T&> { 1.97 + typedef T& ForwardType; 1.98 + typedef T StorageType; 1.99 +}; 1.100 + 1.101 +// Note that for array types, we implicitly add a const in the conversion. This 1.102 +// means that it is not possible to bind array arguments to functions that take 1.103 +// a non-const pointer. Trying to specialize the template based on a "const 1.104 +// T[n]" does not seem to match correctly, so we are stuck with this 1.105 +// restriction. 1.106 +template <typename T, size_t n> 1.107 +struct CallbackParamTraits<T[n]> { 1.108 + typedef const T* ForwardType; 1.109 + typedef const T* StorageType; 1.110 +}; 1.111 + 1.112 +// See comment for CallbackParamTraits<T[n]>. 1.113 +template <typename T> 1.114 +struct CallbackParamTraits<T[]> { 1.115 + typedef const T* ForwardType; 1.116 + typedef const T* StorageType; 1.117 +}; 1.118 + 1.119 +// Parameter traits for movable-but-not-copyable scopers. 1.120 +// 1.121 +// Callback<>/Bind() understands movable-but-not-copyable semantics where 1.122 +// the type cannot be copied but can still have its state destructively 1.123 +// transferred (aka. moved) to another instance of the same type by calling a 1.124 +// helper function. When used with Bind(), this signifies transferal of the 1.125 +// object's state to the target function. 1.126 +// 1.127 +// For these types, the ForwardType must not be a const reference, or a 1.128 +// reference. A const reference is inappropriate, and would break const 1.129 +// correctness, because we are implementing a destructive move. A non-const 1.130 +// reference cannot be used with temporaries which means the result of a 1.131 +// function or a cast would not be usable with Callback<> or Bind(). 1.132 +// 1.133 +// TODO(ajwong): We might be able to use SFINAE to search for the existence of 1.134 +// a Pass() function in the type and avoid the whitelist in CallbackParamTraits 1.135 +// and CallbackForward. 1.136 +template <typename T, typename D> 1.137 +struct CallbackParamTraits<scoped_ptr<T, D> > { 1.138 + typedef scoped_ptr<T, D> ForwardType; 1.139 + typedef scoped_ptr<T, D> StorageType; 1.140 +}; 1.141 + 1.142 +template <typename T, typename R> 1.143 +struct CallbackParamTraits<scoped_ptr_malloc<T, R> > { 1.144 + typedef scoped_ptr_malloc<T, R> ForwardType; 1.145 + typedef scoped_ptr_malloc<T, R> StorageType; 1.146 +}; 1.147 + 1.148 +template <typename T> 1.149 +struct CallbackParamTraits<ScopedVector<T> > { 1.150 + typedef ScopedVector<T> ForwardType; 1.151 + typedef ScopedVector<T> StorageType; 1.152 +}; 1.153 + 1.154 +// CallbackForward() is a very limited simulation of C++11's std::forward() 1.155 +// used by the Callback/Bind system for a set of movable-but-not-copyable 1.156 +// types. It is needed because forwarding a movable-but-not-copyable 1.157 +// argument to another function requires us to invoke the proper move 1.158 +// operator to create a rvalue version of the type. The supported types are 1.159 +// whitelisted below as overloads of the CallbackForward() function. The 1.160 +// default template compiles out to be a no-op. 1.161 +// 1.162 +// In C++11, std::forward would replace all uses of this function. However, it 1.163 +// is impossible to implement a general std::forward with C++11 due to a lack 1.164 +// of rvalue references. 1.165 +// 1.166 +// In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to 1.167 +// simulate std::forward() and forward the result of one Callback as a 1.168 +// parameter to another callback. This is to support Callbacks that return 1.169 +// the movable-but-not-copyable types whitelisted above. 1.170 +template <typename T> 1.171 +T& CallbackForward(T& t) { return t; } 1.172 + 1.173 +template <typename T, typename D> 1.174 +scoped_ptr<T, D> CallbackForward(scoped_ptr<T, D>& p) { return p.Pass(); } 1.175 + 1.176 +template <typename T, typename R> 1.177 +scoped_ptr_malloc<T, R> CallbackForward(scoped_ptr_malloc<T, R>& p) { 1.178 + return p.Pass(); 1.179 +} 1.180 + 1.181 +template <typename T> 1.182 +ScopedVector<T> CallbackForward(ScopedVector<T>& p) { return p.Pass(); } 1.183 + 1.184 +} // namespace internal 1.185 +} // namespace base 1.186 + 1.187 +#endif // BASE_CALLBACK_INTERNAL_H_