OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_PRIVACY_BLACKLIST_BLACKLIST_H_ | 5 #ifndef CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ |
6 #define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ | 6 #define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/linked_ptr.h" |
12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
14 | 15 |
15 class FilePath; | 16 class FilePath; |
16 | 17 |
17 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
18 // | 19 // |
19 // Blacklist Class | 20 // Blacklist Class |
20 // | 21 // |
21 // Represents a blacklist used to protect user from privacy and annoyances. | 22 // Represents a blacklist used to protect user from privacy and annoyances. |
22 // A blacklist is essentially a map from resource-match patterns to filter- | 23 // A blacklist is essentially a map from resource-match patterns to filter- |
23 // attributes. Each time a resources matches a pattern the filter-attributes | 24 // attributes. Each time a resources matches a pattern the filter-attributes |
24 // are used to determine how the browser handles the matching resource. | 25 // are used to determine how the browser handles the matching resource. |
25 // | |
26 // TODO(idanan): Implement this efficiently. | |
27 // To get things started, the initial implementation is as simple as | |
28 // it gets and cannot scale to large blacklists but it should be enough | |
29 // for testing on the order of a hundred or so entries. | |
30 // | |
31 //////////////////////////////////////////////////////////////////////////////// | 26 //////////////////////////////////////////////////////////////////////////////// |
32 class Blacklist { | 27 class Blacklist { |
33 public: | 28 public: |
| 29 class Entry; |
| 30 class Provider; |
| 31 |
| 32 typedef std::vector<linked_ptr<Entry> > EntryList; |
| 33 typedef std::vector<linked_ptr<Provider> > ProviderList; |
| 34 |
34 // Filter attributes (more to come): | 35 // Filter attributes (more to come): |
35 static const unsigned int kBlockAll; | 36 static const unsigned int kBlockAll; |
36 static const unsigned int kDontSendCookies; | 37 static const unsigned int kDontSendCookies; |
37 static const unsigned int kDontStoreCookies; | 38 static const unsigned int kDontStoreCookies; |
38 static const unsigned int kDontPersistCookies; | 39 static const unsigned int kDontPersistCookies; |
39 static const unsigned int kDontSendReferrer; | 40 static const unsigned int kDontSendReferrer; |
40 static const unsigned int kDontSendUserAgent; | 41 static const unsigned int kDontSendUserAgent; |
41 static const unsigned int kBlockByType; | 42 static const unsigned int kBlockByType; |
42 static const unsigned int kBlockUnsecure; | 43 static const unsigned int kBlockUnsecure; |
43 | 44 |
44 // Aggregate filter types: | 45 // Aggregate filter types: |
45 static const unsigned int kBlockRequest; | 46 static const unsigned int kBlockRequest; |
46 static const unsigned int kBlockResponse; | 47 static const unsigned int kBlockResponse; |
47 static const unsigned int kModifySentHeaders; | 48 static const unsigned int kModifySentHeaders; |
48 static const unsigned int kModifyReceivedHeaders; | 49 static const unsigned int kModifyReceivedHeaders; |
49 static const unsigned int kFilterByHeaders; | 50 static const unsigned int kFilterByHeaders; |
50 | 51 |
51 // Key used to access data attached to URLRequest objects. | 52 // Key used to access data attached to URLRequest objects. |
52 static const void* const kRequestDataKey; | 53 static const void* const kRequestDataKey; |
53 | 54 |
54 // Takes a string an returns the matching attribute, 0 if none matches. | 55 // Converts a stringized filter attribute (see above) back to its integer |
| 56 // value. Returns 0 on error. |
55 static unsigned int String2Attribute(const std::string&); | 57 static unsigned int String2Attribute(const std::string&); |
56 | 58 |
57 // Blacklist entries come from a provider, defined by a name and source URL. | 59 // Blacklist entries come from a provider, defined by a name and source URL. |
58 class Provider { | 60 class Provider { |
59 public: | 61 public: |
60 Provider() {} | 62 Provider() {} |
61 Provider(const char* name, const char* url) : name_(name), url_(url) {} | 63 Provider(const char* name, const char* url) : name_(name), url_(url) {} |
| 64 |
62 const std::string& name() const { return name_; } | 65 const std::string& name() const { return name_; } |
| 66 void set_name(const std::string& name) { name_ = name; } |
| 67 |
63 const std::string& url() const { return url_; } | 68 const std::string& url() const { return url_; } |
64 void set_name(const std::string& name) { name_ = name; } | |
65 void set_url(const std::string& url) { url_ = url; } | 69 void set_url(const std::string& url) { url_ = url; } |
| 70 |
66 private: | 71 private: |
67 std::string name_; | 72 std::string name_; |
68 std::string url_; | 73 std::string url_; |
69 }; | 74 }; |
70 | 75 |
71 // A single blacklist entry which is returned when a URL matches one of | 76 // A single blacklist entry which is returned when a URL matches one of |
72 // the patterns. Entry objects are owned by the Blacklist that stores them. | 77 // the patterns. Entry objects are owned by the Blacklist that stores them. |
73 class Entry { | 78 class Entry { |
74 public: | 79 public: |
| 80 // Construct with given pattern. |
| 81 Entry(const std::string& pattern, const Provider* provider); |
| 82 |
75 // Returns the pattern which this entry matches. | 83 // Returns the pattern which this entry matches. |
76 const std::string& pattern() const { return pattern_; } | 84 const std::string& pattern() const { return pattern_; } |
77 | 85 |
78 // Bitfield of filter-attributes matching the pattern. | 86 // Bitfield of filter-attributes matching the pattern. |
79 unsigned int attributes() const { return attributes_; } | 87 unsigned int attributes() const { return attributes_; } |
80 | 88 |
81 // Provider of this blacklist entry, used for assigning blame ;) | 89 // Provider of this blacklist entry, used for assigning blame ;) |
82 const Provider* provider() const { return provider_; } | 90 const Provider* provider() const { return provider_; } |
83 | 91 |
84 // Returns true if the given type matches one of the types for which | 92 // Returns true if the given type matches one of the types for which |
85 // the filter-attributes of this pattern apply. This needs only to be | 93 // the filter-attributes of this pattern apply. This needs only to be |
86 // checked for content-type specific rules, as determined by calling | 94 // checked for content-type specific rules, as determined by calling |
87 // attributes(). | 95 // attributes(). |
88 bool MatchType(const std::string&) const; | 96 bool MatchesType(const std::string&) const; |
89 | 97 |
90 // Returns true of the given URL is blocked, assumes it matches the | 98 // Returns true of the given URL is blocked, assumes it matches the |
91 // pattern of this entry. | 99 // pattern of this entry. |
92 bool IsBlocked(const GURL&) const; | 100 bool IsBlocked(const GURL&) const; |
93 | 101 |
94 private: | |
95 // Construct with given pattern. | |
96 explicit Entry(const std::string& pattern, const Provider* provider); | |
97 | |
98 void AddAttributes(unsigned int attributes); | 102 void AddAttributes(unsigned int attributes); |
99 void AddType(const std::string& type); | 103 void AddType(const std::string& type); |
| 104 |
| 105 // Swap the contents of the internal types vector with the given vector. |
| 106 void SwapTypes(std::vector<std::string>* types); |
100 | 107 |
| 108 private: |
| 109 friend class BlacklistIO; |
| 110 |
101 // Merge the attributes and types of the given entry with this one. | 111 // Merge the attributes and types of the given entry with this one. |
102 void Merge(const Entry& entry); | 112 void Merge(const Entry& entry); |
103 | 113 |
104 // Swap the given vector content for the type vector for quick loading. | |
105 void SwapTypes(std::vector<std::string>* types); | |
106 | |
107 std::string pattern_; | 114 std::string pattern_; |
108 unsigned int attributes_; | 115 unsigned int attributes_; |
109 std::vector<std::string> types_; | 116 std::vector<std::string> types_; |
110 | 117 |
111 // Points to the provider of this entry, the providers are all | 118 // Points to the provider of this entry, the providers are all |
112 // owned by the blacklist. | 119 // owned by the blacklist. |
113 const Provider* provider_; | 120 const Provider* provider_; |
114 | |
115 friend class Blacklist; | |
116 friend class BlacklistIO; | |
117 }; | 121 }; |
118 | 122 |
119 // A request may match one or more Blacklist rules. The Match class packages | 123 // A request may match one or more Blacklist rules. The Match class packages |
120 // all the matching entries behind a single interface with access to the | 124 // all the matching entries behind a single interface with access to the |
121 // underlying set of entries so that we can display provider information. | 125 // underlying set of entries so that we can display provider information. |
122 // Often a match must be applied after a URLRequest has started, so it gets | 126 // Often a match must be applied after a URLRequest has started, so it gets |
123 // tagged with the Match object to avoid doing lookups more than once per | 127 // tagged with the Match object to avoid doing lookups more than once per |
124 // request. | 128 // request. |
125 class Match : public URLRequest::UserData { | 129 class Match : public URLRequest::UserData { |
126 public: | 130 public: |
127 // Functions that return combined results from all entries. | 131 // Functions that return combined results from all entries. |
128 unsigned int attributes() const { return attributes_; } | 132 unsigned int attributes() const { return attributes_; } |
129 bool MatchType(const std::string&) const; | 133 bool MatchType(const std::string&) const; |
130 bool IsBlocked(const GURL&) const; | 134 bool IsBlocked(const GURL&) const; |
131 | 135 |
132 // Access to individual entries, mostly for display/logging purposes. | 136 // Access to individual entries, mostly for display/logging purposes. |
133 const std::vector<const Entry*>& entries() const { return entries_; } | 137 const std::vector<const Entry*>& entries() const { return entries_; } |
134 | 138 |
135 private: | 139 private: |
136 Match(); | 140 Match(); |
137 void AddEntry(const Entry* entry); | 141 void AddEntry(const Entry* entry); |
138 std::vector<const Entry*> entries_; | 142 std::vector<const Entry*> entries_; |
139 unsigned int attributes_; // Precomputed ORed attributes of entries. | 143 unsigned int attributes_; // Precomputed ORed attributes of entries. |
140 | 144 |
141 friend class Blacklist; // Only blacklist constructs and sets these. | 145 friend class Blacklist; // Only blacklist constructs and sets these. |
142 }; | 146 }; |
143 | 147 |
144 // Constructs a Blacklist given the filename of the persistent version. | 148 // Constructs an empty blacklist. |
145 // | 149 Blacklist(); |
146 // For startup efficiency, and because the blacklist must be available | |
147 // before any http request is made (including the homepage, if one is | |
148 // set to be loaded at startup), it is important to load the blacklist | |
149 // from a local source as efficiently as possible. For this reason, the | |
150 // combined rules from all active blacklists are stored in one local file. | |
151 explicit Blacklist(const FilePath& path); | |
152 | 150 |
153 // Destructor. | 151 // Destructor. |
154 ~Blacklist(); | 152 ~Blacklist(); |
| 153 |
| 154 // Adds a new entry to the blacklist. It is now owned by the blacklist. |
| 155 void AddEntry(Entry* entry); |
| 156 |
| 157 // Adds a new provider to the blacklist. It is now owned by the blacklist. |
| 158 void AddProvider(Provider* provider); |
| 159 |
| 160 EntryList::const_iterator entries_begin() const { |
| 161 return blacklist_.begin(); |
| 162 } |
| 163 |
| 164 EntryList::const_iterator entries_end() const { |
| 165 return blacklist_.end(); |
| 166 } |
| 167 |
| 168 ProviderList::const_iterator providers_begin() const { |
| 169 return providers_.begin(); |
| 170 } |
| 171 |
| 172 ProviderList::const_iterator providers_end() const { |
| 173 return providers_.end(); |
| 174 } |
155 | 175 |
156 // Returns a pointer to a Match structure holding all matching entries. | 176 // Returns a pointer to a Match structure holding all matching entries. |
157 // If no matching Entry is found, returns null. Ownership belongs to the | 177 // If no matching Entry is found, returns null. Ownership belongs to the |
158 // caller. | 178 // caller. |
159 Match* findMatch(const GURL&) const; | 179 Match* findMatch(const GURL&) const; |
160 | 180 |
161 // Returns true if the blacklist object is in good health. | |
162 bool is_good() const { return is_good_; } | |
163 | |
164 // Helper to remove cookies from a header. | 181 // Helper to remove cookies from a header. |
165 static std::string StripCookies(const std::string&); | 182 static std::string StripCookies(const std::string&); |
166 | 183 |
167 // Helper to remove cookie expiration from a header. | 184 // Helper to remove cookie expiration from a header. |
168 static std::string StripCookieExpiry(const std::string&); | 185 static std::string StripCookieExpiry(const std::string&); |
169 | 186 |
170 private: | 187 private: |
171 // Matches a pattern to a core URL which is host/path with all the other | 188 // Matches a pattern to a core URL which is host/path with all the other |
172 // optional parts (scheme, user, password, port) stripped away. Used only | 189 // optional parts (scheme, user, password, port) stripped away. Used only |
173 // internally but made static so that access can be given to tests. | 190 // internally but made static so that access can be given to tests. |
174 static bool Matches(const std::string& pattern, const std::string& url); | 191 static bool Matches(const std::string& pattern, const std::string& url); |
175 | 192 |
176 std::vector<Entry*> blacklist_; | 193 EntryList blacklist_; |
177 std::vector<Provider*> providers_; | 194 ProviderList providers_; |
178 | 195 |
179 bool is_good_; // True if the blacklist was read successfully. | |
180 | |
181 FRIEND_TEST(BlacklistTest, Generic); | |
182 FRIEND_TEST(BlacklistTest, PatternMatch); | 196 FRIEND_TEST(BlacklistTest, PatternMatch); |
183 DISALLOW_COPY_AND_ASSIGN(Blacklist); | 197 DISALLOW_COPY_AND_ASSIGN(Blacklist); |
184 }; | 198 }; |
185 | 199 |
186 #endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ | 200 #endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_H_ |
OLD | NEW |