1 // Copyright 2013 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 RenderFrameObservers for a given RenderFrame. 6 // 7 // Example usage: 8 // 9 // class MyRFO : public RenderFrameObserver, 10 // public RenderFrameObserverTracker<MyRFO> { 11 // ... 12 // }; 13 // 14 // MyRFO::MyRFO(RenderFrame* render_frame) 15 // : RenderFrameObserver(render_frame), 16 // RenderFrameObserverTracker<MyRFO>(render_frame) { 17 // ... 18 // } 19 // 20 // void SomeFunction(RenderFrame* rf) { 21 // MyRFO* my_rfo = new MyRFO(rf); 22 // MyRFO* my_rfo_tracked = MyRFO::Get(rf); 23 // // my_rfo == my_rfo_tracked 24 // } 25 26 #ifndef CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_ 27 #define CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_ 28 29 #include <map> 30 31 #include "base/lazy_instance.h" 32 33 namespace content { 34 35 class RenderFrame; 36 37 template <class T> 38 class RenderFrameObserverTracker { 39 public: 40 static T* Get(const RenderFrame* render_frame) { 41 return render_frame_map_.Get()[render_frame]; 42 } 43 44 explicit RenderFrameObserverTracker(const RenderFrame* render_frame) 45 : render_frame_(render_frame) { 46 render_frame_map_.Get()[render_frame] = static_cast<T*>(this); 47 } 48 ~RenderFrameObserverTracker() { 49 render_frame_map_.Get().erase(render_frame_); 50 } 51 52 private: 53 const RenderFrame* render_frame_; 54 55 static base::LazyInstance<std::map<const RenderFrame*, T*> > 56 render_frame_map_; 57 58 DISALLOW_COPY_AND_ASSIGN(RenderFrameObserverTracker<T>); 59 }; 60 61 template <class T> 62 base::LazyInstance<std::map<const RenderFrame*, T*> > 63 RenderFrameObserverTracker<T>::render_frame_map_ = 64 LAZY_INSTANCE_INITIALIZER; 65 66 } // namespace content 67 68 #endif // CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_ 69