Upstream TestHttpServerClient for Android.
[chromium-blink-merge.git] / content / public / renderer / render_view_observer_tracker.h
blob54de713ef90f2464757529ace1d7bd879e35aa7f
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 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_);
52 private:
53 const RenderView* render_view_;
55 static base::LazyInstance<std::map<const RenderView*, T*> >
56 render_view_map_;
58 DISALLOW_COPY_AND_ASSIGN(RenderViewObserverTracker<T>);
61 template <class 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_