| 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 <string> | 9 #include <string> |
| 9 #include <vector> | |
| 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/hash_tables.h" | 14 #include "base/hash_tables.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "chrome/browser/prefs/pref_change_registrar.h" | 17 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 18 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 19 | 20 |
| 20 class GURL; | 21 class GURL; |
| 21 class PrefService; | 22 class PrefService; |
| 22 | 23 |
| 24 namespace base { |
| 25 class ListValue; |
| 26 } |
| 27 |
| 23 namespace policy { | 28 namespace policy { |
| 24 | 29 |
| 25 // Contains a set of filters to block and allow certain URLs, and matches GURLs | 30 // Contains a set of filters to block and allow certain URLs, and matches GURLs |
| 26 // against this set. The filters are currently kept in memory. | 31 // against this set. The filters are currently kept in memory. |
| 27 class URLBlacklist { | 32 class URLBlacklist { |
| 28 public: | 33 public: |
| 29 // A constant mapped to a scheme that can be filtered. | |
| 30 enum SchemeFlag { | |
| 31 SCHEME_HTTP = 1 << 0, | |
| 32 SCHEME_HTTPS = 1 << 1, | |
| 33 SCHEME_FTP = 1 << 2, | |
| 34 SCHEME_WS = 1 << 3, | |
| 35 SCHEME_WSS = 1 << 4, | |
| 36 | |
| 37 SCHEME_ALL = (1 << 5) - 1, | |
| 38 }; | |
| 39 | |
| 40 URLBlacklist(); | 34 URLBlacklist(); |
| 41 virtual ~URLBlacklist(); | 35 virtual ~URLBlacklist(); |
| 42 | 36 |
| 43 // URLs matching |filter| will be blocked. The filter format is documented | 37 // Allows or blocks URLs matching one of the filters, depending on |allow|. |
| 44 // at http://www.chromium.org/administrators/url-blacklist-filter-format | 38 void AddFilters(bool allow, const base::ListValue* filters); |
| 45 void Block(const std::string& filter); | |
| 46 | 39 |
| 47 // URLs matching |filter| will be allowed. If |filter| is both Blocked and | 40 // URLs matching one of the |filters| will be blocked. The filter format is |
| 48 // Allowed, Allow takes precedence. | 41 // documented at |
| 49 void Allow(const std::string& filter); | 42 // http://www.chromium.org/administrators/url-blacklist-filter-format. |
| 43 void Block(const base::ListValue* filters); |
| 44 |
| 45 // URLs matching one of the |filters| will be allowed. If a URL is both |
| 46 // Blocked and Allowed, Allow takes precedence. |
| 47 void Allow(const base::ListValue* filters); |
| 50 | 48 |
| 51 // Returns true if the URL is blocked. | 49 // Returns true if the URL is blocked. |
| 52 bool IsURLBlocked(const GURL& url) const; | 50 bool IsURLBlocked(const GURL& url) const; |
| 53 | 51 |
| 54 // Returns true if |scheme| is a scheme that can be filtered. Returns true | 52 // Returns true if the URL has a standard scheme. Only URLs with standard |
| 55 // and sets |flag| to SCHEME_ALL if |scheme| is empty. | 53 // schemes are filtered. |
| 56 static bool SchemeToFlag(const std::string& scheme, SchemeFlag* flag); | 54 static bool HasStandardScheme(const GURL& url); |
| 57 | 55 |
| 58 // Splits a URL filter into its components. A GURL isn't used because these | 56 // Splits a URL filter into its components. A GURL isn't used because these |
| 59 // can be invalid URLs e.g. "google.com". | 57 // can be invalid URLs e.g. "google.com". |
| 60 // Returns false if the URL couldn't be parsed. | 58 // Returns false if the URL couldn't be parsed. |
| 59 // The |host| is preprocessed so it can be passed to URLMatcher for the |
| 60 // appropriate condition. |
| 61 // The optional username and password are ignored. | 61 // The optional username and password are ignored. |
| 62 // |match_subdomains| specifies whether the filter should include subdomains |
| 63 // of the hostname (if it is one.) |
| 62 // |port| is 0 if none is explicitly defined. | 64 // |port| is 0 if none is explicitly defined. |
| 63 // |path| does not include query parameters. | 65 // |path| does not include query parameters. |
| 64 static bool FilterToComponents(const std::string& filter, | 66 static bool FilterToComponents(const std::string& filter, |
| 65 std::string* scheme, | 67 std::string* scheme, |
| 66 std::string* host, | 68 std::string* host, |
| 69 bool* match_subdomains, |
| 67 uint16* port, | 70 uint16* port, |
| 68 std::string* path); | 71 std::string* path); |
| 72 |
| 73 // Creates a condition set that can be used with the |url_matcher|. |id| needs |
| 74 // to be a unique number that will be returned by the |url_matcher| if the URL |
| 75 // matches that condition set. |
| 76 static scoped_refptr<extensions::URLMatcherConditionSet> CreateConditionSet( |
| 77 extensions::URLMatcher* url_matcher, |
| 78 extensions::URLMatcherConditionSet::ID id, |
| 79 const std::string& scheme, |
| 80 const std::string& host, |
| 81 bool match_subdomains, |
| 82 uint16 port, |
| 83 const std::string& path); |
| 84 |
| 69 private: | 85 private: |
| 70 struct PathFilter; | 86 struct FilterComponents; |
| 71 | 87 |
| 72 typedef std::vector<PathFilter> PathFilterList; | 88 // Returns true if |lhs| takes precedence over |rhs|. |
| 73 typedef base::hash_map<std::string, PathFilterList*> HostFilterTable; | 89 static bool FilterTakesPrecedence(const FilterComponents& lhs, |
| 90 const FilterComponents& rhs); |
| 74 | 91 |
| 75 void AddFilter(const std::string& filter, bool block); | 92 extensions::URLMatcherConditionSet::ID id_; |
| 76 | 93 std::map<extensions::URLMatcherConditionSet::ID, FilterComponents> filters_; |
| 77 HostFilterTable host_filters_; | 94 scoped_ptr<extensions::URLMatcher> url_matcher_; |
| 78 | 95 |
| 79 DISALLOW_COPY_AND_ASSIGN(URLBlacklist); | 96 DISALLOW_COPY_AND_ASSIGN(URLBlacklist); |
| 80 }; | 97 }; |
| 81 | 98 |
| 82 // Tracks the blacklist policies for a given profile, and updates it on changes. | 99 // Tracks the blacklist policies for a given profile, and updates it on changes. |
| 83 // | 100 // |
| 84 // This class interacts with both the UI thread, where notifications of pref | 101 // This class interacts with both the UI thread, where notifications of pref |
| 85 // changes are received from, and the IO thread, which owns it (in the | 102 // changes are received from, and the IO thread, which owns it (in the |
| 86 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). | 103 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). |
| 87 // | 104 // |
| (...skipping 15 matching lines...) Expand all Loading... |
| 103 virtual ~URLBlacklistManager(); | 120 virtual ~URLBlacklistManager(); |
| 104 | 121 |
| 105 // Must be called on the UI thread, before destruction. | 122 // Must be called on the UI thread, before destruction. |
| 106 void ShutdownOnUIThread(); | 123 void ShutdownOnUIThread(); |
| 107 | 124 |
| 108 // Returns true if |url| is blocked by the current blacklist. Must be called | 125 // Returns true if |url| is blocked by the current blacklist. Must be called |
| 109 // from the IO thread. | 126 // from the IO thread. |
| 110 bool IsURLBlocked(const GURL& url) const; | 127 bool IsURLBlocked(const GURL& url) const; |
| 111 | 128 |
| 112 // Replaces the current blacklist. Must be called on the IO thread. | 129 // Replaces the current blacklist. Must be called on the IO thread. |
| 113 void SetBlacklist(URLBlacklist* blacklist); | 130 // Virtual for testing. |
| 131 virtual void SetBlacklist(scoped_ptr<URLBlacklist> blacklist); |
| 114 | 132 |
| 115 // Registers the preferences related to blacklisting in the given PrefService. | 133 // Registers the preferences related to blacklisting in the given PrefService. |
| 116 static void RegisterPrefs(PrefService* pref_service); | 134 static void RegisterPrefs(PrefService* pref_service); |
| 117 | 135 |
| 118 protected: | 136 protected: |
| 119 typedef std::vector<std::string> StringVector; | |
| 120 | |
| 121 // Used to delay updating the blacklist while the preferences are | 137 // Used to delay updating the blacklist while the preferences are |
| 122 // changing, and execute only one update per simultaneous prefs changes. | 138 // changing, and execute only one update per simultaneous prefs changes. |
| 123 void ScheduleUpdate(); | 139 void ScheduleUpdate(); |
| 124 | 140 |
| 125 // Updates the blacklist using the current preference values. | 141 // Updates the blacklist using the current preference values. |
| 126 // Virtual for testing. | 142 // Virtual for testing. |
| 127 virtual void Update(); | 143 virtual void Update(); |
| 128 | 144 |
| 129 // Starts the blacklist update on the IO thread, using the filters in | 145 // Starts the blacklist update on the IO thread, using the filters in |
| 130 // |block| and |allow|. Protected for testing. | 146 // |block| and |allow|. Protected for testing. |
| 131 void UpdateOnIO(StringVector* block, StringVector* allow); | 147 void UpdateOnIO(scoped_ptr<base::ListValue> block, |
| 148 scoped_ptr<base::ListValue> allow); |
| 132 | 149 |
| 133 private: | 150 private: |
| 134 virtual void Observe(int type, | 151 virtual void Observe(int type, |
| 135 const content::NotificationSource& source, | 152 const content::NotificationSource& source, |
| 136 const content::NotificationDetails& details) OVERRIDE; | 153 const content::NotificationDetails& details) OVERRIDE; |
| 137 | 154 |
| 138 // --------- | 155 // --------- |
| 139 // UI thread | 156 // UI thread |
| 140 // --------- | 157 // --------- |
| 141 | 158 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 155 | 172 |
| 156 // The current blacklist. | 173 // The current blacklist. |
| 157 scoped_ptr<URLBlacklist> blacklist_; | 174 scoped_ptr<URLBlacklist> blacklist_; |
| 158 | 175 |
| 159 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); | 176 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); |
| 160 }; | 177 }; |
| 161 | 178 |
| 162 } // namespace policy | 179 } // namespace policy |
| 163 | 180 |
| 164 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | 181 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
| OLD | NEW |