Chromium Code Reviews| Index: chrome/browser/android/data_usage/data_use_tab_model.h |
| diff --git a/chrome/browser/android/data_usage/data_use_tab_model.h b/chrome/browser/android/data_usage/data_use_tab_model.h |
| index 43a07bdb110c5a8ac329cb20af38617e10afe9e3..311d27da99f78272296976683b284d7f68f7dbe4 100644 |
| --- a/chrome/browser/android/data_usage/data_use_tab_model.h |
| +++ b/chrome/browser/android/data_usage/data_use_tab_model.h |
| @@ -14,7 +14,6 @@ |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| -#include "base/observer_list_threadsafe.h" |
| #include "base/threading/thread_checker.h" |
| #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| #include "components/data_usage/core/data_use.h" |
|
sclittle
2015/11/20 22:50:54
Replace this include with a forward declaration of
tbansal1
2015/11/23 17:52:22
Done.
|
| @@ -62,7 +61,7 @@ class DataUseTabModel { |
| }; |
| // TabDataUseObserver provides the interface for getting notifications from |
| - // the DataUseTabModel. |
| + // the DataUseTabModel. TabDataUseObserver is called back on UI thread. |
| class TabDataUseObserver { |
| public: |
| virtual ~TabDataUseObserver() {} |
| @@ -102,10 +101,20 @@ class DataUseTabModel { |
| bool GetLabelForDataUse(const data_usage::DataUse& data_use, |
| std::string* output_label) const; |
| - // Adds or removes observers from the observer list. These functions are |
| - // thread-safe and can be called from any thread. |
| - void AddObserver(TabDataUseObserver* observer); |
| - void RemoveObserver(TabDataUseObserver* observer); |
| + // Adds or removes observers from the observer list. Must be called only on |
| + // IO thread. |weak_ptr_observer| is notified on UI thread. |
| + void AddObserver(const TabDataUseObserver* observer, |
| + const base::WeakPtr<TabDataUseObserver>& weak_ptr_observer); |
| + void RemoveObserver(const TabDataUseObserver* observer); |
| + |
| + protected: |
| + // Notifies the observers that a data usage tracking session started for |
| + // |tab_id|. Protected for testing. |
| + void NotifyObserversOfTrackingStarting(int32_t tab_id); |
| + |
| + // Notifies the observers that an active data usage tracking session ended for |
| + // |tab_id|. Protected for testing. |
| + void NotifyObserversOfTrackingEnding(int32_t tab_id); |
| private: |
| friend class DataUseTabModelTest; |
| @@ -121,20 +130,14 @@ class DataUseTabModel { |
| FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
| CompactTabEntriesWithinMaxLimit); |
| + typedef base::hash_map<const TabDataUseObserver*, |
| + const base::WeakPtr<TabDataUseObserver>&> ObserverMap; |
| typedef base::hash_map<int32_t, TabDataUseEntry> TabEntryMap; |
| // Returns the maximum number of tab entries to maintain session information |
| // about. |
| static size_t GetMaxTabEntriesForTests(); |
| - // Notifies the observers that a data usage tracking session started for |
| - // |tab_id|. |
| - void NotifyObserversOfTrackingStarting(int32_t tab_id); |
| - |
| - // Notifies the observers that an active data usage tracking session ended for |
| - // |tab_id|. |
| - void NotifyObserversOfTrackingEnding(int32_t tab_id); |
| - |
| // Initiates a new tracking session with the |label| for tab with id |tab_id|. |
| void StartTrackingDataUse(int32_t tab_id, const std::string& label); |
| @@ -151,17 +154,19 @@ class DataUseTabModel { |
| // |data_use_observer_| outlives this instance. |
| const ExternalDataUseObserver* data_use_observer_; |
| - // List of observers waiting for tracking session start and end notifications. |
| - const scoped_refptr<base::ObserverListThreadSafe<TabDataUseObserver>> |
| - observer_list_; |
| + // Map of observers waiting for tracking session start and end notifications. |
| + // Map is keyed by raw pointers, and notifications are posted to the weak |
| + // pointers on UI thread. |
| + ObserverMap observers_; |
| // Maintains the tracking sessions of multiple tabs. |
| TabEntryMap active_tabs_; |
| + // |ui_task_runner_| is used to notify TabDataUseObserver on UI thread. |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| + |
| base::ThreadChecker thread_checker_; |
| - // |weak_factory_| is used for generating weak pointers to be passed to |
| - // DataUseTabUIManager on the UI thread. |
| base::WeakPtrFactory<DataUseTabModel> weak_factory_; |
| DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); |