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 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | 5 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
9 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
10 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/default_tick_clock.h" | 16 #include "base/time/default_tick_clock.h" |
15 #include "base/time/tick_clock.h" | 17 #include "base/time/tick_clock.h" |
16 #include "base/time/time.h" | 18 #include "base/time/time.h" |
17 #include "chrome/browser/android/data_usage/data_use_matcher.h" | 19 #include "chrome/browser/android/data_usage/data_use_matcher.h" |
18 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 20 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 21 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" |
19 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
22 #include "url/gurl.h" | 25 #include "url/gurl.h" |
23 | 26 |
24 namespace { | 27 namespace { |
25 | 28 |
26 // Default maximum number of tabs to maintain session information about. May be | 29 // Default maximum number of tabs to maintain session information about. May be |
27 // overridden by the field trial. | 30 // overridden by the field trial. |
28 const size_t kDefaultMaxTabEntries = 200; | 31 const size_t kDefaultMaxTabEntries = 200; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } // namespace | 133 } // namespace |
131 | 134 |
132 namespace chrome { | 135 namespace chrome { |
133 | 136 |
134 namespace android { | 137 namespace android { |
135 | 138 |
136 // static | 139 // static |
137 const char DataUseTabModel::kDefaultTag[] = "ChromeTab"; | 140 const char DataUseTabModel::kDefaultTag[] = "ChromeTab"; |
138 const char DataUseTabModel::kCustomTabTag[] = "ChromeCustomTab"; | 141 const char DataUseTabModel::kCustomTabTag[] = "ChromeCustomTab"; |
139 | 142 |
140 DataUseTabModel::DataUseTabModel() | 143 DataUseTabModel::DataUseTabModel( |
| 144 const base::Closure& fetch_matching_rules_callback, |
| 145 const base::Callback<void(bool)>& on_matching_rules_fetched_callback) |
141 : max_tab_entries_(GetMaxTabEntries()), | 146 : max_tab_entries_(GetMaxTabEntries()), |
142 max_sessions_per_tab_(GetMaxSessionsPerTab()), | 147 max_sessions_per_tab_(GetMaxSessionsPerTab()), |
143 closed_tab_expiration_duration_(GetClosedTabExpirationDuration()), | 148 closed_tab_expiration_duration_(GetClosedTabExpirationDuration()), |
144 open_tab_expiration_duration_(GetOpenTabExpirationDuration()), | 149 open_tab_expiration_duration_(GetOpenTabExpirationDuration()), |
| 150 tick_clock_(new base::DefaultTickClock()), |
| 151 data_use_matcher_(new DataUseMatcher( |
| 152 base::Bind(&DataUseTabModel::OnTrackingLabelRemoved, GetWeakPtr()), |
| 153 on_matching_rules_fetched_callback, |
| 154 GetDefaultMatchingRuleExpirationDuration())), |
| 155 fetch_matching_rules_callback_(fetch_matching_rules_callback), |
145 is_ready_for_navigation_event_(false), | 156 is_ready_for_navigation_event_(false), |
146 is_control_app_installed_(false), | 157 is_control_app_installed_(false), |
147 weak_factory_(this) { | 158 weak_factory_(this) { |
148 // Detach from current thread since rest of DataUseTabModel lives on the UI | 159 // Detach from current thread since rest of DataUseTabModel lives on the UI |
149 // thread and the current thread may not be UI thread.. | 160 // thread and the current thread may not be UI thread.. |
150 thread_checker_.DetachFromThread(); | 161 thread_checker_.DetachFromThread(); |
151 } | 162 } |
152 | 163 |
153 DataUseTabModel::~DataUseTabModel() { | 164 DataUseTabModel::~DataUseTabModel() { |
154 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
155 } | 166 } |
156 | 167 |
157 base::WeakPtr<DataUseTabModel> DataUseTabModel::GetWeakPtr() { | 168 base::WeakPtr<DataUseTabModel> DataUseTabModel::GetWeakPtr() { |
158 DCHECK(thread_checker_.CalledOnValidThread()); | 169 DCHECK(thread_checker_.CalledOnValidThread()); |
159 return weak_factory_.GetWeakPtr(); | 170 return weak_factory_.GetWeakPtr(); |
160 } | 171 } |
161 | 172 |
162 void DataUseTabModel::InitOnUIThread( | 173 void DataUseTabModel::InitOnUIThread() { |
163 const ExternalDataUseObserverBridge* external_data_use_observer_bridge) { | |
164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 174 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
165 DCHECK(thread_checker_.CalledOnValidThread()); | 175 DCHECK(thread_checker_.CalledOnValidThread()); |
166 DCHECK(external_data_use_observer_bridge); | |
167 | |
168 tick_clock_.reset(new base::DefaultTickClock()); | |
169 data_use_matcher_.reset( | |
170 new DataUseMatcher(GetWeakPtr(), external_data_use_observer_bridge, | |
171 GetDefaultMatchingRuleExpirationDuration())); | |
172 } | 176 } |
173 | 177 |
174 void DataUseTabModel::OnNavigationEvent( | 178 void DataUseTabModel::OnNavigationEvent( |
175 SessionID::id_type tab_id, | 179 SessionID::id_type tab_id, |
176 TransitionType transition, | 180 TransitionType transition, |
177 const GURL& url, | 181 const GURL& url, |
178 const std::string& package, | 182 const std::string& package, |
179 content::NavigationEntry* navigation_entry) { | 183 content::NavigationEntry* navigation_entry) { |
180 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
181 DCHECK(IsValidTabID(tab_id)); | 185 DCHECK(IsValidTabID(tab_id)); |
182 DCHECK(!navigation_entry || (navigation_entry->GetURL() == url)); | 186 DCHECK(!navigation_entry || (navigation_entry->GetURL() == url)); |
183 | 187 |
184 std::string current_label, new_label; | 188 std::string current_label, new_label; |
185 bool is_package_match; | 189 bool is_package_match; |
186 | 190 |
187 if (is_control_app_installed_ && !data_use_matcher_->HasRules()) | 191 if (is_control_app_installed_ && !data_use_matcher_->HasRules()) |
188 data_use_matcher_->FetchMatchingRules(); | 192 fetch_matching_rules_callback_.Run(); |
189 | 193 |
190 GetCurrentAndNewLabelForNavigationEvent(tab_id, transition, url, package, | 194 GetCurrentAndNewLabelForNavigationEvent(tab_id, transition, url, package, |
191 navigation_entry, ¤t_label, | 195 navigation_entry, ¤t_label, |
192 &new_label, &is_package_match); | 196 &new_label, &is_package_match); |
193 if (!current_label.empty() && new_label.empty()) { | 197 if (!current_label.empty() && new_label.empty()) { |
194 EndTrackingDataUse(tab_id); | 198 EndTrackingDataUse(tab_id); |
195 } else if (current_label.empty() && !new_label.empty()) { | 199 } else if (current_label.empty() && !new_label.empty()) { |
196 StartTrackingDataUse( | 200 StartTrackingDataUse( |
197 tab_id, new_label, | 201 tab_id, new_label, |
198 ((transition == TRANSITION_CUSTOM_TAB) && is_package_match)); | 202 ((transition == TRANSITION_CUSTOM_TAB) && is_package_match)); |
(...skipping 12 matching lines...) Expand all Loading... |
211 TabEntryMap::iterator tab_entry_iterator = active_tabs_.find(tab_id); | 215 TabEntryMap::iterator tab_entry_iterator = active_tabs_.find(tab_id); |
212 if (tab_entry_iterator == active_tabs_.end()) | 216 if (tab_entry_iterator == active_tabs_.end()) |
213 return; | 217 return; |
214 | 218 |
215 TabDataUseEntry& tab_entry = tab_entry_iterator->second; | 219 TabDataUseEntry& tab_entry = tab_entry_iterator->second; |
216 if (tab_entry.IsTrackingDataUse()) | 220 if (tab_entry.IsTrackingDataUse()) |
217 tab_entry.EndTracking(); | 221 tab_entry.EndTracking(); |
218 tab_entry.OnTabCloseEvent(); | 222 tab_entry.OnTabCloseEvent(); |
219 } | 223 } |
220 | 224 |
221 void DataUseTabModel::OnTrackingLabelRemoved(std::string label) { | 225 void DataUseTabModel::OnTrackingLabelRemoved(const std::string& label) { |
222 for (auto& tab_entry : active_tabs_) | 226 for (auto& tab_entry : active_tabs_) |
223 tab_entry.second.EndTrackingWithLabel(label); | 227 tab_entry.second.EndTrackingWithLabel(label); |
224 } | 228 } |
225 | 229 |
226 bool DataUseTabModel::GetTrackingInfoForTabAtTime( | 230 bool DataUseTabModel::GetTrackingInfoForTabAtTime( |
227 SessionID::id_type tab_id, | 231 SessionID::id_type tab_id, |
228 base::TimeTicks timestamp, | 232 base::TimeTicks timestamp, |
229 TrackingInfo* output_tracking_info) const { | 233 TrackingInfo* output_tracking_info) const { |
230 DCHECK(thread_checker_.CalledOnValidThread()); | 234 DCHECK(thread_checker_.CalledOnValidThread()); |
231 | 235 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 std::vector<std::string> empty; | 308 std::vector<std::string> empty; |
305 if (!is_control_app_installed_) { | 309 if (!is_control_app_installed_) { |
306 // Clear rules. | 310 // Clear rules. |
307 data_use_matcher_->RegisterURLRegexes(empty, empty, empty); | 311 data_use_matcher_->RegisterURLRegexes(empty, empty, empty); |
308 if (!is_ready_for_navigation_event_) { | 312 if (!is_ready_for_navigation_event_) { |
309 is_ready_for_navigation_event_ = true; | 313 is_ready_for_navigation_event_ = true; |
310 NotifyObserversOfDataUseTabModelReady(); | 314 NotifyObserversOfDataUseTabModelReady(); |
311 } | 315 } |
312 } else { | 316 } else { |
313 // Fetch the matching rules when the app is installed. | 317 // Fetch the matching rules when the app is installed. |
314 data_use_matcher_->FetchMatchingRules(); | 318 fetch_matching_rules_callback_.Run(); |
315 } | 319 } |
316 } | 320 } |
317 | 321 |
318 base::TimeTicks DataUseTabModel::NowTicks() const { | 322 base::TimeTicks DataUseTabModel::NowTicks() const { |
319 DCHECK(thread_checker_.CalledOnValidThread()); | 323 DCHECK(thread_checker_.CalledOnValidThread()); |
320 return tick_clock_->NowTicks(); | 324 return tick_clock_->NowTicks(); |
321 } | 325 } |
322 | 326 |
323 bool DataUseTabModel::IsCustomTabPackageMatch(SessionID::id_type tab_id) const { | 327 bool DataUseTabModel::IsCustomTabPackageMatch(SessionID::id_type tab_id) const { |
324 DCHECK(thread_checker_.CalledOnValidThread()); | 328 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 NowTicks() - | 521 NowTicks() - |
518 oldest_tab_entry_iterator->second.GetLatestStartOrEndTime(), | 522 oldest_tab_entry_iterator->second.GetLatestStartOrEndTime(), |
519 base::TimeDelta::FromMinutes(1), base::TimeDelta::FromHours(1), 50); | 523 base::TimeDelta::FromMinutes(1), base::TimeDelta::FromHours(1), 50); |
520 active_tabs_.erase(oldest_tab_entry_iterator); | 524 active_tabs_.erase(oldest_tab_entry_iterator); |
521 } | 525 } |
522 } | 526 } |
523 | 527 |
524 } // namespace android | 528 } // namespace android |
525 | 529 |
526 } // namespace chrome | 530 } // namespace chrome |
OLD | NEW |