[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 1 | // 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" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 15 | #include "base/macros.h" |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | 8481347 | 2013-06-28 00:25:19 | [diff] [blame] | 17 | #include "base/time/time.h" |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 18 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
Ali Juma | fb3dc1f | 2020-01-07 17:33:47 | [diff] [blame] | 19 | #include "components/safe_browsing/content/base_ui_manager.h" |
Ali Juma | ee60293 | 2020-01-24 16:39:18 | [diff] [blame] | 20 | #include "components/security_interstitials/core/unsafe_resource.h" |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 21 | |
| 22 | class GURL; |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 23 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 24 | namespace content { |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 25 | class WebContents; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 26 | } // namespace content |
| 27 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 28 | namespace history { |
| 29 | class HistoryService; |
| 30 | } // namespace history |
| 31 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 32 | namespace safe_browsing { |
| 33 | |
Carlos IL | 3eb9974 | 2019-10-28 22:49:45 | [diff] [blame] | 34 | class BaseBlockingPage; |
| 35 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 36 | struct HitReport; |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 37 | |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 38 | // Construction needs to happen on the main thread. |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 39 | class SafeBrowsingUIManager : public BaseUIManager { |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 40 | public: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 41 | // 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] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 59 | explicit SafeBrowsingUIManager( |
| 60 | const scoped_refptr<SafeBrowsingService>& service); |
| 61 | |
John Abd-El-Malek | 69c14cb | 2018-04-12 19:29:30 | [diff] [blame] | 62 | // Called to stop or shutdown operations on the UI thread. This may be called |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 63 | // multiple times during the life of the UIManager. Should be called |
John Abd-El-Malek | 69c14cb | 2018-04-12 19:29:30 | [diff] [blame] | 64 | // on UI thread. If shutdown is true, the manager is disabled permanently. |
| 65 | void Stop(bool shutdown); |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 66 | |
jialiul | 5b85b20 | 2015-10-27 17:58:25 | [diff] [blame] | 67 | // Called on the IO thread by the ThreatDetails with the serialized |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 68 | // protocol buffer, so the service can send it over. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 69 | void SendSerializedThreatDetails(const std::string& serialized) override; |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 70 | |
Jialiu Lin | 2d201b1 | 2018-05-20 20:46:24 | [diff] [blame] | 71 | // 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 IL | 5edbd0d | 2020-01-28 01:27:46 | [diff] [blame] | 76 | const GURL& main_frame_url, |
| 77 | bool showed_interstitial) override; |
Jialiu Lin | 2d201b1 | 2018-05-20 20:46:24 | [diff] [blame] | 78 | |
mortonm | 0e97150 | 2017-05-22 18:10:00 | [diff] [blame] | 79 | // 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 Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 82 | void MaybeReportSafeBrowsingHit(const safe_browsing::HitReport& hit_report, |
| 83 | content::WebContents* web_contents) override; |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 84 | |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 85 | // 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 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 91 | // Add and remove observers. These methods must be invoked on the UI thread. |
| 92 | void AddObserver(Observer* observer); |
| 93 | void RemoveObserver(Observer* remove); |
| 94 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 95 | 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] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 100 | protected: |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 101 | ~SafeBrowsingUIManager() override; |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 102 | |
ntfschr | 93d57f6 | 2017-02-17 19:07:47 | [diff] [blame] | 103 | // 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 | |
mortonm | cdcd0b7 | 2017-05-19 22:20:00 | [diff] [blame] | 111 | // 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 Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 114 | content::WebContents* web_contents); |
mortonm | cdcd0b7 | 2017-05-19 22:20:00 | [diff] [blame] | 115 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 116 | private: |
| 117 | friend class SafeBrowsingUIManagerTest; |
| 118 | friend class TestSafeBrowsingUIManager; |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 119 | |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 120 | static GURL GetMainFrameWhitelistUrlForResourceForTesting( |
| 121 | const safe_browsing::SafeBrowsingUIManager::UnsafeResource& resource); |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 122 | |
Carlos IL | 3eb9974 | 2019-10-28 22:49:45 | [diff] [blame] | 123 | // 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] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 130 | // Safebrowsing service. |
| 131 | scoped_refptr<SafeBrowsingService> sb_service_; |
| 132 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 133 | base::ObserverList<Observer>::Unchecked observer_list_; |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 134 | |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIManager); |
| 136 | }; |
| 137 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 138 | } // namespace safe_browsing |
| 139 | |
[email protected] | 5006a41 | 2012-11-27 08:22:40 | [diff] [blame] | 140 | #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_ |