// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Helper class used to find the RenderFrameObservers for a given RenderFrame. // // Example usage: // // class MyRFO : public RenderFrameObserver, // public RenderFrameObserverTracker { // ... // }; // // MyRFO::MyRFO(RenderFrame* render_frame) // : RenderFrameObserver(render_frame), // RenderFrameObserverTracker(render_frame) { // ... // } // // void SomeFunction(RenderFrame* rf) { // MyRFO* my_rfo = new MyRFO(rf); // MyRFO* my_rfo_tracked = MyRFO::Get(rf); // // my_rfo == my_rfo_tracked // } #ifndef CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_ #define CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_ #include #include "base/lazy_instance.h" namespace content { class RenderFrame; template class RenderFrameObserverTracker { public: static T* Get(const RenderFrame* render_frame) { return static_cast(render_frame_map_.Get()[render_frame]); } explicit RenderFrameObserverTracker(const RenderFrame* render_frame) : render_frame_(render_frame) { render_frame_map_.Get()[render_frame] = this; } ~RenderFrameObserverTracker() { render_frame_map_.Get().erase(render_frame_); } private: const RenderFrame* render_frame_; static base::LazyInstance< std::map*>> render_frame_map_; DISALLOW_COPY_AND_ASSIGN(RenderFrameObserverTracker); }; template base::LazyInstance*>> RenderFrameObserverTracker::render_frame_map_ = LAZY_INSTANCE_INITIALIZER; } // namespace content #endif // CONTENT_PUBLIC_RENDERER_RENDER_FRAME_OBSERVER_TRACKER_H_