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 2420de1a7a85465d58ade6bbbb54af066d2fd8a7..9185a5dd24e57a3be9ace5dc4947d0ce27df89eb 100644 |
| --- a/chrome/browser/android/data_usage/data_use_tab_model.h |
| +++ b/chrome/browser/android/data_usage/data_use_tab_model.h |
| @@ -14,13 +14,20 @@ |
| #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 "base/time/time.h" |
| #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| -#include "components/data_usage/core/data_use.h" |
| +#include "components/sessions/core/session_id.h" |
| #include "url/gurl.h" |
|
sclittle
2015/11/23 23:06:16
nit: Remove this include and make it a forward dec
tbansal1
2015/11/24 00:42:02
Not sure how to forward declare a typedef without
sclittle
2015/11/24 04:11:50
GURL isn't a typedef, so this shouldn't be a probl
tbansal1
2015/11/24 20:01:12
That's required because some functions pass GURL b
mmenke
2015/11/24 20:44:28
You can forward declare classes you pass by refere
tbansal1
2015/11/24 20:51:40
Done.
sclittle
2015/11/24 23:07:37
Const ref works fine with forward declarations, I
|
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace data_usage { |
| +struct DataUse; |
| +} |
| + |
| namespace chrome { |
| namespace android { |
| @@ -46,12 +53,6 @@ class DataUseTabModel { |
| // TODO(rajendrant): Remove this if not needed. |
| TRANSITION_FROM_EXTERNAL_APP, |
| - // Navigating to another app. |
| - TRANSITION_TO_EXTERNAL_APP, |
| - |
| - // Navigation from NavSuggest below omnibox. |
| - TRANSITION_FROM_NAVSUGGEST, |
| - |
| // Navigation from the omnibox when typing a URL. |
| TRANSITION_OMNIBOX_NAVIGATION, |
| @@ -63,7 +64,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() {} |
| @@ -71,8 +72,8 @@ class DataUseTabModel { |
| // Notification callback when tab tracking sessions are started and ended. |
| // The callback will be received on the same thread AddObserver was called |
| // from. |
| - virtual void NotifyTrackingStarting(int32_t tab_id) = 0; |
| - virtual void NotifyTrackingEnding(int32_t tab_id) = 0; |
| + virtual void NotifyTrackingStarting(SessionID::id_type tab_id) = 0; |
| + virtual void NotifyTrackingEnding(SessionID::id_type tab_id) = 0; |
| }; |
| DataUseTabModel(const ExternalDataUseObserver* data_use_observer, |
| @@ -86,7 +87,7 @@ class DataUseTabModel { |
| // tab of the generated event, |transition| indicates the type of the UI |
| // event/transition, |url| is the URL in the source tab, |package| indicates |
| // the android package name of external application that initiated the event. |
| - void OnNavigationEvent(int32_t tab_id, |
| + void OnNavigationEvent(SessionID::id_type tab_id, |
| TransitionType transition, |
| const GURL& url, |
| const std::string& package); |
| @@ -94,7 +95,7 @@ class DataUseTabModel { |
| // Notifies the DataUseTabModel that tab with |tab_id| is closed. Any active |
| // tracking sessions for the tab are terminated, and the tab is marked as |
| // closed. |
| - void OnTabCloseEvent(int32_t tab_id); |
| + void OnTabCloseEvent(SessionID::id_type tab_id); |
| // Notifies the DataUseTabModel that tracking label |label| is removed. Any |
| // active tracking sessions with the label are ended. |
| @@ -107,10 +108,20 @@ class DataUseTabModel { |
| virtual 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 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(SessionID::id_type tab_id); |
| + |
| + // Notifies the observers that an active data usage tracking session ended for |
| + // |tab_id|. Protected for testing. |
| + void NotifyObserversOfTrackingEnding(SessionID::id_type tab_id); |
| private: |
| friend class DataUseTabModelTest; |
| @@ -134,24 +145,19 @@ class DataUseTabModel { |
| FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
| ExpiredActiveTabEntryRemovaltimeHistogram); |
| - typedef base::hash_map<int32_t, TabDataUseEntry> TabEntryMap; |
| + typedef base::hash_map<const TabDataUseObserver*, |
| + const base::WeakPtr<TabDataUseObserver>> ObserverMap; |
| + typedef base::hash_map<SessionID::id_type, TabDataUseEntry> TabEntryMap; |
| // Virtualized for unit test support. |
| virtual base::TimeTicks Now() const; |
| - // 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); |
| + void StartTrackingDataUse(SessionID::id_type tab_id, |
| + const std::string& label); |
| // Ends the current tracking session for tab with id |tab_id|. |
| - void EndTrackingDataUse(int32_t tab_id); |
| + void EndTrackingDataUse(SessionID::id_type tab_id); |
| // Compacts the tab entry map |active_tabs_| by removing expired tab entries. |
| // After removing expired tab entries, if the size of |active_tabs_| exceeds |
| @@ -163,9 +169,10 @@ 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 that receive 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_; |
| @@ -173,10 +180,11 @@ class DataUseTabModel { |
| // Maximum number of tab entries to maintain session information about. |
| const size_t max_tab_entries_; |
| + // |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); |