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_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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" | 22 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| 23 #include "components/sessions/core/session_id.h" | 23 #include "components/sessions/core/session_id.h" |
| 24 #include "url/gurl.h" | |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
| 27 class TickClock; | 28 class TickClock; |
| 28 } | 29 } |
| 29 | 30 |
| 30 class GURL; | |
| 31 | |
| 32 namespace chrome { | 31 namespace chrome { |
| 33 | 32 |
| 34 namespace android { | 33 namespace android { |
| 35 | 34 |
| 36 class DataUseMatcher; | 35 class DataUseMatcher; |
| 37 class ExternalDataUseObserver; | 36 class ExternalDataUseObserverBridge; |
| 38 | 37 |
| 39 // Models tracking and labeling of data usage within each Tab. Within each tab, | 38 // Models tracking and labeling of data usage within each Tab. Within each tab, |
| 40 // the model tracks the data use of a sequence of navigations in a "tracking | 39 // the model tracks the data use of a sequence of navigations in a "tracking |
| 41 // session" beginning with an entry event and ending with an exit event. | 40 // session" beginning with an entry event and ending with an exit event. |
| 42 // Typically, these events are navigations matching a URL pattern, or various | 41 // Typically, these events are navigations matching a URL pattern, or various |
| 43 // types of browser-initiated navigations. A single tab may have several | 42 // types of browser-initiated navigations. A single tab may have several |
| 44 // disjoint "tracking sessions" depending on the sequence of entry and exit | 43 // disjoint "tracking sessions" depending on the sequence of entry and exit |
| 45 // events that took place. | 44 // events that took place. |
| 46 class DataUseTabModel { | 45 class DataUseTabModel { |
| 47 public: | 46 public: |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 78 | 77 |
| 79 // Notification callback when tab tracking sessions are started and ended. | 78 // Notification callback when tab tracking sessions are started and ended. |
| 80 // The callback will be received on the same thread AddObserver was called | 79 // The callback will be received on the same thread AddObserver was called |
| 81 // from. | 80 // from. |
| 82 virtual void NotifyTrackingStarting(SessionID::id_type tab_id) = 0; | 81 virtual void NotifyTrackingStarting(SessionID::id_type tab_id) = 0; |
| 83 virtual void NotifyTrackingEnding(SessionID::id_type tab_id) = 0; | 82 virtual void NotifyTrackingEnding(SessionID::id_type tab_id) = 0; |
| 84 }; | 83 }; |
| 85 | 84 |
| 86 DataUseTabModel(); | 85 DataUseTabModel(); |
| 87 | 86 |
| 88 // Initializes |this| on UI thread. |external_data_use_observer| is the weak | 87 // Initializes |this| on UI thread. |external_data_use_observer_bridge| is the |
| 89 // pointer to ExternalDataUseObserver object that owns |this|. | 88 // pointer to ExternalDataUseObserverBridge object. |
|
tbansal1
2016/03/08 18:15:01
say more about ownership and lifetime: It seems th
Raj
2016/03/09 02:27:35
Done.
| |
| 90 void InitOnUIThread( | 89 void InitOnUIThread( |
| 91 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 90 const ExternalDataUseObserverBridge* external_data_use_observer_bridge); |
| 92 const base::WeakPtr<ExternalDataUseObserver>& external_data_use_observer); | |
| 93 | 91 |
| 94 virtual ~DataUseTabModel(); | 92 virtual ~DataUseTabModel(); |
| 95 | 93 |
| 96 base::WeakPtr<DataUseTabModel> GetWeakPtr(); | 94 base::WeakPtr<DataUseTabModel> GetWeakPtr(); |
| 97 | 95 |
| 98 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source | 96 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source |
| 99 // tab of the generated event, |transition| indicates the type of the UI | 97 // tab of the generated event, |transition| indicates the type of the UI |
| 100 // event/transition, |url| is the URL in the source tab, |package| indicates | 98 // event/transition, |url| is the URL in the source tab, |package| indicates |
| 101 // the android package name of external application that initiated the event. | 99 // the android package name of external application that initiated the event. |
| 102 void OnNavigationEvent(SessionID::id_type tab_id, | 100 void OnNavigationEvent(SessionID::id_type tab_id, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); | 187 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); |
| 190 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); | 188 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); |
| 191 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking); | 189 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking); |
| 192 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, | 190 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, |
| 193 UnexpiredTabEntryRemovaltimeHistogram); | 191 UnexpiredTabEntryRemovaltimeHistogram); |
| 194 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, | 192 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, |
| 195 MatchingRuleFetchOnControlAppInstall); | 193 MatchingRuleFetchOnControlAppInstall); |
| 196 | 194 |
| 197 typedef base::hash_map<SessionID::id_type, TabDataUseEntry> TabEntryMap; | 195 typedef base::hash_map<SessionID::id_type, TabDataUseEntry> TabEntryMap; |
| 198 | 196 |
| 197 // Contains the details of a single UI navigation event. | |
| 198 struct DataUseUINavigationBuffer { | |
|
tbansal1
2016/03/08 18:15:00
This name seems incorrect. This is not a buffer/co
| |
| 199 SessionID::id_type tab_id; | |
| 200 TransitionType transition; | |
|
tbansal1
2016/03/08 18:15:01
s/transition/transition_type/
| |
| 201 GURL url; | |
| 202 std::string package; | |
|
tbansal1
2016/03/08 18:15:01
nit: may be add comments for the fields. e.g., is
| |
| 203 }; | |
| 204 | |
| 199 // Gets the current label of a tab, and the new label if a navigation event | 205 // Gets the current label of a tab, and the new label if a navigation event |
| 200 // occurs in the tab. |tab_id| is the source tab of the generated event, | 206 // occurs in the tab. |tab_id| is the source tab of the generated event, |
| 201 // |transition| indicates the type of the UI event/transition, |url| is the | 207 // |transition| indicates the type of the UI event/transition, |url| is the |
| 202 // URL in the source tab, |package| indicates the android package name of | 208 // URL in the source tab, |package| indicates the android package name of |
| 203 // external application that initiated the event. |current_label|, |new_label| | 209 // external application that initiated the event. |current_label|, |new_label| |
| 204 // and |is_package_match| should not be null, and are set with current and new | 210 // and |is_package_match| should not be null, and are set with current and new |
| 205 // labels respectively. |current_label| will be set to empty string, if there | 211 // labels respectively. |current_label| will be set to empty string, if there |
| 206 // is no active tracking session. |new_label| will be set to empty string if | 212 // is no active tracking session. |new_label| will be set to empty string if |
| 207 // there would be no active tracking session if the navigation happens. | 213 // there would be no active tracking session if the navigation happens. |
| 208 // |is_package_match| will be set to true if a tracking session will start due | 214 // |is_package_match| will be set to true if a tracking session will start due |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 224 | 230 |
| 225 // Ends the current tracking session for tab with id |tab_id|. | 231 // Ends the current tracking session for tab with id |tab_id|. |
| 226 void EndTrackingDataUse(SessionID::id_type tab_id); | 232 void EndTrackingDataUse(SessionID::id_type tab_id); |
| 227 | 233 |
| 228 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. | 234 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. |
| 229 // After removing expired tab entries, if the size of |active_tabs_| exceeds | 235 // After removing expired tab entries, if the size of |active_tabs_| exceeds |
| 230 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its | 236 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its |
| 231 // size is |kMaxTabEntries|. | 237 // size is |kMaxTabEntries|. |
| 232 void CompactTabEntries(); | 238 void CompactTabEntries(); |
| 233 | 239 |
| 240 void ProcessBufferedNavigationEvents(); | |
|
tbansal1
2016/03/08 18:15:01
function comments.
| |
| 241 | |
| 234 // Collection of observers that receive tracking session start and end | 242 // Collection of observers that receive tracking session start and end |
| 235 // notifications. Notifications are posted on UI thread. | 243 // notifications. Notifications are posted on UI thread. |
| 236 base::ObserverList<TabDataUseObserver> observers_; | 244 base::ObserverList<TabDataUseObserver> observers_; |
| 237 | 245 |
| 238 // Maintains the tracking sessions of multiple tabs. | 246 // Maintains the tracking sessions of multiple tabs. |
| 239 TabEntryMap active_tabs_; | 247 TabEntryMap active_tabs_; |
| 240 | 248 |
| 241 // Maximum number of tab entries to maintain session information about. | 249 // Maximum number of tab entries to maintain session information about. |
| 242 const size_t max_tab_entries_; | 250 const size_t max_tab_entries_; |
| 243 | 251 |
| 244 // Maximum number of tracking sessions to maintain per tab. | 252 // Maximum number of tracking sessions to maintain per tab. |
| 245 const size_t max_sessions_per_tab_; | 253 const size_t max_sessions_per_tab_; |
| 246 | 254 |
| 247 // Expiration duration for a closed tab entry and an open tab entry | 255 // Expiration duration for a closed tab entry and an open tab entry |
| 248 // respectively. | 256 // respectively. |
| 249 const base::TimeDelta closed_tab_expiration_duration_; | 257 const base::TimeDelta closed_tab_expiration_duration_; |
| 250 const base::TimeDelta open_tab_expiration_duration_; | 258 const base::TimeDelta open_tab_expiration_duration_; |
| 251 | 259 |
| 252 // TickClock used for obtaining the current time. | 260 // TickClock used for obtaining the current time. |
| 253 scoped_ptr<base::TickClock> tick_clock_; | 261 scoped_ptr<base::TickClock> tick_clock_; |
| 254 | 262 |
| 255 // Stores the matching patterns. | 263 // Stores the matching patterns. |
| 256 scoped_ptr<DataUseMatcher> data_use_matcher_; | 264 scoped_ptr<DataUseMatcher> data_use_matcher_; |
| 257 | 265 |
| 258 // True if the external control app is installed. | 266 // True if the external control app is installed. |
| 259 bool is_control_app_installed_; | 267 bool is_control_app_installed_; |
| 260 | 268 |
| 269 // Buffers the list of UI navigation events that occurred until the first rule | |
| 270 // fetch is complete or the control app not installed callback was received or | |
| 271 // until |kDefaultMaxNavigationEventsBuffered| navigation events were | |
| 272 // buffered. | |
| 273 scoped_ptr<std::vector<DataUseUINavigationBuffer>> ui_navigation_buffer; | |
|
tbansal1
2016/03/08 18:15:00
s/ui_navigation_buffer/data_use_ui_navigations_/?
tbansal1
2016/03/08 18:15:01
Also, why does this need to be a scoped_ptr, and n
tbansal1
2016/03/08 18:15:01
variable name should end with underscore.
tbansal1
2016/03/08 18:15:01
say more about this buffer: what is the order in w
| |
| 274 | |
| 261 base::ThreadChecker thread_checker_; | 275 base::ThreadChecker thread_checker_; |
| 262 | 276 |
| 263 base::WeakPtrFactory<DataUseTabModel> weak_factory_; | 277 base::WeakPtrFactory<DataUseTabModel> weak_factory_; |
| 264 | 278 |
| 265 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); | 279 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); |
| 266 }; | 280 }; |
| 267 | 281 |
| 268 } // namespace android | 282 } // namespace android |
| 269 | 283 |
| 270 } // namespace chrome | 284 } // namespace chrome |
| 271 | 285 |
| 272 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ | 286 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ |
| OLD | NEW |