1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_COMMON_TRACKER_H_ 6 #define CEF_LIBCEF_COMMON_TRACKER_H_ 7 #pragma once 8 9 #include "include/cef_base.h" 10 11 #include "base/synchronization/lock.h" 12 13 // Class extended by objects that must be tracked. After creating a tracked 14 // object you should add it to the appropriate track manager. 15 class CefTrackNode { 16 public: 17 CefTrackNode(); 18 virtual ~CefTrackNode(); 19 20 // Returns true if the object is currently being tracked. IsTracked()21 inline bool IsTracked() { return (track_prev_ || track_next_); } 22 23 private: GetTrackPrev()24 inline CefTrackNode* GetTrackPrev() { return track_prev_; } SetTrackPrev(CefTrackNode * base)25 inline void SetTrackPrev(CefTrackNode* base) { track_prev_ = base; } GetTrackNext()26 inline CefTrackNode* GetTrackNext() { return track_next_; } SetTrackNext(CefTrackNode * base)27 inline void SetTrackNext(CefTrackNode* base) { track_next_ = base; } 28 29 // Insert a new object into the tracking list before this object. 30 void InsertTrackPrev(CefTrackNode* object); 31 32 // Insert a new object into the tracking list after this object. 33 void InsertTrackNext(CefTrackNode* object); 34 35 // Remove this object from the tracking list. 36 void RemoveTracking(); 37 38 private: 39 CefTrackNode* track_next_; 40 CefTrackNode* track_prev_; 41 42 friend class CefTrackManager; 43 }; 44 45 // Class used to manage tracked objects. A single instance of this class 46 // should be created for each intended usage. Any objects that have not been 47 // removed by explicit calls to the Destroy() method will be removed when the 48 // manager object is destroyed. A manager object can be created as either a 49 // member variable of another class or by using lazy initialization: 50 // base::LazyInstance<CefTrackManager> g_singleton = LAZY_INSTANCE_INITIALIZER; 51 class CefTrackManager : public CefBaseRefCounted { 52 public: 53 CefTrackManager(); 54 ~CefTrackManager() override; 55 56 // Add an object to be tracked by this manager. 57 void Add(CefTrackNode* object); 58 59 // Delete an object tracked by this manager. 60 bool Delete(CefTrackNode* object); 61 62 // Delete all objects tracked by this manager. 63 void DeleteAll(); 64 65 // Returns the number of objects currently being tracked. GetCount()66 inline int GetCount() { return object_count_; } 67 68 private: 69 CefTrackNode tracker_; 70 int object_count_; 71 72 base::Lock lock_; 73 74 IMPLEMENT_REFCOUNTING(CefTrackManager); 75 }; 76 77 #endif // CEF_LIBCEF_COMMON_TRACKER_H_ 78