Commit fabcfccf authored by Ali Juma's avatar Ali Juma Committed by Commit Bot

[iOS] Create a FakeSafeBrowsingService

This changes SafeBrowsingService into a pure virtual interface, moves the
implementation to a new SafeBrowsingServiceImpl, and creates a new
FakeSafeBrowsingService implementation. This fake implementation creates
FakeSafeBrowsingUrlCheckerImpls that treat all queried URLs as safe.

This CL uses FakeSafeBrowsingService in TestingApplicationContext, which
lets us simplify SafeBrowsingTabHelper, since it no longer needs to handle
the case where the current ApplicationContext returns a null
SafeBrowsingService.

Change-Id: I515e0e53a28b4f9173fcae892dd993e159dbc7b5
Bug: 1060300
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2106804
Commit-Queue: Ali Juma <ajuma@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarKurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#762532}
parent 23b42783
......@@ -106,9 +106,9 @@ class SafeBrowsingUrlCheckerImpl : public mojom::SafeBrowsingUrlChecker,
// NOTE: |callback| could be run synchronously before this method returns. Be
// careful if |callback| could destroy this object.
void CheckUrl(const GURL& url,
const std::string& method,
NativeCheckUrlCallback callback);
virtual void CheckUrl(const GURL& url,
const std::string& method,
NativeCheckUrlCallback callback);
private:
class Notifier {
......
......@@ -59,7 +59,7 @@
#include "ios/chrome/browser/pref_names.h"
#include "ios/chrome/browser/prefs/browser_prefs.h"
#include "ios/chrome/browser/prefs/ios_chrome_pref_service_factory.h"
#include "ios/chrome/browser/safe_browsing/safe_browsing_service.h"
#include "ios/chrome/browser/safe_browsing/safe_browsing_service_impl.h"
#include "ios/chrome/browser/update_client/ios_chrome_update_query_params_delegate.h"
#include "ios/chrome/common/channel_info.h"
#include "ios/web/public/thread/web_task_traits.h"
......@@ -393,7 +393,7 @@ SafeBrowsingService* ApplicationContextImpl::GetSafeBrowsingService() {
if (base::FeatureList::IsEnabled(
safe_browsing::kSafeBrowsingAvailableOnIOS) &&
!safe_browsing_service_) {
safe_browsing_service_ = base::MakeRefCounted<SafeBrowsingService>();
safe_browsing_service_ = base::MakeRefCounted<SafeBrowsingServiceImpl>();
}
return safe_browsing_service_.get();
}
......
......@@ -8,7 +8,8 @@ import("//ios/features.gni")
source_set("safe_browsing") {
sources = [
"safe_browsing_service.h",
"safe_browsing_service.mm",
"safe_browsing_service_impl.h",
"safe_browsing_service_impl.mm",
"safe_browsing_tab_helper.h",
"safe_browsing_tab_helper.mm",
"url_checker_delegate_impl.h",
......@@ -41,6 +42,24 @@ source_set("safe_browsing") {
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("test_support") {
testonly = true
sources = [
"fake_safe_browsing_service.h",
"fake_safe_browsing_service.mm",
]
deps = [
":safe_browsing",
"//components/safe_browsing/core/browser",
"//components/safe_browsing/core/db:test_database_manager",
"//ios/web/public",
"//services/network/public/cpp",
]
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("unit_tests") {
testonly = true
sources = [ "safe_browsing_service_unittest.mm" ]
......
// Copyright 2020 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 IOS_CHROME_BROWSER_SAFE_BROWSING_FAKE_SAFE_BROWSING_SERVICE_H_
#define IOS_CHROME_BROWSER_SAFE_BROWSING_FAKE_SAFE_BROWSING_SERVICE_H_
#include "ios/chrome/browser/safe_browsing/safe_browsing_service.h"
// A fake SafeBrowsingService whose database treats all URLs as safe.
class FakeSafeBrowsingService : public SafeBrowsingService {
public:
FakeSafeBrowsingService();
FakeSafeBrowsingService(const FakeSafeBrowsingService&) = delete;
FakeSafeBrowsingService& operator=(const FakeSafeBrowsingService&) = delete;
// SafeBrowsingService:
void Initialize(PrefService* prefs,
const base::FilePath& user_data_path) override;
void ShutDown() override;
std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl> CreateUrlChecker(
safe_browsing::ResourceType resource_type,
web::WebState* web_state) override;
protected:
~FakeSafeBrowsingService() override;
};
#endif // IOS_CHROME_BROWSER_SAFE_BROWSING_FAKE_SAFE_BROWSING_SERVICE_H_
// Copyright 2020 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 "ios/chrome/browser/safe_browsing/fake_safe_browsing_service.h"
#include "base/bind_helpers.h"
#include "components/safe_browsing/core/browser/safe_browsing_url_checker_impl.h"
#include "components/safe_browsing/core/db/test_database_manager.h"
#import "ios/chrome/browser/safe_browsing/url_checker_delegate_impl.h"
#include "ios/web/public/thread/web_thread.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// A SafeBrowsingUrlCheckerImpl that treats all URLs as safe.
class FakeSafeBrowsingUrlCheckerImpl
: public safe_browsing::SafeBrowsingUrlCheckerImpl {
public:
explicit FakeSafeBrowsingUrlCheckerImpl(
safe_browsing::ResourceType resource_type)
: SafeBrowsingUrlCheckerImpl(
resource_type,
base::MakeRefCounted<UrlCheckerDelegateImpl>(
/*database_manager=*/nullptr),
base::Bind([]() { return static_cast<web::WebState*>(nullptr); })) {
}
~FakeSafeBrowsingUrlCheckerImpl() override = default;
// SafeBrowsingUrlCheckerImpl:
void CheckUrl(
const GURL& url,
const std::string& method,
safe_browsing::SafeBrowsingUrlCheckerImpl::NativeCheckUrlCallback
callback) override {
std::move(callback).Run(/*slow_check_notifier=*/nullptr, /*proceed=*/true,
/*showed_interstitial=*/false);
}
};
}
FakeSafeBrowsingService::FakeSafeBrowsingService() = default;
FakeSafeBrowsingService::~FakeSafeBrowsingService() = default;
void FakeSafeBrowsingService::Initialize(PrefService* prefs,
const base::FilePath& user_data_path) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
}
void FakeSafeBrowsingService::ShutDown() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
}
std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl>
FakeSafeBrowsingService::CreateUrlChecker(
safe_browsing::ResourceType resource_type,
web::WebState* web_state) {
return std::make_unique<FakeSafeBrowsingUrlCheckerImpl>(resource_type);
}
......@@ -6,33 +6,16 @@
#define IOS_CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
#include "base/memory/ref_counted.h"
#include "ios/web/public/thread/web_thread.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/network_context.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
class PrefChangeRegistrar;
class PrefService;
namespace base {
class FilePath;
}
namespace net {
class URLRequestContext;
} // namespace net
namespace network {
class SharedURLLoaderFactory;
class WeakWrapperSharedURLLoaderFactory;
} // namespace network
namespace safe_browsing {
enum class ResourceType;
class SafeBrowsingDatabaseManager;
class SafeBrowsingUrlCheckerImpl;
class UrlCheckerDelegate;
} // namespace safe_browsing
namespace web {
......@@ -40,124 +23,35 @@ class WebState;
} // namespace web
// Manages Safe Browsing related functionality. This class owns and provides
// support for constructing and initializing the Safe Browsing database. This
// class must be created and destroyed on the main thread. This class is
// RefCounted so that PostTask'd calls into this class can retain a reference
// to an instance.
// support for constructing and initializing the Safe Browsing database.
// This class is RefCounted so that PostTask'd calls into this class can retain
// a reference to an instance.
class SafeBrowsingService
: public base::RefCountedThreadSafe<SafeBrowsingService,
web::WebThread::DeleteOnUIThread> {
: public base::RefCountedThreadSafe<SafeBrowsingService> {
public:
SafeBrowsingService();
SafeBrowsingService(const SafeBrowsingService&) = delete;
SafeBrowsingService& operator=(const SafeBrowsingService&) = delete;
// Called on the UI thread to initialize the service.
void Initialize(PrefService* prefs, const base::FilePath& user_data_path);
virtual void Initialize(PrefService* prefs,
const base::FilePath& user_data_path) = 0;
// Called on the UI thread to terminate the service. This must be called
// before the IO thread is torn down.
void ShutDown();
virtual void ShutDown() = 0;
// Creates a SafeBrowsingUrlCheckerImpl that can be used to query the
// SafeBrowsingDatabaseManager owned by this service.
std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl> CreateUrlChecker(
safe_browsing::ResourceType resource_type,
web::WebState* web_state);
private:
friend struct web::WebThread::DeleteOnThread<web::WebThread::UI>;
friend class base::DeleteHelper<SafeBrowsingService>;
// A helper class for enabling/disabling Safe Browsing and maintaining state
// on the IO thread. This class may be constructed and destroyed on the UI
// thread, but all of its other methods should only be called on the IO
// thread.
class IOThreadEnabler : public base::RefCountedThreadSafe<IOThreadEnabler> {
public:
IOThreadEnabler(scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
database_manager);
// Creates the network context and URL loader factory used by the
// SafeBrowsingDatabaseManager.
void Initialize(scoped_refptr<SafeBrowsingService> safe_browsing_service,
mojo::PendingReceiver<network::mojom::NetworkContext>
network_context_receiver);
// Disables Safe Browsing, and destroys the network context and URL loader
// factory used by the SafeBrowsingDatabaseManager.
void ShutDown();
// Enables or disables Safe Browsing database updates and lookups.
void SetSafeBrowsingEnabled(bool enabled);
private:
friend base::RefCountedThreadSafe<IOThreadEnabler>;
~IOThreadEnabler();
// Starts the SafeBrowsingDatabaseManager, making it ready to accept
// queries.
void StartSafeBrowsingDBManager();
// Constructs a URLRequestContext.
void SetUpURLRequestContext();
// Constructs a SharedURLLoaderFactory.
void SetUpURLLoaderFactory(
scoped_refptr<SafeBrowsingService> safe_browsing_service);
// This tracks whether the service is running.
bool enabled_ = false;
// This tracks whether ShutDown() has been called.
bool shutting_down_ = false;
virtual std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl>
CreateUrlChecker(safe_browsing::ResourceType resource_type,
web::WebState* web_state) = 0;
// This is wrapped by |network_context|.
std::unique_ptr<net::URLRequestContext> url_request_context_;
protected:
SafeBrowsingService() = default;
virtual ~SafeBrowsingService() = default;
// The network context used for Safe Browsing related network requests.
std::unique_ptr<network::NetworkContext> network_context_;
// An IO thread remote for a URLLoaderFactory created on the UI thread.
mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_;
// A SharedURLLoaderFactory that wraps |url_loader_factory_|.
scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
shared_url_loader_factory_;
// The database manager used for Safe Browsing queries.
scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
DISALLOW_COPY_AND_ASSIGN(IOThreadEnabler);
};
~SafeBrowsingService();
// Called on the UI thread to construct a URLLoaderFactory that is used on
// the IO thread.
void SetUpURLLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver);
// Enables or disables Safe Browsing, depending on the current state of
// preferences.
void UpdateSafeBrowsingEnabledState();
// This is the UI thread remote for IOThreadState's network context.
mojo::Remote<network::mojom::NetworkContext> network_context_client_;
// Constructed on the UI thread, but otherwise its methods are only called on
// the IO thread.
scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
// Delegate for SafeBrowsingUrlCheckerImpl instances.
scoped_refptr<safe_browsing::UrlCheckerDelegate> url_checker_delegate_;
// This watches for changes to the Safe Browsing opt-out preference.
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
// Encapsulates methods and objects that are used on the IO thread.
scoped_refptr<IOThreadEnabler> io_thread_enabler_;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
private:
friend class base::RefCountedThreadSafe<SafeBrowsingService>;
};
#endif // IOS_CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
// Copyright 2020 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 IOS_CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_IMPL_H_
#define IOS_CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_IMPL_H_
#import "ios/chrome/browser/safe_browsing/safe_browsing_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/network_context.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
class PrefChangeRegistrar;
namespace net {
class URLRequestContext;
} // namespace net
namespace network {
class SharedURLLoaderFactory;
class WeakWrapperSharedURLLoaderFactory;
} // namespace network
namespace safe_browsing {
class SafeBrowsingDatabaseManager;
class UrlCheckerDelegate;
} // namespace safe_browsing
// This class must be created on the UI thread.
class SafeBrowsingServiceImpl : public SafeBrowsingService {
public:
SafeBrowsingServiceImpl();
SafeBrowsingServiceImpl(const SafeBrowsingServiceImpl&) = delete;
SafeBrowsingServiceImpl& operator=(const SafeBrowsingServiceImpl&) = delete;
// SafeBrowsingService:
void Initialize(PrefService* prefs,
const base::FilePath& user_data_path) override;
void ShutDown() override;
std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl> CreateUrlChecker(
safe_browsing::ResourceType resource_type,
web::WebState* web_state) override;
private:
// A helper class for enabling/disabling Safe Browsing and maintaining state
// on the IO thread. This class may be constructed and destroyed on the UI
// thread, but all of its other methods should only be called on the IO
// thread.
class IOThreadEnabler : public base::RefCountedThreadSafe<IOThreadEnabler> {
public:
IOThreadEnabler(scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
database_manager);
IOThreadEnabler(const IOThreadEnabler&) = delete;
IOThreadEnabler& operator=(const IOThreadEnabler&) = delete;
// Creates the network context and URL loader factory used by the
// SafeBrowsingDatabaseManager.
void Initialize(
scoped_refptr<SafeBrowsingServiceImpl> safe_browsing_service,
mojo::PendingReceiver<network::mojom::NetworkContext>
network_context_receiver);
// Disables Safe Browsing, and destroys the network context and URL loader
// factory used by the SafeBrowsingDatabaseManager.
void ShutDown();
// Enables or disables Safe Browsing database updates and lookups.
void SetSafeBrowsingEnabled(bool enabled);
private:
friend base::RefCountedThreadSafe<IOThreadEnabler>;
~IOThreadEnabler();
// Starts the SafeBrowsingDatabaseManager, making it ready to accept
// queries.
void StartSafeBrowsingDBManager();
// Constructs a URLRequestContext.
void SetUpURLRequestContext();
// Constructs a SharedURLLoaderFactory.
void SetUpURLLoaderFactory(
scoped_refptr<SafeBrowsingServiceImpl> safe_browsing_service);
// This tracks whether the service is running.
bool enabled_ = false;
// This tracks whether ShutDown() has been called.
bool shutting_down_ = false;
// This is wrapped by |network_context|.
std::unique_ptr<net::URLRequestContext> url_request_context_;
// The network context used for Safe Browsing related network requests.
std::unique_ptr<network::NetworkContext> network_context_;
// An IO thread remote for a URLLoaderFactory created on the UI thread.
mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_;
// A SharedURLLoaderFactory that wraps |url_loader_factory_|.
scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
shared_url_loader_factory_;
// The database manager used for Safe Browsing queries.
scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
};
~SafeBrowsingServiceImpl() override;
// Called on the UI thread to construct a URLLoaderFactory that is used on
// the IO thread.
void SetUpURLLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver);
// Enables or disables Safe Browsing, depending on the current state of
// preferences.
void UpdateSafeBrowsingEnabledState();
// This is the UI thread remote for IOThreadState's network context.
mojo::Remote<network::mojom::NetworkContext> network_context_client_;
// Constructed on the UI thread, but otherwise its methods are only called on
// the IO thread.
scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
// Delegate for SafeBrowsingUrlCheckerImpl instances.
scoped_refptr<safe_browsing::UrlCheckerDelegate> url_checker_delegate_;
// This watches for changes to the Safe Browsing opt-out preference.
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
// Encapsulates methods and objects that are used on the IO thread.
scoped_refptr<IOThreadEnabler> io_thread_enabler_;
};
#endif // IOS_CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_IMPL_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/safe_browsing/safe_browsing_service.h"
#include "ios/chrome/browser/safe_browsing/safe_browsing_service_impl.h"
#include "base/bind.h"
#include "base/files/file_path.h"
......@@ -17,6 +17,7 @@
#include "components/safe_browsing/core/db/v4_local_database_manager.h"
#import "ios/chrome/browser/safe_browsing/url_checker_delegate_impl.h"
#include "ios/web/public/thread/web_task_traits.h"
#include "ios/web/public/thread/web_thread.h"
#import "ios/web/public/web_state.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
......@@ -26,14 +27,14 @@
#error "This file requires ARC support."
#endif
#pragma mark - SafeBrowsingService
#pragma mark - SafeBrowsingServiceImpl
SafeBrowsingService::SafeBrowsingService() = default;
SafeBrowsingServiceImpl::SafeBrowsingServiceImpl() = default;
SafeBrowsingService::~SafeBrowsingService() = default;
SafeBrowsingServiceImpl::~SafeBrowsingServiceImpl() = default;
void SafeBrowsingService::Initialize(PrefService* prefs,
const base::FilePath& user_data_path) {
void SafeBrowsingServiceImpl::Initialize(PrefService* prefs,
const base::FilePath& user_data_path) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
if (io_thread_enabler_) {
......@@ -50,13 +51,12 @@ void SafeBrowsingService::Initialize(PrefService* prefs,
base::MakeRefCounted<UrlCheckerDelegateImpl>(safe_browsing_db_manager_);
io_thread_enabler_ =
base::MakeRefCounted<SafeBrowsingService::IOThreadEnabler>(
safe_browsing_db_manager_);
base::MakeRefCounted<IOThreadEnabler>(safe_browsing_db_manager_);
base::PostTask(
FROM_HERE, {web::WebThread::IO},
base::BindOnce(&SafeBrowsingService::IOThreadEnabler::Initialize,
io_thread_enabler_, base::WrapRefCounted(this),
base::BindOnce(&IOThreadEnabler::Initialize, io_thread_enabler_,
base::WrapRefCounted(this),
network_context_client_.BindNewPipeAndPassReceiver()));
// Watch for changes to the Safe Browsing opt-out preference.
......@@ -64,29 +64,30 @@ void SafeBrowsingService::Initialize(PrefService* prefs,
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(
prefs::kSafeBrowsingEnabled,
base::Bind(&SafeBrowsingService::UpdateSafeBrowsingEnabledState,
base::Bind(&SafeBrowsingServiceImpl::UpdateSafeBrowsingEnabledState,
base::Unretained(this)));
UpdateSafeBrowsingEnabledState();
}
void SafeBrowsingService::ShutDown() {
void SafeBrowsingServiceImpl::ShutDown() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
pref_change_registrar_.reset();
base::PostTask(FROM_HERE, {web::WebThread::IO},
base::BindOnce(&SafeBrowsingService::IOThreadEnabler::ShutDown,
io_thread_enabler_));
base::PostTask(
FROM_HERE, {web::WebThread::IO},
base::BindOnce(&IOThreadEnabler::ShutDown, io_thread_enabler_));
network_context_client_.reset();
}
std::unique_ptr<safe_browsing::SafeBrowsingUrlCheckerImpl>
SafeBrowsingService::CreateUrlChecker(safe_browsing::ResourceType resource_type,
web::WebState* web_state) {
SafeBrowsingServiceImpl::CreateUrlChecker(
safe_browsing::ResourceType resource_type,
web::WebState* web_state) {
return std::make_unique<safe_browsing::SafeBrowsingUrlCheckerImpl>(
resource_type, url_checker_delegate_, web_state->CreateDefaultGetter());
}
void SafeBrowsingService::SetUpURLLoaderFactory(
void SafeBrowsingServiceImpl::SetUpURLLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver) {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
auto url_loader_factory_params =
......@@ -97,26 +98,24 @@ void SafeBrowsingService::SetUpURLLoaderFactory(
std::move(receiver), std::move(url_loader_factory_params));
}
void SafeBrowsingService::UpdateSafeBrowsingEnabledState() {
void SafeBrowsingServiceImpl::UpdateSafeBrowsingEnabledState() {
bool enabled =
pref_change_registrar_->prefs()->GetBoolean(prefs::kSafeBrowsingEnabled);
base::PostTask(
FROM_HERE, {web::WebThread::IO},
base::BindOnce(
&SafeBrowsingService::IOThreadEnabler::SetSafeBrowsingEnabled,
io_thread_enabler_, enabled));
base::PostTask(FROM_HERE, {web::WebThread::IO},
base::BindOnce(&IOThreadEnabler::SetSafeBrowsingEnabled,
io_thread_enabler_, enabled));
}
#pragma mark - SafeBrowsingService::IOThreadEnabler
#pragma mark - SafeBrowsingServiceImpl::IOThreadEnabler
SafeBrowsingService::IOThreadEnabler::IOThreadEnabler(
SafeBrowsingServiceImpl::IOThreadEnabler::IOThreadEnabler(
scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager)
: safe_browsing_db_manager_(database_manager) {}
SafeBrowsingService::IOThreadEnabler::~IOThreadEnabler() = default;
SafeBrowsingServiceImpl::IOThreadEnabler::~IOThreadEnabler() = default;
void SafeBrowsingService::IOThreadEnabler::Initialize(
scoped_refptr<SafeBrowsingService> safe_browsing_service,
void SafeBrowsingServiceImpl::IOThreadEnabler::Initialize(
scoped_refptr<SafeBrowsingServiceImpl> safe_browsing_service,
mojo::PendingReceiver<network::mojom::NetworkContext>
network_context_receiver) {
SetUpURLRequestContext();
......@@ -127,7 +126,7 @@ void SafeBrowsingService::IOThreadEnabler::Initialize(
SetUpURLLoaderFactory(safe_browsing_service);
}
void SafeBrowsingService::IOThreadEnabler::ShutDown() {
void SafeBrowsingServiceImpl::IOThreadEnabler::ShutDown() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
shutting_down_ = true;
SetSafeBrowsingEnabled(false);
......@@ -137,7 +136,7 @@ void SafeBrowsingService::IOThreadEnabler::ShutDown() {
url_request_context_.reset();
}
void SafeBrowsingService::IOThreadEnabler::SetSafeBrowsingEnabled(
void SafeBrowsingServiceImpl::IOThreadEnabler::SetSafeBrowsingEnabled(
bool enabled) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
if (enabled_ == enabled)
......@@ -150,7 +149,7 @@ void SafeBrowsingService::IOThreadEnabler::SetSafeBrowsingEnabled(
safe_browsing_db_manager_->StopOnIOThread(shutting_down_);
}
void SafeBrowsingService::IOThreadEnabler::StartSafeBrowsingDBManager() {
void SafeBrowsingServiceImpl::IOThreadEnabler::StartSafeBrowsingDBManager() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
std::string client_name;
......@@ -167,7 +166,7 @@ void SafeBrowsingService::IOThreadEnabler::StartSafeBrowsingDBManager() {
config);
}
void SafeBrowsingService::IOThreadEnabler::SetUpURLRequestContext() {
void SafeBrowsingServiceImpl::IOThreadEnabler::SetUpURLRequestContext() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// This uses an in-memory non-persistent cookie store. The Safe Browsing V4
......@@ -176,12 +175,12 @@ void SafeBrowsingService::IOThreadEnabler::SetUpURLRequestContext() {
url_request_context_ = builder.Build();
}
void SafeBrowsingService::IOThreadEnabler::SetUpURLLoaderFactory(
scoped_refptr<SafeBrowsingService> safe_browsing_service) {
void SafeBrowsingServiceImpl::IOThreadEnabler::SetUpURLLoaderFactory(
scoped_refptr<SafeBrowsingServiceImpl> safe_browsing_service) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
base::PostTask(
FROM_HERE, {web::WebThread::UI},
base::BindOnce(&SafeBrowsingService::SetUpURLLoaderFactory,
base::BindOnce(&SafeBrowsingServiceImpl::SetUpURLLoaderFactory,
safe_browsing_service,
url_loader_factory_.BindNewPipeAndPassReceiver()));
shared_url_loader_factory_ =
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/safe_browsing/safe_browsing_service.h"
#include "ios/chrome/browser/safe_browsing/safe_browsing_service_impl.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
......@@ -117,7 +117,7 @@ class SafeBrowsingServiceTest : public PlatformTest {
safe_browsing::V4GetHashProtocolManager::RegisterFactory(
base::WrapUnique(v4_get_hash_factory_));
safe_browsing_service_ = base::MakeRefCounted<SafeBrowsingService>();
safe_browsing_service_ = base::MakeRefCounted<SafeBrowsingServiceImpl>();
CHECK(temp_dir_.CreateUniqueTempDir());
safe_browsing_service_->Initialize(&local_state_, temp_dir_.GetPath());
......
......@@ -101,7 +101,7 @@ class SafeBrowsingTabHelper
explicit SafeBrowsingTabHelper(web::WebState* web_state);
std::unique_ptr<UrlCheckerClient> url_checker_client_;
std::unique_ptr<PolicyDecider> policy_decider_;
PolicyDecider policy_decider_;
WEB_STATE_USER_DATA_KEY_DECL();
};
......
......@@ -22,28 +22,16 @@
#pragma mark - SafeBrowsingTabHelper
SafeBrowsingTabHelper::SafeBrowsingTabHelper(web::WebState* web_state) {
SafeBrowsingTabHelper::SafeBrowsingTabHelper(web::WebState* web_state)
: url_checker_client_(std::make_unique<UrlCheckerClient>()),
policy_decider_(web_state, url_checker_client_.get()) {
DCHECK(
base::FeatureList::IsEnabled(safe_browsing::kSafeBrowsingAvailableOnIOS));
// Unit tests that use a TestingApplicationContext don't have a
// SafeBrowsingService.
// TODO(crbug.com/1060300): Create a FakeSafeBrowsingService and use it in
// TestingApplicationContext, so that a special case for tests isn't needed
// here.
if (!GetApplicationContext()->GetSafeBrowsingService())
return;
url_checker_client_ = std::make_unique<UrlCheckerClient>();
policy_decider_ =
std::make_unique<PolicyDecider>(web_state, url_checker_client_.get());
}
SafeBrowsingTabHelper::~SafeBrowsingTabHelper() {
if (url_checker_client_) {
base::DeleteSoon(FROM_HERE, {web::WebThread::IO},
url_checker_client_.release());
}
base::DeleteSoon(FROM_HERE, {web::WebThread::IO},
url_checker_client_.release());
}
WEB_STATE_USER_DATA_KEY_IMPL(SafeBrowsingTabHelper)
......@@ -107,9 +95,6 @@ web::WebStatePolicyDecider::PolicyDecision
SafeBrowsingTabHelper::PolicyDecider::ShouldAllowRequest(
NSURLRequest* request,
const web::WebStatePolicyDecider::RequestInfo& request_info) {
if (!url_checker_client_)
return web::WebStatePolicyDecider::PolicyDecision::Allow();
SafeBrowsingService* safe_browsing_service =
GetApplicationContext()->GetSafeBrowsingService();
......
......@@ -48,11 +48,13 @@ source_set("test_support") {
"//components/network_time",
"//components/prefs",
"//components/prefs:test_support",
"//components/safe_browsing/core:features",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/browser_state:browser_state_impl",
"//ios/chrome/browser/content_settings",
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/safe_browsing:test_support",
"//ios/components/webui:url_constants",
"//ios/public/provider/chrome/browser",
"//ios/public/provider/chrome/browser:test_support",
......
......@@ -10,6 +10,7 @@ include_rules = [
"+components/metrics",
"+components/network_time",
"+components/prefs",
"+components/safe_browsing",
"+components/signin/public/base",
"+components/sync",
"+components/sync_device_info",
......
......@@ -72,6 +72,7 @@ class TestingApplicationContext : public ApplicationContext {
std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
bool was_last_shutdown_clean_;
std::unique_ptr<network::TestURLLoaderFactory> test_url_loader_factory_;
scoped_refptr<SafeBrowsingService> fake_safe_browsing_service_;
std::unique_ptr<network::TestNetworkConnectionTracker>
test_network_connection_tracker_;
DISALLOW_COPY_AND_ASSIGN(TestingApplicationContext);
......
......@@ -4,11 +4,14 @@
#include "ios/chrome/test/testing_application_context.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "components/network_time/network_time_tracker.h"
#include "components/safe_browsing/core/features.h"
#import "ios/chrome/browser/safe_browsing/fake_safe_browsing_service.h"
#import "ios/public/provider/chrome/browser/chrome_browser_provider.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
......@@ -186,7 +189,13 @@ TestingApplicationContext::GetComponentUpdateService() {
SafeBrowsingService* TestingApplicationContext::GetSafeBrowsingService() {
DCHECK(thread_checker_.CalledOnValidThread());
return nullptr;
DCHECK(
base::FeatureList::IsEnabled(safe_browsing::kSafeBrowsingAvailableOnIOS));
if (!fake_safe_browsing_service_) {
fake_safe_browsing_service_ =
base::MakeRefCounted<FakeSafeBrowsingService>();
}
return fake_safe_browsing_service_.get();
}
network::NetworkConnectionTracker*
......
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