OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ | 5 #ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ |
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ | 6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
17 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
18 #include "base/values.h" | 17 #include "base/values.h" |
19 #include "chrome/browser/safe_search_api/safe_search_url_checker.h" | 18 #include "chrome/browser/safe_search_api/safe_search_url_checker.h" |
20 #include "chrome/browser/supervised_user/supervised_user_site_list.h" | 19 #include "chrome/browser/supervised_user/supervised_user_site_list.h" |
21 #include "chrome/browser/supervised_user/supervised_users.h" | 20 #include "chrome/browser/supervised_user/supervised_users.h" |
22 #include "components/supervised_user_error_page/supervised_user_error_page.h" | 21 #include "components/supervised_user_error_page/supervised_user_error_page.h" |
23 #include "third_party/re2/src/re2/re2.h" | 22 #include "third_party/re2/src/re2/re2.h" |
24 | 23 |
25 class GURL; | 24 class GURL; |
26 class SupervisedUserBlacklist; | 25 class SupervisedUserBlacklist; |
27 | 26 |
28 namespace base { | 27 namespace base { |
29 class TaskRunner; | 28 class TaskRunner; |
30 } | 29 } |
31 | 30 |
32 namespace net { | 31 namespace net { |
33 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
34 } | 33 } |
35 | 34 |
36 // This class manages the filtering behavior for a given URL, i.e. it tells | 35 // This class manages the filtering behavior for URLs, i.e. it tells callers |
37 // callers if a given URL should be allowed, blocked or warned about. It uses | 36 // if a URL should be allowed, blocked or warned about. It uses information |
38 // information from multiple sources: | 37 // from multiple sources: |
39 // * A default setting (allow, block or warn). | 38 // * A default setting (allow, block or warn). |
40 // * The set of installed and enabled whitelists which contain URL patterns | 39 // * The set of installed and enabled whitelists which contain URL patterns |
41 // and hostname hashes that should be allowed. | 40 // and hostname hashes that should be allowed. |
42 // * User-specified manual overrides (allow or block) for either sites | 41 // * User-specified manual overrides (allow or block) for either sites |
43 // (hostnames) or exact URLs, which take precedence over the previous | 42 // (hostnames) or exact URLs, which take precedence over the previous |
44 // sources. | 43 // sources. |
45 // References to it can be passed around on different threads (the refcounting | 44 class SupervisedUserURLFilter : public base::NonThreadSafe { |
46 // is thread-safe), but the object itself should always be accessed on the same | |
47 // thread (member access isn't thread-safe). | |
48 class SupervisedUserURLFilter | |
49 : public base::RefCountedThreadSafe<SupervisedUserURLFilter>, | |
50 public base::NonThreadSafe { | |
51 public: | 45 public: |
52 enum FilteringBehavior { | 46 enum FilteringBehavior { |
53 ALLOW, | 47 ALLOW, |
54 WARN, | 48 WARN, |
55 BLOCK, | 49 BLOCK, |
56 INVALID | 50 INVALID |
57 }; | 51 }; |
58 | 52 |
59 using FilteringBehaviorCallback = | 53 using FilteringBehaviorCallback = |
60 base::Callback<void(FilteringBehavior, | 54 base::Callback<void(FilteringBehavior, |
61 supervised_user_error_page::FilteringBehaviorReason, | 55 supervised_user_error_page::FilteringBehaviorReason, |
62 bool /* uncertain */)>; | 56 bool /* uncertain */)>; |
63 | 57 |
64 class Observer { | 58 class Observer { |
65 public: | 59 public: |
66 virtual void OnSiteListUpdated() = 0; | 60 virtual void OnSiteListUpdated() = 0; |
67 virtual void OnURLChecked( | 61 virtual void OnURLChecked( |
68 const GURL& url, | 62 const GURL& url, |
69 FilteringBehavior behavior, | 63 FilteringBehavior behavior, |
70 supervised_user_error_page::FilteringBehaviorReason reason, | 64 supervised_user_error_page::FilteringBehaviorReason reason, |
71 bool uncertain) {} | 65 bool uncertain) {} |
72 }; | 66 }; |
73 | 67 |
74 struct Contents; | 68 struct Contents; |
75 | 69 |
76 SupervisedUserURLFilter(); | 70 SupervisedUserURLFilter(); |
| 71 ~SupervisedUserURLFilter(); |
77 | 72 |
78 static FilteringBehavior BehaviorFromInt(int behavior_value); | 73 static FilteringBehavior BehaviorFromInt(int behavior_value); |
79 | 74 |
80 static bool ReasonIsAutomatic( | 75 static bool ReasonIsAutomatic( |
81 supervised_user_error_page::FilteringBehaviorReason reason); | 76 supervised_user_error_page::FilteringBehaviorReason reason); |
82 | 77 |
83 // Normalizes a URL for matching purposes. | 78 // Normalizes a URL for matching purposes. |
84 static GURL Normalize(const GURL& url); | 79 static GURL Normalize(const GURL& url); |
85 | 80 |
86 // For known "cache" URLs (e.g. from the AMP project CDN), this returns the | 81 // For known "cache" URLs (e.g. from the AMP project CDN), this returns the |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 bool HasBlacklist() const; | 143 bool HasBlacklist() const; |
149 | 144 |
150 // Set the list of matched patterns to the passed in list, for testing. | 145 // Set the list of matched patterns to the passed in list, for testing. |
151 void SetFromPatternsForTesting(const std::vector<std::string>& patterns); | 146 void SetFromPatternsForTesting(const std::vector<std::string>& patterns); |
152 | 147 |
153 // Sets the site lists to the passed list, for testing. | 148 // Sets the site lists to the passed list, for testing. |
154 void SetFromSiteListsForTesting( | 149 void SetFromSiteListsForTesting( |
155 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists); | 150 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists); |
156 | 151 |
157 // Sets the set of manually allowed or blocked hosts. | 152 // Sets the set of manually allowed or blocked hosts. |
158 void SetManualHosts(const std::map<std::string, bool>* host_map); | 153 void SetManualHosts(std::map<std::string, bool> host_map); |
159 | 154 |
160 // Sets the set of manually allowed or blocked URLs. | 155 // Sets the set of manually allowed or blocked URLs. |
161 void SetManualURLs(const std::map<GURL, bool>* url_map); | 156 void SetManualURLs(std::map<GURL, bool> url_map); |
162 | 157 |
163 // Initializes the experimental asynchronous checker. | 158 // Initializes the experimental asynchronous checker. |
164 void InitAsyncURLChecker(net::URLRequestContextGetter* context); | 159 void InitAsyncURLChecker(net::URLRequestContextGetter* context); |
165 | 160 |
166 // Clears any asynchronous checker. | 161 // Clears any asynchronous checker. |
167 void ClearAsyncURLChecker(); | 162 void ClearAsyncURLChecker(); |
168 | 163 |
169 // Returns whether the asynchronous checker is set up. | 164 // Returns whether the asynchronous checker is set up. |
170 bool HasAsyncURLChecker() const; | 165 bool HasAsyncURLChecker() const; |
171 | 166 |
172 // Removes all filter entries, clears the blacklist and async checker if | 167 // Removes all filter entries, clears the blacklist and async checker if |
173 // present, and resets the default behavior to "allow". | 168 // present, and resets the default behavior to "allow". |
174 void Clear(); | 169 void Clear(); |
175 | 170 |
176 void AddObserver(Observer* observer) const; | 171 void AddObserver(Observer* observer) const; |
177 void RemoveObserver(Observer* observer) const; | 172 void RemoveObserver(Observer* observer) const; |
178 | 173 |
179 // Sets a different task runner for testing. | 174 // Sets a different task runner for testing. |
180 void SetBlockingTaskRunnerForTesting( | 175 void SetBlockingTaskRunnerForTesting( |
181 const scoped_refptr<base::TaskRunner>& task_runner); | 176 const scoped_refptr<base::TaskRunner>& task_runner); |
182 | 177 |
183 private: | 178 private: |
184 friend class base::RefCountedThreadSafe<SupervisedUserURLFilter>; | |
185 friend class SupervisedUserURLFilterTest; | 179 friend class SupervisedUserURLFilterTest; |
186 ~SupervisedUserURLFilter(); | |
187 | 180 |
188 void SetContents(std::unique_ptr<Contents> url_matcher); | 181 void SetContents(std::unique_ptr<Contents> url_matcher); |
189 | 182 |
190 FilteringBehavior GetFilteringBehaviorForURL( | 183 FilteringBehavior GetFilteringBehaviorForURL( |
191 const GURL& url, | 184 const GURL& url, |
192 bool manual_only, | 185 bool manual_only, |
193 supervised_user_error_page::FilteringBehaviorReason* reason) const; | 186 supervised_user_error_page::FilteringBehaviorReason* reason) const; |
194 | 187 |
195 void CheckCallback(const FilteringBehaviorCallback& callback, | 188 void CheckCallback(const FilteringBehaviorCallback& callback, |
196 const GURL& url, | 189 const GURL& url, |
(...skipping 18 matching lines...) Expand all Loading... |
215 const SupervisedUserBlacklist* blacklist_; | 208 const SupervisedUserBlacklist* blacklist_; |
216 | 209 |
217 std::unique_ptr<SafeSearchURLChecker> async_url_checker_; | 210 std::unique_ptr<SafeSearchURLChecker> async_url_checker_; |
218 | 211 |
219 re2::RE2 amp_cache_path_regex_; | 212 re2::RE2 amp_cache_path_regex_; |
220 re2::RE2 google_amp_viewer_path_regex_; | 213 re2::RE2 google_amp_viewer_path_regex_; |
221 re2::RE2 google_web_cache_query_regex_; | 214 re2::RE2 google_web_cache_query_regex_; |
222 | 215 |
223 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 216 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
224 | 217 |
| 218 base::WeakPtrFactory<SupervisedUserURLFilter> weak_ptr_factory_; |
| 219 |
225 DISALLOW_COPY_AND_ASSIGN(SupervisedUserURLFilter); | 220 DISALLOW_COPY_AND_ASSIGN(SupervisedUserURLFilter); |
226 }; | 221 }; |
227 | 222 |
228 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ | 223 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_URL_FILTER_H_ |
OLD | NEW |