Make sure webrtc::VideoSourceInterface is released on the main render thread.
[chromium-blink-merge.git] / content / public / renderer / render_frame_observer_tracker.h
blob7c5b332ef32e0022e7900fcff8c5eedabc3b0d3e
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.
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 // };
14 // MyRFO::MyRFO(RenderFrame* render_frame)
15 // : RenderFrameObserver(render_frame),
16 // RenderFrameObserverTracker<MyRFO>(render_frame) {
17 // ...
18 // }
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 // }
26 #ifndef CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_
27 #define CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_
29 #include <map>
31 #include "base/lazy_instance.h"
33 namespace content {
35 class RenderFrame;
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];
44 explicit RenderFrameObserverTracker(const RenderFrame* render_frame)
45 : render_frame_(render_frame) {
46 render_frame_map_.Get()[render_frame] = static_cast<T*>(this);
48 ~RenderFrameObserverTracker() {
49 render_frame_map_.Get().erase(render_frame_);
52 private:
53 const RenderFrame* render_frame_;
55 static base::LazyInstance<std::map<const RenderFrame*, T*> >
56 render_frame_map_;
58 DISALLOW_COPY_AND_ASSIGN(RenderFrameObserverTracker<T>);
61 template <class T>
62 base::LazyInstance<std::map<const RenderFrame*, T*> >
63 RenderFrameObserverTracker<T>::render_frame_map_ =
64 LAZY_INSTANCE_INITIALIZER;
66 } // namespace content
68 #endif // CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_