Skip to content

Commit

Permalink
Skeleton implementation of the V4LocalDatabaseManager
Browse files Browse the repository at this point in the history
TODOs: Tests, fill in skeleton implementation, instantiate in SBService.

BUG=543161

Review URL: https://codereview.chromium.org/1858923002

Cr-Commit-Position: refs/heads/master@{#386133}
  • Loading branch information
aawc authored and Commit bot committed Apr 8, 2016
1 parent fab2d8a commit 26a23d7
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/safe_browsing_db.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
'safe_browsing_db/prefix_set.cc',
'safe_browsing_db/util.h',
'safe_browsing_db/util.cc',
'safe_browsing_db/v4_local_database_manager.h',
'safe_browsing_db/v4_local_database_manager.cc',
'safe_browsing_db/v4_protocol_manager_util.h',
'safe_browsing_db/v4_protocol_manager_util.cc',
'safe_browsing_db/v4_get_hash_protocol_manager.h',
Expand Down
18 changes: 18 additions & 0 deletions components/safe_browsing_db/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ source_set("v4_get_hash_protocol_manager") {
]
}

source_set("v4_local_database_manager") {
sources = [
"v4_local_database_manager.cc",
"v4_local_database_manager.h",
]
deps = [
":database_manager",
":hit_report",
":v4_protocol_manager_util",
":v4_update_protocol_manager",
"//base",
"//content/public/browser",
"//net",
"//url:url",
]
}

source_set("v4_protocol_manager_util") {
sources = [
"v4_protocol_manager_util.cc",
Expand Down Expand Up @@ -213,6 +230,7 @@ source_set("unit_tests") {
":proto",
":util",
":v4_get_hash_protocol_manager",
":v4_local_database_manager",
":v4_protocol_manager_util",
":v4_update_protocol_manager",
"//base",
Expand Down
172 changes: 172 additions & 0 deletions components/safe_browsing_db/v4_local_database_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright 2016 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 "components/safe_browsing_db/v4_local_database_manager.h"

#include <vector>

#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace safe_browsing {

V4LocalDatabaseManager::V4LocalDatabaseManager() : enabled_(false) {}

V4LocalDatabaseManager::~V4LocalDatabaseManager() {
DCHECK(!enabled_);
}

bool V4LocalDatabaseManager::IsSupported() const {
return true;
}

safe_browsing::ThreatSource V4LocalDatabaseManager::GetThreatSource() const {
return safe_browsing::ThreatSource::LOCAL_PVER4;
}

bool V4LocalDatabaseManager::ChecksAreAlwaysAsync() const {
return false;
}

bool V4LocalDatabaseManager::CanCheckResourceType(
content::ResourceType resource_type) const {
// We check all types since most checks are fast.
return true;
}

bool V4LocalDatabaseManager::CanCheckUrl(const GURL& url) const {
return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) ||
url.SchemeIs(url::kFtpScheme);
}

bool V4LocalDatabaseManager::IsDownloadProtectionEnabled() const {
// TODO(vakh): Investigate the possibility of using a command line switch for
// this instead.
return true;
}

bool V4LocalDatabaseManager::CheckDownloadUrl(
const std::vector<GURL>& url_chain,
Client* client) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(vakh): Implement this skeleton.
return true;
}

bool V4LocalDatabaseManager::CheckExtensionIDs(
const std::set<std::string>& extension_ids,
Client* client) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::MatchMalwareIP(const std::string& ip_address) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return false;
}

bool V4LocalDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::MatchDownloadWhitelistUrl(const GURL& url) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::MatchDownloadWhitelistString(
const std::string& str) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::MatchInclusionWhitelistUrl(const GURL& url) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::MatchModuleWhitelistString(
const std::string& str) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::CheckResourceUrl(const GURL& url, Client* client) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::IsMalwareKillSwitchOn() {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::IsCsdWhitelistKillSwitchOn() {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return true;
}

bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!enabled_)
return true;

// Don't defer the resource load.
return true;
}

void V4LocalDatabaseManager::CancelCheck(Client* client) {
// TODO(vakh): Implement this skeleton.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(enabled_);
}

void V4LocalDatabaseManager::StartOnIOThread(
net::URLRequestContextGetter* request_context_getter,
const V4ProtocolConfig& config) {
// TODO(vakh): Implement this skeleton.
VLOG(1) << "V4LocalDatabaseManager starting";
SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config);

V4UpdateCallback callback = base::Bind(
&V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this));
v4_update_protocol_manager_ = V4UpdateProtocolManager::Create(
request_context_getter, config, current_list_states_, callback);

enabled_ = true;
}

void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DVLOG(1) << "V4LocalDatabaseManager stopping";

// Delete the V4UpdateProtocolManager.
// This cancels any in-flight update request.
if (v4_update_protocol_manager_.get()) {
v4_update_protocol_manager_.reset();
}

enabled_ = false;
SafeBrowsingDatabaseManager::StopOnIOThread(shutdown);
}

void V4LocalDatabaseManager::UpdateRequestCompleted(
const std::vector<ListUpdateResponse>& responses) {
// TODO(vakh): Updates downloaded. Store them on disk and record new state.
}

} // namespace safe_browsing
81 changes: 81 additions & 0 deletions components/safe_browsing_db/v4_local_database_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2016 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 COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_
#define COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_

// A class that provides the interface between the SafeBrowsing protocol manager
// and database that holds the downloaded updates.

#include "components/safe_browsing_db/database_manager.h"
#include "components/safe_browsing_db/hit_report.h"
#include "components/safe_browsing_db/v4_protocol_manager_util.h"
#include "components/safe_browsing_db/v4_update_protocol_manager.h"
#include "url/gurl.h"

using content::ResourceType;

namespace safe_browsing {

// Manages the local, on-disk database of updates downloaded from the
// SafeBrowsing service and interfaces with the protocol manager.
class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager {
public:
// Construct V4LocalDatabaseManager.
// Must be initialized by calling StartOnIOThread() before using.
V4LocalDatabaseManager();

//
// SafeBrowsingDatabaseManager implementation
//

bool IsSupported() const override;
safe_browsing::ThreatSource GetThreatSource() const override;
bool ChecksAreAlwaysAsync() const override;
bool CanCheckResourceType(content::ResourceType resource_type) const override;
bool CanCheckUrl(const GURL& url) const override;
bool IsDownloadProtectionEnabled() const override;
bool CheckBrowseUrl(const GURL& url, Client* client) override;
void CancelCheck(Client* client) override;
void StartOnIOThread(
net::URLRequestContextGetter* request_context_getter,
const V4ProtocolConfig& config) override;
void StopOnIOThread(bool shutdown) override;
bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
Client* client) override;
bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
Client* client) override;
bool MatchCsdWhitelistUrl(const GURL& url) override;
bool MatchMalwareIP(const std::string& ip_address) override;
bool MatchDownloadWhitelistUrl(const GURL& url) override;
bool MatchDownloadWhitelistString(const std::string& str) override;
bool MatchInclusionWhitelistUrl(const GURL& url) override;
bool MatchModuleWhitelistString(const std::string& str) override;
bool CheckResourceUrl(const GURL& url, Client* client) override;
bool IsMalwareKillSwitchOn() override;
bool IsCsdWhitelistKillSwitchOn() override;

private:
~V4LocalDatabaseManager() override;

// The callback called each time the protocol manager downloads updates
// successfully.
void UpdateRequestCompleted(const std::vector<ListUpdateResponse>& responses);

bool enabled_;

// Stores the current status of the lists to download from the SafeBrowsing
// servers.
base::hash_map<UpdateListIdentifier, std::string> current_list_states_;

// The protocol manager that downloads the hash prefix updates.
scoped_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_;

friend class base::RefCountedThreadSafe<V4LocalDatabaseManager>;
DISALLOW_COPY_AND_ASSIGN(V4LocalDatabaseManager);
}; // class V4LocalDatabaseManager

} // namespace safe_browsing

#endif // COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_

0 comments on commit 26a23d7

Please sign in to comment.