1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/sequenced_task_runner_helpers.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 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 +#ifndef BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_ 1.9 +#define BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_ 1.10 + 1.11 +#include "base/basictypes.h" 1.12 + 1.13 +// TODO(akalin): Investigate whether it's possible to just have 1.14 +// SequencedTaskRunner use these helpers (instead of MessageLoop). 1.15 +// Then we can just move these to sequenced_task_runner.h. 1.16 + 1.17 +namespace tracked_objects { 1.18 +class Location; 1.19 +} 1.20 + 1.21 +namespace base { 1.22 + 1.23 +namespace subtle { 1.24 +template <class T, class R> class DeleteHelperInternal; 1.25 +template <class T, class R> class ReleaseHelperInternal; 1.26 +} 1.27 + 1.28 +// Template helpers which use function indirection to erase T from the 1.29 +// function signature while still remembering it so we can call the 1.30 +// correct destructor/release function. 1.31 +// 1.32 +// We use this trick so we don't need to include bind.h in a header 1.33 +// file like sequenced_task_runner.h. We also wrap the helpers in a 1.34 +// templated class to make it easier for users of DeleteSoon to 1.35 +// declare the helper as a friend. 1.36 +template <class T> 1.37 +class DeleteHelper { 1.38 + private: 1.39 + template <class T2, class R> friend class subtle::DeleteHelperInternal; 1.40 + 1.41 + static void DoDelete(const void* object) { 1.42 + delete reinterpret_cast<const T*>(object); 1.43 + } 1.44 + 1.45 + DISALLOW_COPY_AND_ASSIGN(DeleteHelper); 1.46 +}; 1.47 + 1.48 +template <class T> 1.49 +class ReleaseHelper { 1.50 + private: 1.51 + template <class T2, class R> friend class subtle::ReleaseHelperInternal; 1.52 + 1.53 + static void DoRelease(const void* object) { 1.54 + reinterpret_cast<const T*>(object)->Release(); 1.55 + } 1.56 + 1.57 + DISALLOW_COPY_AND_ASSIGN(ReleaseHelper); 1.58 +}; 1.59 + 1.60 +namespace subtle { 1.61 + 1.62 +// An internal SequencedTaskRunner-like class helper for DeleteHelper 1.63 +// and ReleaseHelper. We don't want to expose the Do*() functions 1.64 +// directly directly since the void* argument makes it possible to 1.65 +// pass/ an object of the wrong type to delete. Instead, we force 1.66 +// callers to go through these internal helpers for type 1.67 +// safety. SequencedTaskRunner-like classes which expose DeleteSoon or 1.68 +// ReleaseSoon methods should friend the appropriate helper and 1.69 +// implement a corresponding *Internal method with the following 1.70 +// signature: 1.71 +// 1.72 +// bool(const tracked_objects::Location&, 1.73 +// void(*function)(const void*), 1.74 +// void* object) 1.75 +// 1.76 +// An implementation of this function should simply create a 1.77 +// base::Closure from (function, object) and return the result of 1.78 +// posting the task. 1.79 +template <class T, class ReturnType> 1.80 +class DeleteHelperInternal { 1.81 + public: 1.82 + template <class SequencedTaskRunnerType> 1.83 + static ReturnType DeleteViaSequencedTaskRunner( 1.84 + SequencedTaskRunnerType* sequenced_task_runner, 1.85 + const tracked_objects::Location& from_here, 1.86 + const T* object) { 1.87 + return sequenced_task_runner->DeleteSoonInternal( 1.88 + from_here, &DeleteHelper<T>::DoDelete, object); 1.89 + } 1.90 + 1.91 + private: 1.92 + DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal); 1.93 +}; 1.94 + 1.95 +template <class T, class ReturnType> 1.96 +class ReleaseHelperInternal { 1.97 + public: 1.98 + template <class SequencedTaskRunnerType> 1.99 + static ReturnType ReleaseViaSequencedTaskRunner( 1.100 + SequencedTaskRunnerType* sequenced_task_runner, 1.101 + const tracked_objects::Location& from_here, 1.102 + const T* object) { 1.103 + return sequenced_task_runner->ReleaseSoonInternal( 1.104 + from_here, &ReleaseHelper<T>::DoRelease, object); 1.105 + } 1.106 + 1.107 + private: 1.108 + DISALLOW_COPY_AND_ASSIGN(ReleaseHelperInternal); 1.109 +}; 1.110 + 1.111 +} // namespace subtle 1.112 + 1.113 +} // namespace base 1.114 + 1.115 +#endif // BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_