Chromium Code Reviews| Index: chrome/browser/android/data_usage/data_use_ui_tab_model.h |
| diff --git a/chrome/browser/android/data_usage/data_use_ui_tab_model.h b/chrome/browser/android/data_usage/data_use_ui_tab_model.h |
| index a8dc492f521ad5e92f65f86ce1b2ddd2246189d0..0b3087774232b6f46762e3e843d3c445885b3c01 100644 |
| --- a/chrome/browser/android/data_usage/data_use_ui_tab_model.h |
| +++ b/chrome/browser/android/data_usage/data_use_ui_tab_model.h |
| @@ -8,13 +8,15 @@ |
| #include <stdint.h> |
| #include <string> |
| -#include <unordered_map> |
| +#include "base/containers/hash_tables.h" |
| #include "base/gtest_prod_util.h" |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| +#include "base/single_thread_task_runner.h" |
|
sclittle
2015/11/20 22:50:54
Just forward declare it here in the header file, a
tbansal1
2015/11/23 17:52:22
Done.
|
| #include "base/threading/thread_checker.h" |
| +#include "chrome/browser/android/data_usage/data_use_tab_model.h" |
| #include "components/keyed_service/core/keyed_service.h" |
| #include "ui/base/page_transition_types.h" |
| @@ -32,10 +34,14 @@ namespace android { |
| // and tab closure events to the DataUseTabModel, and receives tab tracking |
| // transitions (start/end) from the DataUseTabModel, which it conveys to UI |
| // notification logic. DataUseUITabModel is not thread-safe, and should be |
| -// accessed only on the UI thread. |
| +// accessed only on the UI thread. DataUseUITabModel registers as an observer to |
| +// DataUseTabModel::TabDataUseObserver, and gets notified when tracking has |
| +// started and ended on a tab. TabObserverOnIOThread is not thread safe, |
| +// and should only be accessed only on IO thread. |
| // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab |
| // is removed from the list of tabs. |
| -class DataUseUITabModel : public KeyedService { |
| +class DataUseUITabModel : public KeyedService, |
| + public DataUseTabModel::TabDataUseObserver { |
| public: |
| explicit DataUseUITabModel( |
| scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
| @@ -67,8 +73,15 @@ class DataUseUITabModel : public KeyedService { |
| // Calling this function resets the state of the tab. |
| bool HasDataUseTrackingEnded(int32_t tab_id); |
| + // Sets the weak pointer to DataUseTabModel that can be used on IO thread. |
| + // Should be called at most once. |
| + void SetIODataUseTabModel( |
| + const base::WeakPtr<DataUseTabModel>& data_use_tab_model); |
| + |
| private: |
| FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); |
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); |
| // DataUseTrackingEvent indicates the state of a tab. |
| enum DataUseTrackingEvent { |
| @@ -79,12 +92,11 @@ class DataUseUITabModel : public KeyedService { |
| DATA_USE_TRACKING_ENDED, |
| }; |
| - typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; |
| + typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents; |
| // DataUseTabModel::Observer implementation: |
| - // TODO(tbansal): Add override once DataUseTabModel is checked in. |
| - void OnTrackingStarted(int32_t tab_id); |
| - void OnTrackingEnded(int32_t tab_id); |
| + void NotifyTrackingStarting(int32_t tab_id) override; |
| + void NotifyTrackingEnding(int32_t tab_id) override; |
| // Creates |event| for tab with id |tab_id| and value |event|, if there is no |
| // existing entry for |tab_id|, and returns true. Otherwise, returns false |
| @@ -95,14 +107,28 @@ class DataUseUITabModel : public KeyedService { |
| // returns true. Otherwise, returns false without modifying the entry. |
| bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
| + // Converts |page_transition| to DataUseTabModel::TransitionType enum. |
| + // Returns true if conversion was successful, and updates |transition_type|. |
| + // Otherwise, returns false, and |transition_type| is not changed. |
| + // |transition_type| must not be null. |
| + bool ConvertTransitionType( |
| + ui::PageTransition page_transition, |
| + DataUseTabModel::TransitionType* transition_type) const; |
| + |
| // |tab_events_| stores tracking events of multiple tabs. |
| TabEvents tab_events_; |
| - // |io_task_runner_| accesses DataUseTabModel members on IO thread. |
| + // |data_use_tab_model_| is notified by |this| about browser navigations |
| + // and tab closures on IO thread. |data_use_tab_model_| should only be |
| + // used on IO thread. |
| + base::WeakPtr<DataUseTabModel> data_use_tab_model_; |
| + |
| scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| base::ThreadChecker thread_checker_; |
| + base::WeakPtrFactory<DataUseUITabModel> weak_factory_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
| }; |