Chromium Code Reviews| 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/callback.h" | |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
|
tbansal1
2016/07/18 17:04:38
include not needed anymore.
Raj
2016/07/19 00:19:02
Done.
| |
| 14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/time/default_tick_clock.h" | 17 #include "base/time/default_tick_clock.h" |
| 17 #include "base/time/tick_clock.h" | 18 #include "base/time/tick_clock.h" |
| 18 #include "base/time/time.h" | 19 #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" | 20 #include "third_party/re2/src/re2/re2.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 DataUseMatcher::DataUseMatcher( | 27 DataUseMatcher::DataUseMatcher( |
| 28 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 28 const base::Callback<void(const std::string&)>& |
| 29 const ExternalDataUseObserverBridge* external_data_use_observer_bridge, | 29 on_tracking_label_removed_callback, |
| 30 const base::Callback<void(bool)>& on_matching_rules_fetched_callback, | |
| 30 const base::TimeDelta& default_matching_rule_expiration_duration) | 31 const base::TimeDelta& default_matching_rule_expiration_duration) |
| 31 : data_use_tab_model_(data_use_tab_model), | 32 : default_matching_rule_expiration_duration_( |
| 32 default_matching_rule_expiration_duration_( | |
| 33 default_matching_rule_expiration_duration), | 33 default_matching_rule_expiration_duration), |
| 34 tick_clock_(new base::DefaultTickClock()), | 34 tick_clock_(new base::DefaultTickClock()), |
| 35 external_data_use_observer_bridge_(external_data_use_observer_bridge) { | 35 on_tracking_label_removed_callback_(on_tracking_label_removed_callback), |
| 36 DCHECK(external_data_use_observer_bridge_); | 36 on_matching_rules_fetched_callback_(on_matching_rules_fetched_callback) {} |
| 37 } | |
| 38 | 37 |
| 39 DataUseMatcher::~DataUseMatcher() {} | 38 DataUseMatcher::~DataUseMatcher() {} |
| 40 | 39 |
| 41 void DataUseMatcher::RegisterURLRegexes( | 40 void DataUseMatcher::RegisterURLRegexes( |
| 42 const std::vector<std::string>& app_package_names, | 41 const std::vector<std::string>& app_package_names, |
| 43 const std::vector<std::string>& domain_path_regexes, | 42 const std::vector<std::string>& domain_path_regexes, |
| 44 const std::vector<std::string>& labels) { | 43 const std::vector<std::string>& labels) { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); | 45 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); |
| 47 DCHECK_EQ(app_package_names.size(), labels.size()); | 46 DCHECK_EQ(app_package_names.size(), labels.size()); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 76 if (expiration <= now_ticks) | 75 if (expiration <= now_ticks) |
| 77 continue; // skip expired matching rules. | 76 continue; // skip expired matching rules. |
| 78 DCHECK(!labels.at(i).empty()); | 77 DCHECK(!labels.at(i).empty()); |
| 79 matching_rules_.push_back(base::WrapUnique(new MatchingRule( | 78 matching_rules_.push_back(base::WrapUnique(new MatchingRule( |
| 80 app_package_name, std::move(pattern), labels.at(i), expiration))); | 79 app_package_name, std::move(pattern), labels.at(i), expiration))); |
| 81 | 80 |
| 82 removed_matching_rule_labels.erase(labels.at(i)); | 81 removed_matching_rule_labels.erase(labels.at(i)); |
| 83 } | 82 } |
| 84 | 83 |
| 85 for (const std::string& label : removed_matching_rule_labels) { | 84 for (const std::string& label : removed_matching_rule_labels) { |
| 86 if (data_use_tab_model_) | 85 on_tracking_label_removed_callback_.Run(label); |
| 87 data_use_tab_model_->OnTrackingLabelRemoved(label); | |
| 88 } | 86 } |
| 89 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", | 87 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", |
| 90 matching_rules_.size()); | 88 matching_rules_.size()); |
| 91 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", | 89 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", |
| 92 invalid_rules); | 90 invalid_rules); |
| 93 | 91 |
| 94 external_data_use_observer_bridge_->ShouldRegisterAsDataUseObserver( | 92 on_matching_rules_fetched_callback_.Run(!matching_rules_.empty()); |
|
tbansal1
2016/07/18 17:04:38
Would this callback run on UI thread? Is it expect
Raj
2016/07/19 00:19:02
The callback runs on UI thread. The bridge expects
| |
| 95 !matching_rules_.empty()); | |
| 96 } | 93 } |
| 97 | 94 |
| 98 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { | 95 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { |
| 99 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 96 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 *label = ""; | 98 *label = ""; |
| 102 | 99 |
| 103 if (!url.is_valid() || url.is_empty()) | 100 if (!url.is_valid() || url.is_empty()) |
| 104 return false; | 101 return false; |
| 105 | 102 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 133 continue; // skip expired matching rules. | 130 continue; // skip expired matching rules. |
| 134 if (app_package_name == matching_rule->app_package_name()) { | 131 if (app_package_name == matching_rule->app_package_name()) { |
| 135 *label = matching_rule->label(); | 132 *label = matching_rule->label(); |
| 136 return true; | 133 return true; |
| 137 } | 134 } |
| 138 } | 135 } |
| 139 | 136 |
| 140 return false; | 137 return false; |
| 141 } | 138 } |
| 142 | 139 |
| 143 void DataUseMatcher::FetchMatchingRules() { | |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 145 external_data_use_observer_bridge_->FetchMatchingRules(); | |
| 146 } | |
| 147 | |
| 148 bool DataUseMatcher::HasRules() const { | 140 bool DataUseMatcher::HasRules() const { |
| 149 return !matching_rules_.empty(); | 141 return !matching_rules_.empty(); |
| 150 } | 142 } |
| 151 | 143 |
| 152 bool DataUseMatcher::HasValidRuleWithLabel(const std::string& label) const { | 144 bool DataUseMatcher::HasValidRuleWithLabel(const std::string& label) const { |
| 153 for (const auto& matching_rule : matching_rules_) { | 145 for (const auto& matching_rule : matching_rules_) { |
| 154 if (matching_rule->expiration() > tick_clock_->NowTicks() && | 146 if (matching_rule->expiration() > tick_clock_->NowTicks() && |
| 155 label == matching_rule->label()) { | 147 label == matching_rule->label()) { |
| 156 return true; | 148 return true; |
| 157 } | 149 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 return label_; | 194 return label_; |
| 203 } | 195 } |
| 204 | 196 |
| 205 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { | 197 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { |
| 206 return expiration_; | 198 return expiration_; |
| 207 } | 199 } |
| 208 | 200 |
| 209 } // namespace android | 201 } // namespace android |
| 210 | 202 |
| 211 } // namespace chrome | 203 } // namespace chrome |
| OLD | NEW |