| 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/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/time/default_tick_clock.h" | 16 #include "base/time/default_tick_clock.h" |
| 17 #include "base/time/tick_clock.h" | 17 #include "base/time/tick_clock.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" | |
| 20 #include "third_party/re2/src/re2/re2.h" | 19 #include "third_party/re2/src/re2/re2.h" |
| 21 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 22 | 21 |
| 23 namespace chrome { | 22 namespace chrome { |
| 24 | 23 |
| 25 namespace android { | 24 namespace android { |
| 26 | 25 |
| 27 DataUseMatcher::DataUseMatcher( | 26 DataUseMatcher::DataUseMatcher( |
| 28 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 27 const base::Callback<void(const std::string&)>& |
| 29 const ExternalDataUseObserverBridge* external_data_use_observer_bridge, | 28 on_tracking_label_removed_callback, |
| 29 const base::Callback<void(bool)>& on_matching_rules_fetched_callback, |
| 30 const base::TimeDelta& default_matching_rule_expiration_duration) | 30 const base::TimeDelta& default_matching_rule_expiration_duration) |
| 31 : data_use_tab_model_(data_use_tab_model), | 31 : default_matching_rule_expiration_duration_( |
| 32 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 external_data_use_observer_bridge_(external_data_use_observer_bridge) { | 34 on_tracking_label_removed_callback_(on_tracking_label_removed_callback), |
| 36 DCHECK(external_data_use_observer_bridge_); | 35 on_matching_rules_fetched_callback_(on_matching_rules_fetched_callback) { |
| 36 DCHECK(on_tracking_label_removed_callback_); |
| 37 DCHECK(on_matching_rules_fetched_callback_); |
| 38 // Detach from current thread since rest of DataUseMatcher lives on the UI |
| 39 // thread and the current thread may not be UI thread.. |
| 40 thread_checker_.DetachFromThread(); |
| 37 } | 41 } |
| 38 | 42 |
| 39 DataUseMatcher::~DataUseMatcher() {} | 43 DataUseMatcher::~DataUseMatcher() {} |
| 40 | 44 |
| 41 void DataUseMatcher::RegisterURLRegexes( | 45 void DataUseMatcher::RegisterURLRegexes( |
| 42 const std::vector<std::string>& app_package_names, | 46 const std::vector<std::string>& app_package_names, |
| 43 const std::vector<std::string>& domain_path_regexes, | 47 const std::vector<std::string>& domain_path_regexes, |
| 44 const std::vector<std::string>& labels) { | 48 const std::vector<std::string>& labels) { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); | 50 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 75 | 79 |
| 76 if (expiration <= now_ticks) | 80 if (expiration <= now_ticks) |
| 77 continue; // skip expired matching rules. | 81 continue; // skip expired matching rules. |
| 78 DCHECK(!labels.at(i).empty()); | 82 DCHECK(!labels.at(i).empty()); |
| 79 matching_rules_.push_back(base::WrapUnique(new MatchingRule( | 83 matching_rules_.push_back(base::WrapUnique(new MatchingRule( |
| 80 app_package_name, std::move(pattern), labels.at(i), expiration))); | 84 app_package_name, std::move(pattern), labels.at(i), expiration))); |
| 81 | 85 |
| 82 removed_matching_rule_labels.erase(labels.at(i)); | 86 removed_matching_rule_labels.erase(labels.at(i)); |
| 83 } | 87 } |
| 84 | 88 |
| 85 for (const std::string& label : removed_matching_rule_labels) { | 89 for (const std::string& label : removed_matching_rule_labels) |
| 86 if (data_use_tab_model_) | 90 on_tracking_label_removed_callback_.Run(label); |
| 87 data_use_tab_model_->OnTrackingLabelRemoved(label); | 91 |
| 88 } | |
| 89 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", | 92 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", |
| 90 matching_rules_.size()); | 93 matching_rules_.size()); |
| 91 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", | 94 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", |
| 92 invalid_rules); | 95 invalid_rules); |
| 93 | 96 |
| 94 external_data_use_observer_bridge_->ShouldRegisterAsDataUseObserver( | 97 on_matching_rules_fetched_callback_.Run(!matching_rules_.empty()); |
| 95 !matching_rules_.empty()); | |
| 96 } | 98 } |
| 97 | 99 |
| 98 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { | 100 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { |
| 99 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 101 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 *label = ""; | 103 *label = ""; |
| 102 | 104 |
| 103 if (!url.is_valid() || url.is_empty()) | 105 if (!url.is_valid() || url.is_empty()) |
| 104 return false; | 106 return false; |
| 105 | 107 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 133 continue; // skip expired matching rules. | 135 continue; // skip expired matching rules. |
| 134 if (app_package_name == matching_rule->app_package_name()) { | 136 if (app_package_name == matching_rule->app_package_name()) { |
| 135 *label = matching_rule->label(); | 137 *label = matching_rule->label(); |
| 136 return true; | 138 return true; |
| 137 } | 139 } |
| 138 } | 140 } |
| 139 | 141 |
| 140 return false; | 142 return false; |
| 141 } | 143 } |
| 142 | 144 |
| 143 void DataUseMatcher::FetchMatchingRules() { | |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 145 external_data_use_observer_bridge_->FetchMatchingRules(); | |
| 146 } | |
| 147 | |
| 148 bool DataUseMatcher::HasRules() const { | 145 bool DataUseMatcher::HasRules() const { |
| 149 return !matching_rules_.empty(); | 146 return !matching_rules_.empty(); |
| 150 } | 147 } |
| 151 | 148 |
| 152 bool DataUseMatcher::HasValidRuleWithLabel(const std::string& label) const { | 149 bool DataUseMatcher::HasValidRuleWithLabel(const std::string& label) const { |
| 153 for (const auto& matching_rule : matching_rules_) { | 150 for (const auto& matching_rule : matching_rules_) { |
| 154 if (matching_rule->expiration() > tick_clock_->NowTicks() && | 151 if (matching_rule->expiration() > tick_clock_->NowTicks() && |
| 155 label == matching_rule->label()) { | 152 label == matching_rule->label()) { |
| 156 return true; | 153 return true; |
| 157 } | 154 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 return label_; | 199 return label_; |
| 203 } | 200 } |
| 204 | 201 |
| 205 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { | 202 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { |
| 206 return expiration_; | 203 return expiration_; |
| 207 } | 204 } |
| 208 | 205 |
| 209 } // namespace android | 206 } // namespace android |
| 210 | 207 |
| 211 } // namespace chrome | 208 } // namespace chrome |
| OLD | NEW |