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

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: Rebased, addressed 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>
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" 17 #include "base/observer_list.h"
18 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
19 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" 19 #include "chrome/browser/android/data_usage/tab_data_use_entry.h"
20 #include "components/data_usage/core/data_use.h" 20 #include "components/data_usage/core/data_use.h"
21 #include "url/gurl.h" 21 #include "url/gurl.h"
22 22
23 namespace chrome { 23 namespace chrome {
24 24
25 namespace android { 25 namespace android {
26 26
27 class ExternalDataUseObserver; 27 class ExternalDataUseObserver;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 76
77 DataUseTabModel(const ExternalDataUseObserver* data_use_observer, 77 DataUseTabModel(const ExternalDataUseObserver* data_use_observer,
78 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); 78 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
79 79
80 ~DataUseTabModel(); 80 ~DataUseTabModel();
81 81
82 base::WeakPtr<DataUseTabModel> GetWeakPtr(); 82 base::WeakPtr<DataUseTabModel> GetWeakPtr();
83 83
84 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source 84 // Notifies the DataUseTabModel of navigation events. |tab_id| is the source
85 // 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
86 // event/transition, |url| is the URL in the source tab, |package| indicates 86 // event/transition, |url| is the URL in the source tab.
87 // the android package name of external application that initiated the event.
88 void OnNavigationEvent(int32_t tab_id, 87 void OnNavigationEvent(int32_t tab_id,
89 TransitionType transition, 88 TransitionType transition,
90 const GURL& url, 89 const GURL& url);
91 const std::string& package);
92 90
93 // Notifies the DataUseTabModel that tab with |tab_id| is closed. Any active 91 // Notifies the DataUseTabModel that tab with |tab_id| is closed. Any active
94 // tracking sessions for the tab are terminated, and the tab is marked as 92 // tracking sessions for the tab are terminated, and the tab is marked as
95 // closed. 93 // closed.
96 void OnTabCloseEvent(int32_t tab_id); 94 void OnTabCloseEvent(int32_t tab_id);
97 95
98 // Gets the label for the |data_use| object. |output_label| must not be null. 96 // Gets the label for the |data_use| object. |output_label| must not be null.
99 // If a tab tracking session is found that was active at the time of request 97 // If a tab tracking session is found that was active at the time of request
100 // start of |data_use|, returns true and |output_label| is populated with its 98 // start of |data_use|, returns true and |output_label| is populated with its
101 // label. Otherwise returns false and |output_label| is set to empty string. 99 // label. Otherwise returns false and |output_label| is set to empty string.
102 bool GetLabelForDataUse(const data_usage::DataUse& data_use, 100 bool GetLabelForDataUse(const data_usage::DataUse& data_use,
103 std::string* output_label) const; 101 std::string* output_label) const;
104 102
105 // Adds or removes observers from the observer list. These functions are 103 // Adds or removes observers from the observer list. Must be called only on
106 // thread-safe and can be called from any thread. 104 // IO thread.
107 void AddObserver(TabDataUseObserver* observer); 105 void AddObserver(TabDataUseObserver* observer);
108 void RemoveObserver(TabDataUseObserver* observer); 106 void RemoveObserver(TabDataUseObserver* observer);
109 107
108 protected:
109 // Notifies the observers that a data usage tracking session started for
110 // |tab_id|. Protected for testing.
111 void NotifyObserversOfTrackingStarting(int32_t tab_id);
112
113 // Notifies the observers that an active data usage tracking session ended for
114 // |tab_id|. Protected for testing.
115 void NotifyObserversOfTrackingEnding(int32_t tab_id);
116
110 private: 117 private:
111 friend class DataUseTabModelTest; 118 friend class DataUseTabModelTest;
112 friend class MockTabDataUseEntryTest; 119 friend class MockTabDataUseEntryTest;
113 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking); 120 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, SingleTabTracking);
114 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking); 121 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, MultipleTabTracking);
115 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents); 122 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverStartEndEvents);
116 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverNotNotifiedAfterRemove); 123 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, ObserverNotNotifiedAfterRemove);
117 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, 124 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest,
118 MultipleObserverMultipleStartEndEvents); 125 MultipleObserverMultipleStartEndEvents);
119 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent); 126 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEvent);
120 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking); 127 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, TabCloseEventEndsTracking);
121 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest, 128 FRIEND_TEST_ALL_PREFIXES(DataUseTabModelTest,
122 CompactTabEntriesWithinMaxLimit); 129 CompactTabEntriesWithinMaxLimit);
123 130
124 typedef base::hash_map<int32_t, TabDataUseEntry> TabEntryMap; 131 typedef base::hash_map<int32_t, TabDataUseEntry> TabEntryMap;
125 132
126 // Returns the maximum number of tab entries to maintain session information 133 // Returns the maximum number of tab entries to maintain session information
127 // about. 134 // about.
128 static size_t GetMaxTabEntriesForTests(); 135 static size_t GetMaxTabEntriesForTests();
129 136
130 // Notifies the observers that a data usage tracking session started for
131 // |tab_id|.
132 void NotifyObserversOfTrackingStarting(int32_t tab_id);
133
134 // Notifies the observers that an active data usage tracking session ended for
135 // |tab_id|.
136 void NotifyObserversOfTrackingEnding(int32_t tab_id);
137
138 // Initiates a new tracking session with the |label| for tab with id |tab_id|. 137 // Initiates a new tracking session with the |label| for tab with id |tab_id|.
139 void StartTrackingDataUse(int32_t tab_id, const std::string& label); 138 void StartTrackingDataUse(int32_t tab_id, const std::string& label);
140 139
141 // Ends the current tracking session for tab with id |tab_id|. 140 // Ends the current tracking session for tab with id |tab_id|.
142 void EndTrackingDataUse(int32_t tab_id); 141 void EndTrackingDataUse(int32_t tab_id);
143 142
144 // Compacts the tab entry map |active_tabs_| by removing expired tab entries. 143 // Compacts the tab entry map |active_tabs_| by removing expired tab entries.
145 // After removing expired tab entries, if the size of |active_tabs_| exceeds 144 // After removing expired tab entries, if the size of |active_tabs_| exceeds
146 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its 145 // |kMaxTabEntries|, oldest unexpired tab entries will be removed until its
147 // size is |kMaxTabEntries|. 146 // size is |kMaxTabEntries|.
148 void CompactTabEntries(); 147 void CompactTabEntries();
149 148
150 // Contains the ExternalDataUseObserver. The caller must ensure that the 149 // Contains the ExternalDataUseObserver. The caller must ensure that the
151 // |data_use_observer_| outlives this instance. 150 // |data_use_observer_| outlives this instance.
152 const ExternalDataUseObserver* data_use_observer_; 151 const ExternalDataUseObserver* data_use_observer_;
153 152
154 // List of observers waiting for tracking session start and end notifications. 153 // List of observers waiting for tracking session start and end notifications.
155 const scoped_refptr<base::ObserverListThreadSafe<TabDataUseObserver>> 154 base::ObserverList<TabDataUseObserver> observer_list_;
156 observer_list_;
157 155
158 // Maintains the tracking sessions of multiple tabs. 156 // Maintains the tracking sessions of multiple tabs.
159 TabEntryMap active_tabs_; 157 TabEntryMap active_tabs_;
160 158
161 base::ThreadChecker thread_checker_; 159 base::ThreadChecker thread_checker_;
162 160
163 // |weak_factory_| is used for generating weak pointers to be passed to
164 // DataUseTabUIManager on the UI thread.
165 base::WeakPtrFactory<DataUseTabModel> weak_factory_; 161 base::WeakPtrFactory<DataUseTabModel> weak_factory_;
166 162
167 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel); 163 DISALLOW_COPY_AND_ASSIGN(DataUseTabModel);
168 }; 164 };
169 165
170 } // namespace android 166 } // namespace android
171 167
172 } // namespace chrome 168 } // namespace chrome
173 169
174 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_ 170 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_TAB_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698