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/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | |
18 #include "components/keyed_service/core/keyed_service.h" | 19 #include "components/keyed_service/core/keyed_service.h" |
20 #include "components/sessions/core/session_id.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; |
24 class IOThread; | |
25 | |
26 namespace base { | |
27 class SingleThreadTaskRunner; | |
28 } // namespace base | |
22 | 29 |
23 namespace chrome { | 30 namespace chrome { |
24 | 31 |
25 namespace android { | 32 namespace android { |
26 | 33 |
27 // DataUseUITabModel tracks data use tracking start and end transitions on the | 34 // DataUseUITabModel tracks data use tracking start and end transitions on the |
28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which | 35 // 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, | 36 // 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 | 37 // 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 | 38 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation |
32 // and tab closure events to the DataUseTabModel, and receives tab tracking | 39 // and tab closure events to the DataUseTabModel, and receives tab tracking |
33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI | 40 // transitions (start/end) from the DataUseTabModel, which it conveys to UI |
34 // notification logic. DataUseUITabModel is not thread-safe, and should be | 41 // notification logic. DataUseUITabModel is not thread-safe, and should be |
35 // accessed only on the UI thread. | 42 // accessed only on the UI thread. DataUseUITabModel registers as an observer to |
43 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has | |
44 // started and ended on a tab. DataUseUITabModel is not thread safe, and should | |
45 // only be accessed only on UI thread. | |
36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab | 46 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab |
37 // is removed from the list of tabs. | 47 // is removed from the list of tabs. |
38 class DataUseUITabModel : public KeyedService { | 48 class DataUseUITabModel : public KeyedService, |
49 public DataUseTabModel::TabDataUseObserver { | |
39 public: | 50 public: |
40 explicit DataUseUITabModel( | 51 explicit DataUseUITabModel( |
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | 52 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
42 ~DataUseUITabModel() override; | 53 ~DataUseUITabModel() override; |
43 | 54 |
44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes | 55 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes |
45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that | 56 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that |
46 // are restored when Chromium restarts are not reported. | 57 // are restored when Chromium restarts are not reported. |
47 void ReportBrowserNavigation(const GURL& gurl, | 58 void ReportBrowserNavigation(const GURL& gurl, |
48 ui::PageTransition page_transition, | 59 ui::PageTransition page_transition, |
49 int32_t tab_id) const; | 60 SessionID::id_type tab_id) const; |
50 | 61 |
51 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on | 62 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on |
52 // IO thread. The tab could either have been closed or evicted from the memory | 63 // IO thread. The tab could either have been closed or evicted from the memory |
53 // by Android. | 64 // by Android. |
54 void ReportTabClosure(int32_t tab_id); | 65 void ReportTabClosure(int32_t tab_id); |
sclittle
2015/11/23 23:06:16
nit: I think you forgot to change the type of this
tbansal1
2015/11/24 00:42:02
Done.
| |
55 | 66 |
56 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. | 67 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. |
57 // Includes the |tab_id|, |url|, and |package_name| for the navigation. | 68 // Includes the |tab_id|, |url|, and |package_name| for the navigation. |
58 void ReportCustomTabInitialNavigation(int32_t tab_id, | 69 void ReportCustomTabInitialNavigation(SessionID::id_type tab_id, |
59 const std::string& url, | 70 const std::string& url, |
60 const std::string& package_name); | 71 const std::string& package_name); |
61 | 72 |
62 // Returns true if data use tracking has been started for the tab with id | 73 // 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. | 74 // |tab_id|. Calling this function resets the state of the tab. |
64 bool HasDataUseTrackingStarted(int32_t tab_id); | 75 bool HasDataUseTrackingStarted(SessionID::id_type tab_id); |
65 | 76 |
66 // Returns true if data use tracking has ended for the tab with id |tab_id|. | 77 // 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. | 78 // Calling this function resets the state of the tab. |
68 bool HasDataUseTrackingEnded(int32_t tab_id); | 79 bool HasDataUseTrackingEnded(SessionID::id_type tab_id); |
80 | |
81 // Sets the weak pointer to DataUseTabModel that can be used on IO thread. | |
82 // Should be called at most once. | |
83 void SetIOThread(IOThread* io_thread); | |
69 | 84 |
70 private: | 85 private: |
86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); | |
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); | 87 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
88 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); | |
72 | 89 |
73 // DataUseTrackingEvent indicates the state of a tab. | 90 // DataUseTrackingEvent indicates the state of a tab. |
74 enum DataUseTrackingEvent { | 91 enum DataUseTrackingEvent { |
75 // Indicates that data use tracking has started. | 92 // Indicates that data use tracking has started. |
76 DATA_USE_TRACKING_STARTED, | 93 DATA_USE_TRACKING_STARTED, |
77 | 94 |
78 // Indicates that data use tracking has ended. | 95 // Indicates that data use tracking has ended. |
79 DATA_USE_TRACKING_ENDED, | 96 DATA_USE_TRACKING_ENDED, |
80 }; | 97 }; |
81 | 98 |
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; | 99 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents; |
sclittle
2015/11/23 23:06:16
nit: either consistently use int32_t, or SessionID
tbansal1
2015/11/24 00:42:02
Done.
| |
83 | 100 |
84 // DataUseTabModel::Observer implementation: | 101 // Sets the weak pointer to DataUseTabModel. Also, registers |this| as an |
85 // TODO(tbansal): Add override once DataUseTabModel is checked in. | 102 // observer to the DataUseTabModel::TabDataUseObserver. |
86 void OnTrackingStarted(int32_t tab_id); | 103 void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model); |
87 void OnTrackingEnded(int32_t tab_id); | 104 |
105 // DataUseTabModel::TabDataUseObserver implementation: | |
106 void NotifyTrackingStarting(int32_t tab_id) override; | |
107 void NotifyTrackingEnding(int32_t tab_id) override; | |
88 | 108 |
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no | 109 // 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 | 110 // existing entry for |tab_id|, and returns true. Otherwise, returns false |
91 // without modifying the entry. | 111 // without modifying the entry. |
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 112 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
93 | 113 |
94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and | 114 // Removes event entry for |tab_id|, if the entry is equal to |event|, and |
95 // returns true. Otherwise, returns false without modifying the entry. | 115 // returns true. Otherwise, returns false without modifying the entry. |
96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 116 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); |
97 | 117 |
118 // Converts |page_transition| to DataUseTabModel::TransitionType enum. | |
119 // Returns true if conversion was successful, and updates |transition_type|. | |
120 // Otherwise, returns false, and |transition_type| is not changed. | |
121 // |transition_type| must not be null. | |
122 bool ConvertTransitionType( | |
123 ui::PageTransition page_transition, | |
124 DataUseTabModel::TransitionType* transition_type) const; | |
125 | |
98 // |tab_events_| stores tracking events of multiple tabs. | 126 // |tab_events_| stores tracking events of multiple tabs. |
99 TabEvents tab_events_; | 127 TabEvents tab_events_; |
100 | 128 |
101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. | 129 // |data_use_tab_model_| is notified by |this| about browser navigations |
130 // and tab closures on IO thread. |data_use_tab_model_| should only be | |
131 // used on IO thread. | |
132 base::WeakPtr<DataUseTabModel> data_use_tab_model_; | |
133 | |
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 134 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
103 | 135 |
104 base::ThreadChecker thread_checker_; | 136 base::ThreadChecker thread_checker_; |
105 | 137 |
138 base::WeakPtrFactory<DataUseUITabModel> weak_factory_; | |
139 | |
106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); | 140 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
107 }; | 141 }; |
108 | 142 |
109 } // namespace android | 143 } // namespace android |
110 | 144 |
111 } // namespace chrome | 145 } // namespace chrome |
112 | 146 |
113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 147 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
OLD | NEW |