Commit 824ff459 authored by vakh's avatar vakh Committed by Commit bot

Each DatabaseManager gets to decide which stores to track.

V4LocalDatabaseManager now tracks the same lists for updates as well as full hashes.
SafeBrowsingDatabaseManager tracks only API requests.

Also, since hash_tables.h is deprecated, moved all code from base::hash_set to std::unordered_set using sed:
$ sed -i 's/base::hash_set/std::unordered_set/g' *

BUG=543161, 576864

Review-Url: https://codereview.chromium.org/2345573002
Cr-Commit-Position: refs/heads/master@{#419014}
parent 5ad02862
...@@ -65,6 +65,7 @@ static_library("database_manager") { ...@@ -65,6 +65,7 @@ static_library("database_manager") {
":hit_report", ":hit_report",
":util", ":util",
":v4_get_hash_protocol_manager", ":v4_get_hash_protocol_manager",
":v4_protocol_manager_util",
"//base", "//base",
"//content/public/browser", "//content/public/browser",
"//content/public/common", "//content/public/common",
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
#include "components/safe_browsing_db/v4_protocol_manager_util.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -25,9 +26,8 @@ void SafeBrowsingDatabaseManager::StartOnIOThread( ...@@ -25,9 +26,8 @@ void SafeBrowsingDatabaseManager::StartOnIOThread(
const V4ProtocolConfig& config) { const V4ProtocolConfig& config) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::hash_set<UpdateListIdentifier> stores_to_look({GetChromeUrlApiId()});
v4_get_hash_protocol_manager_ = V4GetHashProtocolManager::Create( v4_get_hash_protocol_manager_ = V4GetHashProtocolManager::Create(
request_context_getter, stores_to_look, config); request_context_getter, GetStoresForFullHashRequests(), config);
} }
// |shutdown| not used. Destroys the v4 protocol managers. This may be called // |shutdown| not used. Destroys the v4 protocol managers. This may be called
...@@ -71,6 +71,11 @@ bool SafeBrowsingDatabaseManager::CancelApiCheck(Client* client) { ...@@ -71,6 +71,11 @@ bool SafeBrowsingDatabaseManager::CancelApiCheck(Client* client) {
return false; return false;
} }
std::unordered_set<UpdateListIdentifier>
SafeBrowsingDatabaseManager::GetStoresForFullHashRequests() {
return std::unordered_set<UpdateListIdentifier>({GetChromeUrlApiId()});
}
bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url,
Client* client) { Client* client) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
......
...@@ -8,10 +8,9 @@ ...@@ -8,10 +8,9 @@
#ifndef COMPONENTS_SAFE_BROWSING_DB_DATABASE_MANAGER_H_ #ifndef COMPONENTS_SAFE_BROWSING_DB_DATABASE_MANAGER_H_
#define COMPONENTS_SAFE_BROWSING_DB_DATABASE_MANAGER_H_ #define COMPONENTS_SAFE_BROWSING_DB_DATABASE_MANAGER_H_
#include <deque>
#include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_set>
#include <vector> #include <vector>
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
...@@ -28,6 +27,7 @@ class URLRequestContextGetter; ...@@ -28,6 +27,7 @@ class URLRequestContextGetter;
namespace safe_browsing { namespace safe_browsing {
struct UpdateListIdentifier;
struct V4ProtocolConfig; struct V4ProtocolConfig;
class V4GetHashProtocolManager; class V4GetHashProtocolManager;
...@@ -221,6 +221,10 @@ class SafeBrowsingDatabaseManager ...@@ -221,6 +221,10 @@ class SafeBrowsingDatabaseManager
typedef std::set<SafeBrowsingApiCheck*> ApiCheckSet; typedef std::set<SafeBrowsingApiCheck*> ApiCheckSet;
// Returns the lists that this DatabaseManager should get full hashes for.
virtual std::unordered_set<UpdateListIdentifier>
GetStoresForFullHashRequests();
// Called on the IO thread when the SafeBrowsingProtocolManager has received // Called on the IO thread when the SafeBrowsingProtocolManager has received
// the full hash and api results for prefixes of the |url| argument in // the full hash and api results for prefixes of the |url| argument in
// CheckApiBlacklistUrl. // CheckApiBlacklistUrl.
......
...@@ -23,24 +23,24 @@ V4StoreFactory* V4Database::factory_ = NULL; ...@@ -23,24 +23,24 @@ V4StoreFactory* V4Database::factory_ = NULL;
void V4Database::Create( void V4Database::Create(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::FilePath& base_path, const base::FilePath& base_path,
const StoreFileNameMap& store_file_name_map, const StoreIdAndFileNames& store_id_file_names,
NewDatabaseReadyCallback new_db_callback) { NewDatabaseReadyCallback new_db_callback) {
DCHECK(base_path.IsAbsolute()); DCHECK(base_path.IsAbsolute());
DCHECK(!store_file_name_map.empty()); DCHECK(!store_id_file_names.empty());
const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner =
base::MessageLoop::current()->task_runner(); base::MessageLoop::current()->task_runner();
db_task_runner->PostTask( db_task_runner->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path,
store_file_name_map, callback_task_runner, new_db_callback)); store_id_file_names, callback_task_runner, new_db_callback));
} }
// static // static
void V4Database::CreateOnTaskRunner( void V4Database::CreateOnTaskRunner(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::FilePath& base_path, const base::FilePath& base_path,
const StoreFileNameMap& store_file_name_map, const StoreIdAndFileNames& store_id_file_names,
const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
NewDatabaseReadyCallback new_db_callback) { NewDatabaseReadyCallback new_db_callback) {
DCHECK(db_task_runner->RunsTasksOnCurrentThread()); DCHECK(db_task_runner->RunsTasksOnCurrentThread());
...@@ -55,10 +55,9 @@ void V4Database::CreateOnTaskRunner( ...@@ -55,10 +55,9 @@ void V4Database::CreateOnTaskRunner(
} }
std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>();
for (const auto& store_info : store_file_name_map) { for (const auto& it : store_id_file_names) {
const UpdateListIdentifier& update_list_identifier = store_info.first; const base::FilePath store_path = base_path.AppendASCII(it.filename);
const base::FilePath store_path = base_path.AppendASCII(store_info.second); (*store_map)[it.list_id].reset(
(*store_map)[update_list_identifier].reset(
factory_->CreateV4Store(db_task_runner, store_path)); factory_->CreateV4Store(db_task_runner, store_path));
} }
std::unique_ptr<V4Database> v4_database( std::unique_ptr<V4Database> v4_database(
...@@ -170,7 +169,7 @@ std::unique_ptr<StoreStateMap> V4Database::GetStoreStateMap() { ...@@ -170,7 +169,7 @@ std::unique_ptr<StoreStateMap> V4Database::GetStoreStateMap() {
void V4Database::GetStoresMatchingFullHash( void V4Database::GetStoresMatchingFullHash(
const FullHash& full_hash, const FullHash& full_hash,
const base::hash_set<UpdateListIdentifier>& stores_to_look, const std::unordered_set<UpdateListIdentifier>& stores_to_look,
StoreAndHashPrefixes* matched_store_and_hash_prefixes) { StoreAndHashPrefixes* matched_store_and_hash_prefixes) {
matched_store_and_hash_prefixes->clear(); matched_store_and_hash_prefixes->clear();
for (const UpdateListIdentifier& identifier : stores_to_look) { for (const UpdateListIdentifier& identifier : stores_to_look) {
...@@ -184,4 +183,12 @@ void V4Database::GetStoresMatchingFullHash( ...@@ -184,4 +183,12 @@ void V4Database::GetStoresMatchingFullHash(
} }
} }
StoreIdAndFileName::StoreIdAndFileName(const UpdateListIdentifier& list_id,
const std::string& filename)
: list_id(list_id), filename(filename) {
DCHECK(!filename.empty());
}
StoreIdAndFileName::~StoreIdAndFileName() {}
} // namespace safe_browsing } // namespace safe_browsing
...@@ -25,19 +25,32 @@ typedef base::Callback<void(std::unique_ptr<V4Database>)> ...@@ -25,19 +25,32 @@ typedef base::Callback<void(std::unique_ptr<V4Database>)>
// requests. // requests.
typedef base::Callback<void()> DatabaseUpdatedCallback; typedef base::Callback<void()> DatabaseUpdatedCallback;
// The set of interesting lists and ASCII filenames for their hash prefix // Maps the UpdateListIdentifiers to their corresponding in-memory stores, which
// stores. The stores are created inside the user-data directory. // contain the hash prefixes for that UpdateListIdentifier as well as manage
// For instance, the UpdateListIdentifier could be for URL expressions for UwS // their storage on disk.
// on Windows platform, and the corresponding file on disk could be named: typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap;
// "uws_win_url.store"
// TODO(vakh): Find the canonical place where these are defined and update the // TODO(vakh): Find the canonical place where these are defined and update the
// comment to point to that place. // comment to point to that place.
typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; struct StoreIdAndFileName {
// The list being read from/written to the disk.
UpdateListIdentifier list_id;
// This hash_map maps the UpdateListIdentifiers to their corresponding in-memory // The ASCII name of the file on disk. This file is created inside the
// stores, which contain the hash prefixes for that UpdateListIdentifier as well // user-data directory. For instance, the UpdateListIdentifier could be for
// as manage their storage on disk. // URL expressions for UwS on Windows platform, and the corresponding file on
typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; // disk could be named: "UrlUws.store"
std::string filename;
StoreIdAndFileName(const UpdateListIdentifier& list_id,
const std::string& filename);
~StoreIdAndFileName();
private:
StoreIdAndFileName();
};
using StoreIdAndFileNames = std::vector<StoreIdAndFileName>;
// Factory for creating V4Database. Tests implement this factory to create fake // Factory for creating V4Database. Tests implement this factory to create fake
// databases for testing. // databases for testing.
...@@ -47,7 +60,7 @@ class V4DatabaseFactory { ...@@ -47,7 +60,7 @@ class V4DatabaseFactory {
virtual V4Database* CreateV4Database( virtual V4Database* CreateV4Database(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::FilePath& base_dir_path, const base::FilePath& base_dir_path,
const StoreFileNameMap& store_file_name_map) = 0; const StoreIdAndFileNames& store_id_file_names) = 0;
}; };
// The on-disk databases are shared among all profiles, as it doesn't contain // The on-disk databases are shared among all profiles, as it doesn't contain
...@@ -66,7 +79,7 @@ class V4Database { ...@@ -66,7 +79,7 @@ class V4Database {
static void Create( static void Create(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::FilePath& base_path, const base::FilePath& base_path,
const StoreFileNameMap& store_file_name_map, const StoreIdAndFileNames& store_id_file_names,
NewDatabaseReadyCallback callback); NewDatabaseReadyCallback callback);
// Destroys the provided v4_database on its task_runner since this may be a // Destroys the provided v4_database on its task_runner since this may be a
...@@ -88,7 +101,7 @@ class V4Database { ...@@ -88,7 +101,7 @@ class V4Database {
// store along with the matching hash prefix in |matched_hash_prefix_map|. // store along with the matching hash prefix in |matched_hash_prefix_map|.
virtual void GetStoresMatchingFullHash( virtual void GetStoresMatchingFullHash(
const FullHash& full_hash, const FullHash& full_hash,
const base::hash_set<UpdateListIdentifier>& stores_to_look, const std::unordered_set<UpdateListIdentifier>& stores_to_look,
StoreAndHashPrefixes* matched_store_and_full_hashes); StoreAndHashPrefixes* matched_store_and_full_hashes);
// Deletes the current database and creates a new one. // Deletes the current database and creates a new one.
...@@ -120,7 +133,7 @@ class V4Database { ...@@ -120,7 +133,7 @@ class V4Database {
static void CreateOnTaskRunner( static void CreateOnTaskRunner(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::FilePath& base_path, const base::FilePath& base_path,
const StoreFileNameMap& store_file_name_map, const StoreIdAndFileNames& store_id_file_names,
const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
NewDatabaseReadyCallback callback); NewDatabaseReadyCallback callback);
......
...@@ -105,12 +105,12 @@ class V4DatabaseTest : public PlatformTest { ...@@ -105,12 +105,12 @@ class V4DatabaseTest : public PlatformTest {
} }
void SetupInfoMapAndExpectedState() { void SetupInfoMapAndExpectedState() {
store_file_name_map_[win_malware_id_] = "win_url_malware"; store_id_file_names_.emplace_back(win_malware_id_, "win_url_malware");
expected_identifiers_.push_back(win_malware_id_); expected_identifiers_.push_back(win_malware_id_);
expected_store_paths_.push_back( expected_store_paths_.push_back(
database_dirname_.AppendASCII("win_url_malware.fake")); database_dirname_.AppendASCII("win_url_malware.fake"));
store_file_name_map_[linux_malware_id_] = "linux_url_malware"; store_id_file_names_.emplace_back(linux_malware_id_, "linux_url_malware");
expected_identifiers_.push_back(linux_malware_id_); expected_identifiers_.push_back(linux_malware_id_);
expected_store_paths_.push_back( expected_store_paths_.push_back(
database_dirname_.AppendASCII("linux_url_malware.fake")); database_dirname_.AppendASCII("linux_url_malware.fake"));
...@@ -200,7 +200,7 @@ class V4DatabaseTest : public PlatformTest { ...@@ -200,7 +200,7 @@ class V4DatabaseTest : public PlatformTest {
content::TestBrowserThreadBundle thread_bundle_; content::TestBrowserThreadBundle thread_bundle_;
bool created_but_not_called_back_; bool created_but_not_called_back_;
bool created_and_called_back_; bool created_and_called_back_;
StoreFileNameMap store_file_name_map_; StoreIdAndFileNames store_id_file_names_;
std::vector<UpdateListIdentifier> expected_identifiers_; std::vector<UpdateListIdentifier> expected_identifiers_;
std::vector<base::FilePath> expected_store_paths_; std::vector<base::FilePath> expected_store_paths_;
bool expected_resets_successfully_; bool expected_resets_successfully_;
...@@ -217,7 +217,7 @@ TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { ...@@ -217,7 +217,7 @@ TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -231,7 +231,7 @@ TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { ...@@ -231,7 +231,7 @@ TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) {
expected_resets_successfully_ = false; expected_resets_successfully_ = false;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -245,7 +245,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { ...@@ -245,7 +245,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -276,7 +276,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { ...@@ -276,7 +276,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -307,7 +307,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { ...@@ -307,7 +307,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -339,7 +339,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { ...@@ -339,7 +339,7 @@ TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_); RegisterFactory(!expected_resets_successfully_);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -370,7 +370,7 @@ TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { ...@@ -370,7 +370,7 @@ TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -378,13 +378,13 @@ TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { ...@@ -378,13 +378,13 @@ TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(true, created_and_called_back_); EXPECT_EQ(true, created_and_called_back_);
base::hash_set<UpdateListIdentifier> stores_to_look( std::unordered_set<UpdateListIdentifier> stores_to_look(
{linux_malware_id_, win_malware_id_}); {linux_malware_id_, win_malware_id_});
StoreAndHashPrefixes store_and_hash_prefixes; StoreAndHashPrefixes store_and_hash_prefixes;
v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, v4_database_->GetStoresMatchingFullHash("anything", stores_to_look,
&store_and_hash_prefixes); &store_and_hash_prefixes);
EXPECT_EQ(2u, store_and_hash_prefixes.size()); EXPECT_EQ(2u, store_and_hash_prefixes.size());
base::hash_set<UpdateListIdentifier> stores_found; std::unordered_set<UpdateListIdentifier> stores_found;
for (const auto& it : store_and_hash_prefixes) { for (const auto& it : store_and_hash_prefixes) {
stores_found.insert(it.list_id); stores_found.insert(it.list_id);
} }
...@@ -397,7 +397,7 @@ TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { ...@@ -397,7 +397,7 @@ TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -405,7 +405,7 @@ TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { ...@@ -405,7 +405,7 @@ TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(true, created_and_called_back_); EXPECT_EQ(true, created_and_called_back_);
base::hash_set<UpdateListIdentifier> stores_to_look( std::unordered_set<UpdateListIdentifier> stores_to_look(
{linux_malware_id_, win_malware_id_}); {linux_malware_id_, win_malware_id_});
StoreAndHashPrefixes store_and_hash_prefixes; StoreAndHashPrefixes store_and_hash_prefixes;
v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, v4_database_->GetStoresMatchingFullHash("anything", stores_to_look,
...@@ -420,7 +420,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { ...@@ -420,7 +420,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -433,7 +433,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { ...@@ -433,7 +433,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) {
v4_database_->store_map_->at(win_malware_id_).get()); v4_database_->store_map_->at(win_malware_id_).get());
store->set_hash_prefix_matches(true); store->set_hash_prefix_matches(true);
base::hash_set<UpdateListIdentifier> stores_to_look( std::unordered_set<UpdateListIdentifier> stores_to_look(
{linux_malware_id_, win_malware_id_}); {linux_malware_id_, win_malware_id_});
StoreAndHashPrefixes store_and_hash_prefixes; StoreAndHashPrefixes store_and_hash_prefixes;
v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, v4_database_->GetStoresMatchingFullHash("anything", stores_to_look,
...@@ -451,7 +451,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { ...@@ -451,7 +451,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) {
expected_resets_successfully_ = true; expected_resets_successfully_ = true;
RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, V4Database::Create(task_runner_, database_dirname_, store_id_file_names_,
callback_db_ready_); callback_db_ready_);
created_but_not_called_back_ = true; created_but_not_called_back_ = true;
task_runner_->RunPendingTasks(); task_runner_->RunPendingTasks();
...@@ -459,7 +459,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { ...@@ -459,7 +459,7 @@ TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(true, created_and_called_back_); EXPECT_EQ(true, created_and_called_back_);
base::hash_set<UpdateListIdentifier> stores_to_look({linux_malware_id_}); std::unordered_set<UpdateListIdentifier> stores_to_look({linux_malware_id_});
// Don't add win_malware_id_ to the stores_to_look. // Don't add win_malware_id_ to the stores_to_look.
StoreAndHashPrefixes store_and_hash_prefixes; StoreAndHashPrefixes store_and_hash_prefixes;
v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, v4_database_->GetStoresMatchingFullHash("anything", stores_to_look,
......
...@@ -140,7 +140,7 @@ class V4GetHashProtocolManagerFactoryImpl ...@@ -140,7 +140,7 @@ class V4GetHashProtocolManagerFactoryImpl
~V4GetHashProtocolManagerFactoryImpl() override {} ~V4GetHashProtocolManagerFactoryImpl() override {}
std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config) override { const V4ProtocolConfig& config) override {
return base::WrapUnique(new V4GetHashProtocolManager( return base::WrapUnique(new V4GetHashProtocolManager(
request_context_getter, stores_to_request, config)); request_context_getter, stores_to_request, config));
...@@ -209,7 +209,7 @@ V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; ...@@ -209,7 +209,7 @@ V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL;
// static // static
std::unique_ptr<V4GetHashProtocolManager> V4GetHashProtocolManager::Create( std::unique_ptr<V4GetHashProtocolManager> V4GetHashProtocolManager::Create(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config) { const V4ProtocolConfig& config) {
if (!factory_) if (!factory_)
factory_ = new V4GetHashProtocolManagerFactoryImpl(); factory_ = new V4GetHashProtocolManagerFactoryImpl();
...@@ -227,7 +227,7 @@ void V4GetHashProtocolManager::RegisterFactory( ...@@ -227,7 +227,7 @@ void V4GetHashProtocolManager::RegisterFactory(
V4GetHashProtocolManager::V4GetHashProtocolManager( V4GetHashProtocolManager::V4GetHashProtocolManager(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config) const V4ProtocolConfig& config)
: gethash_error_count_(0), : gethash_error_count_(0),
gethash_back_off_mult_(1), gethash_back_off_mult_(1),
...@@ -308,7 +308,7 @@ void V4GetHashProtocolManager::GetFullHashesWithApis( ...@@ -308,7 +308,7 @@ void V4GetHashProtocolManager::GetFullHashesWithApis(
ThreatMetadataForApiCallback api_callback) { ThreatMetadataForApiCallback api_callback) {
DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)); DCHECK(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme));
base::hash_set<FullHash> full_hashes; std::unordered_set<FullHash> full_hashes;
V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
...@@ -365,7 +365,7 @@ void V4GetHashProtocolManager::GetFullHashCachedResults( ...@@ -365,7 +365,7 @@ void V4GetHashProtocolManager::GetFullHashCachedResults(
// cache entry if they expire AND their expire time is after the negative // cache entry if they expire AND their expire time is after the negative
// cache expire time. // cache expire time.
base::hash_set<HashPrefix> unique_prefixes_to_request; std::unordered_set<HashPrefix> unique_prefixes_to_request;
for (const auto& it : full_hash_to_store_and_hash_prefixes) { for (const auto& it : full_hash_to_store_and_hash_prefixes) {
const FullHash& full_hash = it.first; const FullHash& full_hash = it.first;
const StoreAndHashPrefixes& matched = it.second; const StoreAndHashPrefixes& matched = it.second;
...@@ -464,7 +464,7 @@ void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { ...@@ -464,7 +464,7 @@ void V4GetHashProtocolManager::HandleGetHashError(const Time& now) {
void V4GetHashProtocolManager::OnFullHashForApi( void V4GetHashProtocolManager::OnFullHashForApi(
const ThreatMetadataForApiCallback& api_callback, const ThreatMetadataForApiCallback& api_callback,
const base::hash_set<FullHash>& full_hashes, const std::unordered_set<FullHash>& full_hashes,
const std::vector<FullHashInfo>& full_hash_infos) { const std::vector<FullHashInfo>& full_hash_infos) {
ThreatMetadata md; ThreatMetadata md;
for (const FullHashInfo& full_hash_info : full_hash_infos) { for (const FullHashInfo& full_hash_info : full_hash_infos) {
......
...@@ -147,7 +147,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate, ...@@ -147,7 +147,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate,
// Create an instance of the safe browsing v4 protocol manager. // Create an instance of the safe browsing v4 protocol manager.
static std::unique_ptr<V4GetHashProtocolManager> Create( static std::unique_ptr<V4GetHashProtocolManager> Create(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config); const V4ProtocolConfig& config);
// Makes the passed |factory| the factory used to instantiate // Makes the passed |factory| the factory used to instantiate
...@@ -187,7 +187,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate, ...@@ -187,7 +187,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate,
// network requests using |request_context_getter|. // network requests using |request_context_getter|.
V4GetHashProtocolManager( V4GetHashProtocolManager(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config); const V4ProtocolConfig& config);
private: private:
...@@ -255,7 +255,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate, ...@@ -255,7 +255,7 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate,
// permission API metadata for full hashes in those |full_hash_infos| that // permission API metadata for full hashes in those |full_hash_infos| that
// have a full hash in |full_hashes|. // have a full hash in |full_hashes|.
void OnFullHashForApi(const ThreatMetadataForApiCallback& api_callback, void OnFullHashForApi(const ThreatMetadataForApiCallback& api_callback,
const base::hash_set<FullHash>& full_hashes, const std::unordered_set<FullHash>& full_hashes,
const std::vector<FullHashInfo>& full_hash_infos); const std::vector<FullHashInfo>& full_hash_infos);
// Parses a FindFullHashesResponse protocol buffer and fills the results in // Parses a FindFullHashesResponse protocol buffer and fills the results in
...@@ -331,9 +331,9 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate, ...@@ -331,9 +331,9 @@ class V4GetHashProtocolManager : public net::URLFetcherDelegate,
// The following sets represent the combination of lists that we would always // The following sets represent the combination of lists that we would always
// request from the server, irrespective of which list we found the hash // request from the server, irrespective of which list we found the hash
// prefix match in. // prefix match in.
base::hash_set<PlatformType> platform_types_; std::unordered_set<PlatformType> platform_types_;
base::hash_set<ThreatEntryType> threat_entry_types_; std::unordered_set<ThreatEntryType> threat_entry_types_;
base::hash_set<ThreatType> threat_types_; std::unordered_set<ThreatType> threat_types_;
DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager); DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager);
}; };
...@@ -345,7 +345,7 @@ class V4GetHashProtocolManagerFactory { ...@@ -345,7 +345,7 @@ class V4GetHashProtocolManagerFactory {
virtual ~V4GetHashProtocolManagerFactory() {} virtual ~V4GetHashProtocolManagerFactory() {}
virtual std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager( virtual std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
const base::hash_set<UpdateListIdentifier>& stores_to_request, const std::unordered_set<UpdateListIdentifier>& stores_to_request,
const V4ProtocolConfig& config) = 0; const V4ProtocolConfig& config) = 0;
private: private:
......
...@@ -79,7 +79,7 @@ class V4GetHashProtocolManagerTest : public PlatformTest { ...@@ -79,7 +79,7 @@ class V4GetHashProtocolManagerTest : public PlatformTest {
config.client_name = kClient; config.client_name = kClient;
config.version = kAppVer; config.version = kAppVer;
config.key_param = kKeyParam; config.key_param = kKeyParam;
base::hash_set<UpdateListIdentifier> stores_to_look( std::unordered_set<UpdateListIdentifier> stores_to_look(
{GetUrlMalwareId(), GetChromeUrlApiId()}); {GetUrlMalwareId(), GetChromeUrlApiId()});
return V4GetHashProtocolManager::Create(NULL, stores_to_look, config); return V4GetHashProtocolManager::Create(NULL, stores_to_look, config);
} }
......
...@@ -18,18 +18,23 @@ namespace safe_browsing { ...@@ -18,18 +18,23 @@ namespace safe_browsing {
namespace { namespace {
// TODO(vakh): Implement this to populate the map appopriately. // TODO(vakh): Implement this to populate the vector appopriately.
// Filed as http://crbug.com/608075 // Filed as http://crbug.com/608075
StoreFileNameMap GetStoreFileNameMap() { StoreIdAndFileNames GetStoreIdAndFileNames() {
return StoreFileNameMap({{GetUrlMalwareId(), "UrlMalware.store"}, return StoreIdAndFileNames(
{GetUrlSocEngId(), "UrlSoceng.store"}}); {StoreIdAndFileName(GetUrlMalwareId(), "UrlMalware.store"),
StoreIdAndFileName(GetUrlSocEngId(), "UrlSoceng.store")});
} }
} // namespace } // namespace
V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path)
: base_path_(base_path), enabled_(false) { : base_path_(base_path),
enabled_(false),
store_id_file_names_(GetStoreIdAndFileNames()) {
DCHECK(!base_path_.empty()); DCHECK(!base_path_.empty());
DCHECK(!store_id_file_names_.empty());
DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: "
<< "base_path_: " << base_path_.AsUTF8Unsafe(); << "base_path_: " << base_path_.AsUTF8Unsafe();
} }
...@@ -140,13 +145,13 @@ bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { ...@@ -140,13 +145,13 @@ bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) {
} }
if (v4_database_) { if (v4_database_) {
base::hash_set<FullHash> full_hashes; std::unordered_set<FullHash> full_hashes;
V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
base::hash_set<UpdateListIdentifier> stores_to_look( std::unordered_set<UpdateListIdentifier> stores_to_look(
{GetUrlMalwareId(), GetUrlSocEngId()}); {GetUrlMalwareId(), GetUrlSocEngId()});
base::hash_set<HashPrefix> matched_hash_prefixes; std::unordered_set<HashPrefix> matched_hash_prefixes;
base::hash_set<UpdateListIdentifier> matched_stores; std::unordered_set<UpdateListIdentifier> matched_stores;
StoreAndHashPrefixes matched_store_and_full_hashes; StoreAndHashPrefixes matched_store_and_full_hashes;
FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
for (const auto& full_hash : full_hashes) { for (const auto& full_hash : full_hashes) {
...@@ -197,7 +202,6 @@ void V4LocalDatabaseManager::StartOnIOThread( ...@@ -197,7 +202,6 @@ void V4LocalDatabaseManager::StartOnIOThread(
base::Unretained(this)); base::Unretained(this));
SetupUpdateProtocolManager(request_context_getter, config); SetupUpdateProtocolManager(request_context_getter, config);
SetupDatabase(); SetupDatabase();
enabled_ = true; enabled_ = true;
...@@ -215,6 +219,7 @@ void V4LocalDatabaseManager::SetupUpdateProtocolManager( ...@@ -215,6 +219,7 @@ void V4LocalDatabaseManager::SetupUpdateProtocolManager(
void V4LocalDatabaseManager::SetupDatabase() { void V4LocalDatabaseManager::SetupDatabase() {
DCHECK(!base_path_.empty()); DCHECK(!base_path_.empty());
DCHECK(!store_id_file_names_.empty());
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Only get a new task runner if there isn't one already. If the service has // Only get a new task runner if there isn't one already. If the service has
...@@ -228,11 +233,9 @@ void V4LocalDatabaseManager::SetupDatabase() { ...@@ -228,11 +233,9 @@ void V4LocalDatabaseManager::SetupDatabase() {
// Do not create the database on the IO thread since this may be an expensive // Do not create the database on the IO thread since this may be an expensive
// operation. Instead, do that on the task_runner and when the new database // operation. Instead, do that on the task_runner and when the new database
// has been created, swap it out on the IO thread. // has been created, swap it out on the IO thread.
StoreFileNameMap store_file_name_map = GetStoreFileNameMap();
DCHECK(!store_file_name_map.empty());
NewDatabaseReadyCallback db_ready_callback = base::Bind( NewDatabaseReadyCallback db_ready_callback = base::Bind(
&V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this));
V4Database::Create(task_runner_, base_path_, store_file_name_map, V4Database::Create(task_runner_, base_path_, store_id_file_names_,
db_ready_callback); db_ready_callback);
} }
...@@ -287,4 +290,13 @@ void V4LocalDatabaseManager::DatabaseUpdated() { ...@@ -287,4 +290,13 @@ void V4LocalDatabaseManager::DatabaseUpdated() {
} }
} }
std::unordered_set<UpdateListIdentifier>
V4LocalDatabaseManager::GetStoresForFullHashRequests() {
std::unordered_set<UpdateListIdentifier> stores_for_full_hash;
for (auto it : store_id_file_names_) {
stores_for_full_hash.insert(it.list_id);
}
return stores_for_full_hash;
}
} // namespace safe_browsing } // namespace safe_browsing
...@@ -58,6 +58,10 @@ class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager { ...@@ -58,6 +58,10 @@ class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager {
bool IsMalwareKillSwitchOn() override; bool IsMalwareKillSwitchOn() override;
bool IsCsdWhitelistKillSwitchOn() override; bool IsCsdWhitelistKillSwitchOn() override;
protected:
std::unordered_set<UpdateListIdentifier> GetStoresForFullHashRequests()
override;
private: private:
friend class V4LocalDatabaseManagerTest; friend class V4LocalDatabaseManagerTest;
void SetTaskRunnerForTest( void SetTaskRunnerForTest(
...@@ -97,6 +101,10 @@ class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager { ...@@ -97,6 +101,10 @@ class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager {
// It should come through the database, from the various V4Stores. // It should come through the database, from the various V4Stores.
base::hash_map<UpdateListIdentifier, std::string> current_list_states_; base::hash_map<UpdateListIdentifier, std::string> current_list_states_;
// The list of stores to manage (for hash prefixes and full hashes), along
// with the corresponding filename on disk for each of them.
StoreIdAndFileNames store_id_file_names_;
// The protocol manager that downloads the hash prefix updates. // The protocol manager that downloads the hash prefix updates.
std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_; std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_;
......
...@@ -25,7 +25,7 @@ class FakeV4Database : public V4Database { ...@@ -25,7 +25,7 @@ class FakeV4Database : public V4Database {
void GetStoresMatchingFullHash( void GetStoresMatchingFullHash(
const FullHash& full_hash, const FullHash& full_hash,
const base::hash_set<UpdateListIdentifier>& stores_to_look, const std::unordered_set<UpdateListIdentifier>& stores_to_look,
StoreAndHashPrefixes* store_and_hash_prefixes) override { StoreAndHashPrefixes* store_and_hash_prefixes) override {
*store_and_hash_prefixes = store_and_hash_prefixes_; *store_and_hash_prefixes = store_and_hash_prefixes_;
} }
......
...@@ -237,7 +237,7 @@ void V4ProtocolManagerUtil::UpdateHeaders(net::HttpRequestHeaders* headers) { ...@@ -237,7 +237,7 @@ void V4ProtocolManagerUtil::UpdateHeaders(net::HttpRequestHeaders* headers) {
// static // static
void V4ProtocolManagerUtil::UrlToFullHashes( void V4ProtocolManagerUtil::UrlToFullHashes(
const GURL& url, const GURL& url,
base::hash_set<FullHash>* full_hashes) { std::unordered_set<FullHash>* full_hashes) {
std::string canon_host, canon_path, canon_query; std::string canon_host, canon_path, canon_query;
CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query); CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query);
......
...@@ -207,7 +207,7 @@ class V4ProtocolManagerUtil { ...@@ -207,7 +207,7 @@ class V4ProtocolManagerUtil {
// Generate the set of FullHashes to check for |url|. // Generate the set of FullHashes to check for |url|.
static void UrlToFullHashes(const GURL& url, static void UrlToFullHashes(const GURL& url,
base::hash_set<FullHash>* full_hashes); std::unordered_set<FullHash>* full_hashes);
static bool FullHashToHashPrefix(const FullHash& full_hash, static bool FullHashToHashPrefix(const FullHash& full_hash,
PrefixSize prefix_size, PrefixSize prefix_size,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment