| 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..8a4ae097166314761a520bf33b01965e26bbb2fd 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"
|
| #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"
|
|
|
| @@ -67,8 +69,24 @@ 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);
|
| +
|
| + // Notifies |this| of starting and ending of data tracking for tab with id
|
| + // |tab_id|.
|
| + void NotifyTrackingStarting(int32_t tab_id);
|
| + void NotifyTrackingEnding(int32_t tab_id);
|
| +
|
| private:
|
| + // Friends for testing.
|
| + friend class DataUseUITabModelTest;
|
| FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
|
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
|
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
|
| +
|
| + class TabObserverOnIOThread;
|
|
|
| // DataUseTrackingEvent indicates the state of a tab.
|
| enum DataUseTrackingEvent {
|
| @@ -79,12 +97,17 @@ class DataUseUITabModel : public KeyedService {
|
| DATA_USE_TRACKING_ENDED,
|
| };
|
|
|
| - typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents;
|
| + typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents;
|
| +
|
| + // Creates TabObserverOnIOThread object and passes |data_use_tab_model| to it
|
| + // on IO thread.
|
| + static scoped_refptr<TabObserverOnIOThread> CreateTabObserverOnIOThread(
|
| + const base::WeakPtr<DataUseTabModel>& data_use_tab_model,
|
| + const base::WeakPtr<DataUseUITabModel>& data_use_ui_tab_model,
|
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
|
|
|
| - // 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 SetTabDataUseObserver(
|
| + scoped_refptr<TabObserverOnIOThread> tab_data_use_observer);
|
|
|
| // 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 +118,32 @@ 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_data_use_observer_| notifies |this| of starting and ending of data
|
| + // tracking. |tab_data_use_observer_| should only be used on the IO thread.
|
| + scoped_refptr<TabObserverOnIOThread> tab_data_use_observer_;
|
| +
|
| // |tab_events_| stores tracking events of multiple tabs.
|
| TabEvents tab_events_;
|
|
|
| - // |io_task_runner_| accesses DataUseTabModel members on IO thread.
|
| + // |io_data_use_tab_model_| is notified by |this| about browser navigations
|
| + // and tab closures on IO thread. |io_data_use_tab_model_| should only be
|
| + // used on IO thread.
|
| + base::WeakPtr<DataUseTabModel> io_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);
|
| };
|
|
|
|
|