| 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_matcher.h" | 5 #include "chrome/browser/android/data_usage/data_use_matcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/default_tick_clock.h" | 15 #include "base/time/default_tick_clock.h" |
| 16 #include "base/time/tick_clock.h" | 16 #include "base/time/tick_clock.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 18 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" |
| 19 #include "third_party/re2/src/re2/re2.h" | 19 #include "third_party/re2/src/re2/re2.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace chrome { | 22 namespace chrome { |
| 23 | 23 |
| 24 namespace android { | 24 namespace android { |
| 25 | 25 |
| 26 DataUseMatcher::DataUseMatcher( | 26 DataUseMatcher::DataUseMatcher( |
| 27 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 27 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, |
| 28 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 28 const ExternalDataUseObserverBridge* external_data_use_observer_bridge, |
| 29 const base::WeakPtr<ExternalDataUseObserver>& external_data_use_observer, | |
| 30 const base::TimeDelta& default_matching_rule_expiration_duration) | 29 const base::TimeDelta& default_matching_rule_expiration_duration) |
| 31 : data_use_tab_model_(data_use_tab_model), | 30 : data_use_tab_model_(data_use_tab_model), |
| 32 default_matching_rule_expiration_duration_( | 31 default_matching_rule_expiration_duration_( |
| 33 default_matching_rule_expiration_duration), | 32 default_matching_rule_expiration_duration), |
| 34 tick_clock_(new base::DefaultTickClock()), | 33 tick_clock_(new base::DefaultTickClock()), |
| 35 io_task_runner_(io_task_runner), | 34 external_data_use_observer_bridge_(external_data_use_observer_bridge) { |
| 36 external_data_use_observer_(external_data_use_observer) { | 35 DCHECK(external_data_use_observer_bridge_); |
| 37 DCHECK(io_task_runner_); | |
| 38 } | 36 } |
| 39 | 37 |
| 40 DataUseMatcher::~DataUseMatcher() {} | 38 DataUseMatcher::~DataUseMatcher() {} |
| 41 | 39 |
| 42 void DataUseMatcher::RegisterURLRegexes( | 40 void DataUseMatcher::RegisterURLRegexes( |
| 43 const std::vector<std::string>& app_package_names, | 41 const std::vector<std::string>& app_package_names, |
| 44 const std::vector<std::string>& domain_path_regexes, | 42 const std::vector<std::string>& domain_path_regexes, |
| 45 const std::vector<std::string>& labels) { | 43 const std::vector<std::string>& labels) { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); | 45 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 83 |
| 86 for (const std::string& label : removed_matching_rule_labels) { | 84 for (const std::string& label : removed_matching_rule_labels) { |
| 87 if (data_use_tab_model_) | 85 if (data_use_tab_model_) |
| 88 data_use_tab_model_->OnTrackingLabelRemoved(label); | 86 data_use_tab_model_->OnTrackingLabelRemoved(label); |
| 89 } | 87 } |
| 90 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", | 88 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", |
| 91 matching_rules_.size()); | 89 matching_rules_.size()); |
| 92 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", | 90 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", |
| 93 invalid_rules); | 91 invalid_rules); |
| 94 | 92 |
| 95 DCHECK(io_task_runner_); | 93 external_data_use_observer_bridge_->ShouldRegisterAsDataUseObserver( |
| 96 | 94 !matching_rules_.empty()); |
| 97 // Notify |external_data_use_observer_| if it should register as a data use | |
| 98 // observer. | |
| 99 io_task_runner_->PostTask( | |
| 100 FROM_HERE, | |
| 101 base::Bind(&ExternalDataUseObserver::ShouldRegisterAsDataUseObserver, | |
| 102 external_data_use_observer_, !matching_rules_.empty())); | |
| 103 } | 95 } |
| 104 | 96 |
| 105 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { | 97 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { |
| 106 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 98 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 *label = ""; | 100 *label = ""; |
| 109 | 101 |
| 110 if (!url.is_valid() || url.is_empty()) | 102 if (!url.is_valid() || url.is_empty()) |
| 111 return false; | 103 return false; |
| 112 | 104 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 142 *label = matching_rule->label(); | 134 *label = matching_rule->label(); |
| 143 return true; | 135 return true; |
| 144 } | 136 } |
| 145 } | 137 } |
| 146 | 138 |
| 147 return false; | 139 return false; |
| 148 } | 140 } |
| 149 | 141 |
| 150 void DataUseMatcher::FetchMatchingRules() { | 142 void DataUseMatcher::FetchMatchingRules() { |
| 151 DCHECK(thread_checker_.CalledOnValidThread()); | 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 152 DCHECK(io_task_runner_); | 144 external_data_use_observer_bridge_->FetchMatchingRules(); |
| 153 | |
| 154 // Notify |external_data_use_observer_| to fetch the rules. | |
| 155 io_task_runner_->PostTask( | |
| 156 FROM_HERE, base::Bind(&ExternalDataUseObserver::FetchMatchingRules, | |
| 157 external_data_use_observer_)); | |
| 158 } | 145 } |
| 159 | 146 |
| 160 bool DataUseMatcher::HasValidRules() const { | 147 bool DataUseMatcher::HasValidRules() const { |
| 161 return !matching_rules_.empty(); | 148 return !matching_rules_.empty(); |
| 162 } | 149 } |
| 163 | 150 |
| 164 void DataUseMatcher::ParsePackageField(const std::string& app_package_name, | 151 void DataUseMatcher::ParsePackageField(const std::string& app_package_name, |
| 165 std::string* new_app_package_name, | 152 std::string* new_app_package_name, |
| 166 base::TimeTicks* expiration) const { | 153 base::TimeTicks* expiration) const { |
| 167 const char separator = '|'; | 154 const char separator = '|'; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 return label_; | 191 return label_; |
| 205 } | 192 } |
| 206 | 193 |
| 207 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { | 194 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { |
| 208 return expiration_; | 195 return expiration_; |
| 209 } | 196 } |
| 210 | 197 |
| 211 } // namespace android | 198 } // namespace android |
| 212 | 199 |
| 213 } // namespace chrome | 200 } // namespace chrome |
| OLD | NEW |