blob: b349f00ef0252296996f0efd60d65a320a0aa588 [file] [log] [blame]
ntfschra3234332016-12-22 01:15:421// 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"
ntfschra3234332016-12-22 01:15:4212#include "base/macros.h"
13#include "base/memory/ref_counted.h"
ntfschra3234332016-12-22 01:15:4214#include "components/security_interstitials/content/unsafe_resource.h"
15
16class GURL;
17
18namespace content {
19class NavigationEntry;
20class WebContents;
21} // namespace content
22
jialiul3d6032e2017-01-12 00:41:3123namespace history {
24class HistoryService;
25} // namespace history
26
ntfschra3234332016-12-22 01:15:4227namespace safe_browsing {
28
29// Construction needs to happen on the main thread.
jialiul3d6032e2017-01-12 00:41:3130class BaseUIManager
31 : public base::RefCountedThreadSafe<BaseUIManager> {
ntfschra3234332016-12-22 01:15:4232 public:
33 typedef security_interstitials::UnsafeResource UnsafeResource;
34
jialiul3d6032e2017-01-12 00:41:3135 BaseUIManager();
ntfschra3234332016-12-22 01:15:4236
ntfschra3234332016-12-22 01:15:4237 // 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
ntfschr54471efa2017-01-12 22:16:5844 // This is a no-op in the base class, but should be overridden to send threat
John Abd-El-Malek7870ba0c2018-04-10 04:49:1745 // details. Called on the UI thread by the ThreatDetails with the serialized
ntfschr54471efa2017-01-12 22:16:5846 // protocol buffer.
ntfschra3234332016-12-22 01:15:4247 virtual void SendSerializedThreatDetails(const std::string& serialized);
48
ntfschr54471efa2017-01-12 22:16:5849 // 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)
mortonm0e971502017-05-22 18:10:0051 // 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.
ntfschra3234332016-12-22 01:15:4253 virtual void MaybeReportSafeBrowsingHit(
mortonmcdcd0b72017-05-19 22:20:0054 const safe_browsing::HitReport& hit_report,
mortonm0e971502017-05-22 18:10:0055 const content::WebContents* web_contents);
ntfschra3234332016-12-22 01:15:4256
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
jialiul3d6032e2017-01-12 00:41:3192 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;
ntfschra3234332016-12-22 01:15:42100
101 protected:
Jialiu Lin6cdf8ec2017-07-30 19:50:01102 friend class ChromePasswordProtectionService;
jialiul3d6032e2017-01-12 00:41:31103 virtual ~BaseUIManager();
ntfschra3234332016-12-22 01:15:42104
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 Lin6cdf8ec2017-07-30 19:50:01111 // 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);
ntfschra3234332016-12-22 01:15:42116
117 // Ensures that |web_contents| has its whitelist set in its userdata
118 static void EnsureWhitelistCreated(content::WebContents* web_contents);
119
jialiul3d6032e2017-01-12 00:41:31120 // 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);
ntfschra3234332016-12-22 01:15:42124
ntfschr93d57f62017-02-17 19:07:47125 // 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
ntfschra3234332016-12-22 01:15:42133 private:
jialiul3d6032e2017-01-12 00:41:31134 friend class base::RefCountedThreadSafe<BaseUIManager>;
ntfschra3234332016-12-22 01:15:42135
jialiul3d6032e2017-01-12 00:41:31136 DISALLOW_COPY_AND_ASSIGN(BaseUIManager);
ntfschra3234332016-12-22 01:15:42137};
138
139} // namespace safe_browsing
140
141#endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_