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 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" | |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | |
| 19 | |
| 20 namespace base { | 19 namespace base { |
| 21 class TickClock; | 20 class TickClock; |
| 22 } | 21 } |
| 23 | 22 |
| 24 namespace re2 { | 23 namespace re2 { |
| 25 class RE2; | 24 class RE2; |
| 26 } | 25 } |
| 27 | 26 |
| 28 class GURL; | 27 class GURL; |
| 29 | 28 |
| 30 namespace chrome { | 29 namespace chrome { |
| 31 | 30 |
| 32 namespace android { | 31 namespace android { |
| 33 | 32 |
| 34 class ExternalDataUseObserverBridge; | |
| 35 | |
| 36 // DataUseMatcher stores the matching URL patterns and package names along with | 33 // DataUseMatcher stores the matching URL patterns and package names along with |
| 37 // the labels. It also provides functionality to get the matching label for a | 34 // the labels. It also provides functionality to get the matching label for a |
| 38 // given URL or package. DataUseMatcher is not thread safe. | 35 // given URL or package. DataUseMatcher is not thread safe. |
| 39 class DataUseMatcher { | 36 class DataUseMatcher { |
| 40 public: | 37 public: |
| 41 DataUseMatcher( | 38 DataUseMatcher( |
|
tbansal1
2016/07/18 17:04:38
add comments about the callback.
Raj
2016/07/19 00:19:02
Done.
| |
| 42 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 39 const base::Callback<void(const std::string&)>& |
| 43 const ExternalDataUseObserverBridge* external_data_use_observer_bridge, | 40 on_tracking_label_removed_callback, |
| 41 const base::Callback<void(bool)>& on_matching_rules_fetched_callback, | |
| 44 const base::TimeDelta& default_matching_rule_expiration_duration); | 42 const base::TimeDelta& default_matching_rule_expiration_duration); |
| 45 | 43 |
| 46 ~DataUseMatcher(); | 44 ~DataUseMatcher(); |
| 47 | 45 |
| 48 // Called by FetchMatchingRulesDoneOnIOThread to register multiple | 46 // Called by FetchMatchingRulesDoneOnIOThread to register multiple |
| 49 // case-insensitive regular expressions. If the url of the data use request | 47 // case-insensitive regular expressions. If the url of the data use request |
| 50 // matches any of the regular expression, the observation is passed to the | 48 // matches any of the regular expression, the observation is passed to the |
| 51 // Java listener. | 49 // Java listener. |
| 52 void RegisterURLRegexes(const std::vector<std::string>& app_package_names, | 50 void RegisterURLRegexes(const std::vector<std::string>& app_package_names, |
| 53 const std::vector<std::string>& domain_path_regexes, | 51 const std::vector<std::string>& domain_path_regexes, |
| 54 const std::vector<std::string>& labels); | 52 const std::vector<std::string>& labels); |
| 55 | 53 |
| 56 // Returns true if the |url| matches the registered regular expressions. | 54 // Returns true if the |url| matches the registered regular expressions. |
| 57 // |label| must not be null. If a match is found, the |label| is set to the | 55 // |label| must not be null. If a match is found, the |label| is set to the |
| 58 // matching rule's label. | 56 // matching rule's label. |
| 59 bool MatchesURL(const GURL& url, std::string* label) const WARN_UNUSED_RESULT; | 57 bool MatchesURL(const GURL& url, std::string* label) const WARN_UNUSED_RESULT; |
| 60 | 58 |
| 61 // Returns true if the |app_package_name| matches the registered package | 59 // Returns true if the |app_package_name| matches the registered package |
| 62 // names. |label| must not be null. If a match is found, the |label| is set | 60 // names. |label| must not be null. If a match is found, the |label| is set |
| 63 // to the matching rule's label. | 61 // to the matching rule's label. |
| 64 bool MatchesAppPackageName(const std::string& app_package_name, | 62 bool MatchesAppPackageName(const std::string& app_package_name, |
| 65 std::string* label) const WARN_UNUSED_RESULT; | 63 std::string* label) const WARN_UNUSED_RESULT; |
| 66 | 64 |
| 67 // Fetches the matching rules asynchronously from | |
| 68 // |external_data_use_observer_bridge_|. | |
| 69 void FetchMatchingRules(); | |
| 70 | |
| 71 // Returns true if there is any matching rule. HasRules may return true even | 65 // Returns true if there is any matching rule. HasRules may return true even |
| 72 // if all rules are expired. | 66 // if all rules are expired. |
| 73 bool HasRules() const; | 67 bool HasRules() const; |
| 74 | 68 |
| 75 // Returns true if there is any valid matching rule with label |label|. | 69 // Returns true if there is any valid matching rule with label |label|. |
| 76 bool HasValidRuleWithLabel(const std::string& label) const; | 70 bool HasValidRuleWithLabel(const std::string& label) const; |
| 77 | 71 |
| 78 private: | 72 private: |
| 79 friend class DataUseMatcherTest; | 73 friend class DataUseMatcherTest; |
| 80 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, | 74 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 // expected format, |expiration| will be set to default expiration duration | 116 // expected format, |expiration| will be set to default expiration duration |
| 123 // from now, and |new_app_package_name| will be set to the |app_package_name|. | 117 // from now, and |new_app_package_name| will be set to the |app_package_name|. |
| 124 void ParsePackageField(const std::string& app_package_name, | 118 void ParsePackageField(const std::string& app_package_name, |
| 125 std::string* new_app_package_name, | 119 std::string* new_app_package_name, |
| 126 base::TimeTicks* expiration) const; | 120 base::TimeTicks* expiration) const; |
| 127 | 121 |
| 128 base::ThreadChecker thread_checker_; | 122 base::ThreadChecker thread_checker_; |
| 129 | 123 |
| 130 std::vector<std::unique_ptr<MatchingRule>> matching_rules_; | 124 std::vector<std::unique_ptr<MatchingRule>> matching_rules_; |
| 131 | 125 |
| 132 // |data_use_tab_model_| is notified if a label is removed from the set of | |
| 133 // matching labels. | |
| 134 base::WeakPtr<DataUseTabModel> data_use_tab_model_; | |
| 135 | |
| 136 // Default expiration duration of a matching rule, if expiration is not | 126 // Default expiration duration of a matching rule, if expiration is not |
| 137 // specified in the rule. | 127 // specified in the rule. |
| 138 const base::TimeDelta default_matching_rule_expiration_duration_; | 128 const base::TimeDelta default_matching_rule_expiration_duration_; |
| 139 | 129 |
| 140 // TickClock used for obtaining the current time. | 130 // TickClock used for obtaining the current time. |
| 141 std::unique_ptr<base::TickClock> tick_clock_; | 131 std::unique_ptr<base::TickClock> tick_clock_; |
| 142 | 132 |
| 143 // Pointer to the ExternalDataUseObserverBridge owned by | 133 // Callback to be run when a label is removed from the set of matching labels. |
| 144 // ExternalDataUseObserver. DataUseTabModel (owner of |this|) and | 134 const base::Callback<void(const std::string&)>& |
| 145 // ExternalDataUseObserverBridge are owned by ExternalDataUseObserver, and are | 135 on_tracking_label_removed_callback_; |
| 146 // destroyed in that order. So |external_data_use_observer_bridge_| is | 136 |
| 147 // guaranteed to be non-null. | 137 // Callback to be run when matching rules are fetched. |
| 148 const ExternalDataUseObserverBridge* external_data_use_observer_bridge_; | 138 const base::Callback<void(bool)>& on_matching_rules_fetched_callback_; |
| 149 | 139 |
| 150 DISALLOW_COPY_AND_ASSIGN(DataUseMatcher); | 140 DISALLOW_COPY_AND_ASSIGN(DataUseMatcher); |
| 151 }; | 141 }; |
| 152 | 142 |
| 153 } // namespace android | 143 } // namespace android |
| 154 | 144 |
| 155 } // namespace chrome | 145 } // namespace chrome |
| 156 | 146 |
| 157 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ | 147 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ |
| OLD | NEW |