| 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/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/default_tick_clock.h" | 15 #include "base/time/default_tick_clock.h" |
| 15 #include "base/time/tick_clock.h" | 16 #include "base/time/tick_clock.h" |
| 17 #include "base/time/time.h" |
| 16 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 18 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 17 #include "third_party/re2/src/re2/re2.h" | 19 #include "third_party/re2/src/re2/re2.h" |
| 18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 19 | 21 |
| 20 namespace chrome { | 22 namespace chrome { |
| 21 | 23 |
| 22 namespace android { | 24 namespace android { |
| 23 | 25 |
| 24 DataUseMatcher::DataUseMatcher( | 26 DataUseMatcher::DataUseMatcher( |
| 25 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 27 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 | 41 |
| 40 void DataUseMatcher::RegisterURLRegexes( | 42 void DataUseMatcher::RegisterURLRegexes( |
| 41 const std::vector<std::string>& app_package_names, | 43 const std::vector<std::string>& app_package_names, |
| 42 const std::vector<std::string>& domain_path_regexes, | 44 const std::vector<std::string>& domain_path_regexes, |
| 43 const std::vector<std::string>& labels) { | 45 const std::vector<std::string>& labels) { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | 46 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); | 47 DCHECK_EQ(app_package_names.size(), domain_path_regexes.size()); |
| 46 DCHECK_EQ(app_package_names.size(), labels.size()); | 48 DCHECK_EQ(app_package_names.size(), labels.size()); |
| 47 | 49 |
| 48 base::hash_set<std::string> removed_matching_rule_labels; | 50 base::hash_set<std::string> removed_matching_rule_labels; |
| 51 uint32_t invalid_rules = 0; |
| 49 | 52 |
| 50 for (const auto& matching_rule : matching_rules_) | 53 for (const auto& matching_rule : matching_rules_) |
| 51 removed_matching_rule_labels.insert(matching_rule->label()); | 54 removed_matching_rule_labels.insert(matching_rule->label()); |
| 52 | 55 |
| 53 matching_rules_.clear(); | 56 matching_rules_.clear(); |
| 54 re2::RE2::Options options(re2::RE2::DefaultOptions); | 57 re2::RE2::Options options(re2::RE2::DefaultOptions); |
| 55 options.set_case_sensitive(false); | 58 options.set_case_sensitive(false); |
| 56 | 59 |
| 57 for (size_t i = 0; i < domain_path_regexes.size(); ++i) { | 60 for (size_t i = 0; i < domain_path_regexes.size(); ++i) { |
| 58 const std::string& url_regex = domain_path_regexes.at(i); | 61 const std::string& url_regex = domain_path_regexes.at(i); |
| 59 std::string app_package_name; | 62 std::string app_package_name; |
| 60 base::TimeTicks expiration; | 63 base::TimeTicks expiration; |
| 61 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 64 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 62 | 65 |
| 63 ParsePackageField(app_package_names.at(i), &app_package_name, &expiration); | 66 ParsePackageField(app_package_names.at(i), &app_package_name, &expiration); |
| 64 if (url_regex.empty() && app_package_name.empty()) | 67 if (url_regex.empty() && app_package_name.empty()) { |
| 68 invalid_rules++; |
| 65 continue; | 69 continue; |
| 70 } |
| 66 scoped_ptr<re2::RE2> pattern(new re2::RE2(url_regex, options)); | 71 scoped_ptr<re2::RE2> pattern(new re2::RE2(url_regex, options)); |
| 67 if (!pattern->ok()) | 72 if (!pattern->ok()) { |
| 73 invalid_rules++; |
| 68 continue; | 74 continue; |
| 75 } |
| 69 | 76 |
| 70 if (expiration <= now_ticks) | 77 if (expiration <= now_ticks) |
| 71 continue; // skip expired matching rules. | 78 continue; // skip expired matching rules. |
| 72 DCHECK(!labels.at(i).empty()); | 79 DCHECK(!labels.at(i).empty()); |
| 73 matching_rules_.push_back(make_scoped_ptr(new MatchingRule( | 80 matching_rules_.push_back(make_scoped_ptr(new MatchingRule( |
| 74 app_package_name, std::move(pattern), labels.at(i), expiration))); | 81 app_package_name, std::move(pattern), labels.at(i), expiration))); |
| 75 | 82 |
| 76 removed_matching_rule_labels.erase(labels.at(i)); | 83 removed_matching_rule_labels.erase(labels.at(i)); |
| 77 } | 84 } |
| 78 | 85 |
| 79 for (const std::string& label : removed_matching_rule_labels) { | 86 for (const std::string& label : removed_matching_rule_labels) { |
| 80 if (data_use_tab_model_) | 87 if (data_use_tab_model_) |
| 81 data_use_tab_model_->OnTrackingLabelRemoved(label); | 88 data_use_tab_model_->OnTrackingLabelRemoved(label); |
| 82 } | 89 } |
| 90 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Valid", |
| 91 matching_rules_.size()); |
| 92 UMA_HISTOGRAM_COUNTS_100("DataUsage.MatchingRulesCount.Invalid", |
| 93 invalid_rules); |
| 94 |
| 83 DCHECK(io_task_runner_); | 95 DCHECK(io_task_runner_); |
| 84 | 96 |
| 85 // Notify |external_data_use_observer_| if it should register as a data use | 97 // Notify |external_data_use_observer_| if it should register as a data use |
| 86 // observer. | 98 // observer. |
| 87 io_task_runner_->PostTask( | 99 io_task_runner_->PostTask( |
| 88 FROM_HERE, | 100 FROM_HERE, |
| 89 base::Bind(&ExternalDataUseObserver::ShouldRegisterAsDataUseObserver, | 101 base::Bind(&ExternalDataUseObserver::ShouldRegisterAsDataUseObserver, |
| 90 external_data_use_observer_, !matching_rules_.empty())); | 102 external_data_use_observer_, !matching_rules_.empty())); |
| 91 } | 103 } |
| 92 | 104 |
| 93 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { | 105 bool DataUseMatcher::MatchesURL(const GURL& url, std::string* label) const { |
| 94 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 106 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 *label = ""; | 108 *label = ""; |
| 97 | 109 |
| 98 if (!url.is_valid() || url.is_empty()) | 110 if (!url.is_valid() || url.is_empty()) |
| 99 return false; | 111 return false; |
| 100 | 112 |
| 101 for (const auto& matching_rule : matching_rules_) { | 113 for (const auto& matching_rule : matching_rules_) { |
| 102 if (matching_rule->expiration() <= now_ticks) | 114 if (matching_rule->expiration() <= now_ticks) |
| 103 continue; // skip expired matching rules. | 115 continue; // skip expired matching rules. |
| 104 if (re2::RE2::FullMatch(url.spec(), *(matching_rule->pattern()))) { | 116 base::TimeTicks begin = base::TimeTicks::Now(); |
| 117 bool match = re2::RE2::FullMatch(url.spec(), *(matching_rule->pattern())); |
| 118 UMA_HISTOGRAM_TIMES("DataUsage.Perf.URLRegexMatchDuration", |
| 119 base::TimeTicks::Now() - begin); |
| 120 if (match) { |
| 105 *label = matching_rule->label(); | 121 *label = matching_rule->label(); |
| 106 return true; | 122 return true; |
| 107 } | 123 } |
| 108 } | 124 } |
| 109 | 125 |
| 110 return false; | 126 return false; |
| 111 } | 127 } |
| 112 | 128 |
| 113 bool DataUseMatcher::MatchesAppPackageName(const std::string& app_package_name, | 129 bool DataUseMatcher::MatchesAppPackageName(const std::string& app_package_name, |
| 114 std::string* label) const { | 130 std::string* label) const { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 return label_; | 204 return label_; |
| 189 } | 205 } |
| 190 | 206 |
| 191 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { | 207 const base::TimeTicks& DataUseMatcher::MatchingRule::expiration() const { |
| 192 return expiration_; | 208 return expiration_; |
| 193 } | 209 } |
| 194 | 210 |
| 195 } // namespace android | 211 } // namespace android |
| 196 | 212 |
| 197 } // namespace chrome | 213 } // namespace chrome |
| OLD | NEW |