Chromium Code Reviews| Index: chrome/browser/safe_browsing/remote_database_manager.cc |
| diff --git a/chrome/browser/safe_browsing/remote_database_manager.cc b/chrome/browser/safe_browsing/remote_database_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a48dbcb349871831a1573a4a3c668a7bbc5b4e29 |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/remote_database_manager.cc |
| @@ -0,0 +1,179 @@ |
| +// Copyright (c) 2015 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/safe_browsing/remote_database_manager.h" |
| + |
| +#include <vector> |
| + |
| +#include "chrome/browser/safe_browsing/android_safe_browsing_api_handler.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +// TODO(nparker): Add tests for this class once implemented. |
| +RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager() |
| + : enabled_(false) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + // TODO(nparker): Implement the initialization glue. |
| + // Check flag to see if this is enabled. |
| + // Ask AndroidSafeBrowsingAPIHandler if Java API is correct version. |
| +} |
| + |
| +RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() { |
| + DCHECK(!enabled_); |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { |
| + return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) || |
| + url.SchemeIs(url::kFtpScheme); |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled() |
| + const { |
| + return false; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl( |
| + const std::vector<GURL>& url_chain, |
| + Client* client) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs( |
| + const std::set<std::string>& extension_ids, |
| + Client* client) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP( |
| + const std::string& ip_address) { |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl( |
| + const GURL& url) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString( |
| + const std::string& str) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl( |
| + const GURL& url) { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { |
| + NOTREACHED(); |
| + return true; |
| +} |
| + |
| +bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, |
| + Client* client) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!enabled_) |
| + return true; |
| + |
| + if (!CanCheckUrl(url)) |
| + return true; // Safe, continue right away. |
| + |
| + scoped_ptr<ClientRequest> req(new ClientRequest()); |
| + req->db_manager_ = this; |
| + req->client_ = client; |
| + req->url_ = url; |
|
mattm
2015/05/08 02:17:26
could just have ClientRequest constructor take the
Nathan Parker
2015/05/12 01:04:01
Done, while making them private.
|
| + |
| + std::vector<SBThreatType> threat_types { SB_THREAT_TYPE_URL_MALWARE }; |
| + |
| + VLOG(1) << "Checking for client " << client << " and URL " << url; |
| + bool started = |
| + api_handler_.StartURLCheck(base::Bind(&ClientRequest::OnRequestDoneWeak, |
| + req->weak_factory_.GetWeakPtr()), |
| + url, threat_types); |
| + if (!started) { |
| + LOG(DFATAL) << "Failed to start Safe Browsing request"; |
| + return true; |
| + } |
| + |
| + current_requests_.push_back(req.release()); |
| + |
| + // Defer the resource load. |
| + return false; |
| +} |
| + |
| +void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client* client) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK(enabled_); |
| + for (auto itr = current_requests_.begin(); itr != current_requests_.end(); |
| + ++itr) { |
| + if ((*itr)->client_ == client) { |
| + VLOG(1) << "Canceling check for URL " << (*itr)->url_; |
| + delete *itr; |
| + current_requests_.erase(itr); |
| + return; |
| + } |
| + } |
| + NOTREACHED(); |
| +} |
| + |
| +void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() { |
| + VLOG(1) << "RemoteSafeBrowsing starting"; |
| + enabled_ = true; |
| +} |
| + |
| +void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { |
| + // |shutdown| is not used. |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + VLOG(1) << "RemoteSafeBrowsing stopping"; |
| + |
| + // Call back and delete any remaining clients. OnRequestDone() modifies |
| + // |current_requests_|, so we make a copy first. |
| + std::vector<ClientRequest*> to_callback(current_requests_); |
| + for (auto req : to_callback) { |
| + VLOG(1) << "Stopping: Invoking unfinished req for URL " << req->url_; |
| + req->OnRequestDone(SB_THREAT_TYPE_SAFE, std::string()); |
| + } |
| + enabled_ = false; |
| +} |
| + |
| +// |
| +// RemoteSafeBrowsingDatabaseManager::ClientRequest methods |
| +// |
| + |
| +// Static |
| +void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDoneWeak( |
| + const base::WeakPtr<ClientRequest>& req, |
| + SBThreatType matched_threat_type, |
| + const std::string& metadata) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!req) |
| + return; // Previously canceled |
| + req->OnRequestDone(matched_threat_type, metadata); |
| +} |
| + |
| +void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone( |
| + SBThreatType matched_threat_type, |
| + const std::string& metadata) { |
| + VLOG(1) << "OnRequestDone for client " << client_ << " and URL " << url_; |
| + client_->OnCheckBrowseUrlResult(url_, matched_threat_type, metadata); |
| + db_manager_->CancelCheck(client_); |
| +} |
| + |