| 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..7ae101629d17220759ad8b2426e49d38b534db5f 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,17 +8,23 @@
|
| #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/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"
|
|
|
| class GURL;
|
| +class IOThread;
|
| +
|
| +namespace base {
|
| +class SingleThreadTaskRunner;
|
| +} // namespace base
|
|
|
| namespace chrome {
|
|
|
| @@ -32,10 +38,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. DataUseUITabModel is not thread safe, and should
|
| +// only be accessed only on UI 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 +77,14 @@ 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 SetIOThread(IOThread* io_thread);
|
| +
|
| private:
|
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
|
| FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
|
| + FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
|
|
|
| // DataUseTrackingEvent indicates the state of a tab.
|
| enum DataUseTrackingEvent {
|
| @@ -79,12 +95,15 @@ class DataUseUITabModel : public KeyedService {
|
| DATA_USE_TRACKING_ENDED,
|
| };
|
|
|
| - typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents;
|
| + typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents;
|
| +
|
| + // Sets the weak pointer to DataUseTabModel. Also, registers |this| as an
|
| + // observer to the DataUseTabModel::TabDataUseObserver.
|
| + void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model);
|
|
|
| - // DataUseTabModel::Observer implementation:
|
| - // TODO(tbansal): Add override once DataUseTabModel is checked in.
|
| - void OnTrackingStarted(int32_t tab_id);
|
| - void OnTrackingEnded(int32_t tab_id);
|
| + // DataUseTabModel::TabDataUseObserver implementation:
|
| + 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 +114,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);
|
| };
|
|
|
|
|