Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: chrome/browser/android/data_usage/data_use_tab_model.h

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

Powered by Google App Engine
This is Rietveld 408576698