Index: chrome/browser/privacy_blacklist/blacklist.h |
diff --git a/chrome/browser/privacy_blacklist/blacklist.h b/chrome/browser/privacy_blacklist/blacklist.h |
index 7c72a0e3fac414a695a032b0b68dd72e05ab2446..c57efff4cc30b6d197bd4d1689080737a34fb0df 100644 |
--- a/chrome/browser/privacy_blacklist/blacklist.h |
+++ b/chrome/browser/privacy_blacklist/blacklist.h |
@@ -9,6 +9,7 @@ |
#include <vector> |
#include "base/basictypes.h" |
+#include "base/linked_ptr.h" |
#include "googleurl/src/gurl.h" |
#include "net/url_request/url_request.h" |
@@ -22,15 +23,15 @@ class FilePath; |
// A blacklist is essentially a map from resource-match patterns to filter- |
// attributes. Each time a resources matches a pattern the filter-attributes |
// are used to determine how the browser handles the matching resource. |
-// |
-// TODO(idanan): Implement this efficiently. |
-// To get things started, the initial implementation is as simple as |
-// it gets and cannot scale to large blacklists but it should be enough |
-// for testing on the order of a hundred or so entries. |
-// |
//////////////////////////////////////////////////////////////////////////////// |
class Blacklist { |
public: |
+ class Entry; |
+ class Provider; |
+ |
+ typedef std::vector<linked_ptr<Entry> > EntryList; |
+ typedef std::vector<linked_ptr<Provider> > ProviderList; |
+ |
// Filter attributes (more to come): |
static const unsigned int kBlockAll; |
static const unsigned int kDontSendCookies; |
@@ -51,7 +52,8 @@ class Blacklist { |
// Key used to access data attached to URLRequest objects. |
static const void* const kRequestDataKey; |
- // Takes a string an returns the matching attribute, 0 if none matches. |
+ // Converts a stringized filter attribute (see above) back to its integer |
+ // value. Returns 0 on error. |
static unsigned int String2Attribute(const std::string&); |
// Blacklist entries come from a provider, defined by a name and source URL. |
@@ -59,10 +61,13 @@ class Blacklist { |
public: |
Provider() {} |
Provider(const char* name, const char* url) : name_(name), url_(url) {} |
+ |
const std::string& name() const { return name_; } |
- const std::string& url() const { return url_; } |
void set_name(const std::string& name) { name_ = name; } |
+ |
+ const std::string& url() const { return url_; } |
void set_url(const std::string& url) { url_ = url; } |
+ |
private: |
std::string name_; |
std::string url_; |
@@ -72,6 +77,9 @@ class Blacklist { |
// the patterns. Entry objects are owned by the Blacklist that stores them. |
class Entry { |
public: |
+ // Construct with given pattern. |
+ Entry(const std::string& pattern, const Provider* provider); |
+ |
// Returns the pattern which this entry matches. |
const std::string& pattern() const { return pattern_; } |
@@ -85,25 +93,24 @@ class Blacklist { |
// the filter-attributes of this pattern apply. This needs only to be |
// checked for content-type specific rules, as determined by calling |
// attributes(). |
- bool MatchType(const std::string&) const; |
+ bool MatchesType(const std::string&) const; |
// Returns true of the given URL is blocked, assumes it matches the |
// pattern of this entry. |
bool IsBlocked(const GURL&) const; |
- private: |
- // Construct with given pattern. |
- explicit Entry(const std::string& pattern, const Provider* provider); |
- |
void AddAttributes(unsigned int attributes); |
void AddType(const std::string& type); |
+ |
+ // Swap the contents of the internal types vector with the given vector. |
+ void SwapTypes(std::vector<std::string>* types); |
+ private: |
+ friend class BlacklistIO; |
+ |
// Merge the attributes and types of the given entry with this one. |
void Merge(const Entry& entry); |
- // Swap the given vector content for the type vector for quick loading. |
- void SwapTypes(std::vector<std::string>* types); |
- |
std::string pattern_; |
unsigned int attributes_; |
std::vector<std::string> types_; |
@@ -111,9 +118,6 @@ class Blacklist { |
// Points to the provider of this entry, the providers are all |
// owned by the blacklist. |
const Provider* provider_; |
- |
- friend class Blacklist; |
- friend class BlacklistIO; |
}; |
// A request may match one or more Blacklist rules. The Match class packages |
@@ -141,26 +145,39 @@ class Blacklist { |
friend class Blacklist; // Only blacklist constructs and sets these. |
}; |
- // Constructs a Blacklist given the filename of the persistent version. |
- // |
- // For startup efficiency, and because the blacklist must be available |
- // before any http request is made (including the homepage, if one is |
- // set to be loaded at startup), it is important to load the blacklist |
- // from a local source as efficiently as possible. For this reason, the |
- // combined rules from all active blacklists are stored in one local file. |
- explicit Blacklist(const FilePath& path); |
+ // Constructs an empty blacklist. |
+ Blacklist(); |
// Destructor. |
~Blacklist(); |
+ |
+ // Adds a new entry to the blacklist. It is now owned by the blacklist. |
+ void AddEntry(Entry* entry); |
+ |
+ // Adds a new provider to the blacklist. It is now owned by the blacklist. |
+ void AddProvider(Provider* provider); |
+ |
+ EntryList::const_iterator entries_begin() const { |
+ return blacklist_.begin(); |
+ } |
+ |
+ EntryList::const_iterator entries_end() const { |
+ return blacklist_.end(); |
+ } |
+ |
+ ProviderList::const_iterator providers_begin() const { |
+ return providers_.begin(); |
+ } |
+ |
+ ProviderList::const_iterator providers_end() const { |
+ return providers_.end(); |
+ } |
// Returns a pointer to a Match structure holding all matching entries. |
// If no matching Entry is found, returns null. Ownership belongs to the |
// caller. |
Match* findMatch(const GURL&) const; |
- // Returns true if the blacklist object is in good health. |
- bool is_good() const { return is_good_; } |
- |
// Helper to remove cookies from a header. |
static std::string StripCookies(const std::string&); |
@@ -173,12 +190,9 @@ class Blacklist { |
// internally but made static so that access can be given to tests. |
static bool Matches(const std::string& pattern, const std::string& url); |
- std::vector<Entry*> blacklist_; |
- std::vector<Provider*> providers_; |
- |
- bool is_good_; // True if the blacklist was read successfully. |
+ EntryList blacklist_; |
+ ProviderList providers_; |
- FRIEND_TEST(BlacklistTest, Generic); |
FRIEND_TEST(BlacklistTest, PatternMatch); |
DISALLOW_COPY_AND_ASSIGN(Blacklist); |
}; |