1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/memory/weak_ptr.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,338 @@ 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 +// Weak pointers are pointers to an object that do not affect its lifetime, 1.9 +// and which may be invalidated (i.e. reset to NULL) by the object, or its 1.10 +// owner, at any time, most commonly when the object is about to be deleted. 1.11 + 1.12 +// Weak pointers are useful when an object needs to be accessed safely by one 1.13 +// or more objects other than its owner, and those callers can cope with the 1.14 +// object vanishing and e.g. tasks posted to it being silently dropped. 1.15 +// Reference-counting such an object would complicate the ownership graph and 1.16 +// make it harder to reason about the object's lifetime. 1.17 + 1.18 +// EXAMPLE: 1.19 +// 1.20 +// class Controller { 1.21 +// public: 1.22 +// void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } 1.23 +// void WorkComplete(const Result& result) { ... } 1.24 +// private: 1.25 +// // Member variables should appear before the WeakPtrFactory, to ensure 1.26 +// // that any WeakPtrs to Controller are invalidated before its members 1.27 +// // variable's destructors are executed, rendering them invalid. 1.28 +// WeakPtrFactory<Controller> weak_factory_; 1.29 +// }; 1.30 +// 1.31 +// class Worker { 1.32 +// public: 1.33 +// static void StartNew(const WeakPtr<Controller>& controller) { 1.34 +// Worker* worker = new Worker(controller); 1.35 +// // Kick off asynchronous processing... 1.36 +// } 1.37 +// private: 1.38 +// Worker(const WeakPtr<Controller>& controller) 1.39 +// : controller_(controller) {} 1.40 +// void DidCompleteAsynchronousProcessing(const Result& result) { 1.41 +// if (controller_) 1.42 +// controller_->WorkComplete(result); 1.43 +// } 1.44 +// WeakPtr<Controller> controller_; 1.45 +// }; 1.46 +// 1.47 +// With this implementation a caller may use SpawnWorker() to dispatch multiple 1.48 +// Workers and subsequently delete the Controller, without waiting for all 1.49 +// Workers to have completed. 1.50 + 1.51 +// ------------------------- IMPORTANT: Thread-safety ------------------------- 1.52 + 1.53 +// Weak pointers may be passed safely between threads, but must always be 1.54 +// dereferenced and invalidated on the same thread otherwise checking the 1.55 +// pointer would be racey. 1.56 +// 1.57 +// To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory 1.58 +// is dereferenced, the factory and its WeakPtrs become bound to the calling 1.59 +// thread, and cannot be dereferenced or invalidated on any other thread. Bound 1.60 +// WeakPtrs can still be handed off to other threads, e.g. to use to post tasks 1.61 +// back to object on the bound thread. 1.62 +// 1.63 +// Invalidating the factory's WeakPtrs un-binds it from the thread, allowing it 1.64 +// to be passed for a different thread to use or delete it. 1.65 + 1.66 +#ifndef BASE_MEMORY_WEAK_PTR_H_ 1.67 +#define BASE_MEMORY_WEAK_PTR_H_ 1.68 + 1.69 +#include "base/basictypes.h" 1.70 +#include "base/base_export.h" 1.71 +#include "base/logging.h" 1.72 +#include "base/memory/ref_counted.h" 1.73 +#include "base/sequence_checker.h" 1.74 +#include "base/template_util.h" 1.75 + 1.76 +namespace base { 1.77 + 1.78 +template <typename T> class SupportsWeakPtr; 1.79 +template <typename T> class WeakPtr; 1.80 + 1.81 +namespace internal { 1.82 +// These classes are part of the WeakPtr implementation. 1.83 +// DO NOT USE THESE CLASSES DIRECTLY YOURSELF. 1.84 + 1.85 +class BASE_EXPORT WeakReference { 1.86 + public: 1.87 + // Although Flag is bound to a specific thread, it may be deleted from another 1.88 + // via base::WeakPtr::~WeakPtr(). 1.89 + class Flag : public RefCountedThreadSafe<Flag> { 1.90 + public: 1.91 + Flag(); 1.92 + 1.93 + void Invalidate(); 1.94 + bool IsValid() const; 1.95 + 1.96 + private: 1.97 + friend class base::RefCountedThreadSafe<Flag>; 1.98 + 1.99 + ~Flag(); 1.100 + 1.101 + SequenceChecker sequence_checker_; 1.102 + bool is_valid_; 1.103 + }; 1.104 + 1.105 + WeakReference(); 1.106 + explicit WeakReference(const Flag* flag); 1.107 + ~WeakReference(); 1.108 + 1.109 + bool is_valid() const; 1.110 + 1.111 + private: 1.112 + scoped_refptr<const Flag> flag_; 1.113 +}; 1.114 + 1.115 +class BASE_EXPORT WeakReferenceOwner { 1.116 + public: 1.117 + WeakReferenceOwner(); 1.118 + ~WeakReferenceOwner(); 1.119 + 1.120 + WeakReference GetRef() const; 1.121 + 1.122 + bool HasRefs() const { 1.123 + return flag_.get() && !flag_->HasOneRef(); 1.124 + } 1.125 + 1.126 + void Invalidate(); 1.127 + 1.128 + private: 1.129 + mutable scoped_refptr<WeakReference::Flag> flag_; 1.130 +}; 1.131 + 1.132 +// This class simplifies the implementation of WeakPtr's type conversion 1.133 +// constructor by avoiding the need for a public accessor for ref_. A 1.134 +// WeakPtr<T> cannot access the private members of WeakPtr<U>, so this 1.135 +// base class gives us a way to access ref_ in a protected fashion. 1.136 +class BASE_EXPORT WeakPtrBase { 1.137 + public: 1.138 + WeakPtrBase(); 1.139 + ~WeakPtrBase(); 1.140 + 1.141 + protected: 1.142 + explicit WeakPtrBase(const WeakReference& ref); 1.143 + 1.144 + WeakReference ref_; 1.145 +}; 1.146 + 1.147 +// This class provides a common implementation of common functions that would 1.148 +// otherwise get instantiated separately for each distinct instantiation of 1.149 +// SupportsWeakPtr<>. 1.150 +class SupportsWeakPtrBase { 1.151 + public: 1.152 + // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This 1.153 + // conversion will only compile if there is exists a Base which inherits 1.154 + // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper 1.155 + // function that makes calling this easier. 1.156 + template<typename Derived> 1.157 + static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { 1.158 + typedef 1.159 + is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible; 1.160 + COMPILE_ASSERT(convertible::value, 1.161 + AsWeakPtr_argument_inherits_from_SupportsWeakPtr); 1.162 + return AsWeakPtrImpl<Derived>(t, *t); 1.163 + } 1.164 + 1.165 + private: 1.166 + // This template function uses type inference to find a Base of Derived 1.167 + // which is an instance of SupportsWeakPtr<Base>. We can then safely 1.168 + // static_cast the Base* to a Derived*. 1.169 + template <typename Derived, typename Base> 1.170 + static WeakPtr<Derived> AsWeakPtrImpl( 1.171 + Derived* t, const SupportsWeakPtr<Base>&) { 1.172 + WeakPtr<Base> ptr = t->Base::AsWeakPtr(); 1.173 + return WeakPtr<Derived>(ptr.ref_, static_cast<Derived*>(ptr.ptr_)); 1.174 + } 1.175 +}; 1.176 + 1.177 +} // namespace internal 1.178 + 1.179 +template <typename T> class WeakPtrFactory; 1.180 + 1.181 +// The WeakPtr class holds a weak reference to |T*|. 1.182 +// 1.183 +// This class is designed to be used like a normal pointer. You should always 1.184 +// null-test an object of this class before using it or invoking a method that 1.185 +// may result in the underlying object being destroyed. 1.186 +// 1.187 +// EXAMPLE: 1.188 +// 1.189 +// class Foo { ... }; 1.190 +// WeakPtr<Foo> foo; 1.191 +// if (foo) 1.192 +// foo->method(); 1.193 +// 1.194 +template <typename T> 1.195 +class WeakPtr : public internal::WeakPtrBase { 1.196 + public: 1.197 + WeakPtr() : ptr_(NULL) { 1.198 + } 1.199 + 1.200 + // Allow conversion from U to T provided U "is a" T. Note that this 1.201 + // is separate from the (implicit) copy constructor. 1.202 + template <typename U> 1.203 + WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) { 1.204 + } 1.205 + 1.206 + T* get() const { return ref_.is_valid() ? ptr_ : NULL; } 1.207 + 1.208 + T& operator*() const { 1.209 + DCHECK(get() != NULL); 1.210 + return *get(); 1.211 + } 1.212 + T* operator->() const { 1.213 + DCHECK(get() != NULL); 1.214 + return get(); 1.215 + } 1.216 + 1.217 + // Allow WeakPtr<element_type> to be used in boolean expressions, but not 1.218 + // implicitly convertible to a real bool (which is dangerous). 1.219 + // 1.220 + // Note that this trick is only safe when the == and != operators 1.221 + // are declared explicitly, as otherwise "weak_ptr1 == weak_ptr2" 1.222 + // will compile but do the wrong thing (i.e., convert to Testable 1.223 + // and then do the comparison). 1.224 + private: 1.225 + typedef T* WeakPtr::*Testable; 1.226 + 1.227 + public: 1.228 + operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; } 1.229 + 1.230 + void reset() { 1.231 + ref_ = internal::WeakReference(); 1.232 + ptr_ = NULL; 1.233 + } 1.234 + 1.235 + private: 1.236 + // Explicitly declare comparison operators as required by the bool 1.237 + // trick, but keep them private. 1.238 + template <class U> bool operator==(WeakPtr<U> const&) const; 1.239 + template <class U> bool operator!=(WeakPtr<U> const&) const; 1.240 + 1.241 + friend class internal::SupportsWeakPtrBase; 1.242 + template <typename U> friend class WeakPtr; 1.243 + friend class SupportsWeakPtr<T>; 1.244 + friend class WeakPtrFactory<T>; 1.245 + 1.246 + WeakPtr(const internal::WeakReference& ref, T* ptr) 1.247 + : WeakPtrBase(ref), 1.248 + ptr_(ptr) { 1.249 + } 1.250 + 1.251 + // This pointer is only valid when ref_.is_valid() is true. Otherwise, its 1.252 + // value is undefined (as opposed to NULL). 1.253 + T* ptr_; 1.254 +}; 1.255 + 1.256 +// A class may be composed of a WeakPtrFactory and thereby 1.257 +// control how it exposes weak pointers to itself. This is helpful if you only 1.258 +// need weak pointers within the implementation of a class. This class is also 1.259 +// useful when working with primitive types. For example, you could have a 1.260 +// WeakPtrFactory<bool> that is used to pass around a weak reference to a bool. 1.261 +template <class T> 1.262 +class WeakPtrFactory { 1.263 + public: 1.264 + explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { 1.265 + } 1.266 + 1.267 + ~WeakPtrFactory() { 1.268 + ptr_ = NULL; 1.269 + } 1.270 + 1.271 + WeakPtr<T> GetWeakPtr() { 1.272 + DCHECK(ptr_); 1.273 + return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); 1.274 + } 1.275 + 1.276 + // Call this method to invalidate all existing weak pointers. 1.277 + void InvalidateWeakPtrs() { 1.278 + DCHECK(ptr_); 1.279 + weak_reference_owner_.Invalidate(); 1.280 + } 1.281 + 1.282 + // Call this method to determine if any weak pointers exist. 1.283 + bool HasWeakPtrs() const { 1.284 + DCHECK(ptr_); 1.285 + return weak_reference_owner_.HasRefs(); 1.286 + } 1.287 + 1.288 + private: 1.289 + internal::WeakReferenceOwner weak_reference_owner_; 1.290 + T* ptr_; 1.291 + DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); 1.292 +}; 1.293 + 1.294 +// A class may extend from SupportsWeakPtr to let others take weak pointers to 1.295 +// it. This avoids the class itself implementing boilerplate to dispense weak 1.296 +// pointers. However, since SupportsWeakPtr's destructor won't invalidate 1.297 +// weak pointers to the class until after the derived class' members have been 1.298 +// destroyed, its use can lead to subtle use-after-destroy issues. 1.299 +template <class T> 1.300 +class SupportsWeakPtr : public internal::SupportsWeakPtrBase { 1.301 + public: 1.302 + SupportsWeakPtr() {} 1.303 + 1.304 + WeakPtr<T> AsWeakPtr() { 1.305 + return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); 1.306 + } 1.307 + 1.308 + protected: 1.309 + ~SupportsWeakPtr() {} 1.310 + 1.311 + private: 1.312 + internal::WeakReferenceOwner weak_reference_owner_; 1.313 + DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); 1.314 +}; 1.315 + 1.316 +// Helper function that uses type deduction to safely return a WeakPtr<Derived> 1.317 +// when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it 1.318 +// extends a Base that extends SupportsWeakPtr<Base>. 1.319 +// 1.320 +// EXAMPLE: 1.321 +// class Base : public base::SupportsWeakPtr<Producer> {}; 1.322 +// class Derived : public Base {}; 1.323 +// 1.324 +// Derived derived; 1.325 +// base::WeakPtr<Derived> ptr = base::AsWeakPtr(&derived); 1.326 +// 1.327 +// Note that the following doesn't work (invalid type conversion) since 1.328 +// Derived::AsWeakPtr() is WeakPtr<Base> SupportsWeakPtr<Base>::AsWeakPtr(), 1.329 +// and there's no way to safely cast WeakPtr<Base> to WeakPtr<Derived> at 1.330 +// the caller. 1.331 +// 1.332 +// base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. 1.333 + 1.334 +template <typename Derived> 1.335 +WeakPtr<Derived> AsWeakPtr(Derived* t) { 1.336 + return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); 1.337 +} 1.338 + 1.339 +} // namespace base 1.340 + 1.341 +#endif // BASE_MEMORY_WEAK_PTR_H_