Commit 1f4a4281 authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Update SafeBrowsingProtocolManager to use SimpleURLLoader.

Bug: 825242
Change-Id: I9e674841ca4199d53b9b50f12050cb4d085daa10
Reviewed-on: https://chromium-review.googlesource.com/1007010Reviewed-by: default avatarJialiu Lin <jialiul@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550173}
parent 405745c9
...@@ -33,14 +33,12 @@ ...@@ -33,14 +33,12 @@
#include "components/safe_browsing/common/safe_browsing_prefs.h" #include "components/safe_browsing/common/safe_browsing_prefs.h"
#include "components/safe_browsing/db/safebrowsing.pb.h" #include "components/safe_browsing/db/safebrowsing.pb.h"
#include "components/safe_browsing/db/util.h" #include "components/safe_browsing/db/util.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace net { namespace network {
class URLFetcher; class SimpleURLLoader;
class URLRequestContextGetter; class SharedURLLoaderFactory;
} // namespace net } // namespace network
namespace safe_browsing { namespace safe_browsing {
...@@ -48,7 +46,7 @@ class SBProtocolManagerFactory; ...@@ -48,7 +46,7 @@ class SBProtocolManagerFactory;
class SafeBrowsingProtocolManagerDelegate; class SafeBrowsingProtocolManagerDelegate;
// Lives on the IO thread. // Lives on the IO thread.
class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { class SafeBrowsingProtocolManager {
public: public:
// FullHashCallback is invoked when GetFullHash completes. // FullHashCallback is invoked when GetFullHash completes.
// Parameters: // Parameters:
...@@ -60,7 +58,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -60,7 +58,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
base::Callback<void(const std::vector<SBFullHashResult>&, base::Callback<void(const std::vector<SBFullHashResult>&,
const base::TimeDelta&)>; const base::TimeDelta&)>;
~SafeBrowsingProtocolManager() override; virtual ~SafeBrowsingProtocolManager();
// Makes the passed |factory| the factory used to instantiate // Makes the passed |factory| the factory used to instantiate
// a SafeBrowsingService. Useful for tests. // a SafeBrowsingService. Useful for tests.
...@@ -71,15 +69,22 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -71,15 +69,22 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// Create an instance of the safe browsing protocol manager. // Create an instance of the safe browsing protocol manager.
static std::unique_ptr<SafeBrowsingProtocolManager> Create( static std::unique_ptr<SafeBrowsingProtocolManager> Create(
SafeBrowsingProtocolManagerDelegate* delegate, SafeBrowsingProtocolManagerDelegate* delegate,
net::URLRequestContextGetter* request_context_getter, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const SafeBrowsingProtocolConfig& config); const SafeBrowsingProtocolConfig& config);
// Sets up the update schedule and internal state for making periodic requests // Sets up the update schedule and internal state for making periodic requests
// of the Safebrowsing servers. // of the Safebrowsing servers.
virtual void Initialize(); virtual void Initialize();
// net::URLFetcherDelegate interface. // Callback when the request completes
void OnURLFetchComplete(const net::URLFetcher* source) override; void OnURLLoaderComplete(network::SimpleURLLoader* url_loader,
std::unique_ptr<std::string> response_body);
// To ease testing.
void OnURLLoaderCompleteInternal(network::SimpleURLLoader* url_loader,
int net_error,
int response_code,
const std::string& data);
// Retrieve the full hash for a set of prefixes, and invoke the callback // Retrieve the full hash for a set of prefixes, and invoke the callback
// argument when the results are retrieved. The callback may be invoked // argument when the results are retrieved. The callback may be invoked
...@@ -164,11 +169,9 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -164,11 +169,9 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// Record HTTP response code when there's no error in fetching an HTTP // Record HTTP response code when there's no error in fetching an HTTP
// request, and the error code, when there is. // request, and the error code, when there is.
// |metric_name| is the name of the UMA metric to record the response code or // |metric_name| is the name of the UMA metric to record the response code or
// error code against, |status| represents the status of the HTTP request, and // error code against, |net_error| represents the status of the HTTP request,
// |response code| represents the HTTP response code received from the server. // and |response code| represents the HTTP response code received from the
static void RecordHttpResponseOrErrorCode( // server.
const char* metric_name, const net::URLRequestStatus& status,
int response_code);
static void RecordHttpResponseOrErrorCode(const char* metric_name, static void RecordHttpResponseOrErrorCode(const char* metric_name,
int net_error, int net_error,
int response_code); int response_code);
...@@ -180,10 +183,10 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -180,10 +183,10 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
protected: protected:
// Constructs a SafeBrowsingProtocolManager for |delegate| that issues // Constructs a SafeBrowsingProtocolManager for |delegate| that issues
// network requests using |request_context_getter|. // network requests using |url_loader_factory|.
SafeBrowsingProtocolManager( SafeBrowsingProtocolManager(
SafeBrowsingProtocolManagerDelegate* delegate, SafeBrowsingProtocolManagerDelegate* delegate,
net::URLRequestContextGetter* request_context_getter, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const SafeBrowsingProtocolConfig& config); const SafeBrowsingProtocolConfig& config);
private: private:
...@@ -267,7 +270,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -267,7 +270,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// Runs the protocol parser on received data and update the // Runs the protocol parser on received data and update the
// SafeBrowsingService with the new content. Returns 'true' on successful // SafeBrowsingService with the new content. Returns 'true' on successful
// parse, 'false' on error. // parse, 'false' on error.
bool HandleServiceResponse(const GURL& url, const char* data, size_t length); bool HandleServiceResponse(const char* data, size_t length);
// Updates internal state for each GetHash response error, assuming that the // Updates internal state for each GetHash response error, assuming that the
// current time is |now|. // current time is |now|.
...@@ -305,7 +308,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -305,7 +308,7 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// Current active request (in case we need to cancel) for updates or chunks // Current active request (in case we need to cancel) for updates or chunks
// from the SafeBrowsing service. We can only have one of these outstanding // from the SafeBrowsing service. We can only have one of these outstanding
// at any given time unlike GetHash requests, which are tracked separately. // at any given time unlike GetHash requests, which are tracked separately.
std::unique_ptr<net::URLFetcher> request_; std::unique_ptr<network::SimpleURLLoader> request_;
// The kind of request that is currently in progress. // The kind of request that is currently in progress.
SafeBrowsingRequestType request_type_; SafeBrowsingRequestType request_type_;
...@@ -337,8 +340,9 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -337,8 +340,9 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// All chunk requests that need to be made. // All chunk requests that need to be made.
base::circular_deque<ChunkUrl> chunk_request_urls_; base::circular_deque<ChunkUrl> chunk_request_urls_;
std::map<const net::URLFetcher*, std::map<
std::pair<std::unique_ptr<net::URLFetcher>, FullHashDetails>> const network::SimpleURLLoader*,
std::pair<std::unique_ptr<network::SimpleURLLoader>, FullHashDetails>>
hash_requests_; hash_requests_;
// True if the service has been given an add/sub chunk but it hasn't been // True if the service has been given an add/sub chunk but it hasn't been
...@@ -367,8 +371,8 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -367,8 +371,8 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// safebrowsing hits and chunk update requests. // safebrowsing hits and chunk update requests.
std::string additional_query_; std::string additional_query_;
// The context we use to issue network requests. // The URLLoaderFactory we use to issue network requests.
scoped_refptr<net::URLRequestContextGetter> request_context_getter_; scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// URL prefix where browser fetches safebrowsing chunk updates, and hashes. // URL prefix where browser fetches safebrowsing chunk updates, and hashes.
std::string url_prefix_; std::string url_prefix_;
...@@ -386,9 +390,6 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate { ...@@ -386,9 +390,6 @@ class SafeBrowsingProtocolManager : public net::URLFetcherDelegate {
// ForceScheduleNextUpdate() is called. This is set for testing purpose. // ForceScheduleNextUpdate() is called. This is set for testing purpose.
bool disable_auto_update_; bool disable_auto_update_;
// ID for URLFetchers for testing.
int url_fetcher_id_;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager); DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager);
}; };
...@@ -400,7 +401,7 @@ class SBProtocolManagerFactory { ...@@ -400,7 +401,7 @@ class SBProtocolManagerFactory {
virtual std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( virtual std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager(
SafeBrowsingProtocolManagerDelegate* delegate, SafeBrowsingProtocolManagerDelegate* delegate,
net::URLRequestContextGetter* request_context_getter, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const SafeBrowsingProtocolConfig& config) = 0; const SafeBrowsingProtocolConfig& config) = 0;
private: private:
......
...@@ -446,7 +446,7 @@ void SafeBrowsingService::StartOnIOThread( ...@@ -446,7 +446,7 @@ void SafeBrowsingService::StartOnIOThread(
GetProtocolManagerDelegate(); GetProtocolManagerDelegate();
if (protocol_manager_delegate) { if (protocol_manager_delegate) {
protocol_manager_ = SafeBrowsingProtocolManager::Create( protocol_manager_ = SafeBrowsingProtocolManager::Create(
protocol_manager_delegate, url_request_context_getter, config); protocol_manager_delegate, GetURLLoaderFactoryOnIOThread(), config);
protocol_manager_->Initialize(); protocol_manager_->Initialize();
} }
} }
......
...@@ -576,10 +576,11 @@ class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { ...@@ -576,10 +576,11 @@ class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory {
// safebrowsing server for testing purpose. // safebrowsing server for testing purpose.
class TestProtocolManager : public SafeBrowsingProtocolManager { class TestProtocolManager : public SafeBrowsingProtocolManager {
public: public:
TestProtocolManager(SafeBrowsingProtocolManagerDelegate* delegate, TestProtocolManager(
net::URLRequestContextGetter* request_context_getter, SafeBrowsingProtocolManagerDelegate* delegate,
const SafeBrowsingProtocolConfig& config) scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
: SafeBrowsingProtocolManager(delegate, request_context_getter, config) { const SafeBrowsingProtocolConfig& config)
: SafeBrowsingProtocolManager(delegate, url_loader_factory, config) {
create_count_++; create_count_++;
} }
...@@ -635,11 +636,11 @@ class TestSBProtocolManagerFactory : public SBProtocolManagerFactory { ...@@ -635,11 +636,11 @@ class TestSBProtocolManagerFactory : public SBProtocolManagerFactory {
std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( std::unique_ptr<SafeBrowsingProtocolManager> CreateProtocolManager(
SafeBrowsingProtocolManagerDelegate* delegate, SafeBrowsingProtocolManagerDelegate* delegate,
net::URLRequestContextGetter* request_context_getter, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const SafeBrowsingProtocolConfig& config) override { const SafeBrowsingProtocolConfig& config) override {
base::AutoLock locker(lock_); base::AutoLock locker(lock_);
pm_ = new TestProtocolManager(delegate, request_context_getter, config); pm_ = new TestProtocolManager(delegate, url_loader_factory, config);
if (!quit_closure_.is_null()) { if (!quit_closure_.is_null()) {
quit_closure_.Run(); quit_closure_.Run();
......
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