ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 1 | // Copyright 2016 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 | #ifndef COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_ |
| 6 | #define COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/bind_helpers.h" |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 14 | #include "components/security_interstitials/content/unsafe_resource.h" |
| 15 | |
| 16 | class GURL; |
| 17 | |
| 18 | namespace content { |
| 19 | class NavigationEntry; |
| 20 | class WebContents; |
| 21 | } // namespace content |
| 22 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 23 | namespace history { |
| 24 | class HistoryService; |
| 25 | } // namespace history |
| 26 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 27 | namespace safe_browsing { |
| 28 | |
| 29 | // Construction needs to happen on the main thread. |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 30 | class BaseUIManager |
| 31 | : public base::RefCountedThreadSafe<BaseUIManager> { |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 32 | public: |
| 33 | typedef security_interstitials::UnsafeResource UnsafeResource; |
| 34 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 35 | BaseUIManager(); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 36 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 37 | // Called on the UI thread to display an interstitial page. |
| 38 | // |url| is the url of the resource that matches a safe browsing list. |
| 39 | // If the request contained a chain of redirects, |url| is the last url |
| 40 | // in the chain, and |original_url| is the first one (the root of the |
| 41 | // chain). Otherwise, |original_url| = |url|. |
| 42 | virtual void DisplayBlockingPage(const UnsafeResource& resource); |
| 43 | |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame] | 44 | // This is a no-op in the base class, but should be overridden to send threat |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 45 | // details. Called on the UI thread by the ThreatDetails with the serialized |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame] | 46 | // protocol buffer. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 47 | virtual void SendSerializedThreatDetails(const std::string& serialized); |
| 48 | |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame] | 49 | // This is a no-op in the base class, but should be overridden to report hits |
| 50 | // to the unsafe contents (malware, phishing, unsafe download URL) |
mortonm | 0e97150 | 2017-05-22 18:10:00 | [diff] [blame] | 51 | // to the server. Can only be called on UI thread. Will only upload a hit |
| 52 | // report if the user has enabled SBER and is not currently in incognito mode. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 53 | virtual void MaybeReportSafeBrowsingHit( |
mortonm | cdcd0b7 | 2017-05-19 22:20:00 | [diff] [blame] | 54 | const safe_browsing::HitReport& hit_report, |
mortonm | 0e97150 | 2017-05-22 18:10:00 | [diff] [blame] | 55 | const content::WebContents* web_contents); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 56 | |
| 57 | // A convenience wrapper method for IsUrlWhitelistedOrPendingForWebContents. |
| 58 | virtual bool IsWhitelisted(const UnsafeResource& resource); |
| 59 | |
| 60 | // Checks if we already displayed or are displaying an interstitial |
| 61 | // for the top-level site |url| in a given WebContents. If |
| 62 | // |whitelist_only|, it returns true only if the user chose to ignore |
| 63 | // the interstitial. Otherwise, it returns true if an interstitial for |
| 64 | // |url| is already displaying *or* if the user has seen an |
| 65 | // interstitial for |url| before in this WebContents and proceeded |
| 66 | // through it. Called on the UI thread. |
| 67 | // |
| 68 | // If the resource was found in the whitelist or pending for the |
| 69 | // whitelist, |threat_type| will be set to the SBThreatType for which |
| 70 | // the URL was first whitelisted. |
| 71 | virtual bool IsUrlWhitelistedOrPendingForWebContents( |
| 72 | const GURL& url, |
| 73 | bool is_subresource, |
| 74 | content::NavigationEntry* entry, |
| 75 | content::WebContents* web_contents, |
| 76 | bool whitelist_only, |
| 77 | SBThreatType* threat_type); |
| 78 | |
| 79 | // The blocking page for |web_contents| on the UI thread has |
| 80 | // completed, with |proceed| set to true if the user has chosen to |
| 81 | // proceed through the blocking page and false |
| 82 | // otherwise. |web_contents| is the WebContents that was displaying |
| 83 | // the blocking page. |main_frame_url| is the top-level URL on which |
| 84 | // the blocking page was displayed. If |proceed| is true, |
| 85 | // |main_frame_url| is whitelisted so that the user will not see |
| 86 | // another warning for that URL in this WebContents. |
| 87 | virtual void OnBlockingPageDone(const std::vector<UnsafeResource>& resources, |
| 88 | bool proceed, |
| 89 | content::WebContents* web_contents, |
| 90 | const GURL& main_frame_url); |
| 91 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 92 | virtual const std::string app_locale() const; |
| 93 | |
| 94 | virtual history::HistoryService* history_service( |
| 95 | content::WebContents* web_contents); |
| 96 | |
| 97 | // The default safe page when there is no entry in the history to go back to. |
| 98 | // e.g. about::blank page, or chrome's new tab page. |
| 99 | virtual const GURL default_safe_page() const; |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 100 | |
| 101 | protected: |
Jialiu Lin | 6cdf8ec | 2017-07-30 19:50:01 | [diff] [blame] | 102 | friend class ChromePasswordProtectionService; |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 103 | virtual ~BaseUIManager(); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 104 | |
| 105 | // Updates the whitelist URL set for |web_contents|. Called on the UI thread. |
| 106 | void AddToWhitelistUrlSet(const GURL& whitelist_url, |
| 107 | content::WebContents* web_contents, |
| 108 | bool is_pending, |
| 109 | SBThreatType threat_type); |
| 110 | |
Jialiu Lin | 6cdf8ec | 2017-07-30 19:50:01 | [diff] [blame] | 111 | // Removes |whitelist_url| from the whitelist for |web_contents|. |
| 112 | // Called on the UI thread. |
| 113 | void RemoveWhitelistUrlSet(const GURL& whitelist_url, |
| 114 | content::WebContents* web_contents, |
| 115 | bool from_pending_only); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 116 | |
| 117 | // Ensures that |web_contents| has its whitelist set in its userdata |
| 118 | static void EnsureWhitelistCreated(content::WebContents* web_contents); |
| 119 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 120 | // Returns the URL that should be used in a WhitelistUrlSet for the given |
| 121 | // |resource|. |
| 122 | static GURL GetMainFrameWhitelistUrlForResource( |
| 123 | const security_interstitials::UnsafeResource& resource); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 124 | |
ntfschr | 93d57f6 | 2017-02-17 19:07:47 | [diff] [blame] | 125 | // BaseUIManager does not send SafeBrowsingHitReport. Subclasses should |
| 126 | // implement the reporting logic themselves if needed. |
| 127 | virtual void CreateAndSendHitReport(const UnsafeResource& resource); |
| 128 | |
| 129 | // Calls BaseBlockingPage::ShowBlockingPage(). Override this if using a |
| 130 | // different blocking page. |
| 131 | virtual void ShowBlockingPageForResource(const UnsafeResource& resource); |
| 132 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 133 | private: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 134 | friend class base::RefCountedThreadSafe<BaseUIManager>; |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 135 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(BaseUIManager); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace safe_browsing |
| 140 | |
| 141 | #endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_ |