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..3a7ef55c3431ddd4f72ec03078cf0354654c98fc |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/remote_database_manager.cc |
| @@ -0,0 +1,172 @@ |
| +// 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; |
| +} |
| + |
| +SafeBrowsingProtocolManagerDelegate* |
| +RemoteSafeBrowsingDatabaseManager::GetProtocolManagerDelegate() { |
| + return NULL; |
| +} |
| + |
| +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_refptr<ClientRequest> req = new ClientRequest(); |
|
mattm
2015/05/06 02:42:45
Refcounting is definitely more common in chrome, b
Nathan Parker
2015/05/06 22:27:36
Good idea. I think that's what you were suggestin
|
| + req->db_manager = this; |
| + req->client = client; |
| + req->url = url; |
| + |
| + 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::OnRequestDone, req), url, threat_types); |
| + if (!started) { |
| + LOG(DFATAL) << "Failed to start Safe Browsing request"; |
| + return true; |
| + } |
| + |
| + // Now api_handler_ holds a ref via the callback, and we hold a ref here. |
| + current_requests_.push_back(req); |
| + |
| + // Defer the resource load. |
| + return false; |
| +} |
| + |
| +void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone( |
| + SBThreatType matched_threat_type, |
| + const std::string& metadata) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + VLOG(1) << "OnRequestDone for client " << client << " and URL " << url; |
| + if (!client) |
|
mattm
2015/05/06 02:42:45
if you do it with weakptrs I think you can also ge
Nathan Parker
2015/05/06 22:27:36
Done.
|
| + return; // Already canceled. |
| + client->OnCheckBrowseUrlResult(url, matched_threat_type, metadata); |
| + db_manager->CancelCheck(client); |
| +} |
| + |
| +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) { |
| + // Mark the req as canceled. |
| + (*itr)->client = NULL; |
| + VLOG(1) << "Canceling check for URL " << (*itr)->url; |
| + 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 remove any remaining clients. This modifies |
| + // |current_requests_|, so make a copy first. |
| + std::vector<scoped_refptr<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; |
| +} |
| + |