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

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

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

Powered by Google App Engine
This is Rietveld 408576698