1 // Copyright (c) 2012 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 // Helper class used to find the RenderViewObservers for a given RenderView. 6 // 7 // Example usage: 8 // 9 // class MyRVO : public RenderViewObserver, 10 // public RenderViewObserverTracker<MyRVO> { 11 // ... 12 // }; 13 // 14 // MyRVO::MyRVO(RenderView* render_view) 15 // : RenderViewObserver(render_view), 16 // RenderViewObserverTracker<MyRVO>(render_view) { 17 // ... 18 // } 19 // 20 // void SomeFunction(RenderView* rv) { 21 // MyRVO* my_rvo = new MyRVO(rv); 22 // MyRVO* my_rvo_tracked = MyRVO::Get(rv); 23 // // my_rvo == my_rvo_tracked 24 // } 25 26 #ifndef CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ 27 #define CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ 28 29 #include <map> 30 31 #include "base/lazy_instance.h" 32 33 namespace content { 34 35 class RenderView; 36 37 template <class T> 38 class RenderViewObserverTracker { 39 public: 40 static T* Get(const RenderView* render_view) { 41 return render_view_map_.Get()[render_view]; 42 } 43 44 explicit RenderViewObserverTracker(const RenderView* render_view) 45 : render_view_(render_view) { 46 render_view_map_.Get()[render_view] = static_cast<T*>(this); 47 } 48 ~RenderViewObserverTracker() { 49 render_view_map_.Get().erase(render_view_); 50 } 51 52 private: 53 const RenderView* render_view_; 54 55 static base::LazyInstance<std::map<const RenderView*, T*> > 56 render_view_map_; 57 58 DISALLOW_COPY_AND_ASSIGN(RenderViewObserverTracker<T>); 59 }; 60 61 template <class T> 62 base::LazyInstance<std::map<const RenderView*, T*> > 63 RenderViewObserverTracker<T>::render_view_map_ = LAZY_INSTANCE_INITIALIZER; 64 65 } // namespace content 66 67 #endif // CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ 68