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 "base/time/time.h" |
| 15 | #include "components/security_interstitials/content/unsafe_resource.h" |
| 16 | |
| 17 | class GURL; |
| 18 | |
| 19 | namespace content { |
| 20 | class NavigationEntry; |
| 21 | class WebContents; |
| 22 | } // namespace content |
| 23 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 24 | namespace history { |
| 25 | class HistoryService; |
| 26 | } // namespace history |
| 27 | |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 28 | namespace safe_browsing { |
| 29 | |
| 30 | // Construction needs to happen on the main thread. |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 31 | class BaseUIManager |
| 32 | : public base::RefCountedThreadSafe<BaseUIManager> { |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 33 | public: |
| 34 | typedef security_interstitials::UnsafeResource UnsafeResource; |
| 35 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 36 | BaseUIManager(); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 37 | |
| 38 | // Called to stop or shutdown operations on the io_thread. This may be called |
| 39 | // multiple times during the life of the UIManager. Should be called |
| 40 | // on IO thread. If shutdown is true, the manager is disabled permanently. |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame^] | 41 | // This currently is a no-op in the base class. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 42 | virtual void StopOnIOThread(bool shutdown); |
| 43 | |
| 44 | // Called on the UI thread to display an interstitial page. |
| 45 | // |url| is the url of the resource that matches a safe browsing list. |
| 46 | // If the request contained a chain of redirects, |url| is the last url |
| 47 | // in the chain, and |original_url| is the first one (the root of the |
| 48 | // chain). Otherwise, |original_url| = |url|. |
| 49 | virtual void DisplayBlockingPage(const UnsafeResource& resource); |
| 50 | |
| 51 | // Log the user perceived delay caused by SafeBrowsing. This delay is the time |
| 52 | // delta starting from when we would have started reading data from the |
| 53 | // network, and ending when the SafeBrowsing check completes indicating that |
| 54 | // the current page is 'safe'. |
| 55 | virtual void LogPauseDelay(base::TimeDelta time); |
| 56 | |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame^] | 57 | // This is a no-op in the base class, but should be overridden to send threat |
| 58 | // details. Called on the IO thread by the ThreatDetails with the serialized |
| 59 | // protocol buffer. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 60 | virtual void SendSerializedThreatDetails(const std::string& serialized); |
| 61 | |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame^] | 62 | // This is a no-op in the base class, but should be overridden to report hits |
| 63 | // to the unsafe contents (malware, phishing, unsafe download URL) |
| 64 | // to the server. Can only be called on UI thread. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 65 | virtual void MaybeReportSafeBrowsingHit( |
| 66 | const safe_browsing::HitReport& hit_report); |
| 67 | |
| 68 | // A convenience wrapper method for IsUrlWhitelistedOrPendingForWebContents. |
| 69 | virtual bool IsWhitelisted(const UnsafeResource& resource); |
| 70 | |
| 71 | // Checks if we already displayed or are displaying an interstitial |
| 72 | // for the top-level site |url| in a given WebContents. If |
| 73 | // |whitelist_only|, it returns true only if the user chose to ignore |
| 74 | // the interstitial. Otherwise, it returns true if an interstitial for |
| 75 | // |url| is already displaying *or* if the user has seen an |
| 76 | // interstitial for |url| before in this WebContents and proceeded |
| 77 | // through it. Called on the UI thread. |
| 78 | // |
| 79 | // If the resource was found in the whitelist or pending for the |
| 80 | // whitelist, |threat_type| will be set to the SBThreatType for which |
| 81 | // the URL was first whitelisted. |
| 82 | virtual bool IsUrlWhitelistedOrPendingForWebContents( |
| 83 | const GURL& url, |
| 84 | bool is_subresource, |
| 85 | content::NavigationEntry* entry, |
| 86 | content::WebContents* web_contents, |
| 87 | bool whitelist_only, |
| 88 | SBThreatType* threat_type); |
| 89 | |
| 90 | // The blocking page for |web_contents| on the UI thread has |
| 91 | // completed, with |proceed| set to true if the user has chosen to |
| 92 | // proceed through the blocking page and false |
| 93 | // otherwise. |web_contents| is the WebContents that was displaying |
| 94 | // the blocking page. |main_frame_url| is the top-level URL on which |
| 95 | // the blocking page was displayed. If |proceed| is true, |
| 96 | // |main_frame_url| is whitelisted so that the user will not see |
| 97 | // another warning for that URL in this WebContents. |
| 98 | virtual void OnBlockingPageDone(const std::vector<UnsafeResource>& resources, |
| 99 | bool proceed, |
| 100 | content::WebContents* web_contents, |
| 101 | const GURL& main_frame_url); |
| 102 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 103 | virtual const std::string app_locale() const; |
| 104 | |
| 105 | virtual history::HistoryService* history_service( |
| 106 | content::WebContents* web_contents); |
| 107 | |
| 108 | // The default safe page when there is no entry in the history to go back to. |
| 109 | // e.g. about::blank page, or chrome's new tab page. |
| 110 | virtual const GURL default_safe_page() const; |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 111 | |
| 112 | protected: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 113 | virtual ~BaseUIManager(); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 114 | |
| 115 | // Updates the whitelist URL set for |web_contents|. Called on the UI thread. |
| 116 | void AddToWhitelistUrlSet(const GURL& whitelist_url, |
| 117 | content::WebContents* web_contents, |
| 118 | bool is_pending, |
| 119 | SBThreatType threat_type); |
| 120 | |
ntfschr | 54471efa | 2017-01-12 22:16:58 | [diff] [blame^] | 121 | // This is a no-op that should be overridden to call protocol manager on IO |
| 122 | // thread to report hits of unsafe contents. |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 123 | virtual void ReportSafeBrowsingHitOnIOThread( |
| 124 | const safe_browsing::HitReport& hit_report); |
| 125 | |
| 126 | // Removes |whitelist_url| from the pending whitelist for |
| 127 | // |web_contents|. Called on the UI thread. |
| 128 | void RemoveFromPendingWhitelistUrlSet(const GURL& whitelist_url, |
| 129 | content::WebContents* web_contents); |
| 130 | |
| 131 | // Ensures that |web_contents| has its whitelist set in its userdata |
| 132 | static void EnsureWhitelistCreated(content::WebContents* web_contents); |
| 133 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 134 | // Returns the URL that should be used in a WhitelistUrlSet for the given |
| 135 | // |resource|. |
| 136 | static GURL GetMainFrameWhitelistUrlForResource( |
| 137 | const security_interstitials::UnsafeResource& resource); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 138 | |
| 139 | private: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 140 | friend class base::RefCountedThreadSafe<BaseUIManager>; |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 141 | |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(BaseUIManager); |
ntfschr | a323433 | 2016-12-22 01:15:42 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | } // namespace safe_browsing |
| 146 | |
| 147 | #endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_ |