blob: 720b2360edb419b944cdad7690ff1815e80126e0 [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 "base/time/time.h"
15#include "components/security_interstitials/content/unsafe_resource.h"
16
17class GURL;
18
19namespace content {
20class NavigationEntry;
21class WebContents;
22} // namespace content
23
jialiul3d6032e2017-01-12 00:41:3124namespace history {
25class HistoryService;
26} // namespace history
27
ntfschra3234332016-12-22 01:15:4228namespace safe_browsing {
29
30// Construction needs to happen on the main thread.
jialiul3d6032e2017-01-12 00:41:3131class BaseUIManager
32 : public base::RefCountedThreadSafe<BaseUIManager> {
ntfschra3234332016-12-22 01:15:4233 public:
34 typedef security_interstitials::UnsafeResource UnsafeResource;
35
jialiul3d6032e2017-01-12 00:41:3136 BaseUIManager();
ntfschra3234332016-12-22 01:15:4237
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.
ntfschr54471efa2017-01-12 22:16:5841 // This currently is a no-op in the base class.
ntfschra3234332016-12-22 01:15:4242 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
ntfschr54471efa2017-01-12 22:16:5857 // 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.
ntfschra3234332016-12-22 01:15:4260 virtual void SendSerializedThreatDetails(const std::string& serialized);
61
ntfschr54471efa2017-01-12 22:16:5862 // 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)
mortonm0e971502017-05-22 18:10:0064 // to the server. Can only be called on UI thread. Will only upload a hit
65 // report if the user has enabled SBER and is not currently in incognito mode.
ntfschra3234332016-12-22 01:15:4266 virtual void MaybeReportSafeBrowsingHit(
mortonmcdcd0b72017-05-19 22:20:0067 const safe_browsing::HitReport& hit_report,
mortonm0e971502017-05-22 18:10:0068 const content::WebContents* web_contents);
ntfschra3234332016-12-22 01:15:4269
70 // A convenience wrapper method for IsUrlWhitelistedOrPendingForWebContents.
71 virtual bool IsWhitelisted(const UnsafeResource& resource);
72
73 // Checks if we already displayed or are displaying an interstitial
74 // for the top-level site |url| in a given WebContents. If
75 // |whitelist_only|, it returns true only if the user chose to ignore
76 // the interstitial. Otherwise, it returns true if an interstitial for
77 // |url| is already displaying *or* if the user has seen an
78 // interstitial for |url| before in this WebContents and proceeded
79 // through it. Called on the UI thread.
80 //
81 // If the resource was found in the whitelist or pending for the
82 // whitelist, |threat_type| will be set to the SBThreatType for which
83 // the URL was first whitelisted.
84 virtual bool IsUrlWhitelistedOrPendingForWebContents(
85 const GURL& url,
86 bool is_subresource,
87 content::NavigationEntry* entry,
88 content::WebContents* web_contents,
89 bool whitelist_only,
90 SBThreatType* threat_type);
91
92 // The blocking page for |web_contents| on the UI thread has
93 // completed, with |proceed| set to true if the user has chosen to
94 // proceed through the blocking page and false
95 // otherwise. |web_contents| is the WebContents that was displaying
96 // the blocking page. |main_frame_url| is the top-level URL on which
97 // the blocking page was displayed. If |proceed| is true,
98 // |main_frame_url| is whitelisted so that the user will not see
99 // another warning for that URL in this WebContents.
100 virtual void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
101 bool proceed,
102 content::WebContents* web_contents,
103 const GURL& main_frame_url);
104
jialiul3d6032e2017-01-12 00:41:31105 virtual const std::string app_locale() const;
106
107 virtual history::HistoryService* history_service(
108 content::WebContents* web_contents);
109
110 // The default safe page when there is no entry in the history to go back to.
111 // e.g. about::blank page, or chrome's new tab page.
112 virtual const GURL default_safe_page() const;
ntfschra3234332016-12-22 01:15:42113
114 protected:
Jialiu Lin6cdf8ec2017-07-30 19:50:01115 friend class ChromePasswordProtectionService;
jialiul3d6032e2017-01-12 00:41:31116 virtual ~BaseUIManager();
ntfschra3234332016-12-22 01:15:42117
118 // Updates the whitelist URL set for |web_contents|. Called on the UI thread.
119 void AddToWhitelistUrlSet(const GURL& whitelist_url,
120 content::WebContents* web_contents,
121 bool is_pending,
122 SBThreatType threat_type);
123
ntfschr54471efa2017-01-12 22:16:58124 // This is a no-op that should be overridden to call protocol manager on IO
125 // thread to report hits of unsafe contents.
ntfschra3234332016-12-22 01:15:42126 virtual void ReportSafeBrowsingHitOnIOThread(
127 const safe_browsing::HitReport& hit_report);
128
Jialiu Lin6cdf8ec2017-07-30 19:50:01129 // Removes |whitelist_url| from the whitelist for |web_contents|.
130 // Called on the UI thread.
131 void RemoveWhitelistUrlSet(const GURL& whitelist_url,
132 content::WebContents* web_contents,
133 bool from_pending_only);
ntfschra3234332016-12-22 01:15:42134
135 // Ensures that |web_contents| has its whitelist set in its userdata
136 static void EnsureWhitelistCreated(content::WebContents* web_contents);
137
jialiul3d6032e2017-01-12 00:41:31138 // Returns the URL that should be used in a WhitelistUrlSet for the given
139 // |resource|.
140 static GURL GetMainFrameWhitelistUrlForResource(
141 const security_interstitials::UnsafeResource& resource);
ntfschra3234332016-12-22 01:15:42142
ntfschr93d57f62017-02-17 19:07:47143 // BaseUIManager does not send SafeBrowsingHitReport. Subclasses should
144 // implement the reporting logic themselves if needed.
145 virtual void CreateAndSendHitReport(const UnsafeResource& resource);
146
147 // Calls BaseBlockingPage::ShowBlockingPage(). Override this if using a
148 // different blocking page.
149 virtual void ShowBlockingPageForResource(const UnsafeResource& resource);
150
ntfschra3234332016-12-22 01:15:42151 private:
jialiul3d6032e2017-01-12 00:41:31152 friend class base::RefCountedThreadSafe<BaseUIManager>;
ntfschra3234332016-12-22 01:15:42153
jialiul3d6032e2017-01-12 00:41:31154 DISALLOW_COPY_AND_ASSIGN(BaseUIManager);
ntfschra3234332016-12-22 01:15:42155};
156
157} // namespace safe_browsing
158
159#endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_