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

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: Remove unnecessary thread checks in the factory class Created 5 years 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 <list>
9
10 #include <string> 9 #include <string>
11 10
12 #include "base/containers/hash_tables.h" 11 #include "base/containers/hash_tables.h"
13 #include "base/gtest_prod_util.h" 12 #include "base/gtest_prod_util.h"
14 #include "base/macros.h" 13 #include "base/macros.h"
15 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list_threadsafe.h"
18 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
19 #include "base/time/time.h" 17 #include "base/time/time.h"
20 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" 18 #include "chrome/browser/android/data_usage/tab_data_use_entry.h"
21 #include "components/data_usage/core/data_use.h" 19 #include "components/sessions/core/session_id.h"
22 #include "url/gurl.h" 20
21 namespace base {
22 class SingleThreadTaskRunner;
23 }
24
25 namespace data_usage {
26 struct DataUse;
27 }
28
29 class GURL;
23 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 observers to the observer list. Must be called on IO thread.
111 // thread-safe and can be called from any thread. 112 // |observer| is notified on UI thread.
112 void AddObserver(TabDataUseObserver* observer); 113 // TODO(tbansal): Remove observers that have been destroyed.
113 void RemoveObserver(TabDataUseObserver* observer); 114 void AddObserver(base::WeakPtr<TabDataUseObserver> observer);
115
116 protected:
117 // Notifies the observers that a data usage tracking session started for
118 // |tab_id|. Protected for testing.
119 void NotifyObserversOfTrackingStarting(SessionID::id_type tab_id);
120
121 // Notifies the observers that an active data usage tracking session ended for
122 // |tab_id|. Protected for testing.
123 void NotifyObserversOfTrackingEnding(SessionID::id_type tab_id);
114 124
115 private: 125 private:
116 friend class DataUseTabModelTest; 126 friend class DataUseTabModelTest;
117 friend class MockTabDataUseEntryTest; 127 friend class MockTabDataUseEntryTest;
118 friend class TestDataUseTabModel; 128 friend class TestDataUseTabModel;
119 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking); 129 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking);
120 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking); 130 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking);
121 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); 131 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents);
122 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<SessionID::id_type, TabDataUseEntry> TabEntryMap;
138 147
139 // Virtualized for unit test support. 148 // Virtualized for unit test support.
140 virtual base::TimeTicks Now() const; 149 virtual base::TimeTicks Now() const;
141 150
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|. 151 // 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); 152 void StartTrackingDataUse(SessionID::id_type tab_id,
153 const std::string& label);
152 154
153 // Ends the current tracking session for tab with id |tab_id|. 155 // Ends the current tracking session for tab with id |tab_id|.
154 void EndTrackingDataUse(int32_t tab_id); 156 void EndTrackingDataUse(SessionID::id_type tab_id);
155 157
156 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. 158 // 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 159 // After removing expired tab entries, if the size of |active_tabs_| exceeds
158 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its 160 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its
159 // size is |kMaxTabEntries|. 161 // size is |kMaxTabEntries|.
160 void CompactTabEntries(); 162 void CompactTabEntries();
161 163
162 // Contains the ExternalDataUseObserver. The caller must ensure that the 164 // Contains the ExternalDataUseObserver. The caller must ensure that the
163 // |data_use_observer_| outlives this instance. 165 // |data_use_observer_| outlives this instance.
164 const ExternalDataUseObserver* data_use_observer_; 166 const ExternalDataUseObserver* data_use_observer_;
165 167
166 // List of observers waiting for tracking session start and end notifications. 168 // Collection of observers that receive tracking session start and end
167 const scoped_refptr<base::ObserverListThreadSafe<TabDataUseObserver>> 169 // notifications. Notifications are posted on UI thread.
168 observer_list_; 170 std::list<base::WeakPtr<TabDataUseObserver>> observers_;
169 171
170 // Maintains the tracking sessions of multiple tabs. 172 // Maintains the tracking sessions of multiple tabs.
171 TabEntryMap active_tabs_; 173 TabEntryMap active_tabs_;
172 174
173 // Maximum number of tab entries to maintain session information about. 175 // Maximum number of tab entries to maintain session information about.
174 const size_t max_tab_entries_; 176 const size_t max_tab_entries_;
175 177
178 // |ui_task_runner_| is used to notify TabDataUseObserver on UI thread.
179 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
180
176 base::ThreadChecker thread_checker_; 181 base::ThreadChecker thread_checker_;
177 182
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_; 183 base::WeakPtrFactory<DataUseTabModel> weak_factory_;
181 184
182 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); 185 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel);
183 }; 186 };
184 187
185 } // namespace android 188 } // namespace android
186 189
187 } // namespace chrome 190 } // namespace chrome
188 191
189 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ 192 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_
OLDNEW
« no previous file with comments | « chrome/browser/android/data_usage/data_use_tab_helper.cc ('k') | chrome/browser/android/data_usage/data_use_tab_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698