safe_browsing_service.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
  5. #define WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
  6. #include "components/safe_browsing/content/browser/base_ui_manager.h"
  7. #include "components/safe_browsing/content/browser/ui_manager.h"
  8. #include "mojo/public/cpp/bindings/pending_receiver.h"
  9. #include "mojo/public/cpp/bindings/remote.h"
  10. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  11. #include "services/service_manager/public/cpp/binder_registry.h"
  12. namespace content {
  13. class NavigationHandle;
  14. class NavigationThrottle;
  15. class RenderProcessHost;
  16. } // namespace content
  17. namespace blink {
  18. class URLLoaderThrottle;
  19. }
  20. namespace network {
  21. namespace mojom {
  22. class NetworkContext;
  23. }
  24. class SharedURLLoaderFactory;
  25. } // namespace network
  26. namespace safe_browsing {
  27. class UrlCheckerDelegate;
  28. class RealTimeUrlLookupServiceBase;
  29. class RemoteSafeBrowsingDatabaseManager;
  30. class SafeBrowsingApiHandlerBridge;
  31. class SafeBrowsingNetworkContext;
  32. class TriggerManager;
  33. } // namespace safe_browsing
  34. namespace weblayer {
  35. class UrlCheckerDelegateImpl;
  36. // Class for managing safebrowsing related functionality. In particular this
  37. // class owns both the safebrowsing database and UI managers and provides
  38. // support for initialization and construction of these objects.
  39. class SafeBrowsingService {
  40. public:
  41. explicit SafeBrowsingService(const std::string& user_agent);
  42. SafeBrowsingService(const SafeBrowsingService&) = delete;
  43. SafeBrowsingService& operator=(const SafeBrowsingService&) = delete;
  44. ~SafeBrowsingService();
  45. // Executed on UI thread
  46. void Initialize();
  47. std::unique_ptr<blink::URLLoaderThrottle> CreateURLLoaderThrottle(
  48. const base::RepeatingCallback<content::WebContents*()>& wc_getter,
  49. int frame_tree_node_id,
  50. safe_browsing::RealTimeUrlLookupServiceBase* url_lookup_service);
  51. std::unique_ptr<content::NavigationThrottle>
  52. MaybeCreateSafeBrowsingNavigationThrottleFor(
  53. content::NavigationHandle* handle);
  54. void AddInterface(service_manager::BinderRegistry* registry,
  55. content::RenderProcessHost* render_process_host);
  56. void StopDBManager();
  57. network::mojom::NetworkContext* GetNetworkContext();
  58. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
  59. // May be called on the UI or IO thread. The instance returned should be
  60. // *accessed* only on the IO thread.
  61. scoped_refptr<safe_browsing::RemoteSafeBrowsingDatabaseManager>
  62. GetSafeBrowsingDBManager();
  63. scoped_refptr<safe_browsing::SafeBrowsingUIManager>
  64. GetSafeBrowsingUIManager();
  65. safe_browsing::TriggerManager* GetTriggerManager();
  66. private:
  67. // Executed on IO thread
  68. scoped_refptr<safe_browsing::UrlCheckerDelegate>
  69. GetSafeBrowsingUrlCheckerDelegate();
  70. // Safe to call multiple times; invocations after the first will be no-ops.
  71. void StartSafeBrowsingDBManagerOnIOThread();
  72. void CreateSafeBrowsingUIManager();
  73. void CreateTriggerManager();
  74. void CreateAndStartSafeBrowsingDBManager();
  75. scoped_refptr<network::SharedURLLoaderFactory>
  76. GetURLLoaderFactoryOnIOThread();
  77. void CreateURLLoaderFactoryForIO(
  78. mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver);
  79. void StopDBManagerOnIOThread();
  80. // The UI manager handles showing interstitials. Accessed on both UI and IO
  81. // thread.
  82. scoped_refptr<safe_browsing::SafeBrowsingUIManager> ui_manager_;
  83. // This is what owns the URLRequestContext inside the network service. This
  84. // is used by SimpleURLLoader for Safe Browsing requests.
  85. std::unique_ptr<safe_browsing::SafeBrowsingNetworkContext> network_context_;
  86. // May be created on UI thread and have references obtained to it on that
  87. // thread for later passing to the IO thread, but should be *accessed* only
  88. // on the IO thread.
  89. scoped_refptr<safe_browsing::RemoteSafeBrowsingDatabaseManager>
  90. safe_browsing_db_manager_;
  91. // A SharedURLLoaderFactory and its remote used on the IO thread.
  92. mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_on_io_;
  93. scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
  94. shared_url_loader_factory_on_io_;
  95. scoped_refptr<UrlCheckerDelegateImpl> safe_browsing_url_checker_delegate_;
  96. std::unique_ptr<safe_browsing::SafeBrowsingApiHandlerBridge>
  97. safe_browsing_api_handler_;
  98. std::string user_agent_;
  99. // Whether |safe_browsing_db_manager_| has been started. Accessed only on the
  100. // IO thread.
  101. bool started_db_manager_ = false;
  102. // Collects data and sends reports to Safe Browsing. Accessed on UI thread.
  103. std::unique_ptr<safe_browsing::TriggerManager> trigger_manager_;
  104. };
  105. } // namespace weblayer
  106. #endif // WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_