diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-23 16:22:14 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-23 16:22:14 +0000 |
commit | c37f8b42ae69eb7070f46c0be0791c0c67bf2163 (patch) | |
tree | 2f1a33c964833b2a2b78b9defe8d76b58ebedcc5 /chrome/browser/privacy_blacklist | |
parent | d63bfa7d4c4ed7a928c4a9cef41847fef8e29109 (diff) | |
download | chromium_src-c37f8b42ae69eb7070f46c0be0791c0c67bf2163.zip chromium_src-c37f8b42ae69eb7070f46c0be0791c0c67bf2163.tar.gz chromium_src-c37f8b42ae69eb7070f46c0be0791c0c67bf2163.tar.bz2 |
Load filter rules from preferences.
BUG=32782
TEST=unit_tests
Review URL: http://codereview.chromium.org/555033
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36962 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/privacy_blacklist')
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist.cc | 86 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist.h | 49 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_io.cc | 288 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_io.h | 29 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_io_unittest.cc | 56 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_perftest.cc | 3 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_store.cc | 130 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_store.h | 101 | ||||
-rw-r--r-- | chrome/browser/privacy_blacklist/blacklist_unittest.cc | 31 |
9 files changed, 125 insertions, 648 deletions
diff --git a/chrome/browser/privacy_blacklist/blacklist.cc b/chrome/browser/privacy_blacklist/blacklist.cc index d9bcf6c..bdefc7e 100644 --- a/chrome/browser/privacy_blacklist/blacklist.cc +++ b/chrome/browser/privacy_blacklist/blacklist.cc @@ -11,12 +11,12 @@ #include "base/file_path.h" #include "base/file_util.h" #include "base/string_util.h" -#include "chrome/browser/privacy_blacklist/blacklist_store.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/pref_service.h" #include "chrome/common/url_constants.h" #include "net/http/http_util.h" -#define STRINGIZE(s) #s - namespace { const char* const cookie_headers[2] = { "cookie", "set-cookie" }; @@ -33,20 +33,6 @@ const unsigned int Blacklist::kModifySentHeaders = kBlockCookies | kDontSendUserAgent | kDontSendReferrer; const unsigned int Blacklist::kModifyReceivedHeaders = kBlockCookies; -unsigned int Blacklist::String2Attribute(const std::string& s) { - if (s == STRINGIZE(kBlockAll)) - return kBlockAll; - else if (s == STRINGIZE(kBlockCookies)) - return kBlockCookies; - else if (s == STRINGIZE(kDontSendReferrer)) - return kDontSendReferrer; - else if (s == STRINGIZE(kDontSendUserAgent)) - return kDontSendUserAgent; - else if (s == STRINGIZE(kBlockUnsecure)) - return kBlockUnsecure; - return 0; -} - // static bool Blacklist::Matches(const std::string& pattern, const std::string& url) { if (pattern.size() > url.size()) @@ -107,10 +93,6 @@ void Blacklist::Entry::AddAttributes(unsigned int attributes) { attributes_ |= attributes; } -void Blacklist::Entry::Merge(const Entry& entry) { - attributes_ |= entry.attributes_; -} - bool Blacklist::Match::IsBlocked(const GURL& url) const { return (attributes() & kBlockAll) || ((attributes() & kBlockUnsecure) && !url.SchemeIsSecure()); @@ -128,7 +110,8 @@ void Blacklist::Match::AddEntry(const Entry* entry) { } } -Blacklist::Blacklist() { +Blacklist::Blacklist(PrefService* prefs) : prefs_(prefs) { + LoadPreferences(); } Blacklist::~Blacklist() { @@ -167,6 +150,59 @@ Blacklist::Match* Blacklist::FindMatch(const GURL& url) const { return match; } +bool Blacklist::LoadEntryPreference(const ListValue& pref, + const Provider* provider) { + EntryList entries; + for (ListValue::const_iterator i = pref.begin(); i != pref.end(); ++i) { + if (!(*i)->IsType(Value::TYPE_DICTIONARY)) + return false; + const DictionaryValue* entry_pref = static_cast<DictionaryValue*>(*i); + std::string pattern; + int attributes; + bool is_exception; + if (!(entry_pref->GetString(L"pattern", &pattern) && + entry_pref->GetInteger(L"attributes", &attributes) && + entry_pref->GetBoolean(L"exception", &is_exception))) + return false; + Entry* entry = new Entry(pattern, provider, is_exception); + entry->AddAttributes(static_cast<unsigned int>(attributes)); + entries.push_back(linked_ptr<Entry>(entry)); + } + blacklist_.insert(blacklist_.end(), entries.begin(), entries.end()); + return true; +} + +bool Blacklist::LoadProviderPreference(const DictionaryValue& pref, + const std::wstring& path) { + std::string name, url; + ListValue* entries; + if (!(pref.GetString(L"name", &name) && pref.GetString(L"url", &url) && + pref.GetList(L"entries", &entries))) + return false; + linked_ptr<Provider> provider(new Provider(name, url, path)); + if (LoadEntryPreference(*entries, provider.get())) + providers_.push_back(provider); + return true; + return false; +} + +bool Blacklist::LoadPreferences() { + DCHECK(prefs_); + const DictionaryValue* blacklist_rules = prefs_->GetDictionary( + prefs::kPrivacyFilterRules); + if (!blacklist_rules) + return false; + bool result = true; + for (DictionaryValue::key_iterator key = blacklist_rules->begin_keys(); + key != blacklist_rules->end_keys(); ++key) { + DictionaryValue* provider; + if (!(blacklist_rules->GetDictionaryWithoutPathExpansion(*key, &provider) && + LoadProviderPreference(*provider, *key))) + result = false; + } + return result; +} + // static std::string Blacklist::GetURLAsLookupString(const GURL& url) { std::string url_spec = url.host() + url.path(); @@ -176,6 +212,12 @@ std::string Blacklist::GetURLAsLookupString(const GURL& url) { return url_spec; } +// static +void Blacklist::RegisterUserPrefs(PrefService* user_prefs) { + user_prefs->RegisterDictionaryPref(prefs::kPrivacyFilterRules); +} + +// static std::string Blacklist::StripCookies(const std::string& header) { return net::HttpUtil::StripHeaders(header, cookie_headers, 2); } diff --git a/chrome/browser/privacy_blacklist/blacklist.h b/chrome/browser/privacy_blacklist/blacklist.h index 7d688ac..4101ac57 100644 --- a/chrome/browser/privacy_blacklist/blacklist.h +++ b/chrome/browser/privacy_blacklist/blacklist.h @@ -13,7 +13,10 @@ #include "googleurl/src/gurl.h" #include "net/url_request/url_request.h" +class DictionaryValue; class FilePath; +class ListValue; +class PrefService; //////////////////////////////////////////////////////////////////////////////// // @@ -45,24 +48,30 @@ class Blacklist { static const unsigned int kModifySentHeaders; static const unsigned int kModifyReceivedHeaders; - // 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. class Provider { public: Provider() {} - Provider(const char* name, const char* url) : name_(name), url_(url) {} + Provider(const std::string& name, const std::string& url, + const std::wstring& pref_path) + : name_(name), + pref_path_(pref_path), + url_(url) {} const std::string& name() const { return name_; } void set_name(const std::string& name) { name_ = name; } + const std::wstring& pref_path() const { return pref_path_; } + void set_pref_path(const std::wstring& pref_path) { + pref_path_ = pref_path; + } + const std::string& url() const { return url_; } void set_url(const std::string& url) { url_ = url; } private: std::string name_; + std::wstring pref_path_; std::string url_; }; @@ -93,11 +102,6 @@ class Blacklist { void AddAttributes(unsigned int attributes); private: - friend class BlacklistIO; - - // Merge the attributes and types of the given entry with this one. - void Merge(const Entry& entry); - unsigned int attributes_; // True if this entry is an exception to the blacklist. @@ -142,8 +146,14 @@ class Blacklist { friend class Blacklist; // Only blacklist constructs and sets these. }; + // Constructs a blacklist and populates it from the preferences associated + // with this profile. + explicit Blacklist(PrefService* prefs); + +#ifdef UNIT_TEST // Constructs an empty blacklist. - Blacklist(); + Blacklist() : prefs_(NULL) {} +#endif // Destructor. ~Blacklist(); @@ -175,6 +185,8 @@ class Blacklist { // caller. Match* FindMatch(const GURL&) const; + static void RegisterUserPrefs(PrefService* user_prefs); + // Helper to remove cookies from a header. static std::string StripCookies(const std::string&); @@ -182,6 +194,18 @@ class Blacklist { // Converts a GURL into the string to match against. static std::string GetURLAsLookupString(const GURL& url); + // Loads the list of entries for a given provider. Returns true on success. + bool LoadEntryPreference(const ListValue& pref, const Provider* provider); + + // Loads patterns from preferences. Returns true on success. + bool LoadPreferences(); + + // Loads a provider and subsequently all of its entries. Returns true on + // success. If an error occurs reading a provider or one of its entries, + // the complete provider is dropped. + bool LoadProviderPreference(const DictionaryValue& pref, + const std::wstring& path); + // Matches a pattern to a core URL which is host/path with all the other // optional parts (scheme, user, password, port) stripped away. static bool Matches(const std::string& pattern, const std::string& url); @@ -189,6 +213,9 @@ class Blacklist { EntryList blacklist_; ProviderList providers_; + // Preferences where blacklist entries are stored. + PrefService* prefs_; + FRIEND_TEST(BlacklistTest, Generic); FRIEND_TEST(BlacklistTest, PatternMatch); DISALLOW_COPY_AND_ASSIGN(Blacklist); diff --git a/chrome/browser/privacy_blacklist/blacklist_io.cc b/chrome/browser/privacy_blacklist/blacklist_io.cc deleted file mode 100644 index 73fbf6c..0000000 --- a/chrome/browser/privacy_blacklist/blacklist_io.cc +++ /dev/null @@ -1,288 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/privacy_blacklist/blacklist_io.h" - -#include <limits> - -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/string_tokenizer.h" -#include "base/string_util.h" -#include "chrome/browser/privacy_blacklist/blacklist.h" -#include "chrome/browser/privacy_blacklist/blacklist_store.h" - -namespace { - -const char header[] = "[Chromium::PrivacyBlacklist]"; -const char name_tag[] = "Name:"; -const char url_tag[] = "URL:"; -const char whitelist_tag[] = "-"; -const char arrow_tag[] = "=>"; -const char eol[] = "\n\r"; - -class IsWhiteSpace { - public: - bool operator()(const char& c) const { - return IsAsciiWhitespace(c); - } -}; - -class IsNotWhiteSpace { - public: - bool operator()(const char& c) const { - return !IsAsciiWhitespace(c); - } -}; - -bool StartsWith(const char* cur, const char* end, - const char* tag, std::size_t size) { - return cur + size <= end && std::equal(tag, tag + size - 1, cur); -} - -} // namespace - -// static -bool BlacklistIO::ReadText(Blacklist* blacklist, - const FilePath& path, - std::string* error_string) { - DCHECK(blacklist); - DCHECK(error_string); - - // Memory map for efficient parsing. If the file cannot fit in available - // memory it would be the least of our worries. Typical blacklist files - // are less than 200K. - file_util::MemoryMappedFile input; - if (!input.Initialize(path) || !input.data()) { - *error_string = "File I/O error. Check path and permissions."; - return false; - } - - const char* cur = reinterpret_cast<const char*>(input.data()); - const char* end = cur + input.length(); - - // Check header. - if (!StartsWith(cur, end, header, arraysize(header))) { - *error_string = "Incorrect header."; - return false; - } - - Blacklist::EntryList entries; - Blacklist::ProviderList providers; - - Blacklist::Provider* provider = new Blacklist::Provider; - providers.push_back(linked_ptr<Blacklist::Provider>(provider)); - - cur = std::find(cur, end, '\n') + 1; // Skip past EOL. - - // Each loop iteration takes care of one input line. - while (cur < end) { - // Skip whitespace at beginning of line. - cur = std::find_if(cur, end, IsNotWhiteSpace()); - if (cur == end) - break; - - if (*cur == '#') { - cur = std::find(cur, end, '\n') + 1; - continue; - } - - if (*cur == '|') { - ++cur; - if (StartsWith(cur, end, name_tag, arraysize(name_tag))) { - // Edge condition: if the find below fails, the next one will too, - // so we'll just skip to the EOF below. - cur = std::find_if(cur + arraysize(name_tag), end, IsNotWhiteSpace()); - const char* skip = std::find_if(cur, end, IsWhiteSpace()); - if (skip < end) - provider->set_name(std::string(cur, skip)); - } else if (StartsWith(cur, end, url_tag, arraysize(url_tag))) { - cur = std::find_if(cur + arraysize(url_tag), end, IsNotWhiteSpace()); - const char* skip = std::find_if(cur, end, IsWhiteSpace()); - if (skip < end) - provider->set_url(std::string(cur, skip)); - } - cur = std::find(cur, end, '\n') + 1; - continue; - } - - bool is_exception = false; - if (StartsWith(cur, end, whitelist_tag, arraysize(whitelist_tag))) { - is_exception = true; - cur++; - if (IsAsciiWhitespace(*cur)) { - *error_string = "Dash followed by white space."; - return false; - } - } - - const char* skip = std::find_if(cur, end, IsWhiteSpace()); - std::string pattern(cur, skip); - - cur = std::find_if(cur + pattern.size(), end, IsNotWhiteSpace()); - if (!StartsWith(cur, end, arrow_tag, arraysize(arrow_tag))) { - *error_string = "Missing => in rule."; - return false; - } - - linked_ptr<Blacklist::Entry> entry(new Blacklist::Entry(pattern, provider, - is_exception)); - - cur = std::find_if(cur + arraysize(arrow_tag), end, IsNotWhiteSpace()); - skip = std::find_first_of(cur, end, eol, eol + 2); - std::string buf(cur, skip); - cur = skip + 1; - - StringTokenizer tokenizer(buf, " (),\n\r"); - tokenizer.set_options(StringTokenizer::RETURN_DELIMS); - - bool in_attribute = false; - unsigned int last_attribute = 0; - - while (tokenizer.GetNext()) { - if (tokenizer.token_is_delim()) { - switch (*tokenizer.token_begin()) { - case '(': - if (in_attribute) { - *error_string = "Unexpected ( in attribute parameters."; - return false; - } - in_attribute = true; - continue; - case ')': - if (!in_attribute) { - *error_string = "Unexpected ) in attribute list."; - return false; - } - in_attribute = false; - continue; - default: - // No state change for other delimiters. - continue; - } - } - - if (in_attribute) { - // TODO(jochen): implement support for parsing arguments to attributes. - *error_string = "Unexpected argument to attribute."; - return false; - } else { - // Filter attribute. Unrecognized attributes are ignored. - last_attribute = Blacklist::String2Attribute(tokenizer.token()); - entry->AddAttributes(last_attribute); - } - } - entries.push_back(entry); - } - - for (Blacklist::EntryList::iterator i = entries.begin(); - i != entries.end(); ++i) { - blacklist->AddEntry(i->release()); - } - for (Blacklist::ProviderList::iterator i = providers.begin(); - i != providers.end(); ++i) { - blacklist->AddProvider(i->release()); - } - - return true; -} - -// static -bool BlacklistIO::ReadBinary(Blacklist* blacklist, const FilePath& path) { - DCHECK(blacklist); - - FILE* fp = file_util::OpenFile(path, "rb"); - if (fp == NULL) - return false; - - BlacklistStoreInput input(fp); - - // Read the providers. - uint32 num_providers = input.ReadNumProviders(); - if (num_providers == std::numeric_limits<uint32>::max()) - return false; - - Blacklist::EntryList entries; - Blacklist::ProviderList providers; - std::map<size_t, Blacklist::Provider*> provider_map; - - std::string name; - std::string url; - for (size_t i = 0; i < num_providers; ++i) { - if (!input.ReadProvider(&name, &url)) - return false; - provider_map[i] = new Blacklist::Provider(name.c_str(), url.c_str()); - providers.push_back(linked_ptr<Blacklist::Provider>(provider_map[i])); - } - - // Read the entries. - uint32 num_entries = input.ReadNumEntries(); - if (num_entries == std::numeric_limits<uint32>::max()) - return false; - - std::string pattern; - unsigned int attributes, provider; - bool is_exception; - for (size_t i = 0; i < num_entries; ++i) { - if (!input.ReadEntry(&pattern, &attributes, &is_exception, &provider)) - return false; - - Blacklist::Entry* entry = - new Blacklist::Entry(pattern, provider_map[provider], is_exception); - entry->AddAttributes(attributes); - entries.push_back(linked_ptr<Blacklist::Entry>(entry)); - } - - for (Blacklist::EntryList::iterator i = entries.begin(); - i != entries.end(); ++i) { - blacklist->AddEntry(i->release()); - } - for (Blacklist::ProviderList::iterator i = providers.begin(); - i != providers.end(); ++i) { - blacklist->AddProvider(i->release()); - } - - return true; -} - -// static -bool BlacklistIO::WriteBinary(const Blacklist* blacklist, - const FilePath& file) { - BlacklistStoreOutput output(file_util::OpenFile(file, "wb")); - if (!output.is_good()) - return false; - - Blacklist::EntryList entries(blacklist->entries_begin(), - blacklist->entries_end()); - Blacklist::ProviderList providers(blacklist->providers_begin(), - blacklist->providers_end()); - - // Output providers, give each one an index. - std::map<const Blacklist::Provider*, uint32> index; - uint32 current = 0; - if (!output.ReserveProviders(providers.size())) - return false; - - for (Blacklist::ProviderList::const_iterator i = providers.begin(); - i != providers.end(); ++i, ++current) { - if (!output.StoreProvider((*i)->name(), (*i)->url())) - return false; - index[i->get()] = current; - } - - // Output entries, replacing the provider with its index. - if (!output.ReserveEntries(entries.size())) - return false; - - for (Blacklist::EntryList::const_iterator i = entries.begin(); - i != entries.end(); ++i) { - if (!output.StoreEntry((*i)->pattern_, - (*i)->attributes_, - (*i)->is_exception_, - index[(*i)->provider_])) { - return false; - } - } - return true; -} diff --git a/chrome/browser/privacy_blacklist/blacklist_io.h b/chrome/browser/privacy_blacklist/blacklist_io.h deleted file mode 100644 index 168c241..0000000 --- a/chrome/browser/privacy_blacklist/blacklist_io.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_IO_H_ -#define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_IO_H_ - -#include <string> - -class Blacklist; -class FilePath; - -// Set of routines to read and write blacklists. -class BlacklistIO { - public: - // Reads a blacklist stored on disk in a text format. - // On error returns false and fills |error_string|. - static bool ReadText(Blacklist* blacklist, const FilePath& path, - std::string* error_string); - - // Reads a blacklist stored on disk in a binary format. - // Returns true on success. - static bool ReadBinary(Blacklist* blacklist, const FilePath& path); - - // Writes |blacklist| to |path| in a binary format. Returns true on success. - static bool WriteBinary(const Blacklist* blacklist, const FilePath& path); -}; - -#endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_IO_H_ diff --git a/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc b/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc deleted file mode 100644 index c76712d..0000000 --- a/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/privacy_blacklist/blacklist_io.h" - -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/path_service.h" -#include "base/string_util.h" -#include "chrome/browser/privacy_blacklist/blacklist.h" -#include "chrome/common/chrome_paths.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(BlacklistIOTest, Generic) { - // Testing data path. - FilePath data_dir; - PathService::Get(chrome::DIR_TEST_DATA, &data_dir); - - FilePath input = data_dir.AppendASCII("blacklist_small.pbl"); - - FilePath expected = data_dir.AppendASCII("blacklist_small.pbr"); - - Blacklist blacklist; - std::string error_string; - ASSERT_TRUE(BlacklistIO::ReadText(&blacklist, input, &error_string)); - EXPECT_TRUE(error_string.empty()); - - const Blacklist::EntryList entries(blacklist.entries_begin(), - blacklist.entries_end()); - ASSERT_EQ(7U, entries.size()); - - EXPECT_EQ("@poor-security-site.com", entries[0]->pattern()); - EXPECT_EQ("@.ad-serving-place.com", entries[1]->pattern()); - EXPECT_EQ("www.site.com/anonymous/folder/@", entries[2]->pattern()); - EXPECT_EQ("www.site.com/bad/url", entries[3]->pattern()); - EXPECT_EQ("@/script?@", entries[4]->pattern()); - EXPECT_EQ("@?badparam@", entries[5]->pattern()); - EXPECT_EQ("www.site.com/bad/url/good", entries[6]->pattern()); - - const Blacklist::ProviderList providers(blacklist.providers_begin(), - blacklist.providers_end()); - - ASSERT_EQ(1U, providers.size()); - EXPECT_EQ("Sample", providers[0]->name()); - EXPECT_EQ("http://www.google.com", providers[0]->url()); - - FilePath output; - PathService::Get(base::DIR_TEMP, &output); - output = output.AppendASCII("blacklist_small.pbr"); - ASSERT_TRUE(BlacklistIO::WriteBinary(&blacklist, output)); - EXPECT_TRUE(file_util::ContentsEqual(output, expected)); - EXPECT_TRUE(file_util::Delete(output, false)); -} - - diff --git a/chrome/browser/privacy_blacklist/blacklist_perftest.cc b/chrome/browser/privacy_blacklist/blacklist_perftest.cc index 0087587..7c259ed 100644 --- a/chrome/browser/privacy_blacklist/blacklist_perftest.cc +++ b/chrome/browser/privacy_blacklist/blacklist_perftest.cc @@ -115,7 +115,8 @@ class BlacklistPerfTest : public testing::Test { // Generate random benchmark blacklist. Blacklist::Provider* provider = new Blacklist::Provider("test", - "http://test.com"); + "http://test.com", + L"test"); blacklist_.AddProvider(provider); // Create host.tld/ patterns. diff --git a/chrome/browser/privacy_blacklist/blacklist_store.cc b/chrome/browser/privacy_blacklist/blacklist_store.cc deleted file mode 100644 index 752ace8..0000000 --- a/chrome/browser/privacy_blacklist/blacklist_store.cc +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/privacy_blacklist/blacklist_store.h" - -#include <cstdio> -#include <limits> - -#include "base/basictypes.h" -#include "base/file_util.h" -#include "base/logging.h" - -namespace { - -const char cookie[] = "GCPBL250"; - -const size_t kMaxBlockedTypes = 256; -const size_t kMaxStringSize = 8192; - -} // namespace - -bool BlacklistStoreOutput::WriteUInt(uint32 i) { - return fwrite(reinterpret_cast<char*>(&i), 1, sizeof(uint32), file_) == - sizeof(uint32); -} - -bool BlacklistStoreOutput::WriteString(const std::string& s) { - uint32 n = s.size(); - return WriteUInt(n) && fwrite(s.c_str(), 1, n, file_) == n; -} - -BlacklistStoreOutput::BlacklistStoreOutput(FILE* file) - : file_(file), is_good_(false) { - is_good_ = fwrite(cookie, 1, sizeof(cookie), file_) == sizeof(cookie); -} - -BlacklistStoreOutput::~BlacklistStoreOutput() { - file_util::CloseFile(file_); -} - -bool BlacklistStoreOutput::ReserveProviders(uint32 num) { - return WriteUInt(num); -} - -bool BlacklistStoreOutput::StoreProvider(const std::string& name, - const std::string& url) { - return WriteString(name) && WriteString(url); -} - -bool BlacklistStoreOutput::ReserveEntries(uint32 num) { - return WriteUInt(num); -} - -bool BlacklistStoreOutput::StoreEntry(const std::string& pattern, - uint32 attributes, - bool is_exception, - uint32 provider) { - return (WriteString(pattern) && - WriteUInt(attributes) && - WriteUInt(is_exception ? 1 : 0) && - WriteUInt(provider)); -} - -uint32 BlacklistStoreInput::ReadUInt() { - uint32 buf; - if (fread(&buf, 1, sizeof(uint32), file_) != sizeof(uint32)) - return std::numeric_limits<uint32>::max(); - return buf; -} - -std::string BlacklistStoreInput::ReadString() { - uint32 size = ReadUInt(); - - // Too long strings are not allowed. Covers the case of ReadUInt failing. - if (size > kMaxStringSize) { - return std::string(); - } - - char buf[kMaxStringSize]; - if (fread(buf, 1, size, file_) != size) - return std::string(); - return std::string(buf, size); -} - -BlacklistStoreInput::BlacklistStoreInput(FILE* file) - : file_(file), is_good_(false) { - is_good_ = !fseek(file_, sizeof(cookie), SEEK_CUR); -} - -BlacklistStoreInput::~BlacklistStoreInput() { - file_util::CloseFile(file_); -} - -uint32 BlacklistStoreInput::ReadNumProviders() { - return ReadUInt(); -} - -bool BlacklistStoreInput::ReadProvider(std::string* name, std::string* url) { - *name = ReadString(); - if (name->empty()) - return false; - *url = ReadString(); - return !url->empty(); -} - -uint32 BlacklistStoreInput::ReadNumEntries() { - return ReadUInt(); -} - -bool BlacklistStoreInput::ReadEntry(std::string* pattern, - uint32* attributes, - bool* is_exception, - uint32* provider) { - *pattern = ReadString(); - if (pattern->empty()) - return false; - - *attributes = ReadUInt(); - if (*attributes == std::numeric_limits<uint32>::max()) - return false; - - uint32 exception = ReadUInt(); - if (exception == std::numeric_limits<uint32>::max()) - return false; - *is_exception = (exception == 1); - - *provider = ReadUInt(); - return *provider != std::numeric_limits<uint32>::max(); -} diff --git a/chrome/browser/privacy_blacklist/blacklist_store.h b/chrome/browser/privacy_blacklist/blacklist_store.h deleted file mode 100644 index 91f5711..0000000 --- a/chrome/browser/privacy_blacklist/blacklist_store.h +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_STORE_H_ -#define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_STORE_H_ - -#include <cstdio> -#include <string> -#include <vector> - -#include "base/basictypes.h" - -class FilePath; - -//////////////////////////////////////////////////////////////////////////////// -// -// Blacklist Binary Storage Output Class -// -// Stores aggregate Privacy Blacklists efficiently on disk. The public -// functions below must be called in the order they are declared, as -// the input class is expected to read them in that order. The provider -// and entry output functions must be called the number of times set. -// -//////////////////////////////////////////////////////////////////////////////// -class BlacklistStoreOutput { - public: - explicit BlacklistStoreOutput(FILE* file); - ~BlacklistStoreOutput(); - - // Returns true if the object initialized without error. - bool is_good() const { return is_good_; } - - // Sets the number of providers stored. Returns true if successful. - bool ReserveProviders(uint32); - - // Stores a provider. Returns true if successful. - bool StoreProvider(const std::string& name, const std::string& url); - - // Sets the number of entries stored. Returns true if successful. - bool ReserveEntries(uint32); - - // Stores an entry. Returns true if successful. - bool StoreEntry(const std::string& pattern, - uint32 attributes, - bool is_exception, - uint32 provider); - - private: - // Writes basic types to the stream. Returns true if successful. - bool WriteUInt(uint32); - bool WriteString(const std::string&); - - FILE* file_; - bool is_good_; - DISALLOW_COPY_AND_ASSIGN(BlacklistStoreOutput); -}; - -//////////////////////////////////////////////////////////////////////////////// -// -// Blacklist Binary Storage Input Class -// -// Stores aggregate Privacy Blacklists efficiently on disk. The public -// functions below must be called in the order they are declared, as -// the output class is expected to write them in that order. The provider -// entries read functions must be called the correct number of times. -// -//////////////////////////////////////////////////////////////////////////////// -class BlacklistStoreInput { - public: - explicit BlacklistStoreInput(FILE* file); - ~BlacklistStoreInput(); - - // Returns true if this object initialized without error. - bool is_good() const { return is_good_; } - - // Reads the number of providers. - uint32 ReadNumProviders(); - - // Reads a provider. Returns true on success. - bool ReadProvider(std::string* name, std::string* url); - - // Reads the number of entries. Returns true on success. - uint32 ReadNumEntries(); - - // Reads an entry. - bool ReadEntry(std::string* pattern, - uint32* attributes, - bool* is_exception, - uint32* provider); - - private: - uint32 ReadUInt(); - std::string ReadString(); - - FILE* file_; - bool is_good_; - DISALLOW_COPY_AND_ASSIGN(BlacklistStoreInput); -}; - -#endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_STORE_H_ diff --git a/chrome/browser/privacy_blacklist/blacklist_unittest.cc b/chrome/browser/privacy_blacklist/blacklist_unittest.cc index d6ccac63..d89cd81 100644 --- a/chrome/browser/privacy_blacklist/blacklist_unittest.cc +++ b/chrome/browser/privacy_blacklist/blacklist_unittest.cc @@ -8,19 +8,30 @@ #include "base/file_util.h" #include "base/path_service.h" #include "base/string_util.h" -#include "chrome/browser/privacy_blacklist/blacklist_io.h" +#include "chrome/browser/browser_prefs.h" +#include "chrome/browser/profile.h" #include "chrome/common/chrome_paths.h" +#include "chrome/common/pref_service.h" #include "testing/gtest/include/gtest/gtest.h" -TEST(BlacklistTest, Generic) { - // Get path relative to test data dir. - FilePath input; - PathService::Get(chrome::DIR_TEST_DATA, &input); - input = input.AppendASCII("blacklist_small.pbr"); +class BlacklistTest : public testing::Test { + protected: + virtual void SetUp() { + FilePath source_path; + PathService::Get(chrome::DIR_TEST_DATA, &source_path); + source_path = source_path.AppendASCII("profiles") + .AppendASCII("blacklist_prefs").AppendASCII("Preferences"); + + prefs_.reset(new PrefService(source_path)); + Profile::RegisterUserPrefs(prefs_.get()); + browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); + } - Blacklist blacklist; - ASSERT_TRUE(BlacklistIO::ReadBinary(&blacklist, input)); + scoped_ptr<PrefService> prefs_; +}; +TEST_F(BlacklistTest, Generic) { + Blacklist blacklist(prefs_.get()); Blacklist::EntryList entries(blacklist.entries_begin(), blacklist.entries_end()); @@ -67,7 +78,7 @@ TEST(BlacklistTest, Generic) { ASSERT_EQ(1U, providers.size()); EXPECT_EQ("Sample", providers[0]->name()); - EXPECT_EQ("http://www.google.com", providers[0]->url()); + EXPECT_EQ("http://www.example.com", providers[0]->url()); // No match for chrome, about or empty URLs. EXPECT_FALSE(blacklist.FindMatch(GURL())); @@ -175,7 +186,7 @@ TEST(BlacklistTest, Generic) { GURL("http://example.com/script?param=1"))); } -TEST(BlacklistTest, PatternMatch) { +TEST_F(BlacklistTest, PatternMatch) { // @ matches all but empty strings. EXPECT_TRUE(Blacklist::Matches("@", "foo.com")); EXPECT_TRUE(Blacklist::Matches("@", "path")); |