blob: 862415c98fc637c21ac4a23e7d01394232d2c142 [file] [log] [blame]
[email protected]5006a412012-11-27 08:22:401// Copyright (c) 2012 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//
5// The Safe Browsing service is responsible for downloading anti-phishing and
6// anti-malware tables and checking urls against them.
7
8#ifndef CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
9#define CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
10
11#include <string>
12#include <vector>
13
14#include "base/callback.h"
avib896c712015-12-26 02:10:4315#include "base/macros.h"
jialiul3d6032e2017-01-12 00:41:3116#include "base/observer_list.h"
[email protected]84813472013-06-28 00:25:1917#include "base/time/time.h"
ntfschra3234332016-12-22 01:15:4218#include "chrome/browser/safe_browsing/safe_browsing_service.h"
Ali Jumafb3dc1f2020-01-07 17:33:4719#include "components/safe_browsing/content/base_ui_manager.h"
Ali Jumaee602932020-01-24 16:39:1820#include "components/security_interstitials/core/unsafe_resource.h"
ntfschra3234332016-12-22 01:15:4221
22class GURL;
[email protected]5006a412012-11-27 08:22:4023
mattmbfc4060d2015-12-18 23:11:3824namespace content {
scottmg22e4f25a2016-08-15 21:09:0325class WebContents;
mattmbfc4060d2015-12-18 23:11:3826} // namespace content
27
jialiul3d6032e2017-01-12 00:41:3128namespace history {
29class HistoryService;
30} // namespace history
31
vakh9a474d832015-11-13 01:43:0932namespace safe_browsing {
33
Carlos IL3eb99742019-10-28 22:49:4534class BaseBlockingPage;
35
ntfschra3234332016-12-22 01:15:4236struct HitReport;
vakh9a474d832015-11-13 01:43:0937
[email protected]5006a412012-11-27 08:22:4038// Construction needs to happen on the main thread.
jialiul3d6032e2017-01-12 00:41:3139class SafeBrowsingUIManager : public BaseUIManager {
[email protected]5006a412012-11-27 08:22:4040 public:
jialiul3d6032e2017-01-12 00:41:3141 // Observer class can be used to get notified when a SafeBrowsing hit
42 // is found.
43 class Observer {
44 public:
45 // Called when |resource| is classified as unsafe by SafeBrowsing, and is
46 // not whitelisted.
47 // The |resource| must not be accessed after OnSafeBrowsingHit returns.
48 // This method will be called on the UI thread.
49 virtual void OnSafeBrowsingHit(const UnsafeResource& resource) = 0;
50
51 protected:
52 Observer() {}
53 virtual ~Observer() {}
54
55 private:
56 DISALLOW_COPY_AND_ASSIGN(Observer);
57 };
58
[email protected]5006a412012-11-27 08:22:4059 explicit SafeBrowsingUIManager(
60 const scoped_refptr<SafeBrowsingService>& service);
61
John Abd-El-Malek69c14cb2018-04-12 19:29:3062 // Called to stop or shutdown operations on the UI thread. This may be called
[email protected]5006a412012-11-27 08:22:4063 // multiple times during the life of the UIManager. Should be called
John Abd-El-Malek69c14cb2018-04-12 19:29:3064 // on UI thread. If shutdown is true, the manager is disabled permanently.
65 void Stop(bool shutdown);
[email protected]5006a412012-11-27 08:22:4066
jialiul5b85b202015-10-27 17:58:2567 // Called on the IO thread by the ThreatDetails with the serialized
[email protected]5006a412012-11-27 08:22:4068 // protocol buffer, so the service can send it over.
ntfschra3234332016-12-22 01:15:4269 void SendSerializedThreatDetails(const std::string& serialized) override;
[email protected]5006a412012-11-27 08:22:4070
Jialiu Lin2d201b12018-05-20 20:46:2471 // Calls |BaseUIManager::OnBlockingPageDone()| and triggers
72 // |OnSecurityInterstitialProceeded| event if |proceed| is true.
73 void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
74 bool proceed,
75 content::WebContents* web_contents,
Carlos IL5edbd0d2020-01-28 01:27:4676 const GURL& main_frame_url,
77 bool showed_interstitial) override;
Jialiu Lin2d201b12018-05-20 20:46:2478
mortonm0e971502017-05-22 18:10:0079 // Report hits to unsafe contents (malware, phishing, unsafe download URL)
80 // to the server. Can only be called on UI thread. The hit report will
81 // only be sent if the user has enabled SBER and is not in incognito mode.
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4182 void MaybeReportSafeBrowsingHit(const safe_browsing::HitReport& hit_report,
83 content::WebContents* web_contents) override;
[email protected]5006a412012-11-27 08:22:4084
estark1ca09ca2016-11-01 04:04:1285 // Creates the whitelist URL set for tests that create a blocking page
86 // themselves and then simulate OnBlockingPageDone(). OnBlockingPageDone()
87 // expects the whitelist to exist, but the tests don't necessarily call
88 // DisplayBlockingPage(), which creates it.
89 static void CreateWhitelistForTesting(content::WebContents* web_contents);
90
jialiul3d6032e2017-01-12 00:41:3191 // Add and remove observers. These methods must be invoked on the UI thread.
92 void AddObserver(Observer* observer);
93 void RemoveObserver(Observer* remove);
94
jialiul3d6032e2017-01-12 00:41:3195 const std::string app_locale() const override;
96 history::HistoryService* history_service(
97 content::WebContents* web_contents) override;
98 const GURL default_safe_page() const override;
99
[email protected]5006a412012-11-27 08:22:40100 protected:
ntfschra3234332016-12-22 01:15:42101 ~SafeBrowsingUIManager() override;
[email protected]5006a412012-11-27 08:22:40102
ntfschr93d57f62017-02-17 19:07:47103 // Creates a hit report for the given resource and calls
104 // MaybeReportSafeBrowsingHit. This also notifies all observers in
105 // |observer_list_|.
106 void CreateAndSendHitReport(const UnsafeResource& resource) override;
107
108 // Calls SafeBrowsingBlockingPage::ShowBlockingPage().
109 void ShowBlockingPageForResource(const UnsafeResource& resource) override;
110
mortonmcdcd0b72017-05-19 22:20:00111 // Helper method to ensure hit reports are only sent when the user has
112 // opted in to extended reporting and is not currently in incognito mode.
113 static bool ShouldSendHitReport(const HitReport& hit_report,
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41114 content::WebContents* web_contents);
mortonmcdcd0b72017-05-19 22:20:00115
ntfschra3234332016-12-22 01:15:42116 private:
117 friend class SafeBrowsingUIManagerTest;
118 friend class TestSafeBrowsingUIManager;
[email protected]5006a412012-11-27 08:22:40119
estark1ca09ca2016-11-01 04:04:12120 static GURL GetMainFrameWhitelistUrlForResourceForTesting(
121 const safe_browsing::SafeBrowsingUIManager::UnsafeResource& resource);
[email protected]5006a412012-11-27 08:22:40122
Carlos IL3eb99742019-10-28 22:49:45123 // Creates a blocking page, used for interstitials triggered by subresources.
124 // Override is using a different blocking page.
125 BaseBlockingPage* CreateBlockingPageForSubresource(
126 content::WebContents* contents,
127 const GURL& blocked_url,
128 const UnsafeResource& unsafe_resource) override;
129
[email protected]5006a412012-11-27 08:22:40130 // Safebrowsing service.
131 scoped_refptr<SafeBrowsingService> sb_service_;
132
Trent Apteda250ec3ab2018-08-19 08:52:19133 base::ObserverList<Observer>::Unchecked observer_list_;
jialiul3d6032e2017-01-12 00:41:31134
[email protected]5006a412012-11-27 08:22:40135 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIManager);
136};
137
vakh9a474d832015-11-13 01:43:09138} // namespace safe_browsing
139
[email protected]5006a412012-11-27 08:22:40140#endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_