Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / renderer / render_view_observer_tracker.h
blob0ef3016403e96ccf43f342bb015ef05b467d22db
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.
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 // };
14 // MyRVO::MyRVO(RenderView* render_view)
15 // : RenderViewObserver(render_view),
16 // RenderViewObserverTracker<MyRVO>(render_view) {
17 // ...
18 // }
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 // }
26 #ifndef CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_
27 #define CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_
29 #include <map>
31 #include "base/lazy_instance.h"
33 namespace content {
35 class RenderView;
37 template <class T>
38 class RenderViewObserverTracker {
39 public:
40 static T* Get(const RenderView* render_view) {
41 return static_cast<T*>(render_view_map_.Get()[render_view]);
44 explicit RenderViewObserverTracker(const RenderView* render_view)
45 : render_view_(render_view) {
46 render_view_map_.Get()[render_view] = this;
48 ~RenderViewObserverTracker() {
49 render_view_map_.Get().erase(render_view_);
52 private:
53 const RenderView* render_view_;
55 static base::LazyInstance<
56 std::map<const RenderView*, RenderViewObserverTracker<T>*>>
57 render_view_map_;
59 DISALLOW_COPY_AND_ASSIGN(RenderViewObserverTracker<T>);
62 template <class T>
63 base::LazyInstance<std::map<const RenderView*, RenderViewObserverTracker<T>*>>
64 RenderViewObserverTracker<T>::render_view_map_ = LAZY_INSTANCE_INITIALIZER;
66 } // namespace content
68 #endif // CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_