| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_POLICY_URL_BLACKLIST_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
| 6 #define CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | 6 #define CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/prefs/pref_change_registrar.h" | 18 #include "base/prefs/pref_change_registrar.h" |
| 18 #include "components/url_matcher/url_matcher.h" | 19 #include "components/url_matcher/url_matcher.h" |
| 20 #include "url/gurl.h" |
| 19 | 21 |
| 20 class GURL; | |
| 21 class PrefService; | 22 class PrefService; |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 class ListValue; | 25 class ListValue; |
| 26 class SequencedTaskRunner; |
| 25 } | 27 } |
| 26 | 28 |
| 27 namespace net { | 29 namespace net { |
| 28 class URLRequest; | 30 class URLRequest; |
| 29 } | 31 } |
| 30 | 32 |
| 31 namespace user_prefs { | 33 namespace user_prefs { |
| 32 class PrefRegistrySyncable; | 34 class PrefRegistrySyncable; |
| 33 } | 35 } |
| 34 | 36 |
| 35 namespace policy { | 37 namespace policy { |
| 36 | 38 |
| 37 // Contains a set of filters to block and allow certain URLs, and matches GURLs | 39 // Contains a set of filters to block and allow certain URLs, and matches GURLs |
| 38 // against this set. The filters are currently kept in memory. | 40 // against this set. The filters are currently kept in memory. |
| 39 class URLBlacklist { | 41 class URLBlacklist { |
| 40 public: | 42 public: |
| 41 URLBlacklist(); | 43 // This is meant to be bound to URLFixerUpper::SegmentURL. See that function |
| 44 // for documentation on the parameters and return value. |
| 45 typedef std::string (*SegmentURLCallback)(const std::string&, |
| 46 url_parse::Parsed*); |
| 47 |
| 48 explicit URLBlacklist(SegmentURLCallback segment_url); |
| 42 virtual ~URLBlacklist(); | 49 virtual ~URLBlacklist(); |
| 43 | 50 |
| 44 // Allows or blocks URLs matching one of the filters, depending on |allow|. | 51 // Allows or blocks URLs matching one of the filters, depending on |allow|. |
| 45 void AddFilters(bool allow, const base::ListValue* filters); | 52 void AddFilters(bool allow, const base::ListValue* filters); |
| 46 | 53 |
| 47 // URLs matching one of the |filters| will be blocked. The filter format is | 54 // URLs matching one of the |filters| will be blocked. The filter format is |
| 48 // documented at | 55 // documented at |
| 49 // http://www.chromium.org/administrators/url-blacklist-filter-format. | 56 // http://www.chromium.org/administrators/url-blacklist-filter-format. |
| 50 void Block(const base::ListValue* filters); | 57 void Block(const base::ListValue* filters); |
| 51 | 58 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 62 // Splits a URL filter into its components. A GURL isn't used because these | 69 // Splits a URL filter into its components. A GURL isn't used because these |
| 63 // can be invalid URLs e.g. "google.com". | 70 // can be invalid URLs e.g. "google.com". |
| 64 // Returns false if the URL couldn't be parsed. | 71 // Returns false if the URL couldn't be parsed. |
| 65 // The |host| is preprocessed so it can be passed to URLMatcher for the | 72 // The |host| is preprocessed so it can be passed to URLMatcher for the |
| 66 // appropriate condition. | 73 // appropriate condition. |
| 67 // The optional username and password are ignored. | 74 // The optional username and password are ignored. |
| 68 // |match_subdomains| specifies whether the filter should include subdomains | 75 // |match_subdomains| specifies whether the filter should include subdomains |
| 69 // of the hostname (if it is one.) | 76 // of the hostname (if it is one.) |
| 70 // |port| is 0 if none is explicitly defined. | 77 // |port| is 0 if none is explicitly defined. |
| 71 // |path| does not include query parameters. | 78 // |path| does not include query parameters. |
| 72 static bool FilterToComponents(const std::string& filter, | 79 static bool FilterToComponents(SegmentURLCallback segment_url, |
| 80 const std::string& filter, |
| 73 std::string* scheme, | 81 std::string* scheme, |
| 74 std::string* host, | 82 std::string* host, |
| 75 bool* match_subdomains, | 83 bool* match_subdomains, |
| 76 uint16* port, | 84 uint16* port, |
| 77 std::string* path); | 85 std::string* path); |
| 78 | 86 |
| 79 // Creates a condition set that can be used with the |url_matcher|. |id| needs | 87 // Creates a condition set that can be used with the |url_matcher|. |id| needs |
| 80 // to be a unique number that will be returned by the |url_matcher| if the URL | 88 // to be a unique number that will be returned by the |url_matcher| if the URL |
| 81 // matches that condition set. | 89 // matches that condition set. |
| 82 static scoped_refptr<url_matcher::URLMatcherConditionSet> CreateConditionSet( | 90 static scoped_refptr<url_matcher::URLMatcherConditionSet> CreateConditionSet( |
| 83 url_matcher::URLMatcher* url_matcher, | 91 url_matcher::URLMatcher* url_matcher, |
| 84 url_matcher::URLMatcherConditionSet::ID id, | 92 url_matcher::URLMatcherConditionSet::ID id, |
| 85 const std::string& scheme, | 93 const std::string& scheme, |
| 86 const std::string& host, | 94 const std::string& host, |
| 87 bool match_subdomains, | 95 bool match_subdomains, |
| 88 uint16 port, | 96 uint16 port, |
| 89 const std::string& path); | 97 const std::string& path); |
| 90 | 98 |
| 91 private: | 99 private: |
| 92 struct FilterComponents; | 100 struct FilterComponents; |
| 93 | 101 |
| 94 // Returns true if |lhs| takes precedence over |rhs|. | 102 // Returns true if |lhs| takes precedence over |rhs|. |
| 95 static bool FilterTakesPrecedence(const FilterComponents& lhs, | 103 static bool FilterTakesPrecedence(const FilterComponents& lhs, |
| 96 const FilterComponents& rhs); | 104 const FilterComponents& rhs); |
| 97 | 105 |
| 106 SegmentURLCallback segment_url_; |
| 98 url_matcher::URLMatcherConditionSet::ID id_; | 107 url_matcher::URLMatcherConditionSet::ID id_; |
| 99 std::map<url_matcher::URLMatcherConditionSet::ID, FilterComponents> filters_; | 108 std::map<url_matcher::URLMatcherConditionSet::ID, FilterComponents> filters_; |
| 100 scoped_ptr<url_matcher::URLMatcher> url_matcher_; | 109 scoped_ptr<url_matcher::URLMatcher> url_matcher_; |
| 101 | 110 |
| 102 DISALLOW_COPY_AND_ASSIGN(URLBlacklist); | 111 DISALLOW_COPY_AND_ASSIGN(URLBlacklist); |
| 103 }; | 112 }; |
| 104 | 113 |
| 105 // Tracks the blacklist policies for a given profile, and updates it on changes. | 114 // Tracks the blacklist policies for a given profile, and updates it on changes. |
| 106 // | 115 // |
| 107 // This class interacts with both the UI thread, where notifications of pref | 116 // This class interacts with both the UI thread, where notifications of pref |
| 108 // changes are received from, and the IO thread, which owns it (in the | 117 // changes are received from, and the IO thread, which owns it (in the |
| 109 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). | 118 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). |
| 110 // | 119 // |
| 111 // It must be constructed on the UI thread, to set up |ui_weak_ptr_factory_| and | 120 // It must be constructed on the UI thread, to set up |ui_weak_ptr_factory_| and |
| 112 // the prefs listeners. | 121 // the prefs listeners. |
| 113 // | 122 // |
| 114 // ShutdownOnUIThread must be called from UI before destruction, to release | 123 // ShutdownOnUIThread must be called from UI before destruction, to release |
| 115 // the prefs listeners on the UI thread. This is done from ProfileIOData. | 124 // the prefs listeners on the UI thread. This is done from ProfileIOData. |
| 116 // | 125 // |
| 117 // Update tasks from the UI thread can post safely to the IO thread, since the | 126 // Update tasks from the UI thread can post safely to the IO thread, since the |
| 118 // destruction order of Profile and ProfileIOData guarantees that if this | 127 // destruction order of Profile and ProfileIOData guarantees that if this |
| 119 // exists in UI, then a potential destruction on IO will come after any task | 128 // exists in UI, then a potential destruction on IO will come after any task |
| 120 // posted to IO from that method on UI. This is used to go through IO before | 129 // posted to IO from that method on UI. This is used to go through IO before |
| 121 // the actual update starts, and grab a WeakPtr. | 130 // the actual update starts, and grab a WeakPtr. |
| 122 class URLBlacklistManager { | 131 class URLBlacklistManager { |
| 123 public: | 132 public: |
| 133 // Returns true if the blacklist should be skipped for |url|. |
| 134 typedef bool (*SkipBlacklistCallback)(const GURL& url); |
| 135 |
| 124 // Must be constructed on the UI thread. | 136 // Must be constructed on the UI thread. |
| 125 explicit URLBlacklistManager(PrefService* pref_service); | 137 // |io_task_runner| must be backed by the IO thread. |
| 138 // |segment_url| is used to break a URL spec into its components. |
| 139 URLBlacklistManager( |
| 140 PrefService* pref_service, |
| 141 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 142 URLBlacklist::SegmentURLCallback segment_url, |
| 143 SkipBlacklistCallback skip_blacklist); |
| 126 virtual ~URLBlacklistManager(); | 144 virtual ~URLBlacklistManager(); |
| 127 | 145 |
| 128 // Must be called on the UI thread, before destruction. | 146 // Must be called on the UI thread, before destruction. |
| 129 void ShutdownOnUIThread(); | 147 void ShutdownOnUIThread(); |
| 130 | 148 |
| 131 // Returns true if |url| is blocked by the current blacklist. Must be called | 149 // Returns true if |url| is blocked by the current blacklist. Must be called |
| 132 // from the IO thread. | 150 // from the IO thread. |
| 133 bool IsURLBlocked(const GURL& url) const; | 151 bool IsURLBlocked(const GURL& url) const; |
| 134 | 152 |
| 135 // Returns true if |request| is blocked by the current blacklist. | 153 // Returns true if |request| is blocked by the current blacklist. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 165 // UI thread | 183 // UI thread |
| 166 // --------- | 184 // --------- |
| 167 | 185 |
| 168 // Used to post update tasks to the UI thread. | 186 // Used to post update tasks to the UI thread. |
| 169 base::WeakPtrFactory<URLBlacklistManager> ui_weak_ptr_factory_; | 187 base::WeakPtrFactory<URLBlacklistManager> ui_weak_ptr_factory_; |
| 170 | 188 |
| 171 // Used to track the policies and update the blacklist on changes. | 189 // Used to track the policies and update the blacklist on changes. |
| 172 PrefChangeRegistrar pref_change_registrar_; | 190 PrefChangeRegistrar pref_change_registrar_; |
| 173 PrefService* pref_service_; // Weak. | 191 PrefService* pref_service_; // Weak. |
| 174 | 192 |
| 193 // Used to post tasks to the IO thread. |
| 194 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 195 |
| 196 // Used to break a URL into its components. |
| 197 URLBlacklist::SegmentURLCallback segment_url_; |
| 198 |
| 199 // Used to optionally skip blacklisting for some URLs. |
| 200 SkipBlacklistCallback skip_blacklist_; |
| 201 |
| 175 // --------- | 202 // --------- |
| 176 // IO thread | 203 // IO thread |
| 177 // --------- | 204 // --------- |
| 178 | 205 |
| 179 // Used to get |weak_ptr_| to self on the IO thread. | 206 // Used to get |weak_ptr_| to self on the IO thread. |
| 180 base::WeakPtrFactory<URLBlacklistManager> io_weak_ptr_factory_; | 207 base::WeakPtrFactory<URLBlacklistManager> io_weak_ptr_factory_; |
| 181 | 208 |
| 209 // Used to post tasks to the UI thread. |
| 210 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 211 |
| 182 // The current blacklist. | 212 // The current blacklist. |
| 183 scoped_ptr<URLBlacklist> blacklist_; | 213 scoped_ptr<URLBlacklist> blacklist_; |
| 184 | 214 |
| 185 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); | 215 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); |
| 186 }; | 216 }; |
| 187 | 217 |
| 188 } // namespace policy | 218 } // namespace policy |
| 189 | 219 |
| 190 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | 220 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
| OLD | NEW |