OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ | |
6 #define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/gtest_prod_util.h" | |
13 #include "base/linked_ptr.h" | |
14 #include "base/ref_counted.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "net/url_request/url_request.h" | |
17 | |
18 class DictionaryValue; | |
19 class FilePath; | |
20 class ListValue; | |
21 class PrefService; | |
22 | |
23 //////////////////////////////////////////////////////////////////////////////// | |
24 // | |
25 // Blacklist Class | |
26 // | |
27 // Represents a blacklist used to protect user from privacy and annoyances. | |
28 // A blacklist is essentially a map from resource-match patterns to filter- | |
29 // attributes. Each time a resources matches a pattern the filter-attributes | |
30 // are used to determine how the browser handles the matching resource. | |
31 //////////////////////////////////////////////////////////////////////////////// | |
32 class Blacklist : public base::RefCountedThreadSafe<Blacklist> { | |
33 public: | |
34 class Entry; | |
35 class Provider; | |
36 | |
37 typedef std::vector<linked_ptr<Entry> > EntryList; | |
38 typedef std::vector<linked_ptr<Provider> > ProviderList; | |
39 | |
40 // Filter attributes (more to come): | |
41 static const unsigned int kBlockAll; | |
42 static const unsigned int kBlockCookies; | |
43 static const unsigned int kDontSendReferrer; | |
44 static const unsigned int kDontSendUserAgent; | |
45 static const unsigned int kBlockUnsecure; | |
46 | |
47 // Aggregate filter types: | |
48 static const unsigned int kBlockRequest; | |
49 static const unsigned int kBlockResponse; | |
50 static const unsigned int kModifySentHeaders; | |
51 static const unsigned int kModifyReceivedHeaders; | |
52 | |
53 // Blacklist entries come from a provider, defined by a name and source URL. | |
54 class Provider { | |
55 public: | |
56 Provider() {} | |
57 Provider(const std::string& name, const std::string& url, | |
58 const std::wstring& pref_path) | |
59 : name_(name), | |
60 pref_path_(pref_path), | |
61 url_(url) {} | |
62 | |
63 const std::string& name() const { return name_; } | |
64 void set_name(const std::string& name) { name_ = name; } | |
65 | |
66 const std::wstring& pref_path() const { return pref_path_; } | |
67 void set_pref_path(const std::wstring& pref_path) { | |
68 pref_path_ = pref_path; | |
69 } | |
70 | |
71 const std::string& url() const { return url_; } | |
72 void set_url(const std::string& url) { url_ = url; } | |
73 | |
74 private: | |
75 std::string name_; | |
76 std::wstring pref_path_; | |
77 std::string url_; | |
78 }; | |
79 | |
80 // A single blacklist entry which is returned when a URL matches one of | |
81 // the patterns. Entry objects are owned by the Blacklist that stores them. | |
82 class Entry { | |
83 public: | |
84 // Construct with given pattern. | |
85 Entry(const std::string& pattern, const Provider* provider, | |
86 bool is_exception); | |
87 | |
88 // Returns the pattern which this entry matches. | |
89 const std::string& pattern() const { return pattern_; } | |
90 | |
91 // Bitfield of filter-attributes matching the pattern. | |
92 unsigned int attributes() const { return attributes_; } | |
93 | |
94 // True if this entry is an exception to the blacklist. | |
95 bool is_exception() const { return is_exception_; } | |
96 | |
97 // Provider of this blacklist entry, used for assigning blame ;) | |
98 const Provider* provider() const { return provider_; } | |
99 | |
100 // Returns true of the given URL is blocked, assumes it matches the | |
101 // pattern of this entry. | |
102 bool IsBlocked(const GURL&) const; | |
103 | |
104 void AddAttributes(unsigned int attributes); | |
105 | |
106 private: | |
107 unsigned int attributes_; | |
108 | |
109 // True if this entry is an exception to the blacklist. | |
110 bool is_exception_; | |
111 std::string pattern_; | |
112 | |
113 // Points to the provider of this entry, the providers are all | |
114 // owned by the blacklist. | |
115 const Provider* provider_; | |
116 }; | |
117 | |
118 // A request may match one or more Blacklist rules. The Match class packages | |
119 // all the matching entries behind a single interface with access to the | |
120 // underlying set of entries so that we can display provider information. | |
121 // Often a match must be applied after a URLRequest has started, so it gets | |
122 // tagged with the Match object to avoid doing lookups more than once per | |
123 // request. | |
124 class Match : public URLRequest::UserData { | |
125 public: | |
126 // Functions that return combined results from all entries. | |
127 unsigned int attributes() const { | |
128 return (matching_attributes_ & (~exception_attributes_)); | |
129 } | |
130 bool IsBlocked(const GURL&) const; | |
131 | |
132 // Access to individual entries, mostly for display/logging purposes. | |
133 const std::vector<const Entry*>& entries() const { | |
134 return matching_entries_; | |
135 } | |
136 | |
137 private: | |
138 Match(); | |
139 void AddEntry(const Entry* entry); | |
140 | |
141 std::vector<const Entry*> matching_entries_; | |
142 std::vector<const Entry*> exception_entries_; | |
143 | |
144 // Precomputed ORed attributes of matching/exception entries. | |
145 unsigned int matching_attributes_; | |
146 unsigned int exception_attributes_; | |
147 | |
148 friend class Blacklist; // Only blacklist constructs and sets these. | |
149 }; | |
150 | |
151 // Constructs a blacklist and populates it from the preferences associated | |
152 // with this profile. | |
153 explicit Blacklist(PrefService* prefs); | |
154 | |
155 #ifdef UNIT_TEST | |
156 // Constructs an empty blacklist. | |
157 Blacklist() : prefs_(NULL) {} | |
158 #endif | |
159 | |
160 // Destructor. | |
161 ~Blacklist(); | |
162 | |
163 // Adds a new entry to the blacklist. It is now owned by the blacklist. | |
164 void AddEntry(Entry* entry); | |
165 | |
166 // Adds a new provider to the blacklist. It is now owned by the blacklist. | |
167 void AddProvider(Provider* provider); | |
168 | |
169 EntryList::const_iterator entries_begin() const { | |
170 return blacklist_.begin(); | |
171 } | |
172 | |
173 EntryList::const_iterator entries_end() const { | |
174 return blacklist_.end(); | |
175 } | |
176 | |
177 ProviderList::const_iterator providers_begin() const { | |
178 return providers_.begin(); | |
179 } | |
180 | |
181 ProviderList::const_iterator providers_end() const { | |
182 return providers_.end(); | |
183 } | |
184 | |
185 // Returns a pointer to a Match structure holding all matching entries. | |
186 // If no matching Entry is found, returns null. Ownership belongs to the | |
187 // caller. | |
188 Match* FindMatch(const GURL&) const; | |
189 | |
190 static void RegisterUserPrefs(PrefService* user_prefs); | |
191 | |
192 // Helper to remove cookies from a header. | |
193 static std::string StripCookies(const std::string&); | |
194 | |
195 private: | |
196 // Converts a GURL into the string to match against. | |
197 static std::string GetURLAsLookupString(const GURL& url); | |
198 | |
199 // Loads the list of entries for a given provider. Returns true on success. | |
200 bool LoadEntryPreference(const ListValue& pref, const Provider* provider); | |
201 | |
202 // Loads patterns from preferences. Returns true on success. | |
203 bool LoadPreferences(); | |
204 | |
205 // Loads a provider and subsequently all of its entries. Returns true on | |
206 // success. If an error occurs reading a provider or one of its entries, | |
207 // the complete provider is dropped. | |
208 bool LoadProviderPreference(const DictionaryValue& pref, | |
209 const std::wstring& path); | |
210 | |
211 // Matches a pattern to a core URL which is host/path with all the other | |
212 // optional parts (scheme, user, password, port) stripped away. | |
213 static bool Matches(const std::string& pattern, const std::string& url); | |
214 | |
215 EntryList blacklist_; | |
216 ProviderList providers_; | |
217 | |
218 // Preferences where blacklist entries are stored. | |
219 PrefService* prefs_; | |
220 | |
221 FRIEND_TEST_ALL_PREFIXES(BlacklistTest, Generic); | |
222 FRIEND_TEST_ALL_PREFIXES(BlacklistTest, PatternMatch); | |
223 | |
224 DISALLOW_COPY_AND_ASSIGN(Blacklist); | |
225 }; | |
226 | |
227 #endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ | |
OLD | NEW |