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.
9 // class MyRVO : public RenderViewObserver,
10 // public RenderViewObserverTracker<MyRVO> {
14 // MyRVO::MyRVO(RenderView* render_view)
15 // : RenderViewObserver(render_view),
16 // RenderViewObserverTracker<MyRVO>(render_view) {
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
26 #ifndef CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_
27 #define CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_
31 #include "base/lazy_instance.h"
38 class RenderViewObserverTracker
{
40 static T
* Get(const RenderView
* render_view
) {
41 return 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
] = static_cast<T
*>(this);
48 ~RenderViewObserverTracker() {
49 render_view_map_
.Get().erase(render_view_
);
53 const RenderView
* render_view_
;
55 static base::LazyInstance
<std::map
<const RenderView
*, T
*> >
58 DISALLOW_COPY_AND_ASSIGN(RenderViewObserverTracker
<T
>);
62 base::LazyInstance
<std::map
<const RenderView
*, T
*> >
63 RenderViewObserverTracker
<T
>::render_view_map_
= LAZY_INSTANCE_INITIALIZER
;
65 } // namespace content
67 #endif // CONTENT_PUBLIC_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_