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_TAB_DATA_USE_ENTRY_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_TAB_DATA_USE_ENTRY_H_ |
6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_TAB_DATA_USE_ENTRY_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_TAB_DATA_USE_ENTRY_H_ |
7 | 7 |
8 #include <deque> | |
9 #include <string> | 8 #include <string> |
| 9 #include <vector> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 | 14 |
15 namespace chrome { | 15 namespace chrome { |
16 | 16 |
17 namespace android { | 17 namespace android { |
18 | 18 |
19 class DataUseTabModel; | 19 class DataUseTabModel; |
20 | 20 |
21 // TabDataUseTrackingSession maintains the information about a single tracking | 21 // TabDataUseTrackingSession maintains the information about a single tracking |
22 // session within a browser tab. | 22 // session within a browser tab. |
23 struct TabDataUseTrackingSession { | 23 struct TabDataUseTrackingSession { |
24 TabDataUseTrackingSession(const std::string& label, | 24 TabDataUseTrackingSession(const std::string& label, |
25 const base::TimeTicks& start_time) | 25 const base::TimeTicks& start_time) |
26 : label(label), start_time(start_time) {} | 26 : label(label), start_time(start_time) {} |
27 | 27 |
28 // Tracking label to be associated with the data usage of this session. | 28 // Tracking label to be associated with the data usage of this session. |
29 const std::string label; | 29 std::string label; |
30 | 30 |
31 // Time the data use tracking session started. | 31 // Time the data use tracking session started. |
32 const base::TimeTicks start_time; | 32 base::TimeTicks start_time; |
33 | 33 |
34 // Time the data use tracking session ended. |end_time| will be null if the | 34 // Time the data use tracking session ended. |end_time| will be null if the |
35 // tracking session is currently active. | 35 // tracking session is currently active. |
36 base::TimeTicks end_time; | 36 base::TimeTicks end_time; |
37 }; | 37 }; |
38 | 38 |
39 // TabDataUseEntry contains the history of the disjoint tracking sessions for a | 39 // TabDataUseEntry contains the history of the disjoint tracking sessions for a |
40 // single browser tab. | 40 // single browser tab. |
41 class TabDataUseEntry { | 41 class TabDataUseEntry { |
42 public: | 42 public: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 bool IsTrackingDataUse() const; | 74 bool IsTrackingDataUse() const; |
75 | 75 |
76 // Returns true if the tab has expired. A closed tab entry expires | 76 // Returns true if the tab has expired. A closed tab entry expires |
77 // |kClosedTabExpirationDurationSeconds| seconds after it was closed. An open | 77 // |kClosedTabExpirationDurationSeconds| seconds after it was closed. An open |
78 // tab entry expires |kOpenTabExpirationDurationSeconds| seconds after the | 78 // tab entry expires |kOpenTabExpirationDurationSeconds| seconds after the |
79 // most recent tracking session start or end event. | 79 // most recent tracking session start or end event. |
80 bool IsExpired() const; | 80 bool IsExpired() const; |
81 | 81 |
82 // Returns the latest time a tracking session was started or ended. Returned | 82 // Returns the latest time a tracking session was started or ended. Returned |
83 // time will be null if no tracking session was ever started or ended. | 83 // time will be null if no tracking session was ever started or ended. |
84 const base::TimeTicks GetLatestStartOrEndTime() const; | 84 base::TimeTicks GetLatestStartOrEndTime() const; |
85 | 85 |
86 // Returns the tracking label for the active tracking session. Empty string is | 86 // Returns the tracking label for the active tracking session. Empty string is |
87 // returned if tracking session is not active. | 87 // returned if tracking session is not active. |
88 const std::string GetActiveTrackingSessionLabel() const; | 88 std::string GetActiveTrackingSessionLabel() const; |
89 | 89 |
90 bool is_custom_tab_package_match() const { | 90 bool is_custom_tab_package_match() const { |
91 return is_custom_tab_package_match_; | 91 return is_custom_tab_package_match_; |
92 } | 92 } |
93 | 93 |
94 void set_custom_tab_package_match(bool is_custom_tab_package_match); | 94 void set_custom_tab_package_match(bool is_custom_tab_package_match); |
95 | 95 |
96 private: | 96 private: |
97 friend class TabDataUseEntryTest; | 97 friend class TabDataUseEntryTest; |
98 FRIEND_TEST_ALL_PREFIXES(TabDataUseEntryTest, MultipleTabSessionCloseEvent); | 98 FRIEND_TEST_ALL_PREFIXES(TabDataUseEntryTest, MultipleTabSessionCloseEvent); |
99 FRIEND_TEST_ALL_PREFIXES(TabDataUseEntryTest, SingleTabSessionCloseEvent); | 99 FRIEND_TEST_ALL_PREFIXES(TabDataUseEntryTest, SingleTabSessionCloseEvent); |
100 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 100 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
101 ExpiredInactiveTabEntryRemovaltimeHistogram); | 101 ExpiredInactiveTabEntryRemovaltimeHistogram); |
102 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); | 102 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); |
103 | 103 |
104 typedef std::deque<TabDataUseTrackingSession> TabSessions; | 104 // This is a std::vector instead of a std::deque because std::deque is |
| 105 // inefficient for small numbers of elements (see crbug/674287). By default, |
| 106 // at most 5 sessions are tracked at a time per tab, so erasing from the front |
| 107 // of the vector is cheap anyways. |
| 108 typedef std::vector<TabDataUseTrackingSession> TabSessions; |
105 | 109 |
106 // Compacts the history of tracking sessions by removing oldest sessions to | 110 // Compacts the history of tracking sessions by removing oldest sessions to |
107 // keep the size of |sessions_| within |kMaxSessionsPerTab| entries. | 111 // keep the size of |sessions_| within |kMaxSessionsPerTab| entries. |
108 void CompactSessionHistory(); | 112 void CompactSessionHistory(); |
109 | 113 |
110 // Contains the history of sessions in chronological order. Oldest sessions | 114 // Contains the history of sessions in chronological order. Oldest sessions |
111 // will be at the front of the queue, and new sessions will get added to the | 115 // will be at the front of the queue, and new sessions will get added to the |
112 // end of the queue. | 116 // end of the queue. |
113 TabSessions sessions_; | 117 TabSessions sessions_; |
114 | 118 |
115 // Indicates the time the tab was closed. |tab_close_time_| will be null if | 119 // Indicates the time the tab was closed. |tab_close_time_| will be null if |
116 // the tab is still open. | 120 // the tab is still open. |
117 base::TimeTicks tab_close_time_; | 121 base::TimeTicks tab_close_time_; |
118 | 122 |
119 // True if tracking was started in a custom tab due to package name match. | 123 // True if tracking was started in a custom tab due to package name match. |
120 bool is_custom_tab_package_match_; | 124 bool is_custom_tab_package_match_; |
121 | 125 |
122 // Pointer to the DataUseTabModel that owns |this|. | 126 // Pointer to the DataUseTabModel that owns |this|. |
123 const DataUseTabModel* tab_model_; | 127 const DataUseTabModel* tab_model_; |
124 }; | 128 }; |
125 | 129 |
126 } // namespace android | 130 } // namespace android |
127 | 131 |
128 } // namespace chrome | 132 } // namespace chrome |
129 | 133 |
130 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_TAB_DATA_USE_ENTRY_H_ | 134 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_TAB_DATA_USE_ENTRY_H_ |
OLD | NEW |