supplementable.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2012 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_SUPPLEMENTABLE_H_
  26. #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SUPPLEMENTABLE_H_
  27. #include "base/macros.h"
  28. #include "third_party/blink/renderer/platform/heap/handle.h"
  29. #include "third_party/blink/renderer/platform/wtf/assertions.h"
  30. #include "third_party/blink/renderer/platform/wtf/hash_map.h"
  31. #if DCHECK_IS_ON()
  32. #include "third_party/blink/renderer/platform/wtf/threading.h"
  33. #endif
  34. namespace blink {
  35. // What you should know about Supplementable and Supplement
  36. // ========================================================
  37. // Supplementable allows a garbage-collected object to be extended with
  38. // additional data.
  39. //
  40. // Most commonly, this is used to attach data to a central object, such as
  41. // LocalFrame, so that it can be easily accessed. This is similar to adding a
  42. // member to that class (e.g. it is kept alive while the supplementable is),
  43. // except that a Supplement is constructed lazily and therefore occupies less
  44. // memory if not used. It can also be used in cases that would otherwise be
  45. // layering violation. For example, it is common for features implemented in
  46. // modules/ to supplement classes in core/.
  47. //
  48. // Supplementable and Supplement instances are meant to be thread local. They
  49. // should only be accessed from within the thread that created them. The
  50. // 2 classes are not designed for safe access from another thread. Violating
  51. // this design assumption can result in memory corruption and unpredictable
  52. // behavior.
  53. //
  54. // What you should know about the Supplement keys
  55. // ==============================================
  56. // The Supplement is expected to use the same const char* string instance
  57. // as its key. The Supplementable's SupplementMap will use the address of the
  58. // string as the key and not the characters themselves. Hence, 2 strings with
  59. // the same characters will be treated as 2 different keys.
  60. //
  61. // In practice, this is mostly hidden. Each Supplement must expose a static
  62. // const char array which provides a human-readable key. Access to supplements
  63. // requires passing the Supplement type, so these cannot collide for unequal
  64. // types.
  65. //
  66. // Use extreme caution when deriving a supplementable class, as misuse can cause
  67. // type confusion.
  68. //
  69. // Typical use is expected to look like this:
  70. //
  71. // class NavigatorFoo : public Supplement<Navigator> {
  72. // public:
  73. // static const char kSupplementName[];
  74. //
  75. // NavigatorFoo& From(Navigator&);
  76. // }
  77. //
  78. // // static
  79. // const char NavigatorFoo::kSupplementName[] = "NavigatorFoo";
  80. //
  81. // NavigatorFoo& NavigatorFoo::From(Navigator& navigator)
  82. // {
  83. // NavigatorFoo* supplement =
  84. // Supplement<Navigator>::From<NavigatorFoo>(navigator);
  85. // if (!supplement) {
  86. // supplement = new NavigatorFoo(navigator);
  87. // ProvideTo(navigator, supplement);
  88. // }
  89. // return *supplement;
  90. // }
  91. //
  92. // The hash map key will automatically be determined from the supplement type
  93. // used.
  94. //
  95. // What you should know about thread checks
  96. // ========================================
  97. // When assertion is enabled this class performs thread-safety check so that
  98. // supplements are provided to and from the same thread.
  99. // If you want to provide some value for Workers, this thread check may be too
  100. // strict, since in you'll be providing the value while worker preparation is
  101. // being done on the main thread, even before the worker thread has started.
  102. // If that's the case you can explicitly call reattachThread() when the
  103. // Supplementable object is passed to the final destination thread (i.e.
  104. // worker thread). This will allow supplements to be accessed on that thread.
  105. // Please be extremely careful to use the method though, as randomly calling
  106. // the method could easily cause racy condition.
  107. //
  108. // Note that reattachThread() does nothing if assertion is not enabled.
  109. template <typename T>
  110. class Supplementable;
  111. template <typename T>
  112. class Supplement : public GarbageCollectedMixin {
  113. public:
  114. // TODO(haraken): Remove the default constructor.
  115. // All Supplement objects should be instantiated with |supplementable_|.
  116. Supplement() {}
  117. explicit Supplement(T& supplementable) : supplementable_(&supplementable) {}
  118. // Supplements are constructed lazily on first access and are destroyed with
  119. // their Supplementable, so GetSupplementable() should never return null (if
  120. // the default constructor is completely removed).
  121. T* GetSupplementable() const { return supplementable_; }
  122. template <typename SupplementType>
  123. static void ProvideTo(Supplementable<T>& supplementable,
  124. SupplementType* supplement) {
  125. supplementable.ProvideSupplement(supplement);
  126. }
  127. template <typename SupplementType>
  128. static SupplementType* From(const Supplementable<T>& supplementable) {
  129. return supplementable.template RequireSupplement<SupplementType>();
  130. }
  131. template <typename SupplementType>
  132. static SupplementType* From(const Supplementable<T>* supplementable) {
  133. return supplementable
  134. ? supplementable->template RequireSupplement<SupplementType>()
  135. : nullptr;
  136. }
  137. void Trace(Visitor* visitor) const override {
  138. visitor->Trace(supplementable_);
  139. }
  140. private:
  141. Member<T> supplementable_;
  142. };
  143. template <typename T>
  144. class Supplementable : public GarbageCollectedMixin {
  145. public:
  146. template <typename SupplementType>
  147. void ProvideSupplement(SupplementType* supplement) {
  148. #if DCHECK_IS_ON()
  149. DCHECK_EQ(creation_thread_id_, CurrentThread());
  150. #endif
  151. static_assert(
  152. std::is_array<decltype(SupplementType::kSupplementName)>::value,
  153. "Declare a const char array kSupplementName. See Supplementable.h for "
  154. "details.");
  155. this->supplements_.Set(SupplementType::kSupplementName, supplement);
  156. }
  157. template <typename SupplementType>
  158. void RemoveSupplement() {
  159. #if DCHECK_IS_ON()
  160. DCHECK_EQ(creation_thread_id_, CurrentThread());
  161. #endif
  162. static_assert(
  163. std::is_array<decltype(SupplementType::kSupplementName)>::value,
  164. "Declare a const char array kSupplementName. See Supplementable.h for "
  165. "details.");
  166. this->supplements_.erase(SupplementType::kSupplementName);
  167. }
  168. template <typename SupplementType>
  169. SupplementType* RequireSupplement() const {
  170. #if DCHECK_IS_ON()
  171. DCHECK_EQ(attached_thread_id_, CurrentThread());
  172. #endif
  173. static_assert(
  174. std::is_array<decltype(SupplementType::kSupplementName)>::value,
  175. "Declare a const char array kSupplementName. See Supplementable.h for "
  176. "details.");
  177. return static_cast<SupplementType*>(
  178. this->supplements_.at(SupplementType::kSupplementName));
  179. }
  180. void ReattachThread() {
  181. #if DCHECK_IS_ON()
  182. attached_thread_id_ = CurrentThread();
  183. #endif
  184. }
  185. void Trace(Visitor* visitor) const override { visitor->Trace(supplements_); }
  186. protected:
  187. using SupplementMap =
  188. HeapHashMap<const char*, Member<Supplement<T>>, PtrHash<const char>>;
  189. SupplementMap supplements_;
  190. Supplementable()
  191. #if DCHECK_IS_ON()
  192. : attached_thread_id_(CurrentThread()),
  193. creation_thread_id_(CurrentThread())
  194. #endif
  195. {
  196. }
  197. #if DCHECK_IS_ON()
  198. private:
  199. base::PlatformThreadId attached_thread_id_;
  200. base::PlatformThreadId creation_thread_id_;
  201. #endif
  202. DISALLOW_COPY_AND_ASSIGN(Supplementable);
  203. };
  204. template <typename T>
  205. struct ThreadingTrait<Supplement<T>> {
  206. static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity;
  207. };
  208. template <typename T>
  209. struct ThreadingTrait<Supplementable<T>> {
  210. static const ThreadAffinity kAffinity = ThreadingTrait<T>::Affinity;
  211. };
  212. } // namespace blink
  213. #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SUPPLEMENTABLE_H_