| 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 <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | 18 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class SingleThreadTaskRunner; | |
| 22 class TickClock; | 21 class TickClock; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace re2 { | 24 namespace re2 { |
| 26 class RE2; | 25 class RE2; |
| 27 } | 26 } |
| 28 | 27 |
| 29 class GURL; | 28 class GURL; |
| 30 | 29 |
| 31 namespace chrome { | 30 namespace chrome { |
| 32 | 31 |
| 33 namespace android { | 32 namespace android { |
| 34 | 33 |
| 35 class ExternalDataUseObserver; | 34 class ExternalDataUseObserverBridge; |
| 36 | 35 |
| 37 // DataUseMatcher stores the matching URL patterns and package names along with | 36 // DataUseMatcher stores the matching URL patterns and package names along with |
| 38 // the labels. It also provides functionality to get the matching label for a | 37 // the labels. It also provides functionality to get the matching label for a |
| 39 // given URL or package. DataUseMatcher is not thread safe. | 38 // given URL or package. DataUseMatcher is not thread safe. |
| 40 class DataUseMatcher { | 39 class DataUseMatcher { |
| 41 public: | 40 public: |
| 42 DataUseMatcher( | 41 DataUseMatcher( |
| 43 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, | 42 const base::WeakPtr<DataUseTabModel>& data_use_tab_model, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 43 const ExternalDataUseObserverBridge* external_data_use_observer_bridge, |
| 45 const base::WeakPtr<ExternalDataUseObserver>& external_data_use_observer, | |
| 46 const base::TimeDelta& default_matching_rule_expiration_duration); | 44 const base::TimeDelta& default_matching_rule_expiration_duration); |
| 47 | 45 |
| 48 ~DataUseMatcher(); | 46 ~DataUseMatcher(); |
| 49 | 47 |
| 50 // Called by FetchMatchingRulesDoneOnIOThread to register multiple | 48 // Called by FetchMatchingRulesDoneOnIOThread to register multiple |
| 51 // case-insensitive regular expressions. If the url of the data use request | 49 // case-insensitive regular expressions. If the url of the data use request |
| 52 // matches any of the regular expression, the observation is passed to the | 50 // matches any of the regular expression, the observation is passed to the |
| 53 // Java listener. | 51 // Java listener. |
| 54 void RegisterURLRegexes(const std::vector<std::string>& app_package_names, | 52 void RegisterURLRegexes(const std::vector<std::string>& app_package_names, |
| 55 const std::vector<std::string>& domain_path_regexes, | 53 const std::vector<std::string>& domain_path_regexes, |
| 56 const std::vector<std::string>& labels); | 54 const std::vector<std::string>& labels); |
| 57 | 55 |
| 58 // Returns true if the |url| matches the registered regular expressions. | 56 // Returns true if the |url| matches the registered regular expressions. |
| 59 // |label| must not be null. If a match is found, the |label| is set to the | 57 // |label| must not be null. If a match is found, the |label| is set to the |
| 60 // matching rule's label. | 58 // matching rule's label. |
| 61 bool MatchesURL(const GURL& url, std::string* label) const WARN_UNUSED_RESULT; | 59 bool MatchesURL(const GURL& url, std::string* label) const WARN_UNUSED_RESULT; |
| 62 | 60 |
| 63 // Returns true if the |app_package_name| matches the registered package | 61 // Returns true if the |app_package_name| matches the registered package |
| 64 // names. |label| must not be null. If a match is found, the |label| is set | 62 // names. |label| must not be null. If a match is found, the |label| is set |
| 65 // to the matching rule's label. | 63 // to the matching rule's label. |
| 66 bool MatchesAppPackageName(const std::string& app_package_name, | 64 bool MatchesAppPackageName(const std::string& app_package_name, |
| 67 std::string* label) const WARN_UNUSED_RESULT; | 65 std::string* label) const WARN_UNUSED_RESULT; |
| 68 | 66 |
| 69 // Fetches the matching rules asynchronously from | 67 // Fetches the matching rules asynchronously from |
| 70 // |external_data_use_observer_|. | 68 // |external_data_use_observer_bridge_|. |
| 71 void FetchMatchingRules(); | 69 void FetchMatchingRules(); |
| 72 | 70 |
| 73 // Returns true if there is any valid matching rule. | 71 // Returns true if there is any valid matching rule. |
| 74 bool HasValidRules() const; | 72 bool HasValidRules() const; |
| 75 | 73 |
| 76 private: | 74 private: |
| 77 friend class DataUseMatcherTest; | 75 friend class DataUseMatcherTest; |
| 78 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, | 76 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, |
| 79 EncodeExpirationTimeInPackageName); | 77 EncodeExpirationTimeInPackageName); |
| 80 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, | 78 FRIEND_TEST_ALL_PREFIXES(DataUseMatcherTest, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 // |data_use_tab_model_| is notified if a label is removed from the set of | 128 // |data_use_tab_model_| is notified if a label is removed from the set of |
| 131 // matching labels. | 129 // matching labels. |
| 132 base::WeakPtr<DataUseTabModel> data_use_tab_model_; | 130 base::WeakPtr<DataUseTabModel> data_use_tab_model_; |
| 133 | 131 |
| 134 // Default expiration duration of a matching rule, if expiration is not | 132 // Default expiration duration of a matching rule, if expiration is not |
| 135 // specified in the rule. | 133 // specified in the rule. |
| 136 const base::TimeDelta default_matching_rule_expiration_duration_; | 134 const base::TimeDelta default_matching_rule_expiration_duration_; |
| 137 | 135 |
| 138 // TickClock used for obtaining the current time. | 136 // TickClock used for obtaining the current time. |
| 139 scoped_ptr<base::TickClock> tick_clock_; | 137 scoped_ptr<base::TickClock> tick_clock_; |
| 140 // |io_task_runner_| is used to call ExternalDataUseObserver methods on | |
| 141 // IO thread. | |
| 142 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 143 | 138 |
| 144 // |external_data_use_observer_| is notified when matching rules are fetched. | 139 // Pointer to the ExternalDataUseObserverBridge owned by |
| 145 base::WeakPtr<ExternalDataUseObserver> external_data_use_observer_; | 140 // ExternalDataUseObserver. DataUseTabModel (owner of |this|) and |
| 141 // ExternalDataUseObserverBridge are owned by ExternalDataUseObserver, and are |
| 142 // destroyed in that order. So |external_data_use_observer_bridge_| is |
| 143 // guaranteed to be non-null. |
| 144 const ExternalDataUseObserverBridge* external_data_use_observer_bridge_; |
| 146 | 145 |
| 147 DISALLOW_COPY_AND_ASSIGN(DataUseMatcher); | 146 DISALLOW_COPY_AND_ASSIGN(DataUseMatcher); |
| 148 }; | 147 }; |
| 149 | 148 |
| 150 } // namespace android | 149 } // namespace android |
| 151 | 150 |
| 152 } // namespace chrome | 151 } // namespace chrome |
| 153 | 152 |
| 154 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ | 153 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_MATCHER_H_ |
| OLD | NEW |