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_TAB_MODEL_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ |
6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ |
7 | 7 |
8 #include <stdint.h> | |
9 | |
10 #include <string> | 8 #include <string> |
11 | 9 |
12 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
13 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
14 #include "base/macros.h" | 12 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list_threadsafe.h" | |
18 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
19 #include "base/time/time.h" | 16 #include "base/time/time.h" |
20 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" | 17 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
21 #include "components/data_usage/core/data_use.h" | 18 #include "components/sessions/core/session_id.h" |
22 #include "url/gurl.h" | 19 #include "url/gurl.h" |
23 | 20 |
| 21 namespace base { |
| 22 class SingleThreadTaskRunner; |
| 23 } |
| 24 |
| 25 namespace data_usage { |
| 26 struct DataUse; |
| 27 } |
| 28 |
24 namespace chrome { | 29 namespace chrome { |
25 | 30 |
26 namespace android { | 31 namespace android { |
27 | 32 |
28 class ExternalDataUseObserver; | 33 class ExternalDataUseObserver; |
29 | 34 |
30 // Models tracking and labeling of data usage within each Tab. Within each tab, | 35 // Models tracking and labeling of data usage within each Tab. Within each tab, |
31 // the model tracks the data use of a sequence of navigations in a "tracking | 36 // the model tracks the data use of a sequence of navigations in a "tracking |
32 // session" beginning with an entry event and ending with an exit event. | 37 // session" beginning with an entry event and ending with an exit event. |
33 // Typically, these events are navigations matching a URL pattern, or various | 38 // Typically, these events are navigations matching a URL pattern, or various |
34 // types of browser-initiated navigations. A single tab may have several | 39 // types of browser-initiated navigations. A single tab may have several |
35 // disjoint "tracking sessions" depending on the sequence of entry and exit | 40 // disjoint "tracking sessions" depending on the sequence of entry and exit |
36 // events that took place. | 41 // events that took place. |
37 class DataUseTabModel { | 42 class DataUseTabModel { |
38 public: | 43 public: |
39 // TransitionType enumerates the types of possible browser navigation events | 44 // TransitionType enumerates the types of possible browser navigation events |
40 // and transitions. | 45 // and transitions. |
41 enum TransitionType { | 46 enum TransitionType { |
42 // Navigation from the omnibox to the SRP. | 47 // Navigation from the omnibox to the SRP. |
43 TRANSITION_OMNIBOX_SEARCH, | 48 TRANSITION_OMNIBOX_SEARCH, |
44 | 49 |
45 // Navigation from external apps such as AGSA app. | 50 // Navigation from external apps such as AGSA app. |
46 // TODO(rajendrant): Remove this if not needed. | 51 // TODO(rajendrant): Remove this if not needed. |
47 TRANSITION_FROM_EXTERNAL_APP, | 52 TRANSITION_FROM_EXTERNAL_APP, |
48 | 53 |
49 // Navigating to another app. | |
50 TRANSITION_TO_EXTERNAL_APP, | |
51 | |
52 // Navigation from NavSuggest below omnibox. | |
53 TRANSITION_FROM_NAVSUGGEST, | |
54 | |
55 // Navigation from the omnibox when typing a URL. | 54 // Navigation from the omnibox when typing a URL. |
56 TRANSITION_OMNIBOX_NAVIGATION, | 55 TRANSITION_OMNIBOX_NAVIGATION, |
57 | 56 |
58 // Navigation from a bookmark. | 57 // Navigation from a bookmark. |
59 TRANSITION_BOOKMARK, | 58 TRANSITION_BOOKMARK, |
60 | 59 |
61 // Navigating from history. | 60 // Navigating from history. |
62 TRANSITION_HISTORY_ITEM, | 61 TRANSITION_HISTORY_ITEM, |
63 }; | 62 }; |
64 | 63 |
65 // TabDataUseObserver provides the interface for getting notifications from | 64 // TabDataUseObserver provides the interface for getting notifications from |
66 // the DataUseTabModel. | 65 // the DataUseTabModel. TabDataUseObserver is called back on UI thread. |
67 class TabDataUseObserver { | 66 class TabDataUseObserver { |
68 public: | 67 public: |
69 virtual ~TabDataUseObserver() {} | 68 virtual ~TabDataUseObserver() {} |
70 | 69 |
71 // Notification callback when tab tracking sessions are started and ended. | 70 // Notification callback when tab tracking sessions are started and ended. |
72 // The callback will be received on the same thread AddObserver was called | 71 // The callback will be received on the same thread AddObserver was called |
73 // from. | 72 // from. |
74 virtual void NotifyTrackingStarting(int32_t tab_id) = 0; | 73 virtual void NotifyTrackingStarting(SessionID::id_type tab_id) = 0; |
75 virtual void NotifyTrackingEnding(int32_t tab_id) = 0; | 74 virtual void NotifyTrackingEnding(SessionID::id_type tab_id) = 0; |
76 }; | 75 }; |
77 | 76 |
78 DataUseTabModel(const ExternalDataUseObserver* data_use_observer, | 77 DataUseTabModel(const ExternalDataUseObserver* data_use_observer, |
79 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 78 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
80 | 79 |
81 virtual ~DataUseTabModel(); | 80 virtual ~DataUseTabModel(); |
82 | 81 |
83 base::WeakPtr<DataUseTabModel> GetWeakPtr(); | 82 base::WeakPtr<DataUseTabModel> GetWeakPtr(); |
84 | 83 |
85 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source | 84 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source |
86 // tab of the generated event, |transition| indicates the type of the UI | 85 // tab of the generated event, |transition| indicates the type of the UI |
87 // event/transition, |url| is the URL in the source tab, |package| indicates | 86 // event/transition, |url| is the URL in the source tab, |package| indicates |
88 // the android package name of external application that initiated the event. | 87 // the android package name of external application that initiated the event. |
89 void OnNavigationEvent(int32_t tab_id, | 88 void OnNavigationEvent(SessionID::id_type tab_id, |
90 TransitionType transition, | 89 TransitionType transition, |
91 const GURL& url, | 90 const GURL& url, |
92 const std::string& package); | 91 const std::string& package); |
93 | 92 |
94 // Notifies the DataUseTabModel that tab with |tab_id| is closed. Any active | 93 // Notifies the DataUseTabModel that tab with |tab_id| is closed. Any active |
95 // tracking sessions for the tab are terminated, and the tab is marked as | 94 // tracking sessions for the tab are terminated, and the tab is marked as |
96 // closed. | 95 // closed. |
97 void OnTabCloseEvent(int32_t tab_id); | 96 void OnTabCloseEvent(SessionID::id_type tab_id); |
98 | 97 |
99 // Notifies the DataUseTabModel that tracking label |label| is removed. Any | 98 // Notifies the DataUseTabModel that tracking label |label| is removed. Any |
100 // active tracking sessions with the label are ended. | 99 // active tracking sessions with the label are ended. |
101 virtual void OnTrackingLabelRemoved(std::string label); | 100 virtual void OnTrackingLabelRemoved(std::string label); |
102 | 101 |
103 // Gets the label for the |data_use| object. |output_label| must not be null. | 102 // Gets the label for the |data_use| object. |output_label| must not be null. |
104 // If a tab tracking session is found that was active at the time of request | 103 // If a tab tracking session is found that was active at the time of request |
105 // start of |data_use|, returns true and |output_label| is populated with its | 104 // start of |data_use|, returns true and |output_label| is populated with its |
106 // label. Otherwise returns false and |output_label| is set to empty string. | 105 // label. Otherwise returns false and |output_label| is set to empty string. |
107 virtual bool GetLabelForDataUse(const data_usage::DataUse& data_use, | 106 virtual bool GetLabelForDataUse(const data_usage::DataUse& data_use, |
108 std::string* output_label) const; | 107 std::string* output_label) const; |
109 | 108 |
110 // Adds or removes observers from the observer list. These functions are | 109 // Adds or removes observers from the observer list. Must be called on IO |
111 // thread-safe and can be called from any thread. | 110 // thread. |weak_ptr_observer| is notified on UI thread. |
112 void AddObserver(TabDataUseObserver* observer); | 111 void AddObserver(const TabDataUseObserver* observer, |
113 void RemoveObserver(TabDataUseObserver* observer); | 112 const base::WeakPtr<TabDataUseObserver> weak_ptr_observer); |
| 113 void RemoveObserver(const TabDataUseObserver* observer); |
| 114 |
| 115 protected: |
| 116 // Notifies the observers that a data usage tracking session started for |
| 117 // |tab_id|. Protected for testing. |
| 118 void NotifyObserversOfTrackingStarting(SessionID::id_type tab_id); |
| 119 |
| 120 // Notifies the observers that an active data usage tracking session ended for |
| 121 // |tab_id|. Protected for testing. |
| 122 void NotifyObserversOfTrackingEnding(SessionID::id_type tab_id); |
114 | 123 |
115 private: | 124 private: |
116 friend class DataUseTabModelTest; | 125 friend class DataUseTabModelTest; |
117 friend class MockTabDataUseEntryTest; | 126 friend class MockTabDataUseEntryTest; |
118 friend class TestDataUseTabModel; | 127 friend class TestDataUseTabModel; |
119 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking); | 128 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking); |
120 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking); | 129 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking); |
121 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); | 130 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); |
122 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverNotNotifiedAfterRemove); | 131 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverNotNotifiedAfterRemove); |
123 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 132 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
124 MultipleObserverMultipleStartEndEvents); | 133 MultipleObserverMultipleStartEndEvents); |
125 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); | 134 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); |
126 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking); | 135 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking); |
127 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, OnTrackingLabelRemoved); | 136 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, OnTrackingLabelRemoved); |
128 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 137 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
129 CompactTabEntriesWithinMaxLimit); | 138 CompactTabEntriesWithinMaxLimit); |
130 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 139 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
131 UnexpiredTabEntryRemovaltimeHistogram); | 140 UnexpiredTabEntryRemovaltimeHistogram); |
132 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 141 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
133 ExpiredInactiveTabEntryRemovaltimeHistogram); | 142 ExpiredInactiveTabEntryRemovaltimeHistogram); |
134 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 143 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
135 ExpiredActiveTabEntryRemovaltimeHistogram); | 144 ExpiredActiveTabEntryRemovaltimeHistogram); |
136 | 145 |
137 typedef base::hash_map<int32_t, TabDataUseEntry> TabEntryMap; | 146 typedef base::hash_map<const TabDataUseObserver*, |
| 147 const base::WeakPtr<TabDataUseObserver>> ObserverMap; |
| 148 typedef base::hash_map<SessionID::id_type, TabDataUseEntry> TabEntryMap; |
138 | 149 |
139 // Virtualized for unit test support. | 150 // Virtualized for unit test support. |
140 virtual base::TimeTicks Now() const; | 151 virtual base::TimeTicks Now() const; |
141 | 152 |
142 // Notifies the observers that a data usage tracking session started for | |
143 // |tab_id|. | |
144 void NotifyObserversOfTrackingStarting(int32_t tab_id); | |
145 | |
146 // Notifies the observers that an active data usage tracking session ended for | |
147 // |tab_id|. | |
148 void NotifyObserversOfTrackingEnding(int32_t tab_id); | |
149 | |
150 // Initiates a new tracking session with the |label| for tab with id |tab_id|. | 153 // Initiates a new tracking session with the |label| for tab with id |tab_id|. |
151 void StartTrackingDataUse(int32_t tab_id, const std::string& label); | 154 void StartTrackingDataUse(SessionID::id_type tab_id, |
| 155 const std::string& label); |
152 | 156 |
153 // Ends the current tracking session for tab with id |tab_id|. | 157 // Ends the current tracking session for tab with id |tab_id|. |
154 void EndTrackingDataUse(int32_t tab_id); | 158 void EndTrackingDataUse(SessionID::id_type tab_id); |
155 | 159 |
156 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. | 160 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. |
157 // After removing expired tab entries, if the size of |active_tabs_| exceeds | 161 // After removing expired tab entries, if the size of |active_tabs_| exceeds |
158 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its | 162 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its |
159 // size is |kMaxTabEntries|. | 163 // size is |kMaxTabEntries|. |
160 void CompactTabEntries(); | 164 void CompactTabEntries(); |
161 | 165 |
162 // Contains the ExternalDataUseObserver. The caller must ensure that the | 166 // Contains the ExternalDataUseObserver. The caller must ensure that the |
163 // |data_use_observer_| outlives this instance. | 167 // |data_use_observer_| outlives this instance. |
164 const ExternalDataUseObserver* data_use_observer_; | 168 const ExternalDataUseObserver* data_use_observer_; |
165 | 169 |
166 // List of observers waiting for tracking session start and end notifications. | 170 // Map of observers that receive tracking session start and end |
167 const scoped_refptr<base::ObserverListThreadSafe<TabDataUseObserver>> | 171 // notifications. Map is keyed by raw pointers, and notifications are posted |
168 observer_list_; | 172 // to the weak pointers on UI thread. |
| 173 ObserverMap observers_; |
169 | 174 |
170 // Maintains the tracking sessions of multiple tabs. | 175 // Maintains the tracking sessions of multiple tabs. |
171 TabEntryMap active_tabs_; | 176 TabEntryMap active_tabs_; |
172 | 177 |
173 // Maximum number of tab entries to maintain session information about. | 178 // Maximum number of tab entries to maintain session information about. |
174 const size_t max_tab_entries_; | 179 const size_t max_tab_entries_; |
175 | 180 |
| 181 // |ui_task_runner_| is used to notify TabDataUseObserver on UI thread. |
| 182 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 183 |
176 base::ThreadChecker thread_checker_; | 184 base::ThreadChecker thread_checker_; |
177 | 185 |
178 // |weak_factory_| is used for generating weak pointers to be passed to | |
179 // DataUseTabUIManager on the UI thread. | |
180 base::WeakPtrFactory<DataUseTabModel> weak_factory_; | 186 base::WeakPtrFactory<DataUseTabModel> weak_factory_; |
181 | 187 |
182 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); | 188 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); |
183 }; | 189 }; |
184 | 190 |
185 } // namespace android | 191 } // namespace android |
186 | 192 |
187 } // namespace chrome | 193 } // namespace chrome |
188 | 194 |
189 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ | 195 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ |
OLD | NEW |