| 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" |
| 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 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 const std::string& package_name); | 62 const std::string& package_name); |
| 61 | 63 |
| 62 // Returns true if data use tracking has been started for the tab with id | 64 // 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. | 65 // |tab_id|. Calling this function resets the state of the tab. |
| 64 bool HasDataUseTrackingStarted(int32_t tab_id); | 66 bool HasDataUseTrackingStarted(int32_t tab_id); |
| 65 | 67 |
| 66 // Returns true if data use tracking has ended for the tab with id |tab_id|. | 68 // 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. | 69 // Calling this function resets the state of the tab. |
| 68 bool HasDataUseTrackingEnded(int32_t tab_id); | 70 bool HasDataUseTrackingEnded(int32_t tab_id); |
| 69 | 71 |
| 72 // Sets the weak pointer to DataUseTabModel that can be used on IO thread. |
| 73 // Should be called at most once. |
| 74 void SetIODataUseTabModel( |
| 75 const base::WeakPtr<DataUseTabModel>& data_use_tab_model); |
| 76 |
| 77 // Notifies |this| of starting and ending of data tracking for tab with id |
| 78 // |tab_id|. |
| 79 void NotifyTrackingStarting(int32_t tab_id); |
| 80 void NotifyTrackingEnding(int32_t tab_id); |
| 81 |
| 70 private: | 82 private: |
| 83 // Friends for testing. |
| 84 friend class DataUseUITabModelTest; |
| 71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); | 85 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
| 86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); |
| 87 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); |
| 88 |
| 89 class TabObserverOnIOThread; |
| 72 | 90 |
| 73 // DataUseTrackingEvent indicates the state of a tab. | 91 // DataUseTrackingEvent indicates the state of a tab. |
| 74 enum DataUseTrackingEvent { | 92 enum DataUseTrackingEvent { |
| 75 // Indicates that data use tracking has started. | 93 // Indicates that data use tracking has started. |
| 76 DATA_USE_TRACKING_STARTED, | 94 DATA_USE_TRACKING_STARTED, |
| 77 | 95 |
| 78 // Indicates that data use tracking has ended. | 96 // Indicates that data use tracking has ended. |
| 79 DATA_USE_TRACKING_ENDED, | 97 DATA_USE_TRACKING_ENDED, |
| 80 }; | 98 }; |
| 81 | 99 |
| 82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; | 100 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents; |
| 83 | 101 |
| 84 // DataUseTabModel::Observer implementation: | 102 // Creates TabObserverOnIOThread object and passes |data_use_tab_model| to it |
| 85 // TODO(tbansal): Add override once DataUseTabModel is checked in. | 103 // on IO thread. |
| 86 void OnTrackingStarted(int32_t tab_id); | 104 static scoped_refptr<TabObserverOnIOThread> CreateTabObserverOnIOThread( |
| 87 void OnTrackingEnded(int32_t tab_id); | 105 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, |
| 106 const base::WeakPtr<DataUseUITabModel>& data_use_ui_tab_model, |
| 107 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
| 108 |
| 109 void SetTabDataUseObserver( |
| 110 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer); |
| 88 | 111 |
| 89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no | 112 // 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 | 113 // existing entry for |tab_id|, and returns true. Otherwise, returns false |
| 91 // without modifying the entry. | 114 // without modifying the entry. |
| 92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 115 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
| 93 | 116 |
| 94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and | 117 // Removes event entry for |tab_id|, if the entry is equal to |event|, and |
| 95 // returns true. Otherwise, returns false without modifying the entry. | 118 // returns true. Otherwise, returns false without modifying the entry. |
| 96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 119 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
| 97 | 120 |
| 121 // Converts |page_transition| to DataUseTabModel::TransitionType enum. |
| 122 // Returns true if conversion was successful, and updates |transition_type|. |
| 123 // Otherwise, returns false, and |transition_type| is not changed. |
| 124 // |transition_type| must not be null. |
| 125 bool ConvertTransitionType( |
| 126 ui::PageTransition page_transition, |
| 127 DataUseTabModel::TransitionType* transition_type) const; |
| 128 |
| 129 // |tab_data_use_observer_| notifies |this| of starting and ending of data |
| 130 // tracking. |tab_data_use_observer_| should only be used on the IO thread. |
| 131 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer_; |
| 132 |
| 98 // |tab_events_| stores tracking events of multiple tabs. | 133 // |tab_events_| stores tracking events of multiple tabs. |
| 99 TabEvents tab_events_; | 134 TabEvents tab_events_; |
| 100 | 135 |
| 101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. | 136 // |io_data_use_tab_model_| is notified by |this| about browser navigations |
| 137 // and tab closures on IO thread. |io_data_use_tab_model_| should only be |
| 138 // used on IO thread. |
| 139 base::WeakPtr<DataUseTabModel> io_data_use_tab_model_; |
| 140 |
| 102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 141 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 103 | 142 |
| 104 base::ThreadChecker thread_checker_; | 143 base::ThreadChecker thread_checker_; |
| 105 | 144 |
| 145 base::WeakPtrFactory<DataUseUITabModel> weak_factory_; |
| 146 |
| 106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); | 147 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
| 107 }; | 148 }; |
| 108 | 149 |
| 109 } // namespace android | 150 } // namespace android |
| 110 | 151 |
| 111 } // namespace chrome | 152 } // namespace chrome |
| 112 | 153 |
| 113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 154 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
| OLD | NEW |