Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
| 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 #include <unordered_map> | |
| 12 | 11 |
| 12 #include "base/containers/hash_tables.h" | |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #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.
| |
| 17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | |
| 18 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 19 #include "ui/base/page_transition_types.h" | 21 #include "ui/base/page_transition_types.h" |
| 20 | 22 |
| 21 class GURL; | 23 class GURL; |
| 22 | 24 |
| 23 namespace chrome { | 25 namespace chrome { |
| 24 | 26 |
| 25 namespace android { | 27 namespace android { |
| 26 | 28 |
| 27 // DataUseUITabModel tracks data use tracking start and end transitions on the | 29 // DataUseUITabModel tracks data use tracking start and end transitions on the |
| 28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which | 30 // browser's tabs. It serves as a bridge between the DataUseTabModel, which |
| 29 // lives on the IO thread, browser navigation events and tab closure events, | 31 // lives on the IO thread, browser navigation events and tab closure events, |
| 30 // which are generated on the UI thread, and UI elements that appear when data | 32 // which are generated on the UI thread, and UI elements that appear when data |
| 31 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation | 33 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation |
| 32 // and tab closure events to the DataUseTabModel, and receives tab tracking | 34 // and tab closure events to the DataUseTabModel, and receives tab tracking |
| 33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI | 35 // transitions (start/end) from the DataUseTabModel, which it conveys to UI |
| 34 // notification logic. DataUseUITabModel is not thread-safe, and should be | 36 // notification logic. DataUseUITabModel is not thread-safe, and should be |
| 35 // accessed only on the UI thread. | 37 // accessed only on the UI thread. DataUseUITabModel registers as an observer to |
| 38 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has | |
| 39 // started and ended on a tab. TabObserverOnIOThread is not thread safe, | |
| 40 // and should only be accessed only on IO thread. | |
| 36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab | 41 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab |
| 37 // is removed from the list of tabs. | 42 // is removed from the list of tabs. |
| 38 class DataUseUITabModel : public KeyedService { | 43 class DataUseUITabModel : public KeyedService, |
| 44 public DataUseTabModel::TabDataUseObserver { | |
| 39 public: | 45 public: |
| 40 explicit DataUseUITabModel( | 46 explicit DataUseUITabModel( |
| 41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | 47 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
| 42 ~DataUseUITabModel() override; | 48 ~DataUseUITabModel() override; |
| 43 | 49 |
| 44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes | 50 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes |
| 45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that | 51 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that |
| 46 // are restored when Chromium restarts are not reported. | 52 // are restored when Chromium restarts are not reported. |
| 47 void ReportBrowserNavigation(const GURL& gurl, | 53 void ReportBrowserNavigation(const GURL& gurl, |
| 48 ui::PageTransition page_transition, | 54 ui::PageTransition page_transition, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 60 const std::string& package_name); | 66 const std::string& package_name); |
| 61 | 67 |
| 62 // Returns true if data use tracking has been started for the tab with id | 68 // Returns true if data use tracking has been started for the tab with id |
| 63 // |tab_id|. Calling this function resets the state of the tab. | 69 // |tab_id|. Calling this function resets the state of the tab. |
| 64 bool HasDataUseTrackingStarted(int32_t tab_id); | 70 bool HasDataUseTrackingStarted(int32_t tab_id); |
| 65 | 71 |
| 66 // Returns true if data use tracking has ended for the tab with id |tab_id|. | 72 // Returns true if data use tracking has ended for the tab with id |tab_id|. |
| 67 // Calling this function resets the state of the tab. | 73 // Calling this function resets the state of the tab. |
| 68 bool HasDataUseTrackingEnded(int32_t tab_id); | 74 bool HasDataUseTrackingEnded(int32_t tab_id); |
| 69 | 75 |
| 76 // Sets the weak pointer to DataUseTabModel that can be used on IO thread. | |
| 77 // Should be called at most once. | |
| 78 void SetIODataUseTabModel( | |
| 79 const base::WeakPtr<DataUseTabModel>& data_use_tab_model); | |
| 80 | |
| 70 private: | 81 private: |
| 71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); | 82 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
| 83 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); | |
| 84 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); | |
| 72 | 85 |
| 73 // DataUseTrackingEvent indicates the state of a tab. | 86 // DataUseTrackingEvent indicates the state of a tab. |
| 74 enum DataUseTrackingEvent { | 87 enum DataUseTrackingEvent { |
| 75 // Indicates that data use tracking has started. | 88 // Indicates that data use tracking has started. |
| 76 DATA_USE_TRACKING_STARTED, | 89 DATA_USE_TRACKING_STARTED, |
| 77 | 90 |
| 78 // Indicates that data use tracking has ended. | 91 // Indicates that data use tracking has ended. |
| 79 DATA_USE_TRACKING_ENDED, | 92 DATA_USE_TRACKING_ENDED, |
| 80 }; | 93 }; |
| 81 | 94 |
| 82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; | 95 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents; |
| 83 | 96 |
| 84 // DataUseTabModel::Observer implementation: | 97 // DataUseTabModel::Observer implementation: |
| 85 // TODO(tbansal): Add override once DataUseTabModel is checked in. | 98 void NotifyTrackingStarting(int32_t tab_id) override; |
| 86 void OnTrackingStarted(int32_t tab_id); | 99 void NotifyTrackingEnding(int32_t tab_id) override; |
| 87 void OnTrackingEnded(int32_t tab_id); | |
| 88 | 100 |
| 89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no | 101 // Creates |event| for tab with id |tab_id| and value |event|, if there is no |
| 90 // existing entry for |tab_id|, and returns true. Otherwise, returns false | 102 // existing entry for |tab_id|, and returns true. Otherwise, returns false |
| 91 // without modifying the entry. | 103 // without modifying the entry. |
| 92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 104 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
| 93 | 105 |
| 94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and | 106 // Removes event entry for |tab_id|, if the entry is equal to |event|, and |
| 95 // returns true. Otherwise, returns false without modifying the entry. | 107 // returns true. Otherwise, returns false without modifying the entry. |
| 96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 108 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
| 97 | 109 |
| 110 // Converts |page_transition| to DataUseTabModel::TransitionType enum. | |
| 111 // Returns true if conversion was successful, and updates |transition_type|. | |
| 112 // Otherwise, returns false, and |transition_type| is not changed. | |
| 113 // |transition_type| must not be null. | |
| 114 bool ConvertTransitionType( | |
| 115 ui::PageTransition page_transition, | |
| 116 DataUseTabModel::TransitionType* transition_type) const; | |
| 117 | |
| 98 // |tab_events_| stores tracking events of multiple tabs. | 118 // |tab_events_| stores tracking events of multiple tabs. |
| 99 TabEvents tab_events_; | 119 TabEvents tab_events_; |
| 100 | 120 |
| 101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. | 121 // |data_use_tab_model_| is notified by |this| about browser navigations |
| 122 // and tab closures on IO thread. |data_use_tab_model_| should only be | |
| 123 // used on IO thread. | |
| 124 base::WeakPtr<DataUseTabModel> data_use_tab_model_; | |
| 125 | |
| 102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 126 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 103 | 127 |
| 104 base::ThreadChecker thread_checker_; | 128 base::ThreadChecker thread_checker_; |
| 105 | 129 |
| 130 base::WeakPtrFactory<DataUseUITabModel> weak_factory_; | |
| 131 | |
| 106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); | 132 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
| 107 }; | 133 }; |
| 108 | 134 |
| 109 } // namespace android | 135 } // namespace android |
| 110 | 136 |
| 111 } // namespace chrome | 137 } // namespace chrome |
| 112 | 138 |
| 113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 139 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
| OLD | NEW |